Helpviewer/UrlBar/UrlBar.py

changeset 653
0540f3c52b46
child 657
099d1ab9073e
equal deleted inserted replaced
651:e8020b9ac2b9 653:0540f3c52b46
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the URL bar widget.
8 """
9
10 from PyQt4.QtCore import Qt, QPointF, QUrl
11 from PyQt4.QtGui import QColor, QPalette, QApplication, QLinearGradient
12 from PyQt4.QtWebKit import QWebSettings
13
14 from E5Gui.E5LineEdit import E5LineEdit
15 from E5Gui.E5LineEditButton import E5LineEditButton
16
17 from .FavIconLabel import FavIconLabel
18
19 import UI.PixmapCache
20 import Preferences
21
22 class UrlBar(E5LineEdit):
23 """
24 Class implementing a line edit for entering URLs.
25 """
26 def __init__(self, mainWindow, parent = None):
27 """
28 Constructor
29
30 @param mainWindow reference to the main window (HelpWindow)
31 @param parent reference to the parent widget (HelpBrowser)
32 """
33 E5LineEdit.__init__(self, parent)
34 self.setInactiveText(self.trUtf8("Enter the location here."))
35
36 self.__mw = mainWindow
37 self.__browser = None
38 self.__privateMode = QWebSettings.globalSettings().testAttribute(
39 QWebSettings.PrivateBrowsingEnabled)
40
41 self.__favicon = FavIconLabel(self)
42 self.addWidget(self.__favicon, E5LineEdit.LeftSide)
43
44 self.__privacyButton = E5LineEditButton(self)
45 self.__privacyButton.setIcon(UI.PixmapCache.getIcon("privateBrowsing.png"))
46 self.addWidget(self.__privacyButton, E5LineEdit.RightSide)
47 self.__privacyButton.setVisible(self.__privateMode)
48
49 self.__clearButton = E5LineEditButton(self)
50 self.__clearButton.setIcon(UI.PixmapCache.getIcon("clearLeft.png"))
51 self.addWidget(self.__clearButton, E5LineEdit.RightSide)
52 self.__clearButton.setVisible(False)
53
54 self.__privacyButton.clicked[()].connect(self.__privacyClicked)
55 self.__clearButton.clicked[()].connect(self.clear)
56 self.__mw.privacyChanged.connect(self.__privacyButton.setVisible)
57 self.textChanged.connect(self.__textChanged)
58
59 def setBrowser(self, browser):
60 """
61 Public method to set the browser connection.
62
63 @param browser reference to the browser widegt (HelpBrowser)
64 """
65 self.__browser = browser
66 self.__favicon.setBrowser(browser)
67
68 self.__browser.urlChanged.connect(self.__browserUrlChanged)
69 self.__browser.loadProgress.connect(self.update)
70
71 def browser(self):
72 """
73 Public method to get the associated browser (HelpBrowser)
74 """
75 return self.__browser
76
77 def __browserUrlChanged(self, url):
78 """
79 Private slot to handle a URL change of the associated browser.
80
81 @param url new URL of the browser (QUrl)
82 """
83 self.setText(str(url.toEncoded(), encoding = "utf-8"))
84 self.setCursorPosition(0)
85
86 def setPrivateMode(self, on):
87 """
88 Public method to set the private mode.
89
90 @param on flag indicating the privacy state (boolean)
91 """
92 self.__privateMode = on
93 self.__privacyButton.setVisible(on)
94
95 def __privacyClicked(self):
96 """
97 Private slot to handle the click of the private mode button.
98 """
99 self.__mw.setPrivateMode(False)
100
101 def __textChanged(self, txt):
102 """
103 Private slot to handle changes of the text.
104
105 @param txt current text (string)
106 """
107 self.__clearButton.setVisible(txt != "")
108
109 def preferencesChanged(self):
110 """
111 Public slot to handle a change of preferences.
112 """
113 self.update()
114
115 def paintEvent(self, evt):
116 """
117 Protected method handling a paint event.
118
119 @param evt reference to the paint event (QPaintEvent)
120 """
121 if self.__privateMode:
122 backgroundColor = QColor(220, 220, 220) # light gray
123 foregroundColor = Qt.black
124 else:
125 backgroundColor = QApplication.palette().color(QPalette.Base)
126 foregroundColor = QApplication.palette().color(QPalette.Text)
127
128 if self.__browser is not None:
129 p = self.palette()
130 progress = self.__browser.progress()
131 if progress == 0:
132 if self.__browser.url().scheme() == "https":
133 backgroundColor = Preferences.getHelp("SaveUrlColor")
134 p.setBrush(QPalette.Base, backgroundColor);
135 p.setBrush(QPalette.Text, foregroundColor);
136 else:
137 if self.__browser.url().scheme() == "https":
138 backgroundColor = Preferences.getHelp("SaveUrlColor")
139 highlight = QApplication.palette().color(QPalette.Highlight)
140 r = (highlight.red() + 2 * backgroundColor.red()) // 3
141 g = (highlight.green() + 2 * backgroundColor.green()) // 3
142 b = (highlight.blue() + 2 * backgroundColor.blue()) // 3
143
144 loadingColor = QColor(r, g, b)
145 if abs(loadingColor.lightness() - backgroundColor.lightness()) < 20:
146 # special handling for special color schemes (e.g Gaia)
147 r = (2 * highlight.red() + backgroundColor.red()) // 3
148 g = (2 * highlight.green() + backgroundColor.green()) // 3
149 b = (2 * highlight.blue() + backgroundColor.blue()) // 3
150 loadingColor = QColor(r, g, b)
151
152 gradient = QLinearGradient(QPointF(0, 0), QPointF(self.width(), 0))
153 gradient.setColorAt(0, loadingColor)
154 gradient.setColorAt(progress / 100 - 0.000001, loadingColor)
155 gradient.setColorAt(progress / 100, backgroundColor)
156 p.setBrush(QPalette.Base, gradient)
157
158 self.setPalette(p)
159
160 E5LineEdit.paintEvent(self, evt)
161
162 def focusOutEvent(self, evt):
163 """
164 Protected method to handle focus out event.
165
166 @param evt reference to the focus event (QFocusEvent)
167 """
168 if self.text() == "" and self.__browser is not None:
169 self.__browserUrlChanged(self.__browser.url())
170 E5LineEdit.focusOutEvent(self, evt)
171
172 def mouseDoubleClickEvent(self, evt):
173 """
174 Protected method to handle mouse double click events.
175
176 @param evt reference to the mouse event (QMouseEvent)
177 """
178 if evt.button() == Qt.LeftButton:
179 self.selectAll()
180 else:
181 E5LineEdit.mouseDoubleClickEvent(self, evt)
182
183 def keyPressEvent(self, evt):
184 """
185 Protected method to handle key presses.
186
187 @param evt reference to the key press event (QKeyEvent)
188 """
189 if evt.key() == Qt.Key_Escape and self.__browser is not None:
190 self.setText(str(self.__browser.url().toEncoded(), encoding = "utf-8"))
191 self.selectAll()
192 return
193
194 currentText = self.text().strip()
195 if evt.key() in [Qt.Key_Enter, Qt.Key_Return] and \
196 not currentText.lower().startswith("http://"):
197 append = ""
198 if evt.modifiers() == Qt.KeyboardModifiers(Qt.ControlModifier):
199 append = ".com"
200 elif evt.modifiers() == \
201 Qt.KeyboardModifiers(Qt.ControlModifier | Qt.ShiftModifier):
202 append = ".org"
203 elif evt.modifiers() == Qt.KeyboardModifiers(Qt.ShiftModifier):
204 append = ".net"
205
206 if append != "":
207 url = QUrl("http://www." + currentText)
208 host = url.host()
209 if not host.lower().endswith(append):
210 host += append
211 url.setHost(host)
212 self.setText(url.toString())
213
214 E5LineEdit.keyPressEvent(self, evt)
215
216 def dragEnterEvent(self, evt):
217 """
218 Protected method to handle drag enter events.
219
220 @param evt reference to the drag enter event (QDragEnterEvent)
221 """
222 mimeData = evt.mimeData()
223 if mimeData.hasUrls() or mimeData.hasText():
224 evt.acceptProposedAction()
225
226 E5LineEdit.dragEnterEvent(self, evt)
227
228 def dropEvent(self, evt):
229 """
230 Protected method to handle drop events.
231
232 @param evt reference to the drop event (QDropEvent)
233 """
234 mimeData = evt.mimeData()
235
236 url = QUrl()
237 if mimeData.hasUrls():
238 url = mimeData.urls()[0]
239 elif mimeData.hasText():
240 url = QUrl.fromEncoded(mimeData.text().encode(), QUrl.TolerantMode)
241
242 if url.isEmpty() or not url.isValid():
243 E5LineEdit.dropEvent(self, evt)
244 return
245
246 self.setText(str(url.toEncoded(), encoding = "utf-8"))
247 self.selectAll()
248
249 evt.acceptProposedAction()

eric ide

mercurial