]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
nsis: contrib/pulseview.nsi: Drop obsolete file.
[pulseview.git] / pv / mainwindow.cpp
CommitLineData
d7bed479 1/*
b3f22de0 2 * This file is part of the PulseView project.
d7bed479
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
269528f5 21#ifdef ENABLE_DECODE
aaabd61b 22#include <libsigrokdecode/libsigrokdecode.h>
269528f5 23#endif
30236a54 24
f2edb557 25#include <boost/bind.hpp>
107ca6d3 26#include <boost/foreach.hpp>
f2edb557 27
7d5425ef
JH
28#include <QAction>
29#include <QApplication>
30#include <QButtonGroup>
2953961c 31#include <QFileDialog>
f2edb557 32#include <QMessageBox>
7d5425ef
JH
33#include <QMenu>
34#include <QMenuBar>
35#include <QStatusBar>
36#include <QVBoxLayout>
37#include <QWidget>
2953961c 38
d7bed479 39#include "mainwindow.h"
107ca6d3
JH
40
41#include "devicemanager.h"
acda14b8 42#include "dialogs/about.h"
9663c82b 43#include "dialogs/connect.h"
0fbda3c2 44#include "dialogs/storeprogress.h"
f4c92e1c 45#include "toolbars/samplingbar.h"
3045c869 46#include "view/logicsignal.h"
4104d7f3 47#include "view/view.h"
269528f5 48#ifdef ENABLE_DECODE
f0d37dab 49#include "widgets/decodermenu.h"
269528f5 50#endif
d7bed479 51
30236a54
JH
52/* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
53#define __STDC_FORMAT_MACROS
54#include <inttypes.h>
55#include <stdint.h>
56#include <stdarg.h>
57#include <glib.h>
58#include <libsigrok/libsigrok.h>
e82fd481 59
819f4c25 60using std::list;
30236a54 61
51e77110
JH
62namespace pv {
63
b1264f56
JH
64namespace view {
65class SelectableItem;
66}
67
107ca6d3
JH
68MainWindow::MainWindow(DeviceManager &device_manager,
69 const char *open_file_name,
1d478458 70 QWidget *parent) :
107ca6d3 71 QMainWindow(parent),
dc0867ff 72 _device_manager(device_manager),
f0d37dab 73 _session(device_manager)
d7bed479 74{
7d5425ef 75 setup_ui();
1d478458
JH
76 if (open_file_name) {
77 const QString s(QString::fromUtf8(open_file_name));
78 QMetaObject::invokeMethod(this, "load_file",
79 Qt::QueuedConnection,
80 Q_ARG(QString, s));
81 }
7d5425ef
JH
82}
83
84void MainWindow::setup_ui()
85{
86 setObjectName(QString::fromUtf8("MainWindow"));
87
88 resize(1024, 768);
89
90 // Set the window icon
91 QIcon icon;
92 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
93 QSize(), QIcon::Normal, QIcon::Off);
94 setWindowIcon(icon);
95
e072efc8
JH
96 // Setup the central widget
97 _central_widget = new QWidget(this);
98 _vertical_layout = new QVBoxLayout(_central_widget);
99 _vertical_layout->setSpacing(6);
100 _vertical_layout->setContentsMargins(0, 0, 0, 0);
101 setCentralWidget(_central_widget);
102
103 _view = new pv::view::View(_session, this);
b1264f56 104
e072efc8
JH
105 _vertical_layout->addWidget(_view);
106
ab1d13ee 107 // Setup the menu bar
ced1cc13
JH
108 QMenuBar *const menu_bar = new QMenuBar(this);
109 menu_bar->setGeometry(QRect(0, 0, 400, 25));
a429590b 110
ab1d13ee 111 // File Menu
ced1cc13
JH
112 QMenu *const menu_file = new QMenu;
113 menu_file->setTitle(QApplication::translate(
ab1d13ee 114 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
e072efc8 115
ced1cc13
JH
116 QAction *const action_open = new QAction(this);
117 action_open->setText(QApplication::translate(
ab1d13ee 118 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
ced1cc13 119 action_open->setIcon(QIcon::fromTheme("document-open",
362eea96 120 QIcon(":/icons/document-open.png")));
ced1cc13
JH
121 action_open->setObjectName(QString::fromUtf8("actionOpen"));
122 menu_file->addAction(action_open);
009e1503 123
0fbda3c2
JH
124 QAction *const action_save_as = new QAction(this);
125 action_save_as->setText(QApplication::translate(
126 "MainWindow", "&Save As...", 0, QApplication::UnicodeUTF8));
127 action_save_as->setIcon(QIcon::fromTheme("document-save-as",
128 QIcon(":/icons/document-save-as.png")));
129 action_save_as->setObjectName(QString::fromUtf8("actionSaveAs"));
130 menu_file->addAction(action_save_as);
131
ced1cc13 132 menu_file->addSeparator();
2a032dcb 133
ced1cc13
JH
134 QAction *const action_connect = new QAction(this);
135 action_connect->setText(QApplication::translate(
9663c82b
JH
136 "MainWindow", "&Connect to Device...", 0,
137 QApplication::UnicodeUTF8));
ced1cc13
JH
138 action_connect->setObjectName(QString::fromUtf8("actionConnect"));
139 menu_file->addAction(action_connect);
9663c82b 140
ced1cc13 141 menu_file->addSeparator();
9663c82b 142
ced1cc13
JH
143 QAction *action_quit = new QAction(this);
144 action_quit->setText(QApplication::translate(
2a032dcb 145 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
ced1cc13 146 action_quit->setIcon(QIcon::fromTheme("application-exit",
2a032dcb 147 QIcon(":/icons/application-exit.png")));
ced1cc13
JH
148 action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
149 action_quit->setObjectName(QString::fromUtf8("actionQuit"));
150 menu_file->addAction(action_quit);
2a032dcb 151
ab1d13ee 152 // View Menu
ced1cc13
JH
153 QMenu *menu_view = new QMenu;
154 menu_view->setTitle(QApplication::translate(
ab1d13ee
JH
155 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
156
ced1cc13
JH
157 QAction *const action_view_zoom_in = new QAction(this);
158 action_view_zoom_in->setText(QApplication::translate(
ab1d13ee 159 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
ced1cc13 160 action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
ab1d13ee 161 QIcon(":/icons/zoom-in.png")));
ced1cc13 162 action_view_zoom_in->setObjectName(
ab1d13ee 163 QString::fromUtf8("actionViewZoomIn"));
ced1cc13 164 menu_view->addAction(action_view_zoom_in);
ab1d13ee 165
ced1cc13
JH
166 QAction *const action_view_zoom_out = new QAction(this);
167 action_view_zoom_out->setText(QApplication::translate(
ab1d13ee 168 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
ced1cc13 169 action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
ab1d13ee 170 QIcon(":/icons/zoom-out.png")));
ced1cc13 171 action_view_zoom_out->setObjectName(
ab1d13ee 172 QString::fromUtf8("actionViewZoomOut"));
ced1cc13 173 menu_view->addAction(action_view_zoom_out);
ab1d13ee 174
ca46b534
JH
175 QAction *const action_view_zoom_fit = new QAction(this);
176 action_view_zoom_fit->setText(QApplication::translate(
177 "MainWindow", "Zoom to &Fit", 0, QApplication::UnicodeUTF8));
178 action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit",
179 QIcon(":/icons/zoom-fit.png")));
180 action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F));
181 action_view_zoom_fit->setObjectName(
182 QString::fromUtf8("actionViewZoomFit"));
183 menu_view->addAction(action_view_zoom_fit);
184
d1e7d82c
JH
185 QAction *const action_view_zoom_one_to_one = new QAction(this);
186 action_view_zoom_one_to_one->setText(QApplication::translate(
187 "MainWindow", "Zoom to &One-to-One", 0,
188 QApplication::UnicodeUTF8));
189 action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original",
190 QIcon(":/icons/zoom-original.png")));
191 action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O));
192 action_view_zoom_one_to_one->setObjectName(
193 QString::fromUtf8("actionViewZoomOneToOne"));
194 menu_view->addAction(action_view_zoom_one_to_one);
195
ced1cc13 196 menu_view->addSeparator();
ab1d13ee 197
ced1cc13
JH
198 QAction *action_view_show_cursors = new QAction(this);
199 action_view_show_cursors->setCheckable(true);
200 action_view_show_cursors->setChecked(_view->cursors_shown());
201 action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
202 action_view_show_cursors->setObjectName(
ab1d13ee 203 QString::fromUtf8("actionViewShowCursors"));
ced1cc13 204 action_view_show_cursors->setText(QApplication::translate(
ab1d13ee 205 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
ced1cc13 206 menu_view->addAction(action_view_show_cursors);
a429590b 207
7dd093df 208 // Decoders Menu
269528f5 209#ifdef ENABLE_DECODE
ced1cc13
JH
210 QMenu *const menu_decoders = new QMenu;
211 menu_decoders->setTitle(QApplication::translate(
7dd093df
JH
212 "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8));
213
ced1cc13
JH
214 pv::widgets::DecoderMenu *const menu_decoders_add =
215 new pv::widgets::DecoderMenu(menu_decoders);
216 menu_decoders_add->setTitle(QApplication::translate(
7dd093df 217 "MainWindow", "&Add", 0, QApplication::UnicodeUTF8));
ced1cc13 218 connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
f0d37dab 219 this, SLOT(add_decoder(srd_decoder*)));
7dd093df 220
ced1cc13 221 menu_decoders->addMenu(menu_decoders_add);
269528f5 222#endif
7dd093df 223
ab1d13ee 224 // Help Menu
ced1cc13
JH
225 QMenu *const menu_help = new QMenu;
226 menu_help->setTitle(QApplication::translate(
ab1d13ee
JH
227 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
228
ced1cc13
JH
229 QAction *const action_about = new QAction(this);
230 action_about->setObjectName(QString::fromUtf8("actionAbout"));
231 action_about->setText(QApplication::translate(
ab1d13ee 232 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
ced1cc13 233 menu_help->addAction(action_about);
7d5425ef 234
ced1cc13
JH
235 menu_bar->addAction(menu_file->menuAction());
236 menu_bar->addAction(menu_view->menuAction());
269528f5 237#ifdef ENABLE_DECODE
ced1cc13 238 menu_bar->addAction(menu_decoders->menuAction());
269528f5 239#endif
ced1cc13 240 menu_bar->addAction(menu_help->menuAction());
7d5425ef 241
ced1cc13 242 setMenuBar(menu_bar);
7d5425ef
JH
243 QMetaObject::connectSlotsByName(this);
244
5ac961e3 245 // Setup the toolbar
ced1cc13
JH
246 QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this);
247 toolbar->addAction(action_open);
248 toolbar->addSeparator();
249 toolbar->addAction(action_view_zoom_in);
250 toolbar->addAction(action_view_zoom_out);
ca46b534 251 toolbar->addAction(action_view_zoom_fit);
ced1cc13 252 addToolBar(toolbar);
0a4db787 253
5ac961e3 254 // Setup the sampling bar
aca00b1e 255 _sampling_bar = new toolbars::SamplingBar(_session, this);
5ac961e3
JH
256
257 // Populate the device list and select the initially selected device
107ca6d3 258 update_device_list();
5ac961e3 259
274d4f13
JH
260 connect(_sampling_bar, SIGNAL(run_stop()), this,
261 SLOT(run_stop()));
d4984fe7
JH
262 addToolBar(_sampling_bar);
263
5ac961e3 264 // Set the title
a8d3fb2d
JH
265 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
266 QApplication::UnicodeUTF8));
267
6ac96c2e
JH
268 // Setup _session events
269 connect(&_session, SIGNAL(capture_state_changed(int)), this,
270 SLOT(capture_state_changed(int)));
271
d7bed479 272}
30236a54 273
f2edb557
JH
274void MainWindow::session_error(
275 const QString text, const QString info_text)
276{
277 QMetaObject::invokeMethod(this, "show_session_error",
278 Qt::QueuedConnection, Q_ARG(QString, text),
279 Q_ARG(QString, info_text));
280}
281
107ca6d3
JH
282void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
283{
284 assert(_sampling_bar);
285
286 const list<sr_dev_inst*> &devices = _device_manager.devices();
287 _sampling_bar->set_device_list(devices);
288
289 if (!selected_device && !devices.empty()) {
290 // Fall back to the first device in the list.
291 selected_device = devices.front();
292
293 // Try and find the demo device and select that by default
294 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
295 if (strcmp(sdi->driver->name, "demo") == 0) {
296 selected_device = sdi;
297 }
298 }
299
300 if (selected_device) {
301 _sampling_bar->set_selected_device(selected_device);
302 _session.set_device(selected_device);
303 }
304}
305
1d478458
JH
306void MainWindow::load_file(QString file_name)
307{
f2edb557
JH
308 const QString errorMessage(
309 QString("Failed to load file %1").arg(file_name));
310 const QString infoMessage;
311 _session.load_file(file_name.toStdString(),
312 boost::bind(&MainWindow::session_error, this,
313 errorMessage, infoMessage));
314}
315
316void MainWindow::show_session_error(
317 const QString text, const QString info_text)
318{
319 QMessageBox msg(this);
320 msg.setText(text);
321 msg.setInformativeText(info_text);
322 msg.setStandardButtons(QMessageBox::Ok);
323 msg.setIcon(QMessageBox::Warning);
324 msg.exec();
1d478458
JH
325}
326
2953961c
JH
327void MainWindow::on_actionOpen_triggered()
328{
1d43d767 329 // Show the dialog
1d478458 330 const QString file_name = QFileDialog::getOpenFileName(
6866d5de
JH
331 this, tr("Open File"), "", tr(
332 "Sigrok Sessions (*.sr);;"
333 "All Files (*.*)"));
92b139d0
JH
334 if (!file_name.isEmpty())
335 load_file(file_name);
2953961c
JH
336}
337
0fbda3c2
JH
338void MainWindow::on_actionSaveAs_triggered()
339{
340 using pv::dialogs::StoreProgress;
341
342 // Stop any currently running capture session
343 _session.stop_capture();
344
345 // Show the dialog
346 const QString file_name = QFileDialog::getSaveFileName(
347 this, tr("Save File"), "", tr("Sigrok Sessions (*.sr)"));
348
349 if (file_name.isEmpty())
350 return;
351
352 StoreProgress *dlg = new StoreProgress(file_name, _session, this);
353 dlg->run();
354}
355
9663c82b
JH
356void MainWindow::on_actionConnect_triggered()
357{
b99cfc42
JH
358 // Stop any currently running capture session
359 _session.stop_capture();
360
107ca6d3 361 dialogs::Connect dlg(this, _device_manager);
5eb0fa13 362
dc0867ff
JH
363 // If the user selected a device, select it in the device list. Select the
364 // current device otherwise.
365 struct sr_dev_inst *const sdi = dlg.exec() ?
366 dlg.get_selected_device() : _session.get_device();
367
107ca6d3 368 update_device_list(sdi);
9663c82b
JH
369}
370
2a032dcb
JH
371void MainWindow::on_actionQuit_triggered()
372{
373 close();
374}
375
a429590b
JH
376void MainWindow::on_actionViewZoomIn_triggered()
377{
378 _view->zoom(1);
379}
380
381void MainWindow::on_actionViewZoomOut_triggered()
382{
383 _view->zoom(-1);
384}
385
ca46b534
JH
386void MainWindow::on_actionViewZoomFit_triggered()
387{
388 _view->zoom_fit();
389}
390
d1e7d82c
JH
391void MainWindow::on_actionViewZoomOneToOne_triggered()
392{
393 _view->zoom_one_to_one();
394}
395
e072efc8
JH
396void MainWindow::on_actionViewShowCursors_triggered()
397{
398 assert(_view);
b4d91e56
JH
399
400 const bool show = !_view->cursors_shown();
401 if(show)
402 _view->centre_cursors();
403
404 _view->show_cursors(show);
e072efc8
JH
405}
406
30236a54
JH
407void MainWindow::on_actionAbout_triggered()
408{
acda14b8 409 dialogs::About dlg(this);
40eb2ff4 410 dlg.exec();
30236a54 411}
274d4f13 412
f0d37dab 413void MainWindow::add_decoder(srd_decoder *decoder)
7dd093df 414{
269528f5 415#ifdef ENABLE_DECODE
f0d37dab
JH
416 assert(decoder);
417 _session.add_decoder(decoder);
269528f5
JH
418#else
419 (void)decoder;
420#endif
7dd093df
JH
421}
422
274d4f13
JH
423void MainWindow::run_stop()
424{
5b7cf66c
JH
425 switch(_session.get_capture_state()) {
426 case SigSession::Stopped:
d99dc9f2
ML
427 _session.start_capture(
428 boost::bind(&MainWindow::session_error, this,
f2edb557 429 QString("Capture failed"), _1));
5b7cf66c
JH
430 break;
431
2b49eeb0 432 case SigSession::AwaitingTrigger:
5b7cf66c
JH
433 case SigSession::Running:
434 _session.stop_capture();
435 break;
436 }
274d4f13 437}
51e77110 438
6ac96c2e
JH
439void MainWindow::capture_state_changed(int state)
440{
2b49eeb0 441 _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);
6ac96c2e
JH
442}
443
51e77110 444} // namespace pv