RowItem: Bundled painting parameters into RowItemPaintParams
[pulseview.git] / pv / mainwindow.cpp
1 /*
2  * This file is part of the PulseView project.
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
21 #include <cassert>
22
23 #ifdef ENABLE_DECODE
24 #include <libsigrokdecode/libsigrokdecode.h>
25 #endif
26
27 #include <algorithm>
28 #include <iterator>
29
30 #include <QAction>
31 #include <QApplication>
32 #include <QButtonGroup>
33 #include <QCloseEvent>
34 #include <QFileDialog>
35 #include <QMessageBox>
36 #include <QMenu>
37 #include <QMenuBar>
38 #include <QSettings>
39 #include <QStatusBar>
40 #include <QVBoxLayout>
41 #include <QWidget>
42
43 #include "mainwindow.hpp"
44
45 #include "devicemanager.hpp"
46 #include "dialogs/about.hpp"
47 #include "dialogs/connect.hpp"
48 #include "dialogs/storeprogress.hpp"
49 #include "toolbars/samplingbar.hpp"
50 #include "view/logicsignal.hpp"
51 #include "view/view.hpp"
52 #ifdef ENABLE_DECODE
53 #include "widgets/decodermenu.hpp"
54 #endif
55
56 #include <inttypes.h>
57 #include <stdint.h>
58 #include <stdarg.h>
59 #include <glib.h>
60 #include <libsigrok/libsigrok.hpp>
61
62 using std::list;
63 using std::map;
64 using std::pair;
65 using std::shared_ptr;
66 using std::string;
67
68 using sigrok::Device;
69 using sigrok::Error;
70 using sigrok::HardwareDevice;
71
72 namespace pv {
73
74 namespace view {
75 class SelectableItem;
76 }
77
78 const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
79 const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
80
81 MainWindow::MainWindow(DeviceManager &device_manager,
82         const char *open_file_name,
83         QWidget *parent) :
84         QMainWindow(parent),
85         device_manager_(device_manager),
86         session_(device_manager)
87 {
88         setup_ui();
89         restore_ui_settings();
90         if (open_file_name) {
91                 const QString s(QString::fromUtf8(open_file_name));
92                 QMetaObject::invokeMethod(this, "load_file",
93                         Qt::QueuedConnection,
94                         Q_ARG(QString, s));
95         }
96 }
97
98 void MainWindow::run_stop()
99 {
100         switch(session_.get_capture_state()) {
101         case Session::Stopped:
102                 session_.start_capture([&](QString message) {
103                         session_error("Capture failed", message); });
104                 break;
105
106         case Session::AwaitingTrigger:
107         case Session::Running:
108                 session_.stop_capture();
109                 break;
110         }
111 }
112
113 void MainWindow::select_device(shared_ptr<Device> device)
114 {
115         try {
116                 session_.set_device(device);
117         } catch(const QString &e) {
118                 QMessageBox msg(this);
119                 msg.setText(e);
120                 msg.setInformativeText(tr("Failed to Select Device"));
121                 msg.setStandardButtons(QMessageBox::Ok);
122                 msg.setIcon(QMessageBox::Warning);
123                 msg.exec();
124         }
125 }
126
127 void MainWindow::setup_ui()
128 {
129         setObjectName(QString::fromUtf8("MainWindow"));
130
131         // Set the window icon
132         QIcon icon;
133         icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
134                 QSize(), QIcon::Normal, QIcon::Off);
135         setWindowIcon(icon);
136
137         // Setup the central widget
138         central_widget_ = new QWidget(this);
139         vertical_layout_ = new QVBoxLayout(central_widget_);
140         vertical_layout_->setSpacing(6);
141         vertical_layout_->setContentsMargins(0, 0, 0, 0);
142         setCentralWidget(central_widget_);
143
144         view_ = new pv::view::View(session_, this);
145
146         vertical_layout_->addWidget(view_);
147
148         // Setup the menu bar
149         QMenuBar *const menu_bar = new QMenuBar(this);
150         menu_bar->setGeometry(QRect(0, 0, 400, 25));
151
152         // File Menu
153         QMenu *const menu_file = new QMenu;
154         menu_file->setTitle(tr("&File"));
155
156         QAction *const action_open = new QAction(this);
157         action_open->setText(tr("&Open..."));
158         action_open->setIcon(QIcon::fromTheme("document-open",
159                 QIcon(":/icons/document-open.png")));
160         action_open->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
161         action_open->setObjectName(QString::fromUtf8("actionOpen"));
162         menu_file->addAction(action_open);
163
164         QAction *const action_save_as = new QAction(this);
165         action_save_as->setText(tr("&Save As..."));
166         action_save_as->setIcon(QIcon::fromTheme("document-save-as",
167                 QIcon(":/icons/document-save-as.png")));
168         action_save_as->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
169         action_save_as->setObjectName(QString::fromUtf8("actionSaveAs"));
170         menu_file->addAction(action_save_as);
171
172         menu_file->addSeparator();
173
174         QAction *const action_connect = new QAction(this);
175         action_connect->setText(tr("&Connect to Device..."));
176         action_connect->setObjectName(QString::fromUtf8("actionConnect"));
177         menu_file->addAction(action_connect);
178
179         menu_file->addSeparator();
180
181         QAction *action_quit = new QAction(this);
182         action_quit->setText(tr("&Quit"));
183         action_quit->setIcon(QIcon::fromTheme("application-exit",
184                 QIcon(":/icons/application-exit.png")));
185         action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
186         action_quit->setObjectName(QString::fromUtf8("actionQuit"));
187         menu_file->addAction(action_quit);
188
189         // View Menu
190         QMenu *menu_view = new QMenu;
191         menu_view->setTitle(tr("&View"));
192
193         QAction *const action_view_zoom_in = new QAction(this);
194         action_view_zoom_in->setText(tr("Zoom &In"));
195         action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
196                 QIcon(":/icons/zoom-in.png")));
197         // simply using Qt::Key_Plus shows no + in the menu
198         action_view_zoom_in->setShortcut(QKeySequence::ZoomIn);
199         action_view_zoom_in->setObjectName(
200                 QString::fromUtf8("actionViewZoomIn"));
201         menu_view->addAction(action_view_zoom_in);
202
203         QAction *const action_view_zoom_out = new QAction(this);
204         action_view_zoom_out->setText(tr("Zoom &Out"));
205         action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
206                 QIcon(":/icons/zoom-out.png")));
207         action_view_zoom_out->setShortcut(QKeySequence::ZoomOut);
208         action_view_zoom_out->setObjectName(
209                 QString::fromUtf8("actionViewZoomOut"));
210         menu_view->addAction(action_view_zoom_out);
211
212         QAction *const action_view_zoom_fit = new QAction(this);
213         action_view_zoom_fit->setText(tr("Zoom to &Fit"));
214         action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit",
215                 QIcon(":/icons/zoom-fit.png")));
216         action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F));
217         action_view_zoom_fit->setObjectName(
218                 QString::fromUtf8("actionViewZoomFit"));
219         menu_view->addAction(action_view_zoom_fit);
220
221         QAction *const action_view_zoom_one_to_one = new QAction(this);
222         action_view_zoom_one_to_one->setText(tr("Zoom to &One-to-One"));
223         action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original",
224                 QIcon(":/icons/zoom-original.png")));
225         action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O));
226         action_view_zoom_one_to_one->setObjectName(
227                 QString::fromUtf8("actionViewZoomOneToOne"));
228         menu_view->addAction(action_view_zoom_one_to_one);
229
230         menu_view->addSeparator();
231
232         QAction *action_view_show_cursors = new QAction(this);
233         action_view_show_cursors->setCheckable(true);
234         action_view_show_cursors->setChecked(view_->cursors_shown());
235         action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
236         action_view_show_cursors->setObjectName(
237                 QString::fromUtf8("actionViewShowCursors"));
238         action_view_show_cursors->setText(tr("Show &Cursors"));
239         menu_view->addAction(action_view_show_cursors);
240
241         // Decoders Menu
242 #ifdef ENABLE_DECODE
243         QMenu *const menu_decoders = new QMenu;
244         menu_decoders->setTitle(tr("&Decoders"));
245
246         pv::widgets::DecoderMenu *const menu_decoders_add =
247                 new pv::widgets::DecoderMenu(menu_decoders, true);
248         menu_decoders_add->setTitle(tr("&Add"));
249         connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
250                 this, SLOT(add_decoder(srd_decoder*)));
251
252         menu_decoders->addMenu(menu_decoders_add);
253 #endif
254
255         // Help Menu
256         QMenu *const menu_help = new QMenu;
257         menu_help->setTitle(tr("&Help"));
258
259         QAction *const action_about = new QAction(this);
260         action_about->setObjectName(QString::fromUtf8("actionAbout"));
261         action_about->setText(tr("&About..."));
262         menu_help->addAction(action_about);
263
264         menu_bar->addAction(menu_file->menuAction());
265         menu_bar->addAction(menu_view->menuAction());
266 #ifdef ENABLE_DECODE
267         menu_bar->addAction(menu_decoders->menuAction());
268 #endif
269         menu_bar->addAction(menu_help->menuAction());
270
271         setMenuBar(menu_bar);
272         QMetaObject::connectSlotsByName(this);
273
274         // Setup the toolbar
275         QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this);
276         toolbar->setObjectName(QString::fromUtf8("MainToolbar"));
277         toolbar->addAction(action_open);
278         toolbar->addAction(action_save_as);
279         toolbar->addSeparator();
280         toolbar->addAction(action_view_zoom_in);
281         toolbar->addAction(action_view_zoom_out);
282         toolbar->addAction(action_view_zoom_fit);
283         toolbar->addAction(action_view_zoom_one_to_one);
284         addToolBar(toolbar);
285
286         // Setup the sampling bar
287         sampling_bar_ = new toolbars::SamplingBar(session_, *this);
288
289         // Populate the device list and select the initially selected device
290         update_device_list();
291
292         addToolBar(sampling_bar_);
293
294         // Set the title
295         setWindowTitle(tr("PulseView"));
296
297         // Setup session_ events
298         connect(&session_, SIGNAL(capture_state_changed(int)), this,
299                 SLOT(capture_state_changed(int)));
300         connect(&session_, SIGNAL(device_selected()), this,
301                 SLOT(device_selected()));
302 }
303
304 void MainWindow::save_ui_settings()
305 {
306         QSettings settings;
307
308         map<string, string> dev_info;
309         list<string> key_list;
310
311         settings.beginGroup("MainWindow");
312         settings.setValue("state", saveState());
313         settings.setValue("geometry", saveGeometry());
314         settings.endGroup();
315
316         if (session_.device()) {
317                 settings.beginGroup("Device");
318                 key_list.push_back("vendor");
319                 key_list.push_back("model");
320                 key_list.push_back("version");
321                 key_list.push_back("serial_num");
322                 key_list.push_back("connection_id");
323
324                 dev_info = device_manager_.get_device_info(
325                         session_.device());
326
327                 for (string key : key_list) {
328
329                         if (dev_info.count(key))
330                                 settings.setValue(QString::fromUtf8(key.c_str()),
331                                                 QString::fromUtf8(dev_info.at(key).c_str()));
332                         else
333                                 settings.remove(QString::fromUtf8(key.c_str()));
334                 }
335
336                 settings.endGroup();
337         }
338 }
339
340 void MainWindow::restore_ui_settings()
341 {
342         QSettings settings;
343
344         shared_ptr<HardwareDevice> device;
345
346         map<string, string> dev_info;
347         list<string> key_list;
348         string value;
349
350         settings.beginGroup("MainWindow");
351
352         if (settings.contains("geometry")) {
353                 restoreGeometry(settings.value("geometry").toByteArray());
354                 restoreState(settings.value("state").toByteArray());
355         } else
356                 resize(1000, 720);
357
358         settings.endGroup();
359
360         // Re-select last used device if possible.
361         settings.beginGroup("Device");
362         key_list.push_back("vendor");
363         key_list.push_back("model");
364         key_list.push_back("version");
365         key_list.push_back("serial_num");
366         key_list.push_back("connection_id");
367
368         for (string key : key_list) {
369                 if (!settings.contains(QString::fromUtf8(key.c_str())))
370                         continue;
371
372                 value = settings.value(QString::fromUtf8(key.c_str())).toString().toStdString();
373
374                 if (value.size() > 0)
375                         dev_info.insert(std::make_pair(key, value));
376         }
377
378         device = device_manager_.find_device_from_info(dev_info);
379
380         if (device) {
381                 select_device(device);
382                 update_device_list();
383         }
384
385         settings.endGroup();
386 }
387
388 void MainWindow::session_error(
389         const QString text, const QString info_text)
390 {
391         QMetaObject::invokeMethod(this, "show_session_error",
392                 Qt::QueuedConnection, Q_ARG(QString, text),
393                 Q_ARG(QString, info_text));
394 }
395
396 void MainWindow::update_device_list()
397 {
398         assert(sampling_bar_);
399
400         shared_ptr<Device> selected_device = session_.device();
401         list< shared_ptr<Device> > devices;
402
403         if (device_manager_.devices().size() == 0)
404                 return;
405
406         std::copy(device_manager_.devices().begin(),
407                 device_manager_.devices().end(), std::back_inserter(devices));
408
409         if (std::find(devices.begin(), devices.end(), selected_device) ==
410                 devices.end())
411                 devices.push_back(selected_device);
412         assert(selected_device);
413
414         list< pair< shared_ptr<Device>, string> > device_list;
415
416         for (auto device : devices)
417                 device_list.push_back(make_pair(
418                         device, device_manager_.get_display_name(device)));
419
420         sampling_bar_->set_device_list(device_list, selected_device);
421 }
422
423 void MainWindow::closeEvent(QCloseEvent *event)
424 {
425         save_ui_settings();
426         event->accept();
427 }
428
429 void MainWindow::load_file(QString file_name)
430 {
431         const QString errorMessage(
432                 QString("Failed to load file %1").arg(file_name));
433         const QString infoMessage;
434
435         try {
436                 session_.set_file(file_name.toStdString());
437         } catch(Error e) {
438                 show_session_error(tr("Failed to load ") + file_name, e.what());
439                 session_.set_default_device();
440                 update_device_list();
441                 return;
442         }
443
444         update_device_list();
445
446         session_.start_capture([&, errorMessage, infoMessage](QString) {
447                 session_error(errorMessage, infoMessage); });
448 }
449
450 void MainWindow::show_session_error(
451         const QString text, const QString info_text)
452 {
453         QMessageBox msg(this);
454         msg.setText(text);
455         msg.setInformativeText(info_text);
456         msg.setStandardButtons(QMessageBox::Ok);
457         msg.setIcon(QMessageBox::Warning);
458         msg.exec();
459 }
460
461 void MainWindow::on_actionOpen_triggered()
462 {
463         QSettings settings;
464         const QString dir = settings.value(SettingOpenDirectory).toString();
465
466         // Show the dialog
467         const QString file_name = QFileDialog::getOpenFileName(
468                 this, tr("Open File"), dir, tr(
469                         "Sigrok Sessions (*.sr);;"
470                         "All Files (*.*)"));
471
472         if (!file_name.isEmpty()) {
473                 load_file(file_name);
474
475                 const QString abs_path = QFileInfo(file_name).absolutePath();
476                 settings.setValue(SettingOpenDirectory, abs_path);
477         }
478 }
479
480 void MainWindow::on_actionSaveAs_triggered()
481 {
482         using pv::dialogs::StoreProgress;
483
484         // Stop any currently running capture session
485         session_.stop_capture();
486
487         QSettings settings;
488         const QString dir = settings.value(SettingSaveDirectory).toString();
489
490         // Show the dialog
491         const QString file_name = QFileDialog::getSaveFileName(
492                 this, tr("Save File"), dir, tr("Sigrok Sessions (*.sr)"));
493
494         if (file_name.isEmpty())
495                 return;
496
497         const QString abs_path = QFileInfo(file_name).absolutePath();
498         settings.setValue(SettingSaveDirectory, abs_path);
499
500         StoreProgress *dlg = new StoreProgress(file_name, session_, this);
501         dlg->run();
502 }
503
504 void MainWindow::on_actionConnect_triggered()
505 {
506         // Stop any currently running capture session
507         session_.stop_capture();
508
509         dialogs::Connect dlg(this, device_manager_);
510
511         // If the user selected a device, select it in the device list. Select the
512         // current device otherwise.
513         if (dlg.exec())
514                 select_device(dlg.get_selected_device());
515
516         update_device_list();
517 }
518
519 void MainWindow::on_actionQuit_triggered()
520 {
521         close();
522 }
523
524 void MainWindow::on_actionViewZoomIn_triggered()
525 {
526         view_->zoom(1);
527 }
528
529 void MainWindow::on_actionViewZoomOut_triggered()
530 {
531         view_->zoom(-1);
532 }
533
534 void MainWindow::on_actionViewZoomFit_triggered()
535 {
536         view_->zoom_fit();
537 }
538
539 void MainWindow::on_actionViewZoomOneToOne_triggered()
540 {
541         view_->zoom_one_to_one();
542 }
543
544 void MainWindow::on_actionViewShowCursors_triggered()
545 {
546         assert(view_);
547
548         const bool show = !view_->cursors_shown();
549         if(show)
550                 view_->centre_cursors();
551
552         view_->show_cursors(show);
553 }
554
555 void MainWindow::on_actionAbout_triggered()
556 {
557         dialogs::About dlg(device_manager_.context(), this);
558         dlg.exec();
559 }
560
561 void MainWindow::add_decoder(srd_decoder *decoder)
562 {
563 #ifdef ENABLE_DECODE
564         assert(decoder);
565         session_.add_decoder(decoder);
566 #else
567         (void)decoder;
568 #endif
569 }
570
571 void MainWindow::capture_state_changed(int state)
572 {
573         sampling_bar_->set_capture_state((pv::Session::capture_state)state);
574 }
575
576 void MainWindow::device_selected()
577 {
578         // Set the title to include the device/file name
579         const shared_ptr<sigrok::Device> device = session_.device();
580         if (!device)
581                 return;
582
583         const string display_name = device_manager_.get_display_name(device);
584         setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));
585 }
586
587 } // namespace pv