-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavbar.py
173 lines (110 loc) · 6.02 KB
/
navbar.py
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
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtWidgets import QMainWindow, QLineEdit, QShortcut, QLabel
from PyQt5.QtWebEngineWidgets import QWebEngineView
from methods.database_methods import DBMethods
from methods.connect_methods import BrowserConnect
from methods.tabs_methods import TabsMethods
from methods.sidebar_methods import SideBarMethods
from methods.json_methods import ConfigMethods
from icecream import ic
import sys, re, datetime
class NavBar(QMainWindow):
def __init__(self):
super().__init__()
self.init_tabs()
def init_tabs(self):
self.tabs = TabsMethods().create_tabs()
self.tabs.tabBarDoubleClicked.connect(self.tab_open_doubleclick)
self.tabs.currentChanged.connect(self.current_tab_changed)
self.tabs.tabCloseRequested.connect(self.close_current_tab)
self.setCentralWidget(self.tabs)
self.nav_toolbar = TabsMethods().create_navigation_toolbar(self)
self.addToolBar(self.nav_toolbar)
self.urlBar = QLineEdit()
self.urlBar.returnPressed.connect(self.goToUrl)
self.nav_toolbar.addWidget(self.urlBar)
self.label = QLabel()
self.nav_toolbar.addWidget(self.label)
self.addShortcut = QShortcut('Ctrl+T', self)
self.addShortcut.activated.connect(self.addNewTab)
self.closeShortcut = QShortcut('Ctrl+W', self)
self.closeShortcut.activated.connect(self.close_current_tab)
#self.setWindowFlags(Qt.WindowType.FramelessWindowHint) -> retira os botoes de fechar, minimizar e maximizar
recentlyUrls = DBMethods().getRecentlyUrls()
titlesUrls = [(item[1], item[2]) for item in recentlyUrls]
ic(titlesUrls)
for title, url in titlesUrls:
self.addNewTab(title, QUrl(url))
self.showMaximized()
self.setWindowTitle('ACS Browser')
self.addToolBar(Qt.ToolBarArea.LeftToolBarArea, SideBarMethods().create_sidebar(self))
def addNewTab(self, title = 'No Title Find', url = None, label="Blank",):
if url is None:
url = QUrl(ConfigMethods().loadJson()['newTabURL'])
browser = QWebEngineView()
browser.load(url)
currentTabIndex = self.tabs.addTab(browser, label)
self.tabs.setCurrentIndex(currentTabIndex)
self.tabs.setTabText(currentTabIndex, title)
browser.urlChanged.connect(lambda url, browser=browser:
BrowserConnect().update_urlBar(self, url, browser))
browser.titleChanged.connect(lambda _, i=currentTabIndex, browser=browser:
self.tabs.setTabText(i, self.tabs.currentWidget().page().title()))
browser.iconChanged.connect(lambda _, browser=browser:
BrowserConnect().updateIcon(self, browser))
browser.page().loadFinished.connect(self.loadDBMethods)
def goToUrl(self):
url = QUrl(self.urlBar.text())
urlBarTxt = self.urlBar.text()
self.searchEngineDefault = ''
match ConfigMethods().loadJson()['searchEngine']:
case 'Google':
self.searchEngineDefault = 'https://www.google.com/search?q='
case 'Yahoo':
self.searchEngineDefault = 'https://search.yahoo.com/search?q='
case 'Bing':
self.searchEngineDefault = 'https://www.bing.com/search?q='
case 'DuckDuckGo':
self.searchEngineDefault = 'https://duckduckgo.com/?q='
case _:
self.searchEngineDefault = 'https://www.google.com/search?q='
valid_url = re.compile(
r"^(http|https)?:?(\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)"
)
if valid_url.search(urlBarTxt) and not any(
i in urlBarTxt for i in ("http://", "https://", "file:///")
):
url = f'https://{urlBarTxt}'
elif '/' not in urlBarTxt:
url = f'{self.searchEngineDefault}{self.urlBar.text()}'
self.tabs.currentWidget().load(QUrl(url))
def tab_open_doubleclick(self, currentTabIndex):
if currentTabIndex == -1:
self.addNewTab()
def current_tab_changed(self):
url = self.tabs.currentWidget().url()
BrowserConnect().update_urlBar(self, url, self.tabs.currentWidget())
def close_current_tab(self):
if self.tabs.count() < 2:
sys.exit()
self.tabs.currentWidget().close()
#o .close() termina o processo do widget atual, impedindo que uma aba ja fechada venha ocupar espaço na memoria
#assim, o browser pelo menos n fica ocupando gigas na memoria qnd se tem apenas 1 aba aberta
self.tabs.removeTab(self.tabs.currentIndex())
def loadDBMethods(self):
title = self.tabs.currentWidget().page().title()
url = DBMethods().convertUrlToStr(self.tabs.currentWidget().page().url())
date = DBMethods().convertDateToBR(datetime.datetime.now())
# history
DBMethods().replaceOldData('history', 'url', url, 2)
DBMethods().insert(
"INSERT INTO history (title,url,date) VALUES (:title,:url,:date)",
{"title": title, "url": url, "date": date}
)
# zoom
zoomInDB = DBMethods().getCurrentZoomPage(self.tabs.currentWidget().page().url())
self.tabs.currentWidget().setZoomFactor(float(zoomInDB))
self.label.setText(f"Zoom atual: {self.tabs.currentWidget().zoomFactor():.1f}")
# save recently page
DBMethods().replaceOldData('tabs', 'url', url, 2)
DBMethods().saveRecentlyPage(title, url)