]> sigrok.org Git - pulseview.git/blame_incremental - pv/mainwindow.cpp
MainWindow: Made QActions into member variables
[pulseview.git] / pv / mainwindow.cpp
... / ...
CommitLineData
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
62using std::list;
63using std::map;
64using std::shared_ptr;
65using std::string;
66
67using sigrok::Device;
68using sigrok::Error;
69using sigrok::HardwareDevice;
70
71namespace pv {
72
73namespace view {
74class ViewItem;
75}
76
77const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
78const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
79
80MainWindow::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
107QAction* MainWindow::action_open() const
108{
109 return action_open_;
110}
111
112QAction* MainWindow::action_save_as() const
113{
114 return action_save_as_;
115}
116
117QAction* MainWindow::action_connect() const
118{
119 return action_connect_;
120}
121
122QAction* MainWindow::action_quit() const
123{
124 return action_quit_;
125}
126
127QAction* MainWindow::action_view_zoom_in() const
128{
129 return action_view_zoom_in_;
130}
131
132QAction* MainWindow::action_view_zoom_out() const
133{
134 return action_view_zoom_out_;
135}
136
137QAction* MainWindow::action_view_zoom_fit() const
138{
139 return action_view_zoom_fit_;
140}
141
142QAction* MainWindow::action_view_zoom_one_to_one() const
143{
144 return action_view_zoom_one_to_one_;
145}
146
147QAction* MainWindow::action_view_show_cursors() const
148{
149 return action_view_show_cursors_;
150}
151
152QAction* MainWindow::action_about() const
153{
154 return action_about_;
155}
156
157void 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
172void 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
186void 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_->setShortcut(QKeySequence(Qt::Key_C));
286 action_view_show_cursors_->setObjectName(
287 QString::fromUtf8("actionViewShowCursors"));
288 action_view_show_cursors_->setText(tr("Show &Cursors"));
289 menu_view->addAction(action_view_show_cursors_);
290
291 // Decoders Menu
292#ifdef ENABLE_DECODE
293 QMenu *const menu_decoders = new QMenu;
294 menu_decoders->setTitle(tr("&Decoders"));
295
296 pv::widgets::DecoderMenu *const menu_decoders_add =
297 new pv::widgets::DecoderMenu(menu_decoders, true);
298 menu_decoders_add->setTitle(tr("&Add"));
299 connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
300 this, SLOT(add_decoder(srd_decoder*)));
301
302 menu_decoders->addMenu(menu_decoders_add);
303#endif
304
305 // Help Menu
306 QMenu *const menu_help = new QMenu;
307 menu_help->setTitle(tr("&Help"));
308
309 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
310 action_about_->setText(tr("&About..."));
311 menu_help->addAction(action_about_);
312
313 menu_bar->addAction(menu_file->menuAction());
314 menu_bar->addAction(menu_view->menuAction());
315#ifdef ENABLE_DECODE
316 menu_bar->addAction(menu_decoders->menuAction());
317#endif
318 menu_bar->addAction(menu_help->menuAction());
319
320 setMenuBar(menu_bar);
321 QMetaObject::connectSlotsByName(this);
322
323 // Setup the toolbar
324 QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this);
325 toolbar->setObjectName(QString::fromUtf8("MainToolbar"));
326 toolbar->addAction(action_open_);
327 toolbar->addAction(action_save_as_);
328 toolbar->addSeparator();
329 toolbar->addAction(action_view_zoom_in_);
330 toolbar->addAction(action_view_zoom_out_);
331 toolbar->addAction(action_view_zoom_fit_);
332 toolbar->addAction(action_view_zoom_one_to_one_);
333 addToolBar(toolbar);
334
335 // Setup the sampling bar
336 main_bar_ = new toolbars::MainBar(session_, *this);
337
338 // Populate the device list and select the initially selected device
339 update_device_list();
340
341 addToolBar(main_bar_);
342
343 // Set the title
344 setWindowTitle(tr("PulseView"));
345
346 // Setup session_ events
347 connect(&session_, SIGNAL(capture_state_changed(int)), this,
348 SLOT(capture_state_changed(int)));
349 connect(&session_, SIGNAL(device_selected()), this,
350 SLOT(device_selected()));
351}
352
353void MainWindow::save_ui_settings()
354{
355 QSettings settings;
356
357 map<string, string> dev_info;
358 list<string> key_list;
359
360 settings.beginGroup("MainWindow");
361 settings.setValue("state", saveState());
362 settings.setValue("geometry", saveGeometry());
363 settings.endGroup();
364
365 if (session_.device()) {
366 settings.beginGroup("Device");
367 key_list.push_back("vendor");
368 key_list.push_back("model");
369 key_list.push_back("version");
370 key_list.push_back("serial_num");
371 key_list.push_back("connection_id");
372
373 dev_info = device_manager_.get_device_info(
374 session_.device());
375
376 for (string key : key_list) {
377
378 if (dev_info.count(key))
379 settings.setValue(QString::fromUtf8(key.c_str()),
380 QString::fromUtf8(dev_info.at(key).c_str()));
381 else
382 settings.remove(QString::fromUtf8(key.c_str()));
383 }
384
385 settings.endGroup();
386 }
387}
388
389void MainWindow::restore_ui_settings()
390{
391 QSettings settings;
392
393 shared_ptr<HardwareDevice> device;
394
395 map<string, string> dev_info;
396 list<string> key_list;
397 string value;
398
399 settings.beginGroup("MainWindow");
400
401 if (settings.contains("geometry")) {
402 restoreGeometry(settings.value("geometry").toByteArray());
403 restoreState(settings.value("state").toByteArray());
404 } else
405 resize(1000, 720);
406
407 settings.endGroup();
408
409 // Re-select last used device if possible.
410 settings.beginGroup("Device");
411 key_list.push_back("vendor");
412 key_list.push_back("model");
413 key_list.push_back("version");
414 key_list.push_back("serial_num");
415 key_list.push_back("connection_id");
416
417 for (string key : key_list) {
418 if (!settings.contains(QString::fromUtf8(key.c_str())))
419 continue;
420
421 value = settings.value(QString::fromUtf8(key.c_str())).toString().toStdString();
422
423 if (value.size() > 0)
424 dev_info.insert(std::make_pair(key, value));
425 }
426
427 device = device_manager_.find_device_from_info(dev_info);
428
429 if (device) {
430 select_device(device);
431 update_device_list();
432 }
433
434 settings.endGroup();
435}
436
437void MainWindow::session_error(
438 const QString text, const QString info_text)
439{
440 QMetaObject::invokeMethod(this, "show_session_error",
441 Qt::QueuedConnection, Q_ARG(QString, text),
442 Q_ARG(QString, info_text));
443}
444
445void MainWindow::update_device_list()
446{
447 assert(main_bar_);
448
449 shared_ptr<Device> selected_device = session_.device();
450 list< shared_ptr<Device> > devices;
451
452 if (device_manager_.devices().size() == 0)
453 return;
454
455 std::copy(device_manager_.devices().begin(),
456 device_manager_.devices().end(), std::back_inserter(devices));
457
458 if (std::find(devices.begin(), devices.end(), selected_device) ==
459 devices.end())
460 devices.push_back(selected_device);
461 assert(selected_device);
462
463 main_bar_->set_device_list(devices, selected_device);
464}
465
466void MainWindow::closeEvent(QCloseEvent *event)
467{
468 save_ui_settings();
469 event->accept();
470}
471
472void MainWindow::load_file(QString file_name)
473{
474 const QString errorMessage(
475 QString("Failed to load file %1").arg(file_name));
476 const QString infoMessage;
477
478 try {
479 session_.set_file(file_name.toStdString());
480 } catch(Error e) {
481 show_session_error(tr("Failed to load ") + file_name, e.what());
482 session_.set_default_device();
483 update_device_list();
484 return;
485 }
486
487 update_device_list();
488
489 session_.start_capture([&, errorMessage, infoMessage](QString) {
490 session_error(errorMessage, infoMessage); });
491}
492
493void MainWindow::show_session_error(
494 const QString text, const QString info_text)
495{
496 QMessageBox msg(this);
497 msg.setText(text);
498 msg.setInformativeText(info_text);
499 msg.setStandardButtons(QMessageBox::Ok);
500 msg.setIcon(QMessageBox::Warning);
501 msg.exec();
502}
503
504void MainWindow::on_actionOpen_triggered()
505{
506 QSettings settings;
507 const QString dir = settings.value(SettingOpenDirectory).toString();
508
509 // Show the dialog
510 const QString file_name = QFileDialog::getOpenFileName(
511 this, tr("Open File"), dir, tr(
512 "Sigrok Sessions (*.sr);;"
513 "All Files (*.*)"));
514
515 if (!file_name.isEmpty()) {
516 load_file(file_name);
517
518 const QString abs_path = QFileInfo(file_name).absolutePath();
519 settings.setValue(SettingOpenDirectory, abs_path);
520 }
521}
522
523void MainWindow::on_actionSaveAs_triggered()
524{
525 using pv::dialogs::StoreProgress;
526
527 // Stop any currently running capture session
528 session_.stop_capture();
529
530 QSettings settings;
531 const QString dir = settings.value(SettingSaveDirectory).toString();
532
533 // Show the dialog
534 const QString file_name = QFileDialog::getSaveFileName(
535 this, tr("Save File"), dir, tr("Sigrok Sessions (*.sr)"));
536
537 if (file_name.isEmpty())
538 return;
539
540 const QString abs_path = QFileInfo(file_name).absolutePath();
541 settings.setValue(SettingSaveDirectory, abs_path);
542
543 StoreProgress *dlg = new StoreProgress(file_name, session_, this);
544 dlg->run();
545}
546
547void MainWindow::on_actionConnect_triggered()
548{
549 // Stop any currently running capture session
550 session_.stop_capture();
551
552 dialogs::Connect dlg(this, device_manager_);
553
554 // If the user selected a device, select it in the device list. Select the
555 // current device otherwise.
556 if (dlg.exec())
557 select_device(dlg.get_selected_device());
558
559 update_device_list();
560}
561
562void MainWindow::on_actionQuit_triggered()
563{
564 close();
565}
566
567void MainWindow::on_actionViewZoomIn_triggered()
568{
569 view_->zoom(1);
570}
571
572void MainWindow::on_actionViewZoomOut_triggered()
573{
574 view_->zoom(-1);
575}
576
577void MainWindow::on_actionViewZoomFit_triggered()
578{
579 view_->zoom_fit();
580}
581
582void MainWindow::on_actionViewZoomOneToOne_triggered()
583{
584 view_->zoom_one_to_one();
585}
586
587void MainWindow::on_actionViewShowCursors_triggered()
588{
589 assert(view_);
590
591 const bool show = !view_->cursors_shown();
592 if(show)
593 view_->centre_cursors();
594
595 view_->show_cursors(show);
596}
597
598void MainWindow::on_actionAbout_triggered()
599{
600 dialogs::About dlg(device_manager_.context(), this);
601 dlg.exec();
602}
603
604void MainWindow::add_decoder(srd_decoder *decoder)
605{
606#ifdef ENABLE_DECODE
607 assert(decoder);
608 session_.add_decoder(decoder);
609#else
610 (void)decoder;
611#endif
612}
613
614void MainWindow::capture_state_changed(int state)
615{
616 main_bar_->set_capture_state((pv::Session::capture_state)state);
617}
618
619void MainWindow::device_selected()
620{
621 // Set the title to include the device/file name
622 const shared_ptr<sigrok::Device> device = session_.device();
623 if (!device)
624 return;
625
626 const string display_name = device_manager_.get_display_name(device);
627 setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));
628}
629
630} // namespace pv