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