]> sigrok.org Git - pulseview.git/blame_incremental - pv/mainwindow.cpp
Set the title of the main toolbar
[pulseview.git] / pv / mainwindow.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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
21#ifdef ENABLE_SIGROKDECODE
22#include <sigrokdecode.h>
23#endif
24
25#include <boost/bind.hpp>
26#include <boost/foreach.hpp>
27
28#include <QAction>
29#include <QApplication>
30#include <QButtonGroup>
31#include <QFileDialog>
32#include <QMessageBox>
33#include <QMenu>
34#include <QMenuBar>
35#include <QStatusBar>
36#include <QVBoxLayout>
37#include <QWidget>
38
39#include "mainwindow.h"
40
41#include "devicemanager.h"
42#include "dialogs/about.h"
43#include "dialogs/connect.h"
44#include "toolbars/samplingbar.h"
45#include "view/view.h"
46
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>
54
55using namespace std;
56
57namespace pv {
58
59MainWindow::MainWindow(DeviceManager &device_manager,
60 const char *open_file_name,
61 QWidget *parent) :
62 QMainWindow(parent),
63 _device_manager(device_manager)
64{
65 setup_ui();
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 }
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
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
96 // Setup the menu bar
97 _menu_bar = new QMenuBar(this);
98 _menu_bar->setGeometry(QRect(0, 0, 400, 25));
99
100 // File Menu
101 _menu_file = new QMenu(_menu_bar);
102 _menu_file->setTitle(QApplication::translate(
103 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
104
105 _action_open = new QAction(this);
106 _action_open->setText(QApplication::translate(
107 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
108 _action_open->setIcon(QIcon::fromTheme("document-open",
109 QIcon(":/icons/document-open.png")));
110 _action_open->setObjectName(QString::fromUtf8("actionOpen"));
111 _menu_file->addAction(_action_open);
112
113 _menu_file->addSeparator();
114
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
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")));
129 _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
130 _action_quit->setObjectName(QString::fromUtf8("actionQuit"));
131 _menu_file->addAction(_action_quit);
132
133 // View Menu
134 _menu_view = new QMenu(_menu_bar);
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"));
145 _menu_view->addAction(_action_view_zoom_in);
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"));
154 _menu_view->addAction(_action_view_zoom_out);
155
156 _menu_view->addSeparator();
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());
161 _action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
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));
166 _menu_view->addAction(_action_view_show_cursors);
167
168 // Help Menu
169 _menu_help = new QMenu(_menu_bar);
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));
177 _menu_help->addAction(_action_about);
178
179 _menu_bar->addAction(_menu_file->menuAction());
180 _menu_bar->addAction(_menu_view->menuAction());
181 _menu_bar->addAction(_menu_help->menuAction());
182
183 setMenuBar(_menu_bar);
184 QMetaObject::connectSlotsByName(this);
185
186 // Setup the toolbar
187 _toolbar = new QToolBar(tr("Main Toolbar"), this);
188 _toolbar->addAction(_action_open);
189 _toolbar->addSeparator();
190 _toolbar->addAction(_action_view_zoom_in);
191 _toolbar->addAction(_action_view_zoom_out);
192 addToolBar(_toolbar);
193
194 // Setup the sampling bar
195 _sampling_bar = new toolbars::SamplingBar(this);
196
197 // Populate the device list and select the initially selected device
198 update_device_list();
199
200 connect(_sampling_bar, SIGNAL(device_selected()), this,
201 SLOT(device_selected()));
202 connect(_sampling_bar, SIGNAL(run_stop()), this,
203 SLOT(run_stop()));
204 addToolBar(_sampling_bar);
205
206 // Set the title
207 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
208 QApplication::UnicodeUTF8));
209
210 // Setup _session events
211 connect(&_session, SIGNAL(capture_state_changed(int)), this,
212 SLOT(capture_state_changed(int)));
213
214}
215
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
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
248void MainWindow::load_file(QString file_name)
249{
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();
267}
268
269void MainWindow::on_actionOpen_triggered()
270{
271 const QString file_name = QFileDialog::getOpenFileName(
272 this, tr("Open File"), "",
273 tr("Sigrok Sessions (*.sr)"));
274 if (!file_name.isEmpty())
275 load_file(file_name);
276}
277
278void MainWindow::on_actionConnect_triggered()
279{
280 dialogs::Connect dlg(this, _device_manager);
281 if (!dlg.exec())
282 return;
283
284 struct sr_dev_inst *const sdi = dlg.get_selected_device();
285 update_device_list(sdi);
286}
287
288void MainWindow::on_actionQuit_triggered()
289{
290 close();
291}
292
293void MainWindow::on_actionViewZoomIn_triggered()
294{
295 _view->zoom(1);
296}
297
298void MainWindow::on_actionViewZoomOut_triggered()
299{
300 _view->zoom(-1);
301}
302
303void MainWindow::on_actionViewShowCursors_triggered()
304{
305 assert(_view);
306
307 const bool show = !_view->cursors_shown();
308 if(show)
309 _view->centre_cursors();
310
311 _view->show_cursors(show);
312}
313
314void MainWindow::on_actionAbout_triggered()
315{
316 dialogs::About dlg(this);
317 dlg.exec();
318}
319
320void MainWindow::device_selected()
321{
322 _session.set_device(_sampling_bar->get_selected_device());
323}
324
325void MainWindow::run_stop()
326{
327 switch(_session.get_capture_state()) {
328 case SigSession::Stopped:
329 _session.start_capture(_sampling_bar->get_record_length(),
330 boost::bind(&MainWindow::session_error, this,
331 QString("Capture failed"), _1));
332 break;
333
334 case SigSession::Running:
335 _session.stop_capture();
336 break;
337 }
338}
339
340void MainWindow::capture_state_changed(int state)
341{
342 _sampling_bar->set_sampling(state != SigSession::Stopped);
343}
344
345} // namespace pv