]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
Renamed get_samplerate to samplerate
[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 103 // Setup the menu bar
ced1cc13
JH
104 QMenuBar *const menu_bar = new QMenuBar(this);
105 menu_bar->setGeometry(QRect(0, 0, 400, 25));
a429590b 106
ab1d13ee 107 // File Menu
ced1cc13
JH
108 QMenu *const menu_file = new QMenu;
109 menu_file->setTitle(QApplication::translate(
ab1d13ee 110 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
e072efc8 111
ced1cc13
JH
112 QAction *const action_open = new QAction(this);
113 action_open->setText(QApplication::translate(
ab1d13ee 114 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
ced1cc13 115 action_open->setIcon(QIcon::fromTheme("document-open",
362eea96 116 QIcon(":/icons/document-open.png")));
ced1cc13
JH
117 action_open->setObjectName(QString::fromUtf8("actionOpen"));
118 menu_file->addAction(action_open);
009e1503 119
ced1cc13 120 menu_file->addSeparator();
2a032dcb 121
ced1cc13
JH
122 QAction *const action_connect = new QAction(this);
123 action_connect->setText(QApplication::translate(
9663c82b
JH
124 "MainWindow", "&Connect to Device...", 0,
125 QApplication::UnicodeUTF8));
ced1cc13
JH
126 action_connect->setObjectName(QString::fromUtf8("actionConnect"));
127 menu_file->addAction(action_connect);
9663c82b 128
ced1cc13 129 menu_file->addSeparator();
9663c82b 130
ced1cc13
JH
131 QAction *action_quit = new QAction(this);
132 action_quit->setText(QApplication::translate(
2a032dcb 133 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
ced1cc13 134 action_quit->setIcon(QIcon::fromTheme("application-exit",
2a032dcb 135 QIcon(":/icons/application-exit.png")));
ced1cc13
JH
136 action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
137 action_quit->setObjectName(QString::fromUtf8("actionQuit"));
138 menu_file->addAction(action_quit);
2a032dcb 139
ab1d13ee 140 // View Menu
ced1cc13
JH
141 QMenu *menu_view = new QMenu;
142 menu_view->setTitle(QApplication::translate(
ab1d13ee
JH
143 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
144
ced1cc13
JH
145 QAction *const action_view_zoom_in = new QAction(this);
146 action_view_zoom_in->setText(QApplication::translate(
ab1d13ee 147 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
ced1cc13 148 action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
ab1d13ee 149 QIcon(":/icons/zoom-in.png")));
ced1cc13 150 action_view_zoom_in->setObjectName(
ab1d13ee 151 QString::fromUtf8("actionViewZoomIn"));
ced1cc13 152 menu_view->addAction(action_view_zoom_in);
ab1d13ee 153
ced1cc13
JH
154 QAction *const action_view_zoom_out = new QAction(this);
155 action_view_zoom_out->setText(QApplication::translate(
ab1d13ee 156 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
ced1cc13 157 action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
ab1d13ee 158 QIcon(":/icons/zoom-out.png")));
ced1cc13 159 action_view_zoom_out->setObjectName(
ab1d13ee 160 QString::fromUtf8("actionViewZoomOut"));
ced1cc13 161 menu_view->addAction(action_view_zoom_out);
ab1d13ee 162
ca46b534
JH
163 QAction *const action_view_zoom_fit = new QAction(this);
164 action_view_zoom_fit->setText(QApplication::translate(
165 "MainWindow", "Zoom to &Fit", 0, QApplication::UnicodeUTF8));
166 action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit",
167 QIcon(":/icons/zoom-fit.png")));
168 action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F));
169 action_view_zoom_fit->setObjectName(
170 QString::fromUtf8("actionViewZoomFit"));
171 menu_view->addAction(action_view_zoom_fit);
172
ced1cc13 173 menu_view->addSeparator();
ab1d13ee 174
ced1cc13
JH
175 QAction *action_view_show_cursors = new QAction(this);
176 action_view_show_cursors->setCheckable(true);
177 action_view_show_cursors->setChecked(_view->cursors_shown());
178 action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
179 action_view_show_cursors->setObjectName(
ab1d13ee 180 QString::fromUtf8("actionViewShowCursors"));
ced1cc13 181 action_view_show_cursors->setText(QApplication::translate(
ab1d13ee 182 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
ced1cc13 183 menu_view->addAction(action_view_show_cursors);
a429590b 184
7dd093df 185 // Decoders Menu
ced1cc13
JH
186 QMenu *const menu_decoders = new QMenu;
187 menu_decoders->setTitle(QApplication::translate(
7dd093df
JH
188 "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8));
189
ced1cc13
JH
190 pv::widgets::DecoderMenu *const menu_decoders_add =
191 new pv::widgets::DecoderMenu(menu_decoders);
192 menu_decoders_add->setTitle(QApplication::translate(
7dd093df 193 "MainWindow", "&Add", 0, QApplication::UnicodeUTF8));
ced1cc13 194 connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
f0d37dab 195 this, SLOT(add_decoder(srd_decoder*)));
7dd093df 196
ced1cc13 197 menu_decoders->addMenu(menu_decoders_add);
7dd093df 198
ab1d13ee 199 // Help Menu
ced1cc13
JH
200 QMenu *const menu_help = new QMenu;
201 menu_help->setTitle(QApplication::translate(
ab1d13ee
JH
202 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
203
ced1cc13
JH
204 QAction *const action_about = new QAction(this);
205 action_about->setObjectName(QString::fromUtf8("actionAbout"));
206 action_about->setText(QApplication::translate(
ab1d13ee 207 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
ced1cc13 208 menu_help->addAction(action_about);
7d5425ef 209
ced1cc13
JH
210 menu_bar->addAction(menu_file->menuAction());
211 menu_bar->addAction(menu_view->menuAction());
212 menu_bar->addAction(menu_decoders->menuAction());
213 menu_bar->addAction(menu_help->menuAction());
7d5425ef 214
ced1cc13 215 setMenuBar(menu_bar);
7d5425ef
JH
216 QMetaObject::connectSlotsByName(this);
217
5ac961e3 218 // Setup the toolbar
ced1cc13
JH
219 QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this);
220 toolbar->addAction(action_open);
221 toolbar->addSeparator();
222 toolbar->addAction(action_view_zoom_in);
223 toolbar->addAction(action_view_zoom_out);
ca46b534 224 toolbar->addAction(action_view_zoom_fit);
ced1cc13 225 addToolBar(toolbar);
0a4db787 226
5ac961e3 227 // Setup the sampling bar
aca00b1e 228 _sampling_bar = new toolbars::SamplingBar(_session, this);
5ac961e3
JH
229
230 // Populate the device list and select the initially selected device
107ca6d3 231 update_device_list();
5ac961e3 232
274d4f13
JH
233 connect(_sampling_bar, SIGNAL(run_stop()), this,
234 SLOT(run_stop()));
d4984fe7
JH
235 addToolBar(_sampling_bar);
236
5ac961e3 237 // Set the title
a8d3fb2d
JH
238 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
239 QApplication::UnicodeUTF8));
240
6ac96c2e
JH
241 // Setup _session events
242 connect(&_session, SIGNAL(capture_state_changed(int)), this,
243 SLOT(capture_state_changed(int)));
244
d7bed479 245}
30236a54 246
f2edb557
JH
247void MainWindow::session_error(
248 const QString text, const QString info_text)
249{
250 QMetaObject::invokeMethod(this, "show_session_error",
251 Qt::QueuedConnection, Q_ARG(QString, text),
252 Q_ARG(QString, info_text));
253}
254
107ca6d3
JH
255void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
256{
257 assert(_sampling_bar);
258
259 const list<sr_dev_inst*> &devices = _device_manager.devices();
260 _sampling_bar->set_device_list(devices);
261
262 if (!selected_device && !devices.empty()) {
263 // Fall back to the first device in the list.
264 selected_device = devices.front();
265
266 // Try and find the demo device and select that by default
267 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
268 if (strcmp(sdi->driver->name, "demo") == 0) {
269 selected_device = sdi;
270 }
271 }
272
273 if (selected_device) {
274 _sampling_bar->set_selected_device(selected_device);
275 _session.set_device(selected_device);
276 }
277}
278
1d478458
JH
279void MainWindow::load_file(QString file_name)
280{
f2edb557
JH
281 const QString errorMessage(
282 QString("Failed to load file %1").arg(file_name));
283 const QString infoMessage;
284 _session.load_file(file_name.toStdString(),
285 boost::bind(&MainWindow::session_error, this,
286 errorMessage, infoMessage));
287}
288
289void MainWindow::show_session_error(
290 const QString text, const QString info_text)
291{
292 QMessageBox msg(this);
293 msg.setText(text);
294 msg.setInformativeText(info_text);
295 msg.setStandardButtons(QMessageBox::Ok);
296 msg.setIcon(QMessageBox::Warning);
297 msg.exec();
1d478458
JH
298}
299
2953961c
JH
300void MainWindow::on_actionOpen_triggered()
301{
1d43d767
JH
302 // Enumerate the file formats
303 QString filters(tr("Sigrok Sessions (*.sr)"));
304 filters.append(tr(";;All Files (*.*)"));
305
306 // Show the dialog
1d478458 307 const QString file_name = QFileDialog::getOpenFileName(
1d43d767 308 this, tr("Open File"), "", filters);
92b139d0
JH
309 if (!file_name.isEmpty())
310 load_file(file_name);
2953961c
JH
311}
312
9663c82b
JH
313void MainWindow::on_actionConnect_triggered()
314{
b99cfc42
JH
315 // Stop any currently running capture session
316 _session.stop_capture();
317
107ca6d3 318 dialogs::Connect dlg(this, _device_manager);
5eb0fa13 319
dc0867ff
JH
320 // If the user selected a device, select it in the device list. Select the
321 // current device otherwise.
322 struct sr_dev_inst *const sdi = dlg.exec() ?
323 dlg.get_selected_device() : _session.get_device();
324
107ca6d3 325 update_device_list(sdi);
9663c82b
JH
326}
327
2a032dcb
JH
328void MainWindow::on_actionQuit_triggered()
329{
330 close();
331}
332
a429590b
JH
333void MainWindow::on_actionViewZoomIn_triggered()
334{
335 _view->zoom(1);
336}
337
338void MainWindow::on_actionViewZoomOut_triggered()
339{
340 _view->zoom(-1);
341}
342
ca46b534
JH
343void MainWindow::on_actionViewZoomFit_triggered()
344{
345 _view->zoom_fit();
346}
347
e072efc8
JH
348void MainWindow::on_actionViewShowCursors_triggered()
349{
350 assert(_view);
b4d91e56
JH
351
352 const bool show = !_view->cursors_shown();
353 if(show)
354 _view->centre_cursors();
355
356 _view->show_cursors(show);
e072efc8
JH
357}
358
30236a54
JH
359void MainWindow::on_actionAbout_triggered()
360{
acda14b8 361 dialogs::About dlg(this);
40eb2ff4 362 dlg.exec();
30236a54 363}
274d4f13 364
f0d37dab 365void MainWindow::add_decoder(srd_decoder *decoder)
7dd093df 366{
f0d37dab
JH
367 assert(decoder);
368 _session.add_decoder(decoder);
7dd093df
JH
369}
370
274d4f13
JH
371void MainWindow::run_stop()
372{
5b7cf66c
JH
373 switch(_session.get_capture_state()) {
374 case SigSession::Stopped:
d64d1596 375 _session.start_capture(_sampling_bar->get_record_length(),
f2edb557
JH
376 boost::bind(&MainWindow::session_error, this,
377 QString("Capture failed"), _1));
5b7cf66c
JH
378 break;
379
2b49eeb0 380 case SigSession::AwaitingTrigger:
5b7cf66c
JH
381 case SigSession::Running:
382 _session.stop_capture();
383 break;
384 }
274d4f13 385}
51e77110 386
6ac96c2e
JH
387void MainWindow::capture_state_changed(int state)
388{
2b49eeb0 389 _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);
6ac96c2e
JH
390}
391
51e77110 392} // namespace pv