/
MainWindow.py
168 lines (133 loc) · 6.55 KB
/
MainWindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from Database import *
from GeneralTab import *
from StatisticsTab import *
__version__ = "2.5"
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.profilePath = QString()
self.createLayout()
self.readSettings()
if self.profilePath.isEmpty() or not QFile.exists(self.profilePath):
self.promptNewProfilePath(callByAction=False)
else:
self.establishConnection()
self.tabChanged(self.tabWidget.indexOf(self.generalTab))
def retrieveProfileName(self):
index = self.profilePath.lastIndexOf("/")
if index == -1 or self.profilePath.isEmpty():
return QString()
profileName = self.profilePath.right(self.profilePath.length() - index - 1)
profileName.chop(3) # chop file extension name
return profileName
def createLayout(self):
self.generalTab = GeneralTab(self)
self.statisticsTab = StatisticsTab(self)
self.tabWidget = QTabWidget()
self.tabWidget.addTab(self.generalTab, self.tr("Ge&neral"))
self.tabWidget.addTab(self.statisticsTab, self.tr("&Statistics"))
self.createMenu()
self.setCentralWidget(self.tabWidget)
self.statusBar()
self.setWindowIcon(QIcon("icon.ico"))
self.setWindowTitle(self.tr("Money Expenser"))
self.tabWidget.currentChanged.connect(self.tabChanged)
def toggleButton(self, status):
self.generalTab.addButton.setEnabled(status)
self.generalTab.removeButton.setEnabled(status)
self.statisticsTab.setEnabled(status)
def promptNewProfilePath(self, callByAction=True):
result = QMessageBox.Ok
if not callByAction:
result = QMessageBox.question(self, self.tr("Profile not found"),
self.tr("Create a new profile?"),
QMessageBox.Ok | QMessageBox.Cancel)
if result == QMessageBox.Ok:
profilePath = QFileDialog.getSaveFileName(self,
self.tr("Create new profile"),
QDir.homePath() + QDir.separator() + QDir.home().dirName() + ".me",
self.tr("Money Expenser database (*.me)"))
if not profilePath.isEmpty():
self.profilePath = profilePath
else:
self.profilePath = QString()
self.establishConnection(showWarning=False)
def loadAction(self):
profilePath = QFileDialog.getOpenFileName(self,
self.tr("Load profile"),
QDir.homePath(),
self.tr("Money Expenser database (*.me)"))
if not profilePath.isEmpty():
if QFile.exists(profilePath):
self.profilePath = profilePath
self.establishConnection()
self.generalTab.updateTable()
else:
QMessageBox.critical(self, self.tr("Error"), self.tr("File not found."))
def establishConnection(self, showWarning=True):
status = db.connectTo(self.profilePath)
self.generalTab.setTitle(self.retrieveProfileName())
self.toggleButton(status)
if not status and showWarning:
QMessageBox.critical(self, self.tr("Error"), self.tr("Cannot open database."), QMessageBox.Ok)
def tabChanged(self, index):
if index == self.tabWidget.indexOf(self.generalTab):
QTimer.singleShot(0, self.generalTab.updateTable)
elif index == self.tabWidget.indexOf(self.statisticsTab):
QTimer.singleShot(0, self.statisticsTab.updateTable)
self.setStatusBarMessage(self.tr("Please specify the condition then press \"GO!\""))
def setStatusBarMessage(self, msg):
self.statusBar().showMessage(msg)
def writeSettings(self):
settings = QSettings(QSettings.IniFormat, QSettings.UserScope, "OGC", "MoneyExpenser")
settings.setValue("lastProfilePath", QVariant(self.profilePath))
settings.beginGroup("Layout")
settings.setValue("size", QVariant(self.size()))
settings.setValue("pos", QVariant(self.pos()))
settings.endGroup()
def readSettings(self):
settings = QSettings(QSettings.IniFormat, QSettings.UserScope, "OGC", "MoneyExpenser")
self.profilePath = settings.value("lastProfilePath", QVariant("")).toString()
settings.beginGroup("Layout")
self.resize(settings.value("size", QVariant(QSize(842, 525))).toSize())
self.move(settings.value("pos", QVariant(QApplication.desktop().availableGeometry().center() / 2)).toPoint())
settings.endGroup()
def closeEvent(self, event):
self.writeSettings()
def createMenu(self):
createProfileAct = QAction(self.tr("Cr&eate new profile"), self)
loadProfileAct = QAction(self.tr("&Load profile"), self)
exitAct = QAction(self.tr("E&xit"), self)
aboutAct = QAction(self.tr("About"), self)
aboutQtAct = QAction(self.tr("About Qt"), self)
createProfileAct.setShortcut("Ctrl+E")
loadProfileAct.setShortcut("Ctrl+L")
exitAct.setShortcut("Ctrl+Q")
createProfileAct.triggered.connect(self.promptNewProfilePath)
loadProfileAct.triggered.connect(self.loadAction)
exitAct.triggered.connect(self.close)
aboutAct.triggered.connect(self.about)
aboutQtAct.triggered.connect(qApp.aboutQt)
fileMenu = self.menuBar().addMenu(self.tr("&File"))
fileMenu.addAction(createProfileAct)
fileMenu.addAction(loadProfileAct)
fileMenu.addAction(exitAct)
helpMenu = self.menuBar().addMenu(self.tr("&Help"))
helpMenu.addAction(aboutAct)
helpMenu.addAction(aboutQtAct)
def about(self):
QMessageBox.about(self, 'About',
'''<p><b>Money Expenser</b> {0}</p>
<p>A personal expense recorder.</p>
<p>By Amulet<br />
<a href="http://ogc-daily.blogspot.com">http://ogc-daily.blogspot.com</a></p>'''
.format(__version__))
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
widget = MainWindow()
widget.show()
app.exec_()