]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
property: add the necessary include file for std::function
[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
85843b14
JH
25#include <algorithm>
26#include <iterator>
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>
643f65f9 35#include <QSettings>
7d5425ef
JH
36#include <QStatusBar>
37#include <QVBoxLayout>
38#include <QWidget>
2953961c 39
d7bed479 40#include "mainwindow.h"
107ca6d3
JH
41
42#include "devicemanager.h"
85843b14 43#include "device/device.h"
acda14b8 44#include "dialogs/about.h"
9663c82b 45#include "dialogs/connect.h"
0fbda3c2 46#include "dialogs/storeprogress.h"
f4c92e1c 47#include "toolbars/samplingbar.h"
3045c869 48#include "view/logicsignal.h"
4104d7f3 49#include "view/view.h"
269528f5 50#ifdef ENABLE_DECODE
f0d37dab 51#include "widgets/decodermenu.h"
269528f5 52#endif
d7bed479 53
30236a54
JH
54/* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
55#define __STDC_FORMAT_MACROS
56#include <inttypes.h>
57#include <stdint.h>
58#include <stdarg.h>
59#include <glib.h>
60#include <libsigrok/libsigrok.h>
e82fd481 61
819f4c25 62using std::list;
f9abf97e 63using std::shared_ptr;
30236a54 64
51e77110
JH
65namespace pv {
66
b1264f56
JH
67namespace view {
68class SelectableItem;
69}
70
643f65f9
JS
71const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
72const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
73
107ca6d3
JH
74MainWindow::MainWindow(DeviceManager &device_manager,
75 const char *open_file_name,
1d478458 76 QWidget *parent) :
107ca6d3 77 QMainWindow(parent),
dc0867ff 78 _device_manager(device_manager),
f0d37dab 79 _session(device_manager)
d7bed479 80{
7d5425ef 81 setup_ui();
1d478458
JH
82 if (open_file_name) {
83 const QString s(QString::fromUtf8(open_file_name));
84 QMetaObject::invokeMethod(this, "load_file",
85 Qt::QueuedConnection,
86 Q_ARG(QString, s));
87 }
7d5425ef
JH
88}
89
90void MainWindow::setup_ui()
91{
92 setObjectName(QString::fromUtf8("MainWindow"));
93
94 resize(1024, 768);
95
96 // Set the window icon
97 QIcon icon;
98 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
99 QSize(), QIcon::Normal, QIcon::Off);
100 setWindowIcon(icon);
101
e072efc8
JH
102 // Setup the central widget
103 _central_widget = new QWidget(this);
104 _vertical_layout = new QVBoxLayout(_central_widget);
105 _vertical_layout->setSpacing(6);
106 _vertical_layout->setContentsMargins(0, 0, 0, 0);
107 setCentralWidget(_central_widget);
108
109 _view = new pv::view::View(_session, this);
b1264f56 110
e072efc8
JH
111 _vertical_layout->addWidget(_view);
112
ab1d13ee 113 // Setup the menu bar
ced1cc13
JH
114 QMenuBar *const menu_bar = new QMenuBar(this);
115 menu_bar->setGeometry(QRect(0, 0, 400, 25));
a429590b 116
ab1d13ee 117 // File Menu
ced1cc13
JH
118 QMenu *const menu_file = new QMenu;
119 menu_file->setTitle(QApplication::translate(
ab1d13ee 120 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
e072efc8 121
ced1cc13
JH
122 QAction *const action_open = new QAction(this);
123 action_open->setText(QApplication::translate(
ab1d13ee 124 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
ced1cc13 125 action_open->setIcon(QIcon::fromTheme("document-open",
362eea96 126 QIcon(":/icons/document-open.png")));
5117748b 127 action_open->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
ced1cc13
JH
128 action_open->setObjectName(QString::fromUtf8("actionOpen"));
129 menu_file->addAction(action_open);
009e1503 130
0fbda3c2
JH
131 QAction *const action_save_as = new QAction(this);
132 action_save_as->setText(QApplication::translate(
133 "MainWindow", "&Save As...", 0, QApplication::UnicodeUTF8));
134 action_save_as->setIcon(QIcon::fromTheme("document-save-as",
135 QIcon(":/icons/document-save-as.png")));
5117748b 136 action_save_as->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
0fbda3c2
JH
137 action_save_as->setObjectName(QString::fromUtf8("actionSaveAs"));
138 menu_file->addAction(action_save_as);
139
ced1cc13 140 menu_file->addSeparator();
2a032dcb 141
ced1cc13
JH
142 QAction *const action_connect = new QAction(this);
143 action_connect->setText(QApplication::translate(
9663c82b
JH
144 "MainWindow", "&Connect to Device...", 0,
145 QApplication::UnicodeUTF8));
ced1cc13
JH
146 action_connect->setObjectName(QString::fromUtf8("actionConnect"));
147 menu_file->addAction(action_connect);
9663c82b 148
ced1cc13 149 menu_file->addSeparator();
9663c82b 150
ced1cc13
JH
151 QAction *action_quit = new QAction(this);
152 action_quit->setText(QApplication::translate(
2a032dcb 153 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
ced1cc13 154 action_quit->setIcon(QIcon::fromTheme("application-exit",
2a032dcb 155 QIcon(":/icons/application-exit.png")));
ced1cc13
JH
156 action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
157 action_quit->setObjectName(QString::fromUtf8("actionQuit"));
158 menu_file->addAction(action_quit);
2a032dcb 159
ab1d13ee 160 // View Menu
ced1cc13
JH
161 QMenu *menu_view = new QMenu;
162 menu_view->setTitle(QApplication::translate(
ab1d13ee
JH
163 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
164
ced1cc13
JH
165 QAction *const action_view_zoom_in = new QAction(this);
166 action_view_zoom_in->setText(QApplication::translate(
ab1d13ee 167 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
ced1cc13 168 action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
ab1d13ee 169 QIcon(":/icons/zoom-in.png")));
d2899ad5 170 // simply using Qt::Key_Plus shows no + in the menu
171 action_view_zoom_in->setShortcut(QKeySequence::ZoomIn);
ced1cc13 172 action_view_zoom_in->setObjectName(
ab1d13ee 173 QString::fromUtf8("actionViewZoomIn"));
ced1cc13 174 menu_view->addAction(action_view_zoom_in);
ab1d13ee 175
ced1cc13
JH
176 QAction *const action_view_zoom_out = new QAction(this);
177 action_view_zoom_out->setText(QApplication::translate(
ab1d13ee 178 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
ced1cc13 179 action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
ab1d13ee 180 QIcon(":/icons/zoom-out.png")));
d2899ad5 181 action_view_zoom_out->setShortcut(QKeySequence::ZoomOut);
ced1cc13 182 action_view_zoom_out->setObjectName(
ab1d13ee 183 QString::fromUtf8("actionViewZoomOut"));
ced1cc13 184 menu_view->addAction(action_view_zoom_out);
ab1d13ee 185
ca46b534
JH
186 QAction *const action_view_zoom_fit = new QAction(this);
187 action_view_zoom_fit->setText(QApplication::translate(
188 "MainWindow", "Zoom to &Fit", 0, QApplication::UnicodeUTF8));
189 action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit",
190 QIcon(":/icons/zoom-fit.png")));
191 action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F));
192 action_view_zoom_fit->setObjectName(
193 QString::fromUtf8("actionViewZoomFit"));
194 menu_view->addAction(action_view_zoom_fit);
195
d1e7d82c
JH
196 QAction *const action_view_zoom_one_to_one = new QAction(this);
197 action_view_zoom_one_to_one->setText(QApplication::translate(
198 "MainWindow", "Zoom to &One-to-One", 0,
199 QApplication::UnicodeUTF8));
200 action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original",
201 QIcon(":/icons/zoom-original.png")));
202 action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O));
203 action_view_zoom_one_to_one->setObjectName(
204 QString::fromUtf8("actionViewZoomOneToOne"));
205 menu_view->addAction(action_view_zoom_one_to_one);
206
ced1cc13 207 menu_view->addSeparator();
ab1d13ee 208
ced1cc13
JH
209 QAction *action_view_show_cursors = new QAction(this);
210 action_view_show_cursors->setCheckable(true);
211 action_view_show_cursors->setChecked(_view->cursors_shown());
212 action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
213 action_view_show_cursors->setObjectName(
ab1d13ee 214 QString::fromUtf8("actionViewShowCursors"));
ced1cc13 215 action_view_show_cursors->setText(QApplication::translate(
ab1d13ee 216 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
ced1cc13 217 menu_view->addAction(action_view_show_cursors);
a429590b 218
7dd093df 219 // Decoders Menu
269528f5 220#ifdef ENABLE_DECODE
ced1cc13
JH
221 QMenu *const menu_decoders = new QMenu;
222 menu_decoders->setTitle(QApplication::translate(
7dd093df
JH
223 "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8));
224
ced1cc13 225 pv::widgets::DecoderMenu *const menu_decoders_add =
f69ec72d 226 new pv::widgets::DecoderMenu(menu_decoders, true);
ced1cc13 227 menu_decoders_add->setTitle(QApplication::translate(
7dd093df 228 "MainWindow", "&Add", 0, QApplication::UnicodeUTF8));
ced1cc13 229 connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
f0d37dab 230 this, SLOT(add_decoder(srd_decoder*)));
7dd093df 231
ced1cc13 232 menu_decoders->addMenu(menu_decoders_add);
269528f5 233#endif
7dd093df 234
ab1d13ee 235 // Help Menu
ced1cc13
JH
236 QMenu *const menu_help = new QMenu;
237 menu_help->setTitle(QApplication::translate(
ab1d13ee
JH
238 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
239
ced1cc13
JH
240 QAction *const action_about = new QAction(this);
241 action_about->setObjectName(QString::fromUtf8("actionAbout"));
242 action_about->setText(QApplication::translate(
ab1d13ee 243 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
ced1cc13 244 menu_help->addAction(action_about);
7d5425ef 245
ced1cc13
JH
246 menu_bar->addAction(menu_file->menuAction());
247 menu_bar->addAction(menu_view->menuAction());
269528f5 248#ifdef ENABLE_DECODE
ced1cc13 249 menu_bar->addAction(menu_decoders->menuAction());
269528f5 250#endif
ced1cc13 251 menu_bar->addAction(menu_help->menuAction());
7d5425ef 252
ced1cc13 253 setMenuBar(menu_bar);
7d5425ef
JH
254 QMetaObject::connectSlotsByName(this);
255
5ac961e3 256 // Setup the toolbar
ced1cc13
JH
257 QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this);
258 toolbar->addAction(action_open);
f45512cb 259 toolbar->addAction(action_save_as);
ced1cc13
JH
260 toolbar->addSeparator();
261 toolbar->addAction(action_view_zoom_in);
262 toolbar->addAction(action_view_zoom_out);
ca46b534 263 toolbar->addAction(action_view_zoom_fit);
f45512cb 264 toolbar->addAction(action_view_zoom_one_to_one);
ced1cc13 265 addToolBar(toolbar);
0a4db787 266
5ac961e3 267 // Setup the sampling bar
aca00b1e 268 _sampling_bar = new toolbars::SamplingBar(_session, this);
5ac961e3
JH
269
270 // Populate the device list and select the initially selected device
107ca6d3 271 update_device_list();
5ac961e3 272
274d4f13
JH
273 connect(_sampling_bar, SIGNAL(run_stop()), this,
274 SLOT(run_stop()));
d4984fe7
JH
275 addToolBar(_sampling_bar);
276
5ac961e3 277 // Set the title
a8d3fb2d
JH
278 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
279 QApplication::UnicodeUTF8));
280
6ac96c2e
JH
281 // Setup _session events
282 connect(&_session, SIGNAL(capture_state_changed(int)), this,
283 SLOT(capture_state_changed(int)));
284
d7bed479 285}
30236a54 286
f2edb557
JH
287void MainWindow::session_error(
288 const QString text, const QString info_text)
289{
290 QMetaObject::invokeMethod(this, "show_session_error",
291 Qt::QueuedConnection, Q_ARG(QString, text),
292 Q_ARG(QString, info_text));
293}
294
d873f4d6 295void MainWindow::update_device_list()
107ca6d3
JH
296{
297 assert(_sampling_bar);
298
d873f4d6 299 shared_ptr<pv::device::DevInst> selected_device = _session.get_device();
85843b14
JH
300 list< shared_ptr<device::DevInst> > devices;
301 std::copy(_device_manager.devices().begin(),
302 _device_manager.devices().end(), std::back_inserter(devices));
303
a5ea63c2
JH
304 if (std::find(devices.begin(), devices.end(), selected_device) ==
305 devices.end())
306 devices.push_back(selected_device);
e95e8563 307 assert(selected_device);
a5ea63c2 308
e95e8563 309 _sampling_bar->set_device_list(devices, selected_device);
107ca6d3
JH
310}
311
1d478458
JH
312void MainWindow::load_file(QString file_name)
313{
f2edb557
JH
314 const QString errorMessage(
315 QString("Failed to load file %1").arg(file_name));
316 const QString infoMessage;
ae2d1bc5
JH
317
318 try {
319 _session.set_file(file_name.toStdString());
320 } catch(QString e) {
321 show_session_error(tr("Failed to load ") + file_name, e);
6fd0b639
JH
322 _session.set_default_device();
323 update_device_list();
324 return;
ae2d1bc5
JH
325 }
326
a5ea63c2
JH
327 update_device_list();
328
6db73158
JH
329 _session.start_capture([&, errorMessage, infoMessage](QString) {
330 session_error(errorMessage, infoMessage); });
f2edb557
JH
331}
332
333void MainWindow::show_session_error(
334 const QString text, const QString info_text)
335{
336 QMessageBox msg(this);
337 msg.setText(text);
338 msg.setInformativeText(info_text);
339 msg.setStandardButtons(QMessageBox::Ok);
340 msg.setIcon(QMessageBox::Warning);
341 msg.exec();
1d478458
JH
342}
343
2953961c
JH
344void MainWindow::on_actionOpen_triggered()
345{
643f65f9
JS
346 QSettings settings;
347 const QString dir = settings.value(SettingOpenDirectory).toString();
348
1d43d767 349 // Show the dialog
1d478458 350 const QString file_name = QFileDialog::getOpenFileName(
643f65f9 351 this, tr("Open File"), dir, tr(
6866d5de
JH
352 "Sigrok Sessions (*.sr);;"
353 "All Files (*.*)"));
643f65f9
JS
354
355 if (!file_name.isEmpty()) {
92b139d0 356 load_file(file_name);
643f65f9
JS
357
358 const QString abs_path = QFileInfo(file_name).absolutePath();
359 settings.setValue(SettingOpenDirectory, abs_path);
360 }
2953961c
JH
361}
362
0fbda3c2
JH
363void MainWindow::on_actionSaveAs_triggered()
364{
365 using pv::dialogs::StoreProgress;
366
367 // Stop any currently running capture session
368 _session.stop_capture();
369
643f65f9
JS
370 QSettings settings;
371 const QString dir = settings.value(SettingSaveDirectory).toString();
372
0fbda3c2
JH
373 // Show the dialog
374 const QString file_name = QFileDialog::getSaveFileName(
643f65f9 375 this, tr("Save File"), dir, tr("Sigrok Sessions (*.sr)"));
0fbda3c2
JH
376
377 if (file_name.isEmpty())
378 return;
379
643f65f9
JS
380 const QString abs_path = QFileInfo(file_name).absolutePath();
381 settings.setValue(SettingSaveDirectory, abs_path);
382
0fbda3c2
JH
383 StoreProgress *dlg = new StoreProgress(file_name, _session, this);
384 dlg->run();
385}
386
9663c82b
JH
387void MainWindow::on_actionConnect_triggered()
388{
b99cfc42
JH
389 // Stop any currently running capture session
390 _session.stop_capture();
391
107ca6d3 392 dialogs::Connect dlg(this, _device_manager);
5eb0fa13 393
dc0867ff
JH
394 // If the user selected a device, select it in the device list. Select the
395 // current device otherwise.
d873f4d6
JH
396 if (dlg.exec())
397 _session.set_device(dlg.get_selected_device());
dc0867ff 398
d873f4d6 399 update_device_list();
9663c82b
JH
400}
401
2a032dcb
JH
402void MainWindow::on_actionQuit_triggered()
403{
404 close();
405}
406
a429590b
JH
407void MainWindow::on_actionViewZoomIn_triggered()
408{
409 _view->zoom(1);
410}
411
412void MainWindow::on_actionViewZoomOut_triggered()
413{
414 _view->zoom(-1);
415}
416
ca46b534
JH
417void MainWindow::on_actionViewZoomFit_triggered()
418{
419 _view->zoom_fit();
420}
421
d1e7d82c
JH
422void MainWindow::on_actionViewZoomOneToOne_triggered()
423{
424 _view->zoom_one_to_one();
425}
426
e072efc8
JH
427void MainWindow::on_actionViewShowCursors_triggered()
428{
429 assert(_view);
b4d91e56
JH
430
431 const bool show = !_view->cursors_shown();
432 if(show)
433 _view->centre_cursors();
434
435 _view->show_cursors(show);
e072efc8
JH
436}
437
30236a54
JH
438void MainWindow::on_actionAbout_triggered()
439{
acda14b8 440 dialogs::About dlg(this);
40eb2ff4 441 dlg.exec();
30236a54 442}
274d4f13 443
f0d37dab 444void MainWindow::add_decoder(srd_decoder *decoder)
7dd093df 445{
269528f5 446#ifdef ENABLE_DECODE
f0d37dab
JH
447 assert(decoder);
448 _session.add_decoder(decoder);
269528f5
JH
449#else
450 (void)decoder;
451#endif
7dd093df
JH
452}
453
274d4f13
JH
454void MainWindow::run_stop()
455{
5b7cf66c
JH
456 switch(_session.get_capture_state()) {
457 case SigSession::Stopped:
6db73158
JH
458 _session.start_capture([&](QString message) {
459 session_error("Capture failed", message); });
5b7cf66c
JH
460 break;
461
2b49eeb0 462 case SigSession::AwaitingTrigger:
5b7cf66c
JH
463 case SigSession::Running:
464 _session.stop_capture();
465 break;
466 }
274d4f13 467}
51e77110 468
6ac96c2e
JH
469void MainWindow::capture_state_changed(int state)
470{
2b49eeb0 471 _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);
6ac96c2e
JH
472}
473
51e77110 474} // namespace pv