]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
Added Popup::closed signal
[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
aaabd61b 21#include <libsigrokdecode/libsigrokdecode.h>
30236a54 22
f2edb557 23#include <boost/bind.hpp>
107ca6d3 24#include <boost/foreach.hpp>
f2edb557 25
7d5425ef
JH
26#include <QAction>
27#include <QApplication>
28#include <QButtonGroup>
2953961c 29#include <QFileDialog>
f2edb557 30#include <QMessageBox>
7d5425ef
JH
31#include <QMenu>
32#include <QMenuBar>
33#include <QStatusBar>
34#include <QVBoxLayout>
35#include <QWidget>
2953961c 36
d7bed479 37#include "mainwindow.h"
107ca6d3
JH
38
39#include "devicemanager.h"
acda14b8 40#include "dialogs/about.h"
9663c82b 41#include "dialogs/connect.h"
f4c92e1c 42#include "toolbars/samplingbar.h"
3045c869 43#include "view/logicsignal.h"
4104d7f3 44#include "view/view.h"
f0d37dab 45#include "widgets/decodermenu.h"
d7bed479 46
30236a54
JH
47/* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
48#define __STDC_FORMAT_MACROS
49#include <inttypes.h>
50#include <stdint.h>
51#include <stdarg.h>
52#include <glib.h>
53#include <libsigrok/libsigrok.h>
e82fd481 54
b1264f56 55using namespace boost;
107ca6d3 56using namespace std;
30236a54 57
51e77110
JH
58namespace pv {
59
b1264f56
JH
60namespace view {
61class SelectableItem;
62}
63
107ca6d3
JH
64MainWindow::MainWindow(DeviceManager &device_manager,
65 const char *open_file_name,
1d478458 66 QWidget *parent) :
107ca6d3 67 QMainWindow(parent),
dc0867ff 68 _device_manager(device_manager),
f0d37dab 69 _session(device_manager)
d7bed479 70{
7d5425ef 71 setup_ui();
1d478458
JH
72 if (open_file_name) {
73 const QString s(QString::fromUtf8(open_file_name));
74 QMetaObject::invokeMethod(this, "load_file",
75 Qt::QueuedConnection,
76 Q_ARG(QString, s));
77 }
7d5425ef
JH
78}
79
80void MainWindow::setup_ui()
81{
82 setObjectName(QString::fromUtf8("MainWindow"));
83
84 resize(1024, 768);
85
86 // Set the window icon
87 QIcon icon;
88 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
89 QSize(), QIcon::Normal, QIcon::Off);
90 setWindowIcon(icon);
91
e072efc8
JH
92 // Setup the central widget
93 _central_widget = new QWidget(this);
94 _vertical_layout = new QVBoxLayout(_central_widget);
95 _vertical_layout->setSpacing(6);
96 _vertical_layout->setContentsMargins(0, 0, 0, 0);
97 setCentralWidget(_central_widget);
98
99 _view = new pv::view::View(_session, this);
b1264f56 100
e072efc8
JH
101 _vertical_layout->addWidget(_view);
102
ab1d13ee
JH
103 // Setup the menu bar
104 _menu_bar = new QMenuBar(this);
105 _menu_bar->setGeometry(QRect(0, 0, 400, 25));
a429590b 106
ab1d13ee
JH
107 // File Menu
108 _menu_file = new QMenu(_menu_bar);
109 _menu_file->setTitle(QApplication::translate(
110 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
e072efc8 111
7d5425ef 112 _action_open = new QAction(this);
ab1d13ee
JH
113 _action_open->setText(QApplication::translate(
114 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
362eea96
JH
115 _action_open->setIcon(QIcon::fromTheme("document-open",
116 QIcon(":/icons/document-open.png")));
7d5425ef 117 _action_open->setObjectName(QString::fromUtf8("actionOpen"));
7d5425ef 118 _menu_file->addAction(_action_open);
009e1503 119
2a032dcb
JH
120 _menu_file->addSeparator();
121
9663c82b
JH
122 _action_connect = new QAction(this);
123 _action_connect->setText(QApplication::translate(
124 "MainWindow", "&Connect to Device...", 0,
125 QApplication::UnicodeUTF8));
126 _action_connect->setObjectName(QString::fromUtf8("actionConnect"));
127 _menu_file->addAction(_action_connect);
128
129 _menu_file->addSeparator();
130
2a032dcb
JH
131 _action_quit = new QAction(this);
132 _action_quit->setText(QApplication::translate(
133 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
134 _action_quit->setIcon(QIcon::fromTheme("application-exit",
135 QIcon(":/icons/application-exit.png")));
d1bb7d7a 136 _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
2a032dcb
JH
137 _action_quit->setObjectName(QString::fromUtf8("actionQuit"));
138 _menu_file->addAction(_action_quit);
139
ab1d13ee 140 // View Menu
a429590b 141 _menu_view = new QMenu(_menu_bar);
ab1d13ee
JH
142 _menu_view->setTitle(QApplication::translate(
143 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
144
145 _action_view_zoom_in = new QAction(this);
146 _action_view_zoom_in->setText(QApplication::translate(
147 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
148 _action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
149 QIcon(":/icons/zoom-in.png")));
150 _action_view_zoom_in->setObjectName(
151 QString::fromUtf8("actionViewZoomIn"));
a429590b 152 _menu_view->addAction(_action_view_zoom_in);
ab1d13ee
JH
153
154 _action_view_zoom_out = new QAction(this);
155 _action_view_zoom_out->setText(QApplication::translate(
156 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
157 _action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
158 QIcon(":/icons/zoom-out.png")));
159 _action_view_zoom_out->setObjectName(
160 QString::fromUtf8("actionViewZoomOut"));
a429590b 161 _menu_view->addAction(_action_view_zoom_out);
ab1d13ee 162
e072efc8 163 _menu_view->addSeparator();
ab1d13ee
JH
164
165 _action_view_show_cursors = new QAction(this);
166 _action_view_show_cursors->setCheckable(true);
167 _action_view_show_cursors->setChecked(_view->cursors_shown());
4166ed6c 168 _action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
ab1d13ee
JH
169 _action_view_show_cursors->setObjectName(
170 QString::fromUtf8("actionViewShowCursors"));
171 _action_view_show_cursors->setText(QApplication::translate(
172 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
e072efc8 173 _menu_view->addAction(_action_view_show_cursors);
a429590b 174
7dd093df
JH
175 // Decoders Menu
176 _menu_decoders = new QMenu(_menu_bar);
177 _menu_decoders->setTitle(QApplication::translate(
178 "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8));
179
f0d37dab 180 _menu_decoders_add = new pv::widgets::DecoderMenu(_menu_decoders);
7dd093df
JH
181 _menu_decoders_add->setTitle(QApplication::translate(
182 "MainWindow", "&Add", 0, QApplication::UnicodeUTF8));
f0d37dab
JH
183 connect(_menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
184 this, SLOT(add_decoder(srd_decoder*)));
7dd093df
JH
185
186 _menu_decoders->addMenu(_menu_decoders_add);
7dd093df 187
ab1d13ee 188 // Help Menu
7d5425ef 189 _menu_help = new QMenu(_menu_bar);
ab1d13ee
JH
190 _menu_help->setTitle(QApplication::translate(
191 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
192
193 _action_about = new QAction(this);
194 _action_about->setObjectName(QString::fromUtf8("actionAbout"));
195 _action_about->setText(QApplication::translate(
196 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
7d5425ef
JH
197 _menu_help->addAction(_action_about);
198
199 _menu_bar->addAction(_menu_file->menuAction());
a429590b 200 _menu_bar->addAction(_menu_view->menuAction());
7dd093df 201 _menu_bar->addAction(_menu_decoders->menuAction());
7d5425ef
JH
202 _menu_bar->addAction(_menu_help->menuAction());
203
204 setMenuBar(_menu_bar);
205 QMetaObject::connectSlotsByName(this);
206
5ac961e3 207 // Setup the toolbar
363107a8 208 _toolbar = new QToolBar(tr("Main Toolbar"), this);
0a4db787 209 _toolbar->addAction(_action_open);
a429590b
JH
210 _toolbar->addSeparator();
211 _toolbar->addAction(_action_view_zoom_in);
212 _toolbar->addAction(_action_view_zoom_out);
0a4db787
JH
213 addToolBar(_toolbar);
214
5ac961e3 215 // Setup the sampling bar
aca00b1e 216 _sampling_bar = new toolbars::SamplingBar(_session, this);
5ac961e3
JH
217
218 // Populate the device list and select the initially selected device
107ca6d3 219 update_device_list();
5ac961e3 220
274d4f13
JH
221 connect(_sampling_bar, SIGNAL(run_stop()), this,
222 SLOT(run_stop()));
d4984fe7
JH
223 addToolBar(_sampling_bar);
224
5ac961e3 225 // Set the title
a8d3fb2d
JH
226 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
227 QApplication::UnicodeUTF8));
228
6ac96c2e
JH
229 // Setup _session events
230 connect(&_session, SIGNAL(capture_state_changed(int)), this,
231 SLOT(capture_state_changed(int)));
232
d7bed479 233}
30236a54 234
f2edb557
JH
235void MainWindow::session_error(
236 const QString text, const QString info_text)
237{
238 QMetaObject::invokeMethod(this, "show_session_error",
239 Qt::QueuedConnection, Q_ARG(QString, text),
240 Q_ARG(QString, info_text));
241}
242
107ca6d3
JH
243void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
244{
245 assert(_sampling_bar);
246
247 const list<sr_dev_inst*> &devices = _device_manager.devices();
248 _sampling_bar->set_device_list(devices);
249
250 if (!selected_device && !devices.empty()) {
251 // Fall back to the first device in the list.
252 selected_device = devices.front();
253
254 // Try and find the demo device and select that by default
255 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
256 if (strcmp(sdi->driver->name, "demo") == 0) {
257 selected_device = sdi;
258 }
259 }
260
261 if (selected_device) {
262 _sampling_bar->set_selected_device(selected_device);
263 _session.set_device(selected_device);
264 }
265}
266
1d478458
JH
267void MainWindow::load_file(QString file_name)
268{
f2edb557
JH
269 const QString errorMessage(
270 QString("Failed to load file %1").arg(file_name));
271 const QString infoMessage;
272 _session.load_file(file_name.toStdString(),
273 boost::bind(&MainWindow::session_error, this,
274 errorMessage, infoMessage));
275}
276
277void MainWindow::show_session_error(
278 const QString text, const QString info_text)
279{
280 QMessageBox msg(this);
281 msg.setText(text);
282 msg.setInformativeText(info_text);
283 msg.setStandardButtons(QMessageBox::Ok);
284 msg.setIcon(QMessageBox::Warning);
285 msg.exec();
1d478458
JH
286}
287
2953961c
JH
288void MainWindow::on_actionOpen_triggered()
289{
1d43d767
JH
290 // Enumerate the file formats
291 QString filters(tr("Sigrok Sessions (*.sr)"));
292 filters.append(tr(";;All Files (*.*)"));
293
294 // Show the dialog
1d478458 295 const QString file_name = QFileDialog::getOpenFileName(
1d43d767 296 this, tr("Open File"), "", filters);
92b139d0
JH
297 if (!file_name.isEmpty())
298 load_file(file_name);
2953961c
JH
299}
300
9663c82b
JH
301void MainWindow::on_actionConnect_triggered()
302{
b99cfc42
JH
303 // Stop any currently running capture session
304 _session.stop_capture();
305
107ca6d3 306 dialogs::Connect dlg(this, _device_manager);
5eb0fa13 307
dc0867ff
JH
308 // If the user selected a device, select it in the device list. Select the
309 // current device otherwise.
310 struct sr_dev_inst *const sdi = dlg.exec() ?
311 dlg.get_selected_device() : _session.get_device();
312
107ca6d3 313 update_device_list(sdi);
9663c82b
JH
314}
315
2a032dcb
JH
316void MainWindow::on_actionQuit_triggered()
317{
318 close();
319}
320
a429590b
JH
321void MainWindow::on_actionViewZoomIn_triggered()
322{
323 _view->zoom(1);
324}
325
326void MainWindow::on_actionViewZoomOut_triggered()
327{
328 _view->zoom(-1);
329}
330
e072efc8
JH
331void MainWindow::on_actionViewShowCursors_triggered()
332{
333 assert(_view);
b4d91e56
JH
334
335 const bool show = !_view->cursors_shown();
336 if(show)
337 _view->centre_cursors();
338
339 _view->show_cursors(show);
e072efc8
JH
340}
341
30236a54
JH
342void MainWindow::on_actionAbout_triggered()
343{
acda14b8 344 dialogs::About dlg(this);
40eb2ff4 345 dlg.exec();
30236a54 346}
274d4f13 347
f0d37dab 348void MainWindow::add_decoder(srd_decoder *decoder)
7dd093df 349{
f0d37dab
JH
350 assert(decoder);
351 _session.add_decoder(decoder);
7dd093df
JH
352}
353
274d4f13
JH
354void MainWindow::run_stop()
355{
5b7cf66c
JH
356 switch(_session.get_capture_state()) {
357 case SigSession::Stopped:
d64d1596 358 _session.start_capture(_sampling_bar->get_record_length(),
f2edb557
JH
359 boost::bind(&MainWindow::session_error, this,
360 QString("Capture failed"), _1));
5b7cf66c
JH
361 break;
362
2b49eeb0 363 case SigSession::AwaitingTrigger:
5b7cf66c
JH
364 case SigSession::Running:
365 _session.stop_capture();
366 break;
367 }
274d4f13 368}
51e77110 369
6ac96c2e
JH
370void MainWindow::capture_state_changed(int state)
371{
2b49eeb0 372 _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);
6ac96c2e
JH
373}
374
51e77110 375} // namespace pv