3 from __future__ import with_statement
4 from __future__ import division
8 import util.qt_compat as qt_compat
9 QtCore = qt_compat.QtCore
10 QtGui = qt_compat.import_module("QtGui")
13 from util import qui_utils
14 from util import misc as misc_utils
18 _moduleLogger = logging.getLogger(__name__)
21 class FavoritesWindow(object):
23 def __init__(self, parent, app, source, hidden):
25 self._source = list(source)
28 self._categories = QtGui.QTreeWidget()
29 self._categories.setHeaderLabels(["Categories"])
30 self._categories.setHeaderHidden(True)
31 self._categories.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
32 if not constants.IS_MAEMO:
33 self._categories.setAlternatingRowColors(True)
34 self._childWidgets = []
35 for catName in self._source:
36 twi = QtGui.QTreeWidgetItem(self._categories)
37 twi.setText(0, catName)
38 self._childWidgets.append(twi)
39 if catName in self._hidden:
40 twi.setCheckState(0, QtCore.Qt.Unchecked)
42 twi.setCheckState(0, QtCore.Qt.Checked)
43 self._categories.itemChanged.connect(self._on_item_changed)
45 self._allButton = QtGui.QPushButton("All")
46 self._allButton.clicked.connect(self._on_select_all)
47 self._invertButton = QtGui.QPushButton("Invert")
48 self._invertButton.clicked.connect(self._on_invert_selection)
49 self._noneButton = QtGui.QPushButton("None")
50 self._noneButton.clicked.connect(self._on_select_none)
52 self._buttonLayout = QtGui.QHBoxLayout()
53 self._buttonLayout.addWidget(self._allButton)
54 self._buttonLayout.addWidget(self._invertButton)
55 self._buttonLayout.addWidget(self._noneButton)
57 self._layout = QtGui.QVBoxLayout()
58 self._layout.addWidget(self._categories)
59 self._layout.addLayout(self._buttonLayout)
61 centralWidget = QtGui.QWidget()
62 centralWidget.setLayout(self._layout)
64 self._window = QtGui.QMainWindow(parent)
65 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
66 qui_utils.set_stackable(self._window, True)
67 self._window.setWindowTitle("%s - Favorites" % constants.__pretty_app_name__)
68 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
69 self._window.setCentralWidget(centralWidget)
71 self._closeWindowAction = QtGui.QAction(None)
72 self._closeWindowAction.setText("Close")
73 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
74 self._closeWindowAction.triggered.connect(self._on_close_window)
76 if constants.IS_MAEMO:
77 self._window.addAction(self._closeWindowAction)
78 self._window.addAction(self._app.quitAction)
79 self._window.addAction(self._app.fullscreenAction)
81 fileMenu = self._window.menuBar().addMenu("&Units")
82 fileMenu.addAction(self._closeWindowAction)
83 fileMenu.addAction(self._app.quitAction)
85 viewMenu = self._window.menuBar().addMenu("&View")
86 viewMenu.addAction(self._app.fullscreenAction)
88 self._window.addAction(self._app.logAction)
90 self.set_fullscreen(self._app.fullscreenAction.isChecked())
106 def set_fullscreen(self, isFullscreen):
108 self._window.showFullScreen()
110 self._window.showNormal()
112 @misc_utils.log_exception(_moduleLogger)
113 def _on_select_all(self, checked = False):
114 for child in self._childWidgets:
115 child.setCheckState(0, QtCore.Qt.Checked)
117 @misc_utils.log_exception(_moduleLogger)
118 def _on_invert_selection(self, checked = False):
119 for child in self._childWidgets:
120 state = child.checkState(0)
121 if state == QtCore.Qt.Unchecked:
122 newState = QtCore.Qt.Checked
123 elif state == QtCore.Qt.Checked:
124 newState = QtCore.Qt.Unchecked
126 raise RuntimeError("Bad check state %r" % state)
127 child.setCheckState(0, newState)
129 @misc_utils.log_exception(_moduleLogger)
130 def _on_select_none(self, checked = False):
131 for child in self._childWidgets:
132 child.setCheckState(0, QtCore.Qt.Unchecked)
134 @misc_utils.log_exception(_moduleLogger)
135 def _on_item_changed(self, item, column):
136 state = item.checkState(column)
137 if state == QtCore.Qt.Unchecked:
138 name = str(item.text(column))
139 self._hidden.add(name)
140 elif state == QtCore.Qt.Checked:
141 name = str(item.text(column))
142 self._hidden.remove(name)
144 raise RuntimeError("Bad check state %r" % state)
146 @misc_utils.log_exception(_moduleLogger)
147 def _on_close_window(self, checked = True):
151 class QuickJump(object):
155 def __init__(self, parent, app):
158 self._searchLabel = QtGui.QLabel("Search:")
159 self._searchEntry = QtGui.QLineEdit("")
160 self._searchEntry.textEdited.connect(self._on_search_edited)
162 self._entryLayout = QtGui.QHBoxLayout()
163 self._entryLayout.addWidget(self._searchLabel)
164 self._entryLayout.addWidget(self._searchEntry)
166 self._resultsBox = QtGui.QTreeWidget()
167 self._resultsBox.setHeaderLabels(["Categories", "Units"])
168 self._resultsBox.setHeaderHidden(True)
169 if not constants.IS_MAEMO:
170 self._resultsBox.setAlternatingRowColors(True)
171 self._resultsBox.itemClicked.connect(self._on_result_clicked)
173 self._layout = QtGui.QVBoxLayout()
174 self._layout.addLayout(self._entryLayout)
175 self._layout.addWidget(self._resultsBox)
177 centralWidget = QtGui.QWidget()
178 centralWidget.setLayout(self._layout)
180 self._window = QtGui.QMainWindow(parent)
181 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
182 qui_utils.set_stackable(self._window, True)
183 self._window.setWindowTitle("%s - Quick Jump" % constants.__pretty_app_name__)
184 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
185 self._window.setCentralWidget(centralWidget)
187 self._closeWindowAction = QtGui.QAction(None)
188 self._closeWindowAction.setText("Close")
189 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
190 self._closeWindowAction.triggered.connect(self._on_close_window)
192 if constants.IS_MAEMO:
193 self._window.addAction(self._closeWindowAction)
194 self._window.addAction(self._app.quitAction)
195 self._window.addAction(self._app.fullscreenAction)
197 fileMenu = self._window.menuBar().addMenu("&Units")
198 fileMenu.addAction(self._closeWindowAction)
199 fileMenu.addAction(self._app.quitAction)
201 viewMenu = self._window.menuBar().addMenu("&View")
202 viewMenu.addAction(self._app.fullscreenAction)
204 self._window.addAction(self._app.logAction)
206 self.set_fullscreen(self._app.fullscreenAction.isChecked())
222 def set_fullscreen(self, isFullscreen):
224 self._window.showFullScreen()
226 self._window.showNormal()
228 @misc_utils.log_exception(_moduleLogger)
229 def _on_close_window(self, checked = True):
232 @misc_utils.log_exception(_moduleLogger)
233 def _on_result_clicked(self, item, columnIndex):
234 categoryName = unicode(item.text(0))
235 unitName = unicode(item.text(1))
236 catWindow = self._app.request_category()
237 unitsWindow = catWindow.select_category(categoryName)
238 unitsWindow.select_unit(unitName)
241 @misc_utils.log_exception(_moduleLogger)
242 def _on_search_edited(self, *args):
243 userInput = self._searchEntry.text()
244 if len(userInput) < self.MINIMAL_ENTRY:
247 self._resultsBox.clear()
248 lowerInput = str(userInput).lower()
249 for catIndex, category in enumerate(unit_data.UNIT_CATEGORIES):
250 units = unit_data.get_units(category)
251 for unitIndex, unit in enumerate(units):
252 loweredUnit = unit.lower()
253 if lowerInput in loweredUnit:
254 twi = QtGui.QTreeWidgetItem(self._resultsBox)
255 twi.setText(0, category)
259 class Recent(object):
261 def __init__(self, parent, app):
264 self._resultsBox = QtGui.QTreeWidget()
265 self._resultsBox.setHeaderLabels(["Categories", "Units"])
266 self._resultsBox.setHeaderHidden(True)
267 if not constants.IS_MAEMO:
268 self._resultsBox.setAlternatingRowColors(True)
269 self._resultsBox.itemClicked.connect(self._on_result_clicked)
271 self._layout = QtGui.QVBoxLayout()
272 self._layout.addWidget(self._resultsBox)
274 centralWidget = QtGui.QWidget()
275 centralWidget.setLayout(self._layout)
277 self._window = QtGui.QMainWindow(parent)
278 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
279 qui_utils.set_stackable(self._window, True)
280 self._window.setWindowTitle("%s - Recent" % constants.__pretty_app_name__)
281 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
282 self._window.setCentralWidget(centralWidget)
284 for cat, unit in self._app.get_recent():
285 twi = QtGui.QTreeWidgetItem(self._resultsBox)
289 self._closeWindowAction = QtGui.QAction(None)
290 self._closeWindowAction.setText("Close")
291 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
292 self._closeWindowAction.triggered.connect(self._on_close_window)
294 if constants.IS_MAEMO:
295 self._window.addAction(self._closeWindowAction)
296 self._window.addAction(self._app.quitAction)
297 self._window.addAction(self._app.fullscreenAction)
299 fileMenu = self._window.menuBar().addMenu("&Units")
300 fileMenu.addAction(self._closeWindowAction)
301 fileMenu.addAction(self._app.quitAction)
303 viewMenu = self._window.menuBar().addMenu("&View")
304 viewMenu.addAction(self._app.fullscreenAction)
306 self._window.addAction(self._app.logAction)
308 self.set_fullscreen(self._app.fullscreenAction.isChecked())
324 def set_fullscreen(self, isFullscreen):
326 self._window.showFullScreen()
328 self._window.showNormal()
330 @misc_utils.log_exception(_moduleLogger)
331 def _on_close_window(self, checked = True):
334 @misc_utils.log_exception(_moduleLogger)
335 def _on_result_clicked(self, item, columnIndex):
336 categoryName = unicode(item.text(0))
337 unitName = unicode(item.text(1))
338 catWindow = self._app.request_category()
339 unitsWindow = catWindow.select_category(categoryName)
340 unitsWindow.select_unit(unitName)