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