3 from __future__ import with_statement
4 from __future__ import division
8 from PyQt4 import QtGui
9 from PyQt4 import QtCore
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 maeqt.set_autorient(self._window, True)
66 maeqt.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 maeqt.set_autorient(self._window, True)
183 maeqt.set_stackable(self._window, True)
184 self._window.setWindowTitle("%s - Quick Jump" % constants.__pretty_app_name__)
185 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
186 self._window.setCentralWidget(centralWidget)
188 self._closeWindowAction = QtGui.QAction(None)
189 self._closeWindowAction.setText("Close")
190 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
191 self._closeWindowAction.triggered.connect(self._on_close_window)
193 if constants.IS_MAEMO:
194 self._window.addAction(self._closeWindowAction)
195 self._window.addAction(self._app.quitAction)
196 self._window.addAction(self._app.fullscreenAction)
198 fileMenu = self._window.menuBar().addMenu("&Units")
199 fileMenu.addAction(self._closeWindowAction)
200 fileMenu.addAction(self._app.quitAction)
202 viewMenu = self._window.menuBar().addMenu("&View")
203 viewMenu.addAction(self._app.fullscreenAction)
205 self._window.addAction(self._app.logAction)
207 self.set_fullscreen(self._app.fullscreenAction.isChecked())
223 def set_fullscreen(self, isFullscreen):
225 self._window.showFullScreen()
227 self._window.showNormal()
229 @misc_utils.log_exception(_moduleLogger)
230 def _on_close_window(self, checked = True):
233 @misc_utils.log_exception(_moduleLogger)
234 def _on_result_clicked(self, item, columnIndex):
235 categoryName = unicode(item.text(0))
236 unitName = unicode(item.text(1))
237 catWindow = self._app.request_category()
238 unitsWindow = catWindow.select_category(categoryName)
239 unitsWindow.select_unit(unitName)
242 @misc_utils.log_exception(_moduleLogger)
243 def _on_search_edited(self, *args):
244 userInput = self._searchEntry.text()
245 if len(userInput) < self.MINIMAL_ENTRY:
248 self._resultsBox.clear()
249 lowerInput = str(userInput).lower()
250 for catIndex, category in enumerate(unit_data.UNIT_CATEGORIES):
251 units = unit_data.get_units(category)
252 for unitIndex, unit in enumerate(units):
253 loweredUnit = unit.lower()
254 if lowerInput in loweredUnit:
255 twi = QtGui.QTreeWidgetItem(self._resultsBox)
256 twi.setText(0, category)
260 class Recent(object):
262 def __init__(self, parent, app):
265 self._resultsBox = QtGui.QTreeWidget()
266 self._resultsBox.setHeaderLabels(["Categories", "Units"])
267 self._resultsBox.setHeaderHidden(True)
268 if not constants.IS_MAEMO:
269 self._resultsBox.setAlternatingRowColors(True)
270 self._resultsBox.itemClicked.connect(self._on_result_clicked)
272 self._layout = QtGui.QVBoxLayout()
273 self._layout.addWidget(self._resultsBox)
275 centralWidget = QtGui.QWidget()
276 centralWidget.setLayout(self._layout)
278 self._window = QtGui.QMainWindow(parent)
279 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
280 maeqt.set_autorient(self._window, True)
281 maeqt.set_stackable(self._window, True)
282 self._window.setWindowTitle("%s - Recent" % constants.__pretty_app_name__)
283 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
284 self._window.setCentralWidget(centralWidget)
286 for cat, unit in self._app.get_recent():
287 twi = QtGui.QTreeWidgetItem(self._resultsBox)
291 self._closeWindowAction = QtGui.QAction(None)
292 self._closeWindowAction.setText("Close")
293 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
294 self._closeWindowAction.triggered.connect(self._on_close_window)
296 if constants.IS_MAEMO:
297 self._window.addAction(self._closeWindowAction)
298 self._window.addAction(self._app.quitAction)
299 self._window.addAction(self._app.fullscreenAction)
301 fileMenu = self._window.menuBar().addMenu("&Units")
302 fileMenu.addAction(self._closeWindowAction)
303 fileMenu.addAction(self._app.quitAction)
305 viewMenu = self._window.menuBar().addMenu("&View")
306 viewMenu.addAction(self._app.fullscreenAction)
308 self._window.addAction(self._app.logAction)
310 self.set_fullscreen(self._app.fullscreenAction.isChecked())
326 def set_fullscreen(self, isFullscreen):
328 self._window.showFullScreen()
330 self._window.showNormal()
332 @misc_utils.log_exception(_moduleLogger)
333 def _on_close_window(self, checked = True):
336 @misc_utils.log_exception(_moduleLogger)
337 def _on_result_clicked(self, item, columnIndex):
338 categoryName = unicode(item.text(0))
339 unitName = unicode(item.text(1))
340 catWindow = self._app.request_category()
341 unitsWindow = catWindow.select_category(categoryName)
342 unitsWindow.select_unit(unitName)