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