Bump to 1.1.6 to fix a user issue
[gonvert] / gonvert / 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 import util.qt_compat as qt_compat
9 QtCore = qt_compat.QtCore
10 QtGui = qt_compat.import_module("QtGui")
11
12 import constants
13 from util import qui_utils
14 from util import misc as misc_utils
15 import unit_data
16
17
18 _moduleLogger = logging.getLogger(__name__)
19
20
21 class FavoritesWindow(object):
22
23         def __init__(self, parent, app, source, hidden):
24                 self._app = app
25                 self._source = list(source)
26                 self._hidden = hidden
27
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)
41                         else:
42                                 twi.setCheckState(0, QtCore.Qt.Checked)
43                 self._categories.itemChanged.connect(self._on_item_changed)
44
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)
51
52                 self._buttonLayout = QtGui.QHBoxLayout()
53                 self._buttonLayout.addWidget(self._allButton)
54                 self._buttonLayout.addWidget(self._invertButton)
55                 self._buttonLayout.addWidget(self._noneButton)
56
57                 self._layout = QtGui.QVBoxLayout()
58                 self._layout.addWidget(self._categories)
59                 self._layout.addLayout(self._buttonLayout)
60
61                 centralWidget = QtGui.QWidget()
62                 centralWidget.setLayout(self._layout)
63
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)
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                 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)
186
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)
191
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)
196                 else:
197                         fileMenu = self._window.menuBar().addMenu("&Units")
198                         fileMenu.addAction(self._closeWindowAction)
199                         fileMenu.addAction(self._app.quitAction)
200
201                         viewMenu = self._window.menuBar().addMenu("&View")
202                         viewMenu.addAction(self._app.fullscreenAction)
203
204                 self._window.addAction(self._app.logAction)
205
206                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
207                 self._window.show()
208
209         @property
210         def window(self):
211                 return self._window
212
213         def show(self):
214                 self._window.show()
215
216         def hide(self):
217                 self._window.hide()
218
219         def close(self):
220                 self._window.close()
221
222         def set_fullscreen(self, isFullscreen):
223                 if isFullscreen:
224                         self._window.showFullScreen()
225                 else:
226                         self._window.showNormal()
227
228         @misc_utils.log_exception(_moduleLogger)
229         def _on_close_window(self, checked = True):
230                 self.close()
231
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)
239                 self.close()
240
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:
245                         return
246
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)
256                                         twi.setText(1, unit)
257
258
259 class Recent(object):
260
261         def __init__(self, parent, app):
262                 self._app = app
263
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)
270
271                 self._layout = QtGui.QVBoxLayout()
272                 self._layout.addWidget(self._resultsBox)
273
274                 centralWidget = QtGui.QWidget()
275                 centralWidget.setLayout(self._layout)
276
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)
283
284                 for cat, unit in self._app.get_recent():
285                         twi = QtGui.QTreeWidgetItem(self._resultsBox)
286                         twi.setText(0, cat)
287                         twi.setText(1, unit)
288
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)
293
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)
298                 else:
299                         fileMenu = self._window.menuBar().addMenu("&Units")
300                         fileMenu.addAction(self._closeWindowAction)
301                         fileMenu.addAction(self._app.quitAction)
302
303                         viewMenu = self._window.menuBar().addMenu("&View")
304                         viewMenu.addAction(self._app.fullscreenAction)
305
306                 self._window.addAction(self._app.logAction)
307
308                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
309                 self._window.show()
310
311         @property
312         def window(self):
313                 return self._window
314
315         def show(self):
316                 self._window.show()
317
318         def hide(self):
319                 self._window.hide()
320
321         def close(self):
322                 self._window.close()
323
324         def set_fullscreen(self, isFullscreen):
325                 if isFullscreen:
326                         self._window.showFullScreen()
327                 else:
328                         self._window.showNormal()
329
330         @misc_utils.log_exception(_moduleLogger)
331         def _on_close_window(self, checked = True):
332                 self.close()
333
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)
341                 self.close()