]>
Commit | Line | Data |
---|---|---|
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_DECODE | |
22 | #include <libsigrokdecode/libsigrokdecode.h> | |
23 | #endif | |
24 | ||
25 | #include <algorithm> | |
26 | #include <iterator> | |
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 "device/device.h" | |
43 | #include "dialogs/about.h" | |
44 | #include "dialogs/connect.h" | |
45 | #include "dialogs/storeprogress.h" | |
46 | #include "toolbars/samplingbar.h" | |
47 | #include "view/logicsignal.h" | |
48 | #include "view/view.h" | |
49 | #ifdef ENABLE_DECODE | |
50 | #include "widgets/decodermenu.h" | |
51 | #endif | |
52 | ||
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> | |
60 | ||
61 | using std::list; | |
62 | using std::shared_ptr; | |
63 | ||
64 | namespace pv { | |
65 | ||
66 | namespace view { | |
67 | class SelectableItem; | |
68 | } | |
69 | ||
70 | MainWindow::MainWindow(DeviceManager &device_manager, | |
71 | const char *open_file_name, | |
72 | QWidget *parent) : | |
73 | QMainWindow(parent), | |
74 | _device_manager(device_manager), | |
75 | _session(device_manager) | |
76 | { | |
77 | setup_ui(); | |
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 | } | |
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 | ||
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); | |
106 | ||
107 | _vertical_layout->addWidget(_view); | |
108 | ||
109 | // Setup the menu bar | |
110 | QMenuBar *const menu_bar = new QMenuBar(this); | |
111 | menu_bar->setGeometry(QRect(0, 0, 400, 25)); | |
112 | ||
113 | // File Menu | |
114 | QMenu *const menu_file = new QMenu; | |
115 | menu_file->setTitle(QApplication::translate( | |
116 | "MainWindow", "&File", 0, QApplication::UnicodeUTF8)); | |
117 | ||
118 | QAction *const action_open = new QAction(this); | |
119 | action_open->setText(QApplication::translate( | |
120 | "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8)); | |
121 | action_open->setIcon(QIcon::fromTheme("document-open", | |
122 | QIcon(":/icons/document-open.png"))); | |
123 | action_open->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); | |
124 | action_open->setObjectName(QString::fromUtf8("actionOpen")); | |
125 | menu_file->addAction(action_open); | |
126 | ||
127 | QAction *const action_save_as = new QAction(this); | |
128 | action_save_as->setText(QApplication::translate( | |
129 | "MainWindow", "&Save As...", 0, QApplication::UnicodeUTF8)); | |
130 | action_save_as->setIcon(QIcon::fromTheme("document-save-as", | |
131 | QIcon(":/icons/document-save-as.png"))); | |
132 | action_save_as->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); | |
133 | action_save_as->setObjectName(QString::fromUtf8("actionSaveAs")); | |
134 | menu_file->addAction(action_save_as); | |
135 | ||
136 | menu_file->addSeparator(); | |
137 | ||
138 | QAction *const action_connect = new QAction(this); | |
139 | action_connect->setText(QApplication::translate( | |
140 | "MainWindow", "&Connect to Device...", 0, | |
141 | QApplication::UnicodeUTF8)); | |
142 | action_connect->setObjectName(QString::fromUtf8("actionConnect")); | |
143 | menu_file->addAction(action_connect); | |
144 | ||
145 | menu_file->addSeparator(); | |
146 | ||
147 | QAction *action_quit = new QAction(this); | |
148 | action_quit->setText(QApplication::translate( | |
149 | "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8)); | |
150 | action_quit->setIcon(QIcon::fromTheme("application-exit", | |
151 | QIcon(":/icons/application-exit.png"))); | |
152 | action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); | |
153 | action_quit->setObjectName(QString::fromUtf8("actionQuit")); | |
154 | menu_file->addAction(action_quit); | |
155 | ||
156 | // View Menu | |
157 | QMenu *menu_view = new QMenu; | |
158 | menu_view->setTitle(QApplication::translate( | |
159 | "MainWindow", "&View", 0, QApplication::UnicodeUTF8)); | |
160 | ||
161 | QAction *const action_view_zoom_in = new QAction(this); | |
162 | action_view_zoom_in->setText(QApplication::translate( | |
163 | "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8)); | |
164 | action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in", | |
165 | QIcon(":/icons/zoom-in.png"))); | |
166 | // simply using Qt::Key_Plus shows no + in the menu | |
167 | action_view_zoom_in->setShortcut(QKeySequence::ZoomIn); | |
168 | action_view_zoom_in->setObjectName( | |
169 | QString::fromUtf8("actionViewZoomIn")); | |
170 | menu_view->addAction(action_view_zoom_in); | |
171 | ||
172 | QAction *const action_view_zoom_out = new QAction(this); | |
173 | action_view_zoom_out->setText(QApplication::translate( | |
174 | "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8)); | |
175 | action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out", | |
176 | QIcon(":/icons/zoom-out.png"))); | |
177 | action_view_zoom_out->setShortcut(QKeySequence::ZoomOut); | |
178 | action_view_zoom_out->setObjectName( | |
179 | QString::fromUtf8("actionViewZoomOut")); | |
180 | menu_view->addAction(action_view_zoom_out); | |
181 | ||
182 | QAction *const action_view_zoom_fit = new QAction(this); | |
183 | action_view_zoom_fit->setText(QApplication::translate( | |
184 | "MainWindow", "Zoom to &Fit", 0, QApplication::UnicodeUTF8)); | |
185 | action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit", | |
186 | QIcon(":/icons/zoom-fit.png"))); | |
187 | action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F)); | |
188 | action_view_zoom_fit->setObjectName( | |
189 | QString::fromUtf8("actionViewZoomFit")); | |
190 | menu_view->addAction(action_view_zoom_fit); | |
191 | ||
192 | QAction *const action_view_zoom_one_to_one = new QAction(this); | |
193 | action_view_zoom_one_to_one->setText(QApplication::translate( | |
194 | "MainWindow", "Zoom to &One-to-One", 0, | |
195 | QApplication::UnicodeUTF8)); | |
196 | action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original", | |
197 | QIcon(":/icons/zoom-original.png"))); | |
198 | action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O)); | |
199 | action_view_zoom_one_to_one->setObjectName( | |
200 | QString::fromUtf8("actionViewZoomOneToOne")); | |
201 | menu_view->addAction(action_view_zoom_one_to_one); | |
202 | ||
203 | menu_view->addSeparator(); | |
204 | ||
205 | QAction *action_view_show_cursors = new QAction(this); | |
206 | action_view_show_cursors->setCheckable(true); | |
207 | action_view_show_cursors->setChecked(_view->cursors_shown()); | |
208 | action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C)); | |
209 | action_view_show_cursors->setObjectName( | |
210 | QString::fromUtf8("actionViewShowCursors")); | |
211 | action_view_show_cursors->setText(QApplication::translate( | |
212 | "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8)); | |
213 | menu_view->addAction(action_view_show_cursors); | |
214 | ||
215 | // Decoders Menu | |
216 | #ifdef ENABLE_DECODE | |
217 | QMenu *const menu_decoders = new QMenu; | |
218 | menu_decoders->setTitle(QApplication::translate( | |
219 | "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8)); | |
220 | ||
221 | pv::widgets::DecoderMenu *const menu_decoders_add = | |
222 | new pv::widgets::DecoderMenu(menu_decoders, true); | |
223 | menu_decoders_add->setTitle(QApplication::translate( | |
224 | "MainWindow", "&Add", 0, QApplication::UnicodeUTF8)); | |
225 | connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)), | |
226 | this, SLOT(add_decoder(srd_decoder*))); | |
227 | ||
228 | menu_decoders->addMenu(menu_decoders_add); | |
229 | #endif | |
230 | ||
231 | // Help Menu | |
232 | QMenu *const menu_help = new QMenu; | |
233 | menu_help->setTitle(QApplication::translate( | |
234 | "MainWindow", "&Help", 0, QApplication::UnicodeUTF8)); | |
235 | ||
236 | QAction *const action_about = new QAction(this); | |
237 | action_about->setObjectName(QString::fromUtf8("actionAbout")); | |
238 | action_about->setText(QApplication::translate( | |
239 | "MainWindow", "&About...", 0, QApplication::UnicodeUTF8)); | |
240 | menu_help->addAction(action_about); | |
241 | ||
242 | menu_bar->addAction(menu_file->menuAction()); | |
243 | menu_bar->addAction(menu_view->menuAction()); | |
244 | #ifdef ENABLE_DECODE | |
245 | menu_bar->addAction(menu_decoders->menuAction()); | |
246 | #endif | |
247 | menu_bar->addAction(menu_help->menuAction()); | |
248 | ||
249 | setMenuBar(menu_bar); | |
250 | QMetaObject::connectSlotsByName(this); | |
251 | ||
252 | // Setup the toolbar | |
253 | QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this); | |
254 | toolbar->addAction(action_open); | |
255 | toolbar->addAction(action_save_as); | |
256 | toolbar->addSeparator(); | |
257 | toolbar->addAction(action_view_zoom_in); | |
258 | toolbar->addAction(action_view_zoom_out); | |
259 | toolbar->addAction(action_view_zoom_fit); | |
260 | toolbar->addAction(action_view_zoom_one_to_one); | |
261 | addToolBar(toolbar); | |
262 | ||
263 | // Setup the sampling bar | |
264 | _sampling_bar = new toolbars::SamplingBar(_session, this); | |
265 | ||
266 | // Populate the device list and select the initially selected device | |
267 | update_device_list(); | |
268 | ||
269 | connect(_sampling_bar, SIGNAL(run_stop()), this, | |
270 | SLOT(run_stop())); | |
271 | addToolBar(_sampling_bar); | |
272 | ||
273 | // Set the title | |
274 | setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0, | |
275 | QApplication::UnicodeUTF8)); | |
276 | ||
277 | // Setup _session events | |
278 | connect(&_session, SIGNAL(capture_state_changed(int)), this, | |
279 | SLOT(capture_state_changed(int))); | |
280 | ||
281 | } | |
282 | ||
283 | void MainWindow::session_error( | |
284 | const QString text, const QString info_text) | |
285 | { | |
286 | QMetaObject::invokeMethod(this, "show_session_error", | |
287 | Qt::QueuedConnection, Q_ARG(QString, text), | |
288 | Q_ARG(QString, info_text)); | |
289 | } | |
290 | ||
291 | void MainWindow::update_device_list() | |
292 | { | |
293 | assert(_sampling_bar); | |
294 | ||
295 | shared_ptr<pv::device::DevInst> selected_device = _session.get_device(); | |
296 | list< shared_ptr<device::DevInst> > devices; | |
297 | std::copy(_device_manager.devices().begin(), | |
298 | _device_manager.devices().end(), std::back_inserter(devices)); | |
299 | ||
300 | if (std::find(devices.begin(), devices.end(), selected_device) == | |
301 | devices.end()) | |
302 | devices.push_back(selected_device); | |
303 | assert(selected_device); | |
304 | ||
305 | _sampling_bar->set_device_list(devices, selected_device); | |
306 | } | |
307 | ||
308 | void MainWindow::load_file(QString file_name) | |
309 | { | |
310 | const QString errorMessage( | |
311 | QString("Failed to load file %1").arg(file_name)); | |
312 | const QString infoMessage; | |
313 | ||
314 | try { | |
315 | _session.set_file(file_name.toStdString()); | |
316 | } catch(QString e) { | |
317 | show_session_error(tr("Failed to load ") + file_name, e); | |
318 | _session.set_default_device(); | |
319 | update_device_list(); | |
320 | return; | |
321 | } | |
322 | ||
323 | update_device_list(); | |
324 | ||
325 | _session.start_capture([&, errorMessage, infoMessage](QString) { | |
326 | session_error(errorMessage, infoMessage); }); | |
327 | } | |
328 | ||
329 | void MainWindow::show_session_error( | |
330 | const QString text, const QString info_text) | |
331 | { | |
332 | QMessageBox msg(this); | |
333 | msg.setText(text); | |
334 | msg.setInformativeText(info_text); | |
335 | msg.setStandardButtons(QMessageBox::Ok); | |
336 | msg.setIcon(QMessageBox::Warning); | |
337 | msg.exec(); | |
338 | } | |
339 | ||
340 | void MainWindow::on_actionOpen_triggered() | |
341 | { | |
342 | // Show the dialog | |
343 | const QString file_name = QFileDialog::getOpenFileName( | |
344 | this, tr("Open File"), "", tr( | |
345 | "Sigrok Sessions (*.sr);;" | |
346 | "All Files (*.*)")); | |
347 | if (!file_name.isEmpty()) | |
348 | load_file(file_name); | |
349 | } | |
350 | ||
351 | void MainWindow::on_actionSaveAs_triggered() | |
352 | { | |
353 | using pv::dialogs::StoreProgress; | |
354 | ||
355 | // Stop any currently running capture session | |
356 | _session.stop_capture(); | |
357 | ||
358 | // Show the dialog | |
359 | const QString file_name = QFileDialog::getSaveFileName( | |
360 | this, tr("Save File"), "", tr("Sigrok Sessions (*.sr)")); | |
361 | ||
362 | if (file_name.isEmpty()) | |
363 | return; | |
364 | ||
365 | StoreProgress *dlg = new StoreProgress(file_name, _session, this); | |
366 | dlg->run(); | |
367 | } | |
368 | ||
369 | void MainWindow::on_actionConnect_triggered() | |
370 | { | |
371 | // Stop any currently running capture session | |
372 | _session.stop_capture(); | |
373 | ||
374 | dialogs::Connect dlg(this, _device_manager); | |
375 | ||
376 | // If the user selected a device, select it in the device list. Select the | |
377 | // current device otherwise. | |
378 | if (dlg.exec()) | |
379 | _session.set_device(dlg.get_selected_device()); | |
380 | ||
381 | update_device_list(); | |
382 | } | |
383 | ||
384 | void MainWindow::on_actionQuit_triggered() | |
385 | { | |
386 | close(); | |
387 | } | |
388 | ||
389 | void MainWindow::on_actionViewZoomIn_triggered() | |
390 | { | |
391 | _view->zoom(1); | |
392 | } | |
393 | ||
394 | void MainWindow::on_actionViewZoomOut_triggered() | |
395 | { | |
396 | _view->zoom(-1); | |
397 | } | |
398 | ||
399 | void MainWindow::on_actionViewZoomFit_triggered() | |
400 | { | |
401 | _view->zoom_fit(); | |
402 | } | |
403 | ||
404 | void MainWindow::on_actionViewZoomOneToOne_triggered() | |
405 | { | |
406 | _view->zoom_one_to_one(); | |
407 | } | |
408 | ||
409 | void MainWindow::on_actionViewShowCursors_triggered() | |
410 | { | |
411 | assert(_view); | |
412 | ||
413 | const bool show = !_view->cursors_shown(); | |
414 | if(show) | |
415 | _view->centre_cursors(); | |
416 | ||
417 | _view->show_cursors(show); | |
418 | } | |
419 | ||
420 | void MainWindow::on_actionAbout_triggered() | |
421 | { | |
422 | dialogs::About dlg(this); | |
423 | dlg.exec(); | |
424 | } | |
425 | ||
426 | void MainWindow::add_decoder(srd_decoder *decoder) | |
427 | { | |
428 | #ifdef ENABLE_DECODE | |
429 | assert(decoder); | |
430 | _session.add_decoder(decoder); | |
431 | #else | |
432 | (void)decoder; | |
433 | #endif | |
434 | } | |
435 | ||
436 | void MainWindow::run_stop() | |
437 | { | |
438 | switch(_session.get_capture_state()) { | |
439 | case SigSession::Stopped: | |
440 | _session.start_capture([&](QString message) { | |
441 | session_error("Capture failed", message); }); | |
442 | break; | |
443 | ||
444 | case SigSession::AwaitingTrigger: | |
445 | case SigSession::Running: | |
446 | _session.stop_capture(); | |
447 | break; | |
448 | } | |
449 | } | |
450 | ||
451 | void MainWindow::capture_state_changed(int state) | |
452 | { | |
453 | _sampling_bar->set_capture_state((pv::SigSession::capture_state)state); | |
454 | } | |
455 | ||
456 | } // namespace pv |