]>
Commit | Line | Data |
---|---|---|
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 | ||
269528f5 | 21 | #ifdef ENABLE_DECODE |
aaabd61b | 22 | #include <libsigrokdecode/libsigrokdecode.h> |
269528f5 | 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" | |
94574501 | 42 | #include "device/devinst.h" |
acda14b8 | 43 | #include "dialogs/about.h" |
9663c82b | 44 | #include "dialogs/connect.h" |
0fbda3c2 | 45 | #include "dialogs/storeprogress.h" |
f4c92e1c | 46 | #include "toolbars/samplingbar.h" |
3045c869 | 47 | #include "view/logicsignal.h" |
4104d7f3 | 48 | #include "view/view.h" |
269528f5 | 49 | #ifdef ENABLE_DECODE |
f0d37dab | 50 | #include "widgets/decodermenu.h" |
269528f5 | 51 | #endif |
d7bed479 | 52 | |
30236a54 JH |
53 | /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */ |
54 | #define __STDC_FORMAT_MACROS | |
55 | #include <inttypes.h> | |
56 | #include <stdint.h> | |
57 | #include <stdarg.h> | |
58 | #include <glib.h> | |
59 | #include <libsigrok/libsigrok.h> | |
e82fd481 | 60 | |
19adbc2c | 61 | using boost::shared_ptr; |
819f4c25 | 62 | using std::list; |
30236a54 | 63 | |
51e77110 JH |
64 | namespace pv { |
65 | ||
b1264f56 JH |
66 | namespace view { |
67 | class SelectableItem; | |
68 | } | |
69 | ||
107ca6d3 JH |
70 | MainWindow::MainWindow(DeviceManager &device_manager, |
71 | const char *open_file_name, | |
1d478458 | 72 | QWidget *parent) : |
107ca6d3 | 73 | QMainWindow(parent), |
dc0867ff | 74 | _device_manager(device_manager), |
f0d37dab | 75 | _session(device_manager) |
d7bed479 | 76 | { |
7d5425ef | 77 | setup_ui(); |
1d478458 JH |
78 | if (open_file_name) { |
79 | const QString s(QString::fromUtf8(open_file_name)); | |
80 | QMetaObject::invokeMethod(this, "load_file", | |
81 | Qt::QueuedConnection, | |
82 | Q_ARG(QString, s)); | |
83 | } | |
7d5425ef JH |
84 | } |
85 | ||
86 | void MainWindow::setup_ui() | |
87 | { | |
88 | setObjectName(QString::fromUtf8("MainWindow")); | |
89 | ||
90 | resize(1024, 768); | |
91 | ||
92 | // Set the window icon | |
93 | QIcon icon; | |
94 | icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"), | |
95 | QSize(), QIcon::Normal, QIcon::Off); | |
96 | setWindowIcon(icon); | |
97 | ||
e072efc8 JH |
98 | // Setup the central widget |
99 | _central_widget = new QWidget(this); | |
100 | _vertical_layout = new QVBoxLayout(_central_widget); | |
101 | _vertical_layout->setSpacing(6); | |
102 | _vertical_layout->setContentsMargins(0, 0, 0, 0); | |
103 | setCentralWidget(_central_widget); | |
104 | ||
105 | _view = new pv::view::View(_session, this); | |
b1264f56 | 106 | |
e072efc8 JH |
107 | _vertical_layout->addWidget(_view); |
108 | ||
ab1d13ee | 109 | // Setup the menu bar |
ced1cc13 JH |
110 | QMenuBar *const menu_bar = new QMenuBar(this); |
111 | menu_bar->setGeometry(QRect(0, 0, 400, 25)); | |
a429590b | 112 | |
ab1d13ee | 113 | // File Menu |
ced1cc13 JH |
114 | QMenu *const menu_file = new QMenu; |
115 | menu_file->setTitle(QApplication::translate( | |
ab1d13ee | 116 | "MainWindow", "&File", 0, QApplication::UnicodeUTF8)); |
e072efc8 | 117 | |
ced1cc13 JH |
118 | QAction *const action_open = new QAction(this); |
119 | action_open->setText(QApplication::translate( | |
ab1d13ee | 120 | "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8)); |
ced1cc13 | 121 | action_open->setIcon(QIcon::fromTheme("document-open", |
362eea96 | 122 | QIcon(":/icons/document-open.png"))); |
ced1cc13 JH |
123 | action_open->setObjectName(QString::fromUtf8("actionOpen")); |
124 | menu_file->addAction(action_open); | |
009e1503 | 125 | |
0fbda3c2 JH |
126 | QAction *const action_save_as = new QAction(this); |
127 | action_save_as->setText(QApplication::translate( | |
128 | "MainWindow", "&Save As...", 0, QApplication::UnicodeUTF8)); | |
129 | action_save_as->setIcon(QIcon::fromTheme("document-save-as", | |
130 | QIcon(":/icons/document-save-as.png"))); | |
131 | action_save_as->setObjectName(QString::fromUtf8("actionSaveAs")); | |
132 | menu_file->addAction(action_save_as); | |
133 | ||
ced1cc13 | 134 | menu_file->addSeparator(); |
2a032dcb | 135 | |
ced1cc13 JH |
136 | QAction *const action_connect = new QAction(this); |
137 | action_connect->setText(QApplication::translate( | |
9663c82b JH |
138 | "MainWindow", "&Connect to Device...", 0, |
139 | QApplication::UnicodeUTF8)); | |
ced1cc13 JH |
140 | action_connect->setObjectName(QString::fromUtf8("actionConnect")); |
141 | menu_file->addAction(action_connect); | |
9663c82b | 142 | |
ced1cc13 | 143 | menu_file->addSeparator(); |
9663c82b | 144 | |
ced1cc13 JH |
145 | QAction *action_quit = new QAction(this); |
146 | action_quit->setText(QApplication::translate( | |
2a032dcb | 147 | "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8)); |
ced1cc13 | 148 | action_quit->setIcon(QIcon::fromTheme("application-exit", |
2a032dcb | 149 | QIcon(":/icons/application-exit.png"))); |
ced1cc13 JH |
150 | action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); |
151 | action_quit->setObjectName(QString::fromUtf8("actionQuit")); | |
152 | menu_file->addAction(action_quit); | |
2a032dcb | 153 | |
ab1d13ee | 154 | // View Menu |
ced1cc13 JH |
155 | QMenu *menu_view = new QMenu; |
156 | menu_view->setTitle(QApplication::translate( | |
ab1d13ee JH |
157 | "MainWindow", "&View", 0, QApplication::UnicodeUTF8)); |
158 | ||
ced1cc13 JH |
159 | QAction *const action_view_zoom_in = new QAction(this); |
160 | action_view_zoom_in->setText(QApplication::translate( | |
ab1d13ee | 161 | "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8)); |
ced1cc13 | 162 | action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in", |
ab1d13ee | 163 | QIcon(":/icons/zoom-in.png"))); |
d2899ad5 | 164 | // simply using Qt::Key_Plus shows no + in the menu |
165 | action_view_zoom_in->setShortcut(QKeySequence::ZoomIn); | |
ced1cc13 | 166 | action_view_zoom_in->setObjectName( |
ab1d13ee | 167 | QString::fromUtf8("actionViewZoomIn")); |
ced1cc13 | 168 | menu_view->addAction(action_view_zoom_in); |
ab1d13ee | 169 | |
ced1cc13 JH |
170 | QAction *const action_view_zoom_out = new QAction(this); |
171 | action_view_zoom_out->setText(QApplication::translate( | |
ab1d13ee | 172 | "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8)); |
ced1cc13 | 173 | action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out", |
ab1d13ee | 174 | QIcon(":/icons/zoom-out.png"))); |
d2899ad5 | 175 | action_view_zoom_out->setShortcut(QKeySequence::ZoomOut); |
ced1cc13 | 176 | action_view_zoom_out->setObjectName( |
ab1d13ee | 177 | QString::fromUtf8("actionViewZoomOut")); |
ced1cc13 | 178 | menu_view->addAction(action_view_zoom_out); |
ab1d13ee | 179 | |
ca46b534 JH |
180 | QAction *const action_view_zoom_fit = new QAction(this); |
181 | action_view_zoom_fit->setText(QApplication::translate( | |
182 | "MainWindow", "Zoom to &Fit", 0, QApplication::UnicodeUTF8)); | |
183 | action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit", | |
184 | QIcon(":/icons/zoom-fit.png"))); | |
185 | action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F)); | |
186 | action_view_zoom_fit->setObjectName( | |
187 | QString::fromUtf8("actionViewZoomFit")); | |
188 | menu_view->addAction(action_view_zoom_fit); | |
189 | ||
d1e7d82c JH |
190 | QAction *const action_view_zoom_one_to_one = new QAction(this); |
191 | action_view_zoom_one_to_one->setText(QApplication::translate( | |
192 | "MainWindow", "Zoom to &One-to-One", 0, | |
193 | QApplication::UnicodeUTF8)); | |
194 | action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original", | |
195 | QIcon(":/icons/zoom-original.png"))); | |
196 | action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O)); | |
197 | action_view_zoom_one_to_one->setObjectName( | |
198 | QString::fromUtf8("actionViewZoomOneToOne")); | |
199 | menu_view->addAction(action_view_zoom_one_to_one); | |
200 | ||
ced1cc13 | 201 | menu_view->addSeparator(); |
ab1d13ee | 202 | |
ced1cc13 JH |
203 | QAction *action_view_show_cursors = new QAction(this); |
204 | action_view_show_cursors->setCheckable(true); | |
205 | action_view_show_cursors->setChecked(_view->cursors_shown()); | |
206 | action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C)); | |
207 | action_view_show_cursors->setObjectName( | |
ab1d13ee | 208 | QString::fromUtf8("actionViewShowCursors")); |
ced1cc13 | 209 | action_view_show_cursors->setText(QApplication::translate( |
ab1d13ee | 210 | "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8)); |
ced1cc13 | 211 | menu_view->addAction(action_view_show_cursors); |
a429590b | 212 | |
7dd093df | 213 | // Decoders Menu |
269528f5 | 214 | #ifdef ENABLE_DECODE |
ced1cc13 JH |
215 | QMenu *const menu_decoders = new QMenu; |
216 | menu_decoders->setTitle(QApplication::translate( | |
7dd093df JH |
217 | "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8)); |
218 | ||
ced1cc13 JH |
219 | pv::widgets::DecoderMenu *const menu_decoders_add = |
220 | new pv::widgets::DecoderMenu(menu_decoders); | |
221 | menu_decoders_add->setTitle(QApplication::translate( | |
7dd093df | 222 | "MainWindow", "&Add", 0, QApplication::UnicodeUTF8)); |
ced1cc13 | 223 | connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)), |
f0d37dab | 224 | this, SLOT(add_decoder(srd_decoder*))); |
7dd093df | 225 | |
ced1cc13 | 226 | menu_decoders->addMenu(menu_decoders_add); |
269528f5 | 227 | #endif |
7dd093df | 228 | |
ab1d13ee | 229 | // Help Menu |
ced1cc13 JH |
230 | QMenu *const menu_help = new QMenu; |
231 | menu_help->setTitle(QApplication::translate( | |
ab1d13ee JH |
232 | "MainWindow", "&Help", 0, QApplication::UnicodeUTF8)); |
233 | ||
ced1cc13 JH |
234 | QAction *const action_about = new QAction(this); |
235 | action_about->setObjectName(QString::fromUtf8("actionAbout")); | |
236 | action_about->setText(QApplication::translate( | |
ab1d13ee | 237 | "MainWindow", "&About...", 0, QApplication::UnicodeUTF8)); |
ced1cc13 | 238 | menu_help->addAction(action_about); |
7d5425ef | 239 | |
ced1cc13 JH |
240 | menu_bar->addAction(menu_file->menuAction()); |
241 | menu_bar->addAction(menu_view->menuAction()); | |
269528f5 | 242 | #ifdef ENABLE_DECODE |
ced1cc13 | 243 | menu_bar->addAction(menu_decoders->menuAction()); |
269528f5 | 244 | #endif |
ced1cc13 | 245 | menu_bar->addAction(menu_help->menuAction()); |
7d5425ef | 246 | |
ced1cc13 | 247 | setMenuBar(menu_bar); |
7d5425ef JH |
248 | QMetaObject::connectSlotsByName(this); |
249 | ||
5ac961e3 | 250 | // Setup the toolbar |
ced1cc13 JH |
251 | QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this); |
252 | toolbar->addAction(action_open); | |
253 | toolbar->addSeparator(); | |
254 | toolbar->addAction(action_view_zoom_in); | |
255 | toolbar->addAction(action_view_zoom_out); | |
ca46b534 | 256 | toolbar->addAction(action_view_zoom_fit); |
ced1cc13 | 257 | addToolBar(toolbar); |
0a4db787 | 258 | |
5ac961e3 | 259 | // Setup the sampling bar |
aca00b1e | 260 | _sampling_bar = new toolbars::SamplingBar(_session, this); |
5ac961e3 JH |
261 | |
262 | // Populate the device list and select the initially selected device | |
107ca6d3 | 263 | update_device_list(); |
5ac961e3 | 264 | |
274d4f13 JH |
265 | connect(_sampling_bar, SIGNAL(run_stop()), this, |
266 | SLOT(run_stop())); | |
d4984fe7 JH |
267 | addToolBar(_sampling_bar); |
268 | ||
5ac961e3 | 269 | // Set the title |
a8d3fb2d JH |
270 | setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0, |
271 | QApplication::UnicodeUTF8)); | |
272 | ||
6ac96c2e JH |
273 | // Setup _session events |
274 | connect(&_session, SIGNAL(capture_state_changed(int)), this, | |
275 | SLOT(capture_state_changed(int))); | |
276 | ||
d7bed479 | 277 | } |
30236a54 | 278 | |
f2edb557 JH |
279 | void MainWindow::session_error( |
280 | const QString text, const QString info_text) | |
281 | { | |
282 | QMetaObject::invokeMethod(this, "show_session_error", | |
283 | Qt::QueuedConnection, Q_ARG(QString, text), | |
284 | Q_ARG(QString, info_text)); | |
285 | } | |
286 | ||
94574501 JH |
287 | void MainWindow::update_device_list( |
288 | shared_ptr<pv::device::DevInst> selected_device) | |
107ca6d3 JH |
289 | { |
290 | assert(_sampling_bar); | |
291 | ||
94574501 JH |
292 | const list< shared_ptr<device::DevInst> > &devices = |
293 | _device_manager.devices(); | |
107ca6d3 JH |
294 | _sampling_bar->set_device_list(devices); |
295 | ||
296 | if (!selected_device && !devices.empty()) { | |
297 | // Fall back to the first device in the list. | |
298 | selected_device = devices.front(); | |
299 | ||
300 | // Try and find the demo device and select that by default | |
94574501 | 301 | BOOST_FOREACH (shared_ptr<pv::device::DevInst> dev_inst, devices) |
19adbc2c JH |
302 | if (strcmp(dev_inst->dev_inst()->driver->name, |
303 | "demo") == 0) { | |
304 | selected_device = dev_inst; | |
107ca6d3 JH |
305 | } |
306 | } | |
307 | ||
308 | if (selected_device) { | |
309 | _sampling_bar->set_selected_device(selected_device); | |
310 | _session.set_device(selected_device); | |
311 | } | |
312 | } | |
313 | ||
1d478458 JH |
314 | void MainWindow::load_file(QString file_name) |
315 | { | |
f2edb557 JH |
316 | const QString errorMessage( |
317 | QString("Failed to load file %1").arg(file_name)); | |
318 | const QString infoMessage; | |
319 | _session.load_file(file_name.toStdString(), | |
320 | boost::bind(&MainWindow::session_error, this, | |
321 | errorMessage, infoMessage)); | |
322 | } | |
323 | ||
324 | void MainWindow::show_session_error( | |
325 | const QString text, const QString info_text) | |
326 | { | |
327 | QMessageBox msg(this); | |
328 | msg.setText(text); | |
329 | msg.setInformativeText(info_text); | |
330 | msg.setStandardButtons(QMessageBox::Ok); | |
331 | msg.setIcon(QMessageBox::Warning); | |
332 | msg.exec(); | |
1d478458 JH |
333 | } |
334 | ||
2953961c JH |
335 | void MainWindow::on_actionOpen_triggered() |
336 | { | |
1d43d767 | 337 | // Show the dialog |
1d478458 | 338 | const QString file_name = QFileDialog::getOpenFileName( |
6866d5de JH |
339 | this, tr("Open File"), "", tr( |
340 | "Sigrok Sessions (*.sr);;" | |
341 | "All Files (*.*)")); | |
92b139d0 JH |
342 | if (!file_name.isEmpty()) |
343 | load_file(file_name); | |
2953961c JH |
344 | } |
345 | ||
0fbda3c2 JH |
346 | void MainWindow::on_actionSaveAs_triggered() |
347 | { | |
348 | using pv::dialogs::StoreProgress; | |
349 | ||
350 | // Stop any currently running capture session | |
351 | _session.stop_capture(); | |
352 | ||
353 | // Show the dialog | |
354 | const QString file_name = QFileDialog::getSaveFileName( | |
355 | this, tr("Save File"), "", tr("Sigrok Sessions (*.sr)")); | |
356 | ||
357 | if (file_name.isEmpty()) | |
358 | return; | |
359 | ||
360 | StoreProgress *dlg = new StoreProgress(file_name, _session, this); | |
361 | dlg->run(); | |
362 | } | |
363 | ||
9663c82b JH |
364 | void MainWindow::on_actionConnect_triggered() |
365 | { | |
b99cfc42 JH |
366 | // Stop any currently running capture session |
367 | _session.stop_capture(); | |
368 | ||
107ca6d3 | 369 | dialogs::Connect dlg(this, _device_manager); |
5eb0fa13 | 370 | |
dc0867ff JH |
371 | // If the user selected a device, select it in the device list. Select the |
372 | // current device otherwise. | |
94574501 | 373 | shared_ptr<device::DevInst> dev_inst = dlg.exec() ? |
dc0867ff JH |
374 | dlg.get_selected_device() : _session.get_device(); |
375 | ||
19adbc2c | 376 | update_device_list(dev_inst); |
9663c82b JH |
377 | } |
378 | ||
2a032dcb JH |
379 | void MainWindow::on_actionQuit_triggered() |
380 | { | |
381 | close(); | |
382 | } | |
383 | ||
a429590b JH |
384 | void MainWindow::on_actionViewZoomIn_triggered() |
385 | { | |
386 | _view->zoom(1); | |
387 | } | |
388 | ||
389 | void MainWindow::on_actionViewZoomOut_triggered() | |
390 | { | |
391 | _view->zoom(-1); | |
392 | } | |
393 | ||
ca46b534 JH |
394 | void MainWindow::on_actionViewZoomFit_triggered() |
395 | { | |
396 | _view->zoom_fit(); | |
397 | } | |
398 | ||
d1e7d82c JH |
399 | void MainWindow::on_actionViewZoomOneToOne_triggered() |
400 | { | |
401 | _view->zoom_one_to_one(); | |
402 | } | |
403 | ||
e072efc8 JH |
404 | void MainWindow::on_actionViewShowCursors_triggered() |
405 | { | |
406 | assert(_view); | |
b4d91e56 JH |
407 | |
408 | const bool show = !_view->cursors_shown(); | |
409 | if(show) | |
410 | _view->centre_cursors(); | |
411 | ||
412 | _view->show_cursors(show); | |
e072efc8 JH |
413 | } |
414 | ||
30236a54 JH |
415 | void MainWindow::on_actionAbout_triggered() |
416 | { | |
acda14b8 | 417 | dialogs::About dlg(this); |
40eb2ff4 | 418 | dlg.exec(); |
30236a54 | 419 | } |
274d4f13 | 420 | |
f0d37dab | 421 | void MainWindow::add_decoder(srd_decoder *decoder) |
7dd093df | 422 | { |
269528f5 | 423 | #ifdef ENABLE_DECODE |
f0d37dab JH |
424 | assert(decoder); |
425 | _session.add_decoder(decoder); | |
269528f5 JH |
426 | #else |
427 | (void)decoder; | |
428 | #endif | |
7dd093df JH |
429 | } |
430 | ||
274d4f13 JH |
431 | void MainWindow::run_stop() |
432 | { | |
5b7cf66c JH |
433 | switch(_session.get_capture_state()) { |
434 | case SigSession::Stopped: | |
d99dc9f2 ML |
435 | _session.start_capture( |
436 | boost::bind(&MainWindow::session_error, this, | |
f2edb557 | 437 | QString("Capture failed"), _1)); |
5b7cf66c JH |
438 | break; |
439 | ||
2b49eeb0 | 440 | case SigSession::AwaitingTrigger: |
5b7cf66c JH |
441 | case SigSession::Running: |
442 | _session.stop_capture(); | |
443 | break; | |
444 | } | |
274d4f13 | 445 | } |
51e77110 | 446 | |
6ac96c2e JH |
447 | void MainWindow::capture_state_changed(int state) |
448 | { | |
2b49eeb0 | 449 | _sampling_bar->set_capture_state((pv::SigSession::capture_state)state); |
6ac96c2e JH |
450 | } |
451 | ||
51e77110 | 452 | } // namespace pv |