4 from __future__ import with_statement
12 from PyQt4 import QtGui
13 from PyQt4 import QtCore
27 _moduleLogger = logging.getLogger("gonvert_glade")
29 if gettext is not None:
30 gettext.bindtextdomain('gonvert', '/usr/share/locale')
31 gettext.textdomain('gonvert')
34 def change_menu_label(widgets, labelname, newtext):
35 item_label = widgets.get_widget(labelname).get_children()[0]
36 item_label.set_text(newtext)
39 def split_number(number):
41 fractional, integer = math.modf(number)
43 integerDisplay = number
44 fractionalDisplay = ""
46 integerDisplay = str(integer)
47 fractionalDisplay = str(fractional)
48 if "e+" in integerDisplay:
49 integerDisplay = number
50 fractionalDisplay = ""
51 elif "e-" in fractionalDisplay and 0.0 < integer:
52 integerDisplay = number
53 fractionalDisplay = ""
54 elif "e-" in fractionalDisplay:
56 fractionalDisplay = number
58 integerDisplay = integerDisplay.split(".", 1)[0] + "."
59 fractionalDisplay = fractionalDisplay.rsplit(".", 1)[-1]
61 return integerDisplay, fractionalDisplay
64 class Gonvert(object):
68 self._unitCategory = QtGui.QPushButton()
70 self._selectedUnitName = QtGui.QLabel()
71 self._selectedUnitValue = QtGui.QLineEdit()
72 self._selectedUnitSymbol = QtGui.QLabel()
74 self._selectedUnitLayout = QtGui.QHBoxLayout()
75 self._selectedUnitLayout.addWidget(self._selectedUnitName)
76 self._selectedUnitLayout.addWidget(self._selectedUnitValue)
77 self._selectedUnitLayout.addWidget(self._selectedUnitSymbol)
79 self._units = QtGui.QTreeWidget()
81 self._searchButton = QtGui.QPushButton()
82 self._searchEntry = QtGui.QLineEdit()
83 self._searchCloseButton = QtGui.QPushButton()
85 self._searchLayout = QtGui.QHBoxLayout()
86 self._searchLayout.addWidget(self._searchButton)
87 self._searchLayout.addWidget(self._searchEntry)
88 self._searchLayout.addWidget(self._searchCloseButton)
90 self._layout = QtGui.QVBoxLayout()
91 self._layout.addWidget(self._unitCategory)
92 self._layout.addLayout(self._selectedUnitLayout)
93 self._layout.addWidget(self._units)
94 self._layout.addLayout(self._searchLayout)
96 centralWidget = QtGui.QWidget()
97 centralWidget.setLayout(self._layout)
99 self._window = QtGui.QMainWindow()
100 self._window.setCentralWidget(centralWidget)
101 #self._load_settings()
105 def _load_settings(self):
106 #Restore window size from previously saved settings if it exists and is valid.
107 windowDatPath = "/".join((constants._data_path_, "window.dat"))
108 if os.path.exists(windowDatPath):
109 saved_window = pickle.load(open(windowDatPath, "r"))
111 a, b = saved_window['size']
115 self._mainWindow.resize(a, b)
117 isFullscreen = saved_window["isFullscreen"]
122 self._mainWindow.fullscreen()
124 isPortrait = saved_window["isPortrait"]
128 if isPortrait ^ self.__isPortrait:
130 orientation = gtk.ORIENTATION_VERTICAL
132 orientation = gtk.ORIENTATION_HORIZONTAL
133 self.set_orientation(orientation)
135 #Restore selections from previously saved settings if it exists and is valid.
137 selectedCategoryName = unit_data.UNIT_CATEGORIES[0]
138 selectionsDatPath = "/".join((constants._data_path_, "selections.dat"))
139 if os.path.exists(selectionsDatPath):
140 selections = pickle.load(open(selectionsDatPath, 'r'))
142 self._defaultUnitForCategory = selections['selected_units']
147 selectedCategoryName = selections['selected_category']
152 categoryIndex = unit_data.UNIT_CATEGORIES.index(selectedCategoryName)
154 _moduleLogger.warn("Unknown category: %s" % selectedCategoryName)
156 self._categorySelectionButton.get_child().set_markup("<big>%s</big>" % selectedCategoryName)
157 self._categoryView.set_cursor(categoryIndex, self._categoryColumn, False)
158 self._categoryView.grab_focus()
160 self._select_default_unit()
162 def _save_settings(self):
164 This routine saves the selections to a file, and
165 should therefore only be called when exiting the program.
167 Update selections dictionary which consists of the following keys:
168 'self._selectedCategoryName': full name of selected category
169 'self._defaultUnitForCategory': self._defaultUnitForCategory dictionary which contains:
170 [categoryname: #1 displayed unit, #2 displayed unit]
172 #Determine the contents of the selected category row
173 selected, iter = self._categoryView.get_selection().get_selected()
174 self._selectedCategoryName = self._categoryModel.get_value(iter, 0)
177 'selected_category': self._selectedCategoryName,
178 'selected_units': self._defaultUnitForCategory
180 selectionsDatPath = "/".join((constants._data_path_, "selections.dat"))
181 pickle.dump(selections, open(selectionsDatPath, 'w'))
183 #Get last size of app and save it
185 'size': self._mainWindow.get_size(),
186 "isFullscreen": self._isFullScreen,
187 "isPortrait": self.__isPortrait,
189 windowDatPath = "/".join((constants._data_path_, "window.dat"))
190 pickle.dump(window_settings, open(windowDatPath, 'w'))
194 app = QtGui.QApplication([])
199 if __name__ == "__main__":
200 logging.basicConfig(level = logging.DEBUG)
202 os.makedirs(constants._data_path_)