2 ## This file is part of the sigrok-meter project.
4 ## Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
5 ## Copyright (C) 2014 Jens Steinhauser <jens.steinhauser@gmail.com>
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ## GNU General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, write to the Free Software
19 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 import multiplotwidget
32 QtCore = qtcompat.QtCore
33 QtGui = qtcompat.QtGui
34 pyqtgraph = qtcompat.pyqtgraph
36 class EmptyMessageListView(QtGui.QListView):
37 '''List view that shows a message if the model im empty.'''
39 def __init__(self, message, parent=None):
40 super(self.__class__, self).__init__(parent)
42 self._message = message
44 def paintEvent(self, event):
46 if m and m.rowCount():
47 super(self.__class__, self).paintEvent(event)
50 painter = QtGui.QPainter(self.viewport())
51 painter.drawText(self.rect(), QtCore.Qt.AlignCenter, self._message)
53 class MainWindow(QtGui.QMainWindow):
54 '''The main window of the application.'''
56 # Number of seconds that the plots display.
59 # Update interval of the plots in milliseconds.
62 def __init__(self, context, drivers):
63 super(self.__class__, self).__init__()
65 # Used to coordinate the stopping of the acquisition and
66 # the closing of the window.
69 self.context = context
70 self.drivers = drivers
72 self.delegate = datamodel.MultimeterDelegate(self, self.font())
73 self.model = datamodel.MeasurementDataModel(self)
75 # Maps from 'unit' to the corresponding plot.
77 # Maps from '(plot, device)' to the corresponding curve.
82 self._plot_update_timer = QtCore.QTimer()
83 self._plot_update_timer.setInterval(MainWindow.UPDATEINTERVAL)
84 self._plot_update_timer.timeout.connect(self._updatePlots)
86 QtCore.QTimer.singleShot(0, self._start_acquisition)
88 def _start_acquisition(self):
89 self.acquisition = acquisition.Acquisition(self.context)
90 self.acquisition.measured.connect(self.model.update)
91 self.acquisition.stopped.connect(self._stopped)
94 for (ds, cs) in self.drivers:
95 self.acquisition.add_device(ds, cs)
96 except Exception as e:
97 QtGui.QMessageBox.critical(self, 'Error', str(e))
101 self.start_stop_acquisition()
104 self.setWindowTitle('sigrok-meter')
105 # Resizing the listView below will increase this again.
108 self.setWindowIcon(QtGui.QIcon(':/logo.png'))
110 self._setup_graphPage()
111 self._setup_addDevicePage()
112 self._setup_logPage()
113 self._setup_preferencesPage()
122 self.stackedWidget = QtGui.QStackedWidget(self)
123 for page in self._pages:
124 self.stackedWidget.addWidget(page)
126 self._setup_sidebar()
128 self.setCentralWidget(QtGui.QWidget())
129 self.centralWidget().setContentsMargins(0, 0, 0, 0)
131 layout = QtGui.QHBoxLayout(self.centralWidget())
132 layout.addWidget(self.sideBar)
133 layout.addWidget(self.stackedWidget)
135 layout.setContentsMargins(0, 0, 0, 0)
137 self.resize(900, 550)
139 def _setup_sidebar(self):
140 self.sideBar = QtGui.QToolBar(self)
141 self.sideBar.setOrientation(QtCore.Qt.Vertical)
143 actionGraph = self.sideBar.addAction('Instantaneous Values and Graphs')
144 actionGraph.setCheckable(True)
145 actionGraph.setIcon(icons.graph)
146 actionGraph.triggered.connect(self.showGraphPage)
148 #actionAdd = self.sideBar.addAction('Add Device')
149 #actionAdd.setCheckable(True)
150 #actionAdd.setIcon(icons.add)
151 #actionAdd.triggered.connect(self.showAddDevicePage)
153 #actionLog = self.sideBar.addAction('Logs')
154 #actionLog.setCheckable(True)
155 #actionLog.setIcon(icons.log)
156 #actionLog.triggered.connect(self.showLogPage)
158 #actionPreferences = self.sideBar.addAction('Preferences')
159 #actionPreferences.setCheckable(True)
160 #actionPreferences.setIcon(icons.preferences)
161 #actionPreferences.triggered.connect(self.showPreferencesPage)
163 # make the buttons at the top exclusive
164 self.actionGroup = QtGui.QActionGroup(self)
165 self.actionGroup.addAction(actionGraph)
166 #self.actionGroup.addAction(actionAdd)
167 #self.actionGroup.addAction(actionLog)
168 #self.actionGroup.addAction(actionPreferences)
170 # show graph at startup
171 actionGraph.setChecked(True)
173 # fill space between buttons on the top and on the bottom
174 fill = QtGui.QWidget(self)
175 fill.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding)
176 self.sideBar.addWidget(fill)
178 self.actionStartStop = self.sideBar.addAction('Start Acquisition')
179 self.actionStartStop.setIcon(icons.start)
180 self.actionStartStop.triggered.connect(self.start_stop_acquisition)
182 actionAbout = self.sideBar.addAction('About')
183 actionAbout.setIcon(icons.about)
184 actionAbout.triggered.connect(self.show_about)
186 actionQuit = self.sideBar.addAction('Quit')
187 actionQuit.setIcon(icons.exit)
188 actionQuit.triggered.connect(self.close)
190 s = self.style().pixelMetric(QtGui.QStyle.PM_LargeIconSize)
191 self.sideBar.setIconSize(QtCore.QSize(s, s))
193 self.sideBar.setStyleSheet('''
195 background-color: white;
198 border-right: 1px solid black;
204 border-right: 1px solid black;
208 QToolButton[checkable="false"]:hover {
209 background-color: #c0d0e8;
213 def _setup_graphPage(self):
214 listView = EmptyMessageListView('waiting for data...')
215 listView.setFrameShape(QtGui.QFrame.NoFrame)
216 listView.viewport().setBackgroundRole(QtGui.QPalette.Window)
217 listView.viewport().setAutoFillBackground(True)
218 listView.setMinimumWidth(260)
219 listView.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
220 listView.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
221 listView.setVerticalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
222 listView.setItemDelegate(self.delegate)
223 listView.setModel(self.model)
224 listView.setUniformItemSizes(True)
225 listView.setMinimumSize(self.delegate.sizeHint())
227 self.plotwidget = multiplotwidget.MultiPlotWidget(self)
228 self.plotwidget.plotHidden.connect(self._on_plotHidden)
230 self.graphPage = QtGui.QSplitter(QtCore.Qt.Horizontal, self)
231 self.graphPage.addWidget(listView)
232 self.graphPage.addWidget(self.plotwidget)
233 self.graphPage.setStretchFactor(0, 0)
234 self.graphPage.setStretchFactor(1, 1)
236 def _setup_addDevicePage(self):
237 self.addDevicePage = QtGui.QWidget(self)
238 layout = QtGui.QVBoxLayout(self.addDevicePage)
239 label = QtGui.QLabel('add device page')
240 layout.addWidget(label)
242 def _setup_logPage(self):
243 self.logPage = QtGui.QWidget(self)
244 layout = QtGui.QVBoxLayout(self.logPage)
245 label = QtGui.QLabel('log page')
246 layout.addWidget(label)
248 def _setup_preferencesPage(self):
249 self.preferencesPage = QtGui.QWidget(self)
250 layout = QtGui.QVBoxLayout(self.preferencesPage)
251 label = QtGui.QLabel('preferences page')
252 layout.addWidget(label)
254 def showPage(self, page):
255 self.stackedWidget.setCurrentIndex(self._pages.index(page))
258 def showGraphPage(self):
259 self.showPage(self.graphPage)
262 def showAddDevicePage(self):
263 self.showPage(self.addDevicePage)
266 def showLogPage(self):
267 self.showPage(self.logPage)
270 def showPreferencesPage(self):
271 self.showPage(self.preferencesPage)
273 def _getPlot(self, unit):
274 '''Looks up or creates a new plot for 'unit'.'''
276 if unit in self._plots:
277 return self._plots[unit]
279 # create a new plot for the unit
280 plot = self.plotwidget.addPlot()
281 plot.yaxis.setLabel(util.quantity_from_unit(unit), units=util.format_unit(unit))
282 plot.view.setXRange(-MainWindow.BACKLOG, 0, update=False)
283 plot.view.setYRange(-1, 1)
284 plot.view.enableAutoRange(axis=pyqtgraph.ViewBox.YAxis)
285 # lock to the range calculated by the view using additional padding,
286 # looks nicer this way
287 r = plot.view.viewRange()
288 plot.view.setLimits(xMin=r[0][0], xMax=r[0][1])
290 self._plots[unit] = plot
293 def _getCurve(self, plot, deviceID):
294 '''Looks up or creates a new curve for '(plot, deviceID)'.'''
296 key = (plot, deviceID)
297 if key in self._curves:
298 return self._curves[key]
301 curve = pyqtgraph.PlotDataItem(
303 symbolPen=pyqtgraph.mkPen(QtGui.QColor(QtCore.Qt.black)),
304 symbolBrush=pyqtgraph.mkBrush(QtGui.QColor(QtCore.Qt.black)),
307 plot.view.addItem(curve)
309 self._curves[key] = curve
312 def _updatePlots(self):
313 '''Updates all plots.'''
315 # loop over all devices and channels
316 for row in range(self.model.rowCount()):
317 idx = self.model.index(row, 0)
318 deviceID = self.model.data(idx,
319 datamodel.MeasurementDataModel.idRole)
320 deviceID = tuple(deviceID) # PySide returns a list.
321 traces = self.model.data(idx,
322 datamodel.MeasurementDataModel.tracesRole)
324 for unit, trace in traces.items():
328 l = now - MainWindow.BACKLOG
329 while trace.samples and trace.samples[0][0] < l:
332 plot = self._getPlot(unit)
335 self.plotwidget.showPlot(plot)
338 xdata = [s[0] - now for s in trace.samples]
339 ydata = [s[1] for s in trace.samples]
341 color = self.model.data(idx,
342 datamodel.MeasurementDataModel.colorRole)
344 curve = self._getCurve(plot, deviceID)
345 curve.setPen(pyqtgraph.mkPen(color=color))
346 curve.setData(xdata, ydata)
348 @QtCore.Slot(multiplotwidget.Plot)
349 def _on_plotHidden(self, plot):
350 plotunit = [u for u, p in self._plots.items() if p == plot][0]
352 # Mark all traces of all devices/channels with the same unit as the
353 # plot as "old" ('trace.new = False'). As soon as a new sample arrives
354 # on one trace, the plot will be shown again
355 for row in range(self.model.rowCount()):
356 idx = self.model.index(row, 0)
357 traces = self.model.data(idx, datamodel.MeasurementDataModel.tracesRole)
359 for traceunit, trace in traces.items():
360 if traceunit == plotunit:
366 # The acquisition was stopped by the 'closeEvent()', close the
367 # window again now that the acquisition has stopped.
370 def closeEvent(self, event):
371 if self.acquisition.is_running():
372 # Stop the acquisition before closing the window.
374 self.start_stop_acquisition()
380 def start_stop_acquisition(self):
381 if self.acquisition.is_running():
382 self.acquisition.stop()
383 self._plot_update_timer.stop()
384 self.actionStartStop.setText('Start Acquisition')
385 self.actionStartStop.setIcon(icons.start)
387 # before starting (again), remove all old samples and old curves
388 self.model.clear_samples()
390 for key in self._curves:
392 curve = self._curves[key]
393 plot.view.removeItem(curve)
396 self.acquisition.start()
397 self._plot_update_timer.start()
398 self.actionStartStop.setText('Stop Acquisition')
399 self.actionStartStop.setIcon(icons.stop)
402 def show_about(self):
403 text = textwrap.dedent('''\
405 <b>sigrok-meter 0.1.0</b><br/><br/>
406 Using libsigrok {} (lib version {}).<br/><br/>
407 <a href='http://www.sigrok.org'>
408 http://www.sigrok.org</a><br/>
410 License: GNU GPL, version 3 or later<br/>
412 This program comes with ABSOLUTELY NO WARRANTY;<br/>
414 <a href='http://www.gnu.org/licenses/gpl.html'>
415 http://www.gnu.org/licenses/gpl.html</a><br/>
417 Some icons by <a href='https://www.gnome.org'>
418 the GNOME project</a>
420 '''.format(self.context.package_version, self.context.lib_version))
422 QtGui.QMessageBox.about(self, 'About sigrok-meter', text)