Pulling in latest skeleton changes
[gonvert] / src / windows.py
1 #!/usr/bin/env python
2
3 from __future__ import with_statement
4 from __future__ import division
5
6 import logging
7
8 from PyQt4 import QtGui
9 from PyQt4 import QtCore
10
11 import constants
12 import maeqt
13 from util import misc as misc_utils
14 import unit_data
15
16
17 _moduleLogger = logging.getLogger(__name__)
18
19
20 class FavoritesWindow(object):
21
22         def __init__(self, parent, app, source, hidden):
23                 self._app = app
24                 self._source = list(source)
25                 self._hidden = hidden
26
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)
40                         else:
41                                 twi.setCheckState(0, QtCore.Qt.Checked)
42                 self._categories.itemChanged.connect(self._on_item_changed)
43
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)
50
51                 self._buttonLayout = QtGui.QHBoxLayout()
52                 self._buttonLayout.addWidget(self._allButton)
53                 self._buttonLayout.addWidget(self._invertButton)
54                 self._buttonLayout.addWidget(self._noneButton)
55
56                 self._layout = QtGui.QVBoxLayout()
57                 self._layout.addWidget(self._categories)
58                 self._layout.addLayout(self._buttonLayout)
59
60                 centralWidget = QtGui.QWidget()
61                 centralWidget.setLayout(self._layout)
62
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)
70
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)
75
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)
80                 else:
81                         fileMenu = self._window.menuBar().addMenu("&Units")
82                         fileMenu.addAction(self._closeWindowAction)
83                         fileMenu.addAction(self._app.quitAction)
84
85                         viewMenu = self._window.menuBar().addMenu("&View")
86                         viewMenu.addAction(self._app.fullscreenAction)
87
88                 self._window.addAction(self._app.logAction)
89
90                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
91                 self._window.show()
92
93         @property
94         def window(self):
95                 return self._window
96
97         def show(self):
98                 self._window.show()
99
100         def hide(self):
101                 self._window.hide()
102
103         def close(self):
104                 self._window.close()
105
106         def set_fullscreen(self, isFullscreen):
107                 if isFullscreen:
108                         self._window.showFullScreen()
109                 else:
110                         self._window.showNormal()
111
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)
116
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
125                         else:
126                                 raise RuntimeError("Bad check state %r" % state)
127                         child.setCheckState(0, newState)
128
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)
133
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)
143                 else:
144                         raise RuntimeError("Bad check state %r" % state)
145
146         @misc_utils.log_exception(_moduleLogger)
147         def _on_close_window(self, checked = True):
148                 self.close()
149
150
151 class QuickJump(object):
152
153         MINIMAL_ENTRY = 3
154
155         def __init__(self, parent, app):
156                 self._app = app
157
158                 self._searchLabel = QtGui.QLabel("Search:")
159                 self._searchEntry = QtGui.QLineEdit("")
160                 self._searchEntry.textEdited.connect(self._on_search_edited)
161
162                 self._entryLayout = QtGui.QHBoxLayout()
163                 self._entryLayout.addWidget(self._searchLabel)
164                 self._entryLayout.addWidget(self._searchEntry)
165
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)
172
173                 self._layout = QtGui.QVBoxLayout()
174                 self._layout.addLayout(self._entryLayout)
175                 self._layout.addWidget(self._resultsBox)
176
177                 centralWidget = QtGui.QWidget()
178                 centralWidget.setLayout(self._layout)
179
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)
187
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)
192
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)
197                 else:
198                         fileMenu = self._window.menuBar().addMenu("&Units")
199                         fileMenu.addAction(self._closeWindowAction)
200                         fileMenu.addAction(self._app.quitAction)
201
202                         viewMenu = self._window.menuBar().addMenu("&View")
203                         viewMenu.addAction(self._app.fullscreenAction)
204
205                 self._window.addAction(self._app.logAction)
206
207                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
208                 self._window.show()
209
210         @property
211         def window(self):
212                 return self._window
213
214         def show(self):
215                 self._window.show()
216
217         def hide(self):
218                 self._window.hide()
219
220         def close(self):
221                 self._window.close()
222
223         def set_fullscreen(self, isFullscreen):
224                 if isFullscreen:
225                         self._window.showFullScreen()
226                 else:
227                         self._window.showNormal()
228
229         @misc_utils.log_exception(_moduleLogger)
230         def _on_close_window(self, checked = True):
231                 self.close()
232
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)
240                 self.close()
241
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:
246                         return
247
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)
257                                         twi.setText(1, unit)
258
259
260 class Recent(object):
261
262         def __init__(self, parent, app):
263                 self._app = app
264
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)
271
272                 self._layout = QtGui.QVBoxLayout()
273                 self._layout.addWidget(self._resultsBox)
274
275                 centralWidget = QtGui.QWidget()
276                 centralWidget.setLayout(self._layout)
277
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)
285
286                 for cat, unit in self._app.get_recent():
287                         twi = QtGui.QTreeWidgetItem(self._resultsBox)
288                         twi.setText(0, cat)
289                         twi.setText(1, unit)
290
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)
295
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)
300                 else:
301                         fileMenu = self._window.menuBar().addMenu("&Units")
302                         fileMenu.addAction(self._closeWindowAction)
303                         fileMenu.addAction(self._app.quitAction)
304
305                         viewMenu = self._window.menuBar().addMenu("&View")
306                         viewMenu.addAction(self._app.fullscreenAction)
307
308                 self._window.addAction(self._app.logAction)
309
310                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
311                 self._window.show()
312
313         @property
314         def window(self):
315                 return self._window
316
317         def show(self):
318                 self._window.show()
319
320         def hide(self):
321                 self._window.hide()
322
323         def close(self):
324                 self._window.close()
325
326         def set_fullscreen(self, isFullscreen):
327                 if isFullscreen:
328                         self._window.showFullScreen()
329                 else:
330                         self._window.showNormal()
331
332         @misc_utils.log_exception(_moduleLogger)
333         def _on_close_window(self, checked = True):
334                 self.close()
335
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)
343                 self.close()