-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.py
More file actions
246 lines (208 loc) · 9.81 KB
/
Copy pathbrowser.py
File metadata and controls
246 lines (208 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLineEdit, QLabel, QTextEdit
from PyQt5.QtGui import QFont, QTextCharFormat, QCursor
from PyQt5.QtCore import Qt, QPoint
import re
import client as pttp
class BrowserWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Ion Browser")
self.resize(1920, 1080)
self.history = ["pttp://main.com/main.pst"] # Store visited URLs
self.history_index = 0
self.url_input = QLineEdit(self)
self.url_input.setPlaceholderText("Enter URL")
self.url_input.setText("pttp://main.com/main.pst")
self.url_input.setFixedHeight(45)
self.back_button = QPushButton("←", self)
self.forward_button = QPushButton("→", self)
self.go_button = QPushButton("Go", self)
self.page_label = QTextEdit()
self.page_label.setReadOnly(True)
cursor = self.page_label.textCursor()
self.page_label.setStyleSheet("""
background-color: white;
border: 2px solid #ccc;
padding: 50px;
font-size: 22px;
""")
self.page_label.setAlignment(Qt.AlignTop | Qt.AlignLeft)
# Set larger fonts
font_large = QFont("Arial", 24)
font_medium = QFont("Arial", 15)
self.url_input.setFont(font_medium)
self.page_label.setFont(font_large)
# Set button sizes
button_size = "padding: 15px 30px; font-size: 20px; border-radius: 10px;"
self.back_button.setStyleSheet(f"background-color: #E0E0E0; {button_size}")
self.forward_button.setStyleSheet(f"background-color: #E0E0E0; {button_size}")
self.go_button.setStyleSheet(f"background-color: #E0E0E0; {button_size}")
self.back_button.setFixedSize(80, 60)
self.forward_button.setFixedSize(80, 60)
self.go_button.setFixedSize(100, 60)
self.url_input.setFixedHeight(45)
top_layout = QHBoxLayout()
top_layout.setSpacing(10)
top_layout.addWidget(self.back_button)
top_layout.addWidget(self.forward_button)
top_layout.addWidget(self.url_input, stretch=1)
top_layout.addWidget(self.go_button)
main_layout = QVBoxLayout()
main_layout.setContentsMargins(20, 20, 20, 20)
main_layout.setSpacing(20)
main_layout.addLayout(top_layout)
main_layout.addWidget(self.page_label, stretch=1)
self.setLayout(main_layout)
self.back_button.clicked.connect(self.on_back_click)
self.forward_button.clicked.connect(self.on_forward_click)
self.go_button.clicked.connect(self.on_go_click)
self.url_input.returnPressed.connect(self.on_go_click)
self.on_go_click()
def on_back_click(self):
if self.history_index > 0:
self.history_index -= 1
self.load_page(self.history[self.history_index])
self.url_input.setText(self.history[self.history_index])
def on_forward_click(self):
if self.history_index < len(self.history) - 1:
self.history_index += 1
self.load_page(self.history[self.history_index])
self.url_input.setText(self.history[self.history_index])
def on_go_click(self):
url = self.url_input.text()
if url.strip() != "":
if self.history_index < len(self.history) - 1:
self.history = self.history[:self.history_index + 1]
self.history.append(url)
self.history_index = len(self.history) - 1
self.load_page(url)
def load_page(self, url):
def insert_clickable_link(text_edit, text, linkto, formatval):
cursor = text_edit.textCursor()
formatval.setFontUnderline(True)
formatval.setForeground(Qt.blue)
start_pos = cursor.position()
cursor.insertText(text, formatval)
end_pos = cursor.position()
formatval.setFontUnderline(False)
formatval.setForeground(Qt.black)
if not hasattr(text_edit, "links"):
text_edit.links = {}
text_edit.links[(start_pos, end_pos)] = linkto
def handle_click(event):
pos = text_edit.cursorForPosition(event.pos()).position()
for (start, end), link in text_edit.links.items():
if start <= pos <= end:
self.url_input.setText(link)
self.on_go_click()
return
text_edit.mouseReleaseEvent = handle_click
self.page_label.clear()
self.page_label.setStyleSheet("""
background-color: white;
border: 2px solid #ccc;
padding: 50px;
font-size: 22px;
""")
self.page_label.setAlignment(Qt.AlignTop | Qt.AlignLeft)
if url.startswith("pttp://"):
url = url.split("pttp://")[1]
else:
pass
url = url.strip()
if "/" not in url or url.endswith("/"):
domain = url
content = pttp.pttpGET(domain)
if "index.pst" in content:
page = "index.pst"
content = pttp.pttpGET(domain, page)
else:
domain = url.split("/")[0].strip()
page = url.split("/")[1].strip()
content = pttp.pttpGET(domain, page)
content = content.removeprefix("PTTP/1.0 200 OK").strip()
if content.strip().startswith("@"):
redirect = content.strip().removeprefix("@").strip()
content = pttp.pttpGET(domain, redirect)
if url.endswith("/"):
self.url_input.setText("pttp://" + domain + redirect)
else:
self.url_input.setText("pttp://" + domain + "/" + redirect)
if content.startswith("PTTP/1.0 404 Not Found"):
lines = ["[s=50][u][center][font=Ariel]", "line>Error 404: Not Found<", "[s=25][/u]", "line>Page does not exist or was removed<"]
else:
content = content.removeprefix("PTTP/1.0 200 OK").strip()
lines = content.splitlines()
formatted = False
for i, line in enumerate(lines):
if "<" not in line:
formatted = True
formatval = QTextCharFormat()
formatval.setFontPointSize(16)
if "[b" in line:
weight = line.split("[b")[1].split("]")[0].strip().removeprefix("=")
formatval.setFontWeight(int(weight))
if "[s" in line:
size = line.split("[s")[1].split("]")[0].strip().removeprefix("=")
formatval.setFontPointSize(float(size))
if "[u]" in line:
formatval.setFontUnderline(True)
if "[i]" in line:
formatval.setFontItalic(True)
if "[center]" in line:
self.page_label.setAlignment(Qt.AlignTop | Qt.AlignHCenter)
if "[right]" in line:
self.page_label.setAlignment(Qt.AlignTop | Qt.AlignRight)
if "[/b]" in line:
formatval.setFontWeight(0)
if "[/u]" in line:
formatval.setFontUnderline(False)
if "[/i]" in line:
formatval.setFontItalic(False)
if "[left]" in line:
self.page_label.setAlignment(Qt.AlignTop | Qt.AlignLeft)
if "[font" in line:
font = line.split("[font")[1].split("]")[0].strip().removeprefix("=")
formatval.setFontFamily(font)
else:
if line.startswith("line>"):
cursor = self.page_label.textCursor()
value = line.removeprefix("line>").removesuffix("<") + "\n"
if formatted:
cursor.insertText(value, formatval)
else:
formatval = QTextCharFormat()
cursor.insertText(value, formatval)
if line.startswith("text>"):
cursor = self.page_label.textCursor()
value = line.removeprefix("text>").removesuffix("<")
if formatted:
cursor.insertText(value, formatval)
else:
formatval = QTextCharFormat()
cursor.insertText(value, formatval)
elif line.startswith("link<"):
cursor = self.page_label.textCursor()
linkto = line.split("link<")[1].split(">")[0].strip()
text = line.split("link<")[1].split(">")[1].strip().removesuffix("<")
if "pttp://" in linkto:
pass
elif "pttp://" not in linkto:
linkto = "pttp://" + linkto
insert_clickable_link(self.page_label, text, linkto, formatval)
# Override mouseMoveEvent to change cursor shape
def mouseMoveEvent(event):
pos = self.page_label.cursorForPosition(event.pos()).position()
for (start, end), link in self.page_label.links.items():
if start <= pos <= end:
self.page_label.viewport().setCursor(QCursor(Qt.PointingHandCursor))
return
self.page_label.viewport().setCursor(QCursor(Qt.IBeamCursor))
self.page_label.mouseMoveEvent = mouseMoveEvent
# Main execution
if __name__ == "__main__":
app = QApplication(sys.argv)
window = BrowserWindow()
window.show()
sys.exit(app.exec_())