This repository has been archived by the owner on Jul 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gui_mainwindow.py
236 lines (209 loc) · 9.62 KB
/
gui_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import sys
import math
import PyQt4
import data
import gui_graphicsscene
import gui_graphicsview
import gui_settings
import gui_settings_bars
import gui_settings_axes
import gui_settings_informationbox
import gui_console
import gui_menubar
import gui_textitems
import task_loadfile
import task_changepointcount
import task_changemode
import task_fit
class MenuBarWithActions(gui_menubar.MenuBar):
"""
Provides the menu bar for the main window. This class connects the
internals of gui_menubar.MenuBar class with the internals of
MainWindow class.
"""
def __init__(self, parent):
gui_menubar.MenuBar.__init__(self, parent)
self.openAct.triggered.connect(self.openFile)
self.saveAct.triggered.connect(parent.saveAsImage)
for act in self.recentFileActs:
act.triggered.connect(self.openRecentFile)
self.quitAct.triggered.connect(parent.close)
self.informationBoxSettingsAct.triggered.connect(self.editInformationBoxSettings)
self.barsSettingsAct.triggered.connect(self.editBarsSettings)
self.axesSettingsAct.triggered.connect(self.editAxesSettings)
self.absorbanceModeAct.triggered.connect(self.setAbsorbanceMode)
self.luminiscenceModeAct.triggered.connect(self.setLuminiscenceMode)
def editInformationBoxSettings(self):
"""
Opens the Appearance editor, where user can select font sizes,
format details etc.
"""
dialog = gui_settings_informationbox.Dialog(self.parent())
if dialog.exec_() == PyQt4.QtGui.QDialog.Accepted:
self.parent().scene.updateAppearance()
self.parent().view.fitSceneInView()
def editBarsSettings(self):
dialog = gui_settings_bars.Dialog(self.parent())
if dialog.exec_() == PyQt4.QtGui.QDialog.Accepted:
self.parent().scene.updateAppearance()
self.parent().view.fitSceneInView()
def editAxesSettings(self):
dialog = gui_settings_axes.Dialog(self.parent())
if dialog.exec_() == PyQt4.QtGui.QDialog.Accepted:
self.parent().scene.updateAppearance()
self.parent().view.fitSceneInView()
def openFile(self, bool):
name = PyQt4.QtGui.QFileDialog.getOpenFileName(self.parent(),
"Open file",
"",
"Oscilloscope Data (*.csv)")
self.parent().loadFile(name)
def openRecentFile(self):
if self.sender():
self.parent().loadFile(self.sender().data().toString())
def setAbsorbanceMode(self):
self.parent().changeMode(self.parent().data.originalData.ABSORBANCE)
def setLuminiscenceMode(self):
self.parent().changeMode(self.parent().data.originalData.LUMINISCENCE)
class MainWindow(PyQt4.QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
# Set initial value of self.loadFilePath
self.setLoadedFilePath("")
# Create and connect Main Menu Bar
self.setMenuBar(MenuBarWithActions(self))
self.data = data.Data()
self.textItems = gui_textitems.List(self.data)
self.settings = gui_settings.Settings(self)
self.addDockWidget(PyQt4.QtCore.Qt.LeftDockWidgetArea, self.settings)
self.console = gui_console.Console(self)
self.addDockWidget(PyQt4.QtCore.Qt.BottomDockWidgetArea, self.console)
self.setCorner(PyQt4.QtCore.Qt.BottomLeftCorner,
PyQt4.QtCore.Qt.LeftDockWidgetArea)
self.createStatusBar()
self.scene = gui_graphicsscene.GraphicsScene(self.data, self)
self.scene.sceneRectChanged.connect(self.settings.onSceneRectChanged)
self.settings.timeAxisLength.valueChangeFinished.connect(self.scene.changeWidth)
self.settings.usedPoints.valueChangeFinished.connect(self.reloadFromOriginalData)
self.settings.experimental.fit.clicked.connect(self.fit)
self.settings.compatible.fit.clicked.connect(self.fit)
self.menuBar().showMenuToggleConnect(self.scene.informationTable.recreateFromData)
self.scene.fullLightBars.bar1.signals.positionChangeFinished.connect(self.data.setFullLightVoltageTime1)
self.scene.fullLightBars.bar2.signals.positionChangeFinished.connect(self.data.setFullLightVoltageTime2)
self.scene.fitBars.bar1.signals.positionChangeFinished.connect(self.data.setFitTime1)
self.scene.fitBars.bar2.signals.positionChangeFinished.connect(self.data.setFitTime2)
self.data.dataChanged.connect(self.scene.onDataChanged)
self.view = gui_graphicsview.GraphicsView(self.scene)
self.setCentralWidget(self.view)
# Current task (separate thread doing some work)
self.task = None
# Refresh GUI
self.scene.updateFromData(True)
self.settings.onDataLoaded(self.data)
def saveAsImage(self):
"""
TODO: Design Save image Dialog
"""
fileName = PyQt4.QtCore.QFileInfo(self.loadedFilePath).completeBaseName()
image = PyQt4.QtGui.QFileDialog.getSaveFileName(self, "Save file", fileName, "PNG Image (*.png)")
if len(image) == 0:
return
if not image.endsWith(".png") and not image.endsWith(".PNG"):
image = image + ".png"
if PyQt4.QtCore.QFileInfo(image).exists():
fileName = PyQt4.QtCore.QFileInfo(image).fileName()
result = PyQt4.QtGui.QMessageBox.question(self, "Image file already exists",
"Image file {0} already exists. Overwrite?".format(fileName),
PyQt4.QtGui.QMessageBox.Yes | PyQt4.QtGui.QMessageBox.No,
PyQt4.QtGui.QMessageBox.Yes)
if result != PyQt4.QtGui.QMessageBox.Yes:
return
BORDER_WIDTH = 50 # pixels
imageWidth = self.scene.width() + 2 * BORDER_WIDTH
imageHeight = self.scene.height() + 2 * BORDER_WIDTH
pixmap = PyQt4.QtGui.QPixmap(imageWidth, imageHeight)
pixmap.fill() # White background.
targetRect = PyQt4.QtCore.QRectF(BORDER_WIDTH, BORDER_WIDTH,
self.scene.width(),
self.scene.height())
painter = PyQt4.QtGui.QPainter(pixmap)
self.scene.render(painter, targetRect)
painter.end()
pixmap.save(image)
def loadFile(self, name):
"""
Loads input file and displays its content in graph.
"""
fi = PyQt4.QtCore.QFileInfo(name)
if not fi.isFile() or not fi.isReadable():
return
# The task must be stored in self to prevent Python from
# deleting it.
self.task = task_loadfile.Task(name, self)
self.runTask(self.task)
def reloadFromOriginalData(self, pointCount):
"""
Changes the number of measured points used from all points
loaded from input file.
"""
# Do nothing if the number of points hasn't changed.
if self.data.maxPoints == pointCount:
return
# The task must be stored in self to prevent Python from
# deleting it.
self.task = task_changepointcount.ChangePointCountTask(pointCount, self)
self.runTask(self.task)
def changeMode(self, mode):
# Do nothing if the type has not changed.
if self.data.originalData.type == mode:
return
# The task must be stored in self to prevent Python from
# deleting it.
self.task = task_changemode.ChangeModeTask(mode, self)
self.runTask(self.task)
def fit(self):
self.task = task_fit.Task(self)
self.runTask(self.task)
def runTask(self, task):
"""
Runs a task. This involves disabling most of the UI during the
task.
"""
self.settings.setEnabled(False)
self.menuBar().setEnabled(False)
self.scene.fullLightBars.setEnabled(False)
self.scene.fitBars.setEnabled(False)
self.scene.setBackgroundBrush(PyQt4.QtGui.QBrush(PyQt4.QtGui.QColor("#d0d0d0")));
task.finished.connect(self.onTaskFinished)
task.messageAdded.connect(self.statusBar().showMessage)
task.messageAdded.connect(self.console.showMessage)
task.start()
def onTaskFinished(self):
self.settings.setEnabled(True)
self.menuBar().setEnabled(True)
self.scene.fullLightBars.setEnabled(True)
self.scene.fitBars.setEnabled(True)
self.statusBar().showMessage("Done", 3000)
self.scene.setBackgroundBrush(PyQt4.QtGui.QBrush(PyQt4.QtCore.Qt.NoBrush));
def setLoadedFilePath(self, path):
self.loadedFilePath = path
fileName = PyQt4.QtCore.QFileInfo(path).fileName()
if len(fileName) == 0:
self.setWindowTitle("flashfit")
else:
self.setWindowTitle(fileName + " - flashfit")
def createStatusBar(self):
statusBar = PyQt4.QtGui.QStatusBar();
self.setStatusBar(statusBar);
statusBar.showMessage("Ready", 3000)
consoleButton = PyQt4.QtGui.QPushButton("Console");
consoleButton.setFlat(True)
consoleButton.setCheckable(True)
consoleButton.toggled.connect(self.console.setVisible)
statusBar.addPermanentWidget(consoleButton)
def keyPressEvent(self, event):
if event.key() == PyQt4.QtCore.Qt.Key_Escape and \
self.task is not None and self.task.isRunning():
self.task.terminate()
else:
super(MainWindow, self).keyPressEvent(event)