3 from __future__ import with_statement
4 from __future__ import division
8 from PyQt4 import QtGui
9 from PyQt4 import QtCore
12 from util import qui_utils
13 from util import misc as misc_utils
17 _moduleLogger = logging.getLogger(__name__)
20 class FavoritesWindow(object):
22 def __init__(self, parent, app, source, hidden):
24 self._source = list(source)
27 self._categories = QtGui.QTreeWidget()
28 self._categories.setHeaderLabels(["Categories"])
29 self._categories.setHeaderHidden(True)
30 self._categories.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
31 if not constants.IS_MAEMO:
32 self._categories.setAlternatingRowColors(True)
33 self._childWidgets = []
34 for catName in self._source:
35 twi = QtGui.QTreeWidgetItem(self._categories)
36 twi.setText(0, catName)
37 self._childWidgets.append(twi)
38 if catName in self._hidden:
39 twi.setCheckState(0, QtCore.Qt.Unchecked)
41 twi.setCheckState(0, QtCore.Qt.Checked)
42 self._categories.itemChanged.connect(self._on_item_changed)
44 self._allButton = QtGui.QPushButton("All")
45 self._allButton.clicked.connect(self._on_select_all)
46 self._invertButton = QtGui.QPushButton("Invert")
47 self._invertButton.clicked.connect(self._on_invert_selection)
48 self._noneButton = QtGui.QPushButton("None")
49 self._noneButton.clicked.connect(self._on_select_none)
51 self._buttonLayout = QtGui.QHBoxLayout()
52 self._buttonLayout.addWidget(self._allButton)
53 self._buttonLayout.addWidget(self._invertButton)
54 self._buttonLayout.addWidget(self._noneButton)
56 self._layout = QtGui.QVBoxLayout()
57 self._layout.addWidget(self._categories)
58 self._layout.addLayout(self._buttonLayout)
60 centralWidget = QtGui.QWidget()
61 centralWidget.setLayout(self._layout)
63 self._window = QtGui.QMainWindow(parent)
64 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
65 qui_utils.set_stackable(self._window, True)
66 self._window.setWindowTitle("%s - Favorites" % constants.__pretty_app_name__)
67 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
68 self._window.setCentralWidget(centralWidget)
70 self._closeWindowAction = QtGui.QAction(None)
71 self._closeWindowAction.setText("Close")
72 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
73 self._closeWindowAction.triggered.connect(self._on_close_window)
75 if constants.IS_MAEMO:
76 self._window.addAction(self._closeWindowAction)
77 self._window.addAction(self._app.quitAction)
78 self._window.addAction(self._app.fullscreenAction)
80 fileMenu = self._window.menuBar().addMenu("&Units")
81 fileMenu.addAction(self._closeWindowAction)
82 fileMenu.addAction(self._app.quitAction)
84 viewMenu = self._window.menuBar().addMenu("&View")
85 viewMenu.addAction(self._app.fullscreenAction)
87 self._window.addAction(self._app.logAction)
89 self.set_fullscreen(self._app.fullscreenAction.isChecked())
105 def set_fullscreen(self, isFullscreen):
107 self._window.showFullScreen()
109 self._window.showNormal()
111 @misc_utils.log_exception(_moduleLogger)
112 def _on_select_all(self, checked = False):
113 for child in self._childWidgets:
114 child.setCheckState(0, QtCore.Qt.Checked)
116 @misc_utils.log_exception(_moduleLogger)
117 def _on_invert_selection(self, checked = False):
118 for child in self._childWidgets:
119 state = child.checkState(0)
120 if state == QtCore.Qt.Unchecked:
121 newState = QtCore.Qt.Checked
122 elif state == QtCore.Qt.Checked:
123 newState = QtCore.Qt.Unchecked
125 raise RuntimeError("Bad check state %r" % state)
126 child.setCheckState(0, newState)
128 @misc_utils.log_exception(_moduleLogger)
129 def _on_select_none(self, checked = False):
130 for child in self._childWidgets:
131 child.setCheckState(0, QtCore.Qt.Unchecked)
133 @misc_utils.log_exception(_moduleLogger)
134 def _on_item_changed(self, item, column):
135 state = item.checkState(column)
136 if state == QtCore.Qt.Unchecked:
137 name = str(item.text(column))
138 self._hidden.add(name)
139 elif state == QtCore.Qt.Checked:
140 name = str(item.text(column))
141 self._hidden.remove(name)
143 raise RuntimeError("Bad check state %r" % state)
145 @misc_utils.log_exception(_moduleLogger)
146 def _on_close_window(self, checked = True):
150 class QuickJump(object):
154 def __init__(self, parent, app):
157 self._searchLabel = QtGui.QLabel("Search:")
158 self._searchEntry = QtGui.QLineEdit("")
159 self._searchEntry.textEdited.connect(self._on_search_edited)
161 self._entryLayout = QtGui.QHBoxLayout()
162 self._entryLayout.addWidget(self._searchLabel)
163 self._entryLayout.addWidget(self._searchEntry)
165 self._resultsBox = QtGui.QTreeWidget()
166 self._resultsBox.setHeaderLabels(["Categories", "Units"])
167 self._resultsBox.setHeaderHidden(True)
168 if not constants.IS_MAEMO:
169 self._resultsBox.setAlternatingRowColors(True)
170 self._resultsBox.itemClicked.connect(self._on_result_clicked)
172 self._layout = QtGui.QVBoxLayout()
173 self._layout.addLayout(self._entryLayout)
174 self._layout.addWidget(self._resultsBox)
176 centralWidget = QtGui.QWidget()
177 centralWidget.setLayout(self._layout)
179 self._window = QtGui.QMainWindow(parent)
180 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
181 qui_utils.set_stackable(self._window, True)
182 self._window.setWindowTitle("%s - Quick Jump" % constants.__pretty_app_name__)
183 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
184 self._window.setCentralWidget(centralWidget)
186 self._closeWindowAction = QtGui.QAction(None)
187 self._closeWindowAction.setText("Close")
188 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
189 self._closeWindowAction.triggered.connect(self._on_close_window)
191 if constants.IS_MAEMO:
192 self._window.addAction(self._closeWindowAction)
193 self._window.addAction(self._app.quitAction)
194 self._window.addAction(self._app.fullscreenAction)
196 fileMenu = self._window.menuBar().addMenu("&Units")
197 fileMenu.addAction(self._closeWindowAction)
198 fileMenu.addAction(self._app.quitAction)
200 viewMenu = self._window.menuBar().addMenu("&View")
201 viewMenu.addAction(self._app.fullscreenAction)
203 self._window.addAction(self._app.logAction)
205 self.set_fullscreen(self._app.fullscreenAction.isChecked())
221 def set_fullscreen(self, isFullscreen):
223 self._window.showFullScreen()
225 self._window.showNormal()
227 @misc_utils.log_exception(_moduleLogger)
228 def _on_close_window(self, checked = True):
231 @misc_utils.log_exception(_moduleLogger)
232 def _on_result_clicked(self, item, columnIndex):
233 categoryName = unicode(item.text(0))
234 unitName = unicode(item.text(1))
235 catWindow = self._app.request_category()
236 unitsWindow = catWindow.select_category(categoryName)
237 unitsWindow.select_unit(unitName)
240 @misc_utils.log_exception(_moduleLogger)
241 def _on_search_edited(self, *args):
242 userInput = self._searchEntry.text()
243 if len(userInput) < self.MINIMAL_ENTRY:
246 self._resultsBox.clear()
247 lowerInput = str(userInput).lower()
248 for catIndex, category in enumerate(unit_data.UNIT_CATEGORIES):
249 units = unit_data.get_units(category)
250 for unitIndex, unit in enumerate(units):
251 loweredUnit = unit.lower()
252 if lowerInput in loweredUnit:
253 twi = QtGui.QTreeWidgetItem(self._resultsBox)
254 twi.setText(0, category)
258 class Recent(object):
260 def __init__(self, parent, app):
263 self._resultsBox = QtGui.QTreeWidget()
264 self._resultsBox.setHeaderLabels(["Categories", "Units"])
265 self._resultsBox.setHeaderHidden(True)
266 if not constants.IS_MAEMO:
267 self._resultsBox.setAlternatingRowColors(True)
268 self._resultsBox.itemClicked.connect(self._on_result_clicked)
270 self._layout = QtGui.QVBoxLayout()
271 self._layout.addWidget(self._resultsBox)
273 centralWidget = QtGui.QWidget()
274 centralWidget.setLayout(self._layout)
276 self._window = QtGui.QMainWindow(parent)
277 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
278 qui_utils.set_stackable(self._window, True)
279 self._window.setWindowTitle("%s - Recent" % constants.__pretty_app_name__)
280 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
281 self._window.setCentralWidget(centralWidget)
283 for cat, unit in self._app.get_recent():
284 twi = QtGui.QTreeWidgetItem(self._resultsBox)
288 self._closeWindowAction = QtGui.QAction(None)
289 self._closeWindowAction.setText("Close")
290 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
291 self._closeWindowAction.triggered.connect(self._on_close_window)
293 if constants.IS_MAEMO:
294 self._window.addAction(self._closeWindowAction)
295 self._window.addAction(self._app.quitAction)
296 self._window.addAction(self._app.fullscreenAction)
298 fileMenu = self._window.menuBar().addMenu("&Units")
299 fileMenu.addAction(self._closeWindowAction)
300 fileMenu.addAction(self._app.quitAction)
302 viewMenu = self._window.menuBar().addMenu("&View")
303 viewMenu.addAction(self._app.fullscreenAction)
305 self._window.addAction(self._app.logAction)
307 self.set_fullscreen(self._app.fullscreenAction.isChecked())
323 def set_fullscreen(self, isFullscreen):
325 self._window.showFullScreen()
327 self._window.showNormal()
329 @misc_utils.log_exception(_moduleLogger)
330 def _on_close_window(self, checked = True):
333 @misc_utils.log_exception(_moduleLogger)
334 def _on_result_clicked(self, item, columnIndex):
335 categoryName = unicode(item.text(0))
336 unitName = unicode(item.text(1))
337 catWindow = self._app.request_category()
338 unitsWindow = catWindow.select_category(categoryName)
339 unitsWindow.select_unit(unitName)