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