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