]>
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 <QSettings> | |
36 | #include <QStatusBar> | |
37 | #include <QVBoxLayout> | |
38 | #include <QWidget> | |
39 | ||
40 | #include "mainwindow.h" | |
41 | ||
42 | #include "devicemanager.h" | |
43 | #include "device/device.h" | |
44 | #include "dialogs/about.h" | |
45 | #include "dialogs/connect.h" | |
46 | #include "dialogs/storeprogress.h" | |
47 | #include "toolbars/samplingbar.h" | |
48 | #include "view/logicsignal.h" | |
49 | #include "view/view.h" | |
50 | #ifdef ENABLE_DECODE | |
51 | #include "widgets/decodermenu.h" | |
52 | #endif | |
53 | ||
54 | /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */ | |
55 | #define __STDC_FORMAT_MACROS | |
56 | #include <inttypes.h> | |
57 | #include <stdint.h> | |
58 | #include <stdarg.h> | |
59 | #include <glib.h> | |
60 | #include <libsigrok/libsigrok.h> | |
61 | ||
62 | using std::list; | |
63 | using std::shared_ptr; | |
64 | ||
65 | namespace pv { | |
66 | ||
67 | namespace view { | |
68 | class SelectableItem; | |
69 | } | |
70 | ||
71 | const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory"; | |
72 | const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory"; | |
73 | ||
74 | MainWindow::MainWindow(DeviceManager &device_manager, | |
75 | const char *open_file_name, | |
76 | QWidget *parent) : | |
77 | QMainWindow(parent), | |
78 | _device_manager(device_manager), | |
79 | _session(device_manager) | |
80 | { | |
81 | setup_ui(); | |
82 | if (open_file_name) { | |
83 | const QString s(QString::fromUtf8(open_file_name)); | |
84 | QMetaObject::invokeMethod(this, "load_file", | |
85 | Qt::QueuedConnection, | |
86 | Q_ARG(QString, s)); | |
87 | } | |
88 | } | |
89 | ||
90 | void MainWindow::setup_ui() | |
91 | { | |
92 | setObjectName(QString::fromUtf8("MainWindow")); | |
93 | ||
94 | resize(1024, 768); | |
95 | ||
96 | // Set the window icon | |
97 | QIcon icon; | |
98 | icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"), | |
99 | QSize(), QIcon::Normal, QIcon::Off); | |
100 | setWindowIcon(icon); | |
101 | ||
102 | // Setup the central widget | |
103 | _central_widget = new QWidget(this); | |
104 | _vertical_layout = new QVBoxLayout(_central_widget); | |
105 | _vertical_layout->setSpacing(6); | |
106 | _vertical_layout->setContentsMargins(0, 0, 0, 0); | |
107 | setCentralWidget(_central_widget); | |
108 | ||
109 | _view = new pv::view::View(_session, this); | |
110 | ||
111 | _vertical_layout->addWidget(_view); | |
112 | ||
113 | // Setup the menu bar | |
114 | QMenuBar *const menu_bar = new QMenuBar(this); | |
115 | menu_bar->setGeometry(QRect(0, 0, 400, 25)); | |
116 | ||
117 | // File Menu | |
118 | QMenu *const menu_file = new QMenu; | |
119 | menu_file->setTitle(tr("&File")); | |
120 | ||
121 | QAction *const action_open = new QAction(this); | |
122 | action_open->setText(tr("&Open...")); | |
123 | action_open->setIcon(QIcon::fromTheme("document-open", | |
124 | QIcon(":/icons/document-open.png"))); | |
125 | action_open->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); | |
126 | action_open->setObjectName(QString::fromUtf8("actionOpen")); | |
127 | menu_file->addAction(action_open); | |
128 | ||
129 | QAction *const action_save_as = new QAction(this); | |
130 | action_save_as->setText(tr("&Save As...")); | |
131 | action_save_as->setIcon(QIcon::fromTheme("document-save-as", | |
132 | QIcon(":/icons/document-save-as.png"))); | |
133 | action_save_as->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); | |
134 | action_save_as->setObjectName(QString::fromUtf8("actionSaveAs")); | |
135 | menu_file->addAction(action_save_as); | |
136 | ||
137 | menu_file->addSeparator(); | |
138 | ||
139 | QAction *const action_connect = new QAction(this); | |
140 | action_connect->setText(tr("&Connect to Device...")); | |
141 | action_connect->setObjectName(QString::fromUtf8("actionConnect")); | |
142 | menu_file->addAction(action_connect); | |
143 | ||
144 | menu_file->addSeparator(); | |
145 | ||
146 | QAction *action_quit = new QAction(this); | |
147 | action_quit->setText(tr("&Quit")); | |
148 | action_quit->setIcon(QIcon::fromTheme("application-exit", | |
149 | QIcon(":/icons/application-exit.png"))); | |
150 | action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); | |
151 | action_quit->setObjectName(QString::fromUtf8("actionQuit")); | |
152 | menu_file->addAction(action_quit); | |
153 | ||
154 | // View Menu | |
155 | QMenu *menu_view = new QMenu; | |
156 | menu_view->setTitle(tr("&View")); | |
157 | ||
158 | QAction *const action_view_zoom_in = new QAction(this); | |
159 | action_view_zoom_in->setText(tr("Zoom &In")); | |
160 | action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in", | |
161 | QIcon(":/icons/zoom-in.png"))); | |
162 | // simply using Qt::Key_Plus shows no + in the menu | |
163 | action_view_zoom_in->setShortcut(QKeySequence::ZoomIn); | |
164 | action_view_zoom_in->setObjectName( | |
165 | QString::fromUtf8("actionViewZoomIn")); | |
166 | menu_view->addAction(action_view_zoom_in); | |
167 | ||
168 | QAction *const action_view_zoom_out = new QAction(this); | |
169 | action_view_zoom_out->setText(tr("Zoom &Out")); | |
170 | action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out", | |
171 | QIcon(":/icons/zoom-out.png"))); | |
172 | action_view_zoom_out->setShortcut(QKeySequence::ZoomOut); | |
173 | action_view_zoom_out->setObjectName( | |
174 | QString::fromUtf8("actionViewZoomOut")); | |
175 | menu_view->addAction(action_view_zoom_out); | |
176 | ||
177 | QAction *const action_view_zoom_fit = new QAction(this); | |
178 | action_view_zoom_fit->setText(tr("Zoom to &Fit")); | |
179 | action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit", | |
180 | QIcon(":/icons/zoom-fit.png"))); | |
181 | action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F)); | |
182 | action_view_zoom_fit->setObjectName( | |
183 | QString::fromUtf8("actionViewZoomFit")); | |
184 | menu_view->addAction(action_view_zoom_fit); | |
185 | ||
186 | QAction *const action_view_zoom_one_to_one = new QAction(this); | |
187 | action_view_zoom_one_to_one->setText(tr("Zoom to &One-to-One")); | |
188 | action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original", | |
189 | QIcon(":/icons/zoom-original.png"))); | |
190 | action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O)); | |
191 | action_view_zoom_one_to_one->setObjectName( | |
192 | QString::fromUtf8("actionViewZoomOneToOne")); | |
193 | menu_view->addAction(action_view_zoom_one_to_one); | |
194 | ||
195 | menu_view->addSeparator(); | |
196 | ||
197 | QAction *action_view_show_cursors = new QAction(this); | |
198 | action_view_show_cursors->setCheckable(true); | |
199 | action_view_show_cursors->setChecked(_view->cursors_shown()); | |
200 | action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C)); | |
201 | action_view_show_cursors->setObjectName( | |
202 | QString::fromUtf8("actionViewShowCursors")); | |
203 | action_view_show_cursors->setText(tr("Show &Cursors")); | |
204 | menu_view->addAction(action_view_show_cursors); | |
205 | ||
206 | // Decoders Menu | |
207 | #ifdef ENABLE_DECODE | |
208 | QMenu *const menu_decoders = new QMenu; | |
209 | menu_decoders->setTitle(tr("&Decoders")); | |
210 | ||
211 | pv::widgets::DecoderMenu *const menu_decoders_add = | |
212 | new pv::widgets::DecoderMenu(menu_decoders, true); | |
213 | menu_decoders_add->setTitle(tr("&Add")); | |
214 | connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)), | |
215 | this, SLOT(add_decoder(srd_decoder*))); | |
216 | ||
217 | menu_decoders->addMenu(menu_decoders_add); | |
218 | #endif | |
219 | ||
220 | // Help Menu | |
221 | QMenu *const menu_help = new QMenu; | |
222 | menu_help->setTitle(tr("&Help")); | |
223 | ||
224 | QAction *const action_about = new QAction(this); | |
225 | action_about->setObjectName(QString::fromUtf8("actionAbout")); | |
226 | action_about->setText(tr("&About...")); | |
227 | menu_help->addAction(action_about); | |
228 | ||
229 | menu_bar->addAction(menu_file->menuAction()); | |
230 | menu_bar->addAction(menu_view->menuAction()); | |
231 | #ifdef ENABLE_DECODE | |
232 | menu_bar->addAction(menu_decoders->menuAction()); | |
233 | #endif | |
234 | menu_bar->addAction(menu_help->menuAction()); | |
235 | ||
236 | setMenuBar(menu_bar); | |
237 | QMetaObject::connectSlotsByName(this); | |
238 | ||
239 | // Setup the toolbar | |
240 | QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this); | |
241 | toolbar->addAction(action_open); | |
242 | toolbar->addAction(action_save_as); | |
243 | toolbar->addSeparator(); | |
244 | toolbar->addAction(action_view_zoom_in); | |
245 | toolbar->addAction(action_view_zoom_out); | |
246 | toolbar->addAction(action_view_zoom_fit); | |
247 | toolbar->addAction(action_view_zoom_one_to_one); | |
248 | addToolBar(toolbar); | |
249 | ||
250 | // Setup the sampling bar | |
251 | _sampling_bar = new toolbars::SamplingBar(_session, this); | |
252 | ||
253 | // Populate the device list and select the initially selected device | |
254 | update_device_list(); | |
255 | ||
256 | connect(_sampling_bar, SIGNAL(run_stop()), this, | |
257 | SLOT(run_stop())); | |
258 | addToolBar(_sampling_bar); | |
259 | ||
260 | // Set the title | |
261 | setWindowTitle(tr("PulseView")); | |
262 | ||
263 | // Setup _session events | |
264 | connect(&_session, SIGNAL(capture_state_changed(int)), this, | |
265 | SLOT(capture_state_changed(int))); | |
266 | ||
267 | } | |
268 | ||
269 | void MainWindow::session_error( | |
270 | const QString text, const QString info_text) | |
271 | { | |
272 | QMetaObject::invokeMethod(this, "show_session_error", | |
273 | Qt::QueuedConnection, Q_ARG(QString, text), | |
274 | Q_ARG(QString, info_text)); | |
275 | } | |
276 | ||
277 | void MainWindow::update_device_list() | |
278 | { | |
279 | assert(_sampling_bar); | |
280 | ||
281 | shared_ptr<pv::device::DevInst> selected_device = _session.get_device(); | |
282 | list< shared_ptr<device::DevInst> > devices; | |
283 | std::copy(_device_manager.devices().begin(), | |
284 | _device_manager.devices().end(), std::back_inserter(devices)); | |
285 | ||
286 | if (std::find(devices.begin(), devices.end(), selected_device) == | |
287 | devices.end()) | |
288 | devices.push_back(selected_device); | |
289 | assert(selected_device); | |
290 | ||
291 | _sampling_bar->set_device_list(devices, selected_device); | |
292 | } | |
293 | ||
294 | void MainWindow::load_file(QString file_name) | |
295 | { | |
296 | const QString errorMessage( | |
297 | QString("Failed to load file %1").arg(file_name)); | |
298 | const QString infoMessage; | |
299 | ||
300 | try { | |
301 | _session.set_file(file_name.toStdString()); | |
302 | } catch(QString e) { | |
303 | show_session_error(tr("Failed to load ") + file_name, e); | |
304 | _session.set_default_device(); | |
305 | update_device_list(); | |
306 | return; | |
307 | } | |
308 | ||
309 | update_device_list(); | |
310 | ||
311 | _session.start_capture([&, errorMessage, infoMessage](QString) { | |
312 | session_error(errorMessage, infoMessage); }); | |
313 | } | |
314 | ||
315 | void MainWindow::show_session_error( | |
316 | const QString text, const QString info_text) | |
317 | { | |
318 | QMessageBox msg(this); | |
319 | msg.setText(text); | |
320 | msg.setInformativeText(info_text); | |
321 | msg.setStandardButtons(QMessageBox::Ok); | |
322 | msg.setIcon(QMessageBox::Warning); | |
323 | msg.exec(); | |
324 | } | |
325 | ||
326 | void MainWindow::on_actionOpen_triggered() | |
327 | { | |
328 | QSettings settings; | |
329 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
330 | ||
331 | // Show the dialog | |
332 | const QString file_name = QFileDialog::getOpenFileName( | |
333 | this, tr("Open File"), dir, tr( | |
334 | "Sigrok Sessions (*.sr);;" | |
335 | "All Files (*.*)")); | |
336 | ||
337 | if (!file_name.isEmpty()) { | |
338 | load_file(file_name); | |
339 | ||
340 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
341 | settings.setValue(SettingOpenDirectory, abs_path); | |
342 | } | |
343 | } | |
344 | ||
345 | void MainWindow::on_actionSaveAs_triggered() | |
346 | { | |
347 | using pv::dialogs::StoreProgress; | |
348 | ||
349 | // Stop any currently running capture session | |
350 | _session.stop_capture(); | |
351 | ||
352 | QSettings settings; | |
353 | const QString dir = settings.value(SettingSaveDirectory).toString(); | |
354 | ||
355 | // Show the dialog | |
356 | const QString file_name = QFileDialog::getSaveFileName( | |
357 | this, tr("Save File"), dir, tr("Sigrok Sessions (*.sr)")); | |
358 | ||
359 | if (file_name.isEmpty()) | |
360 | return; | |
361 | ||
362 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
363 | settings.setValue(SettingSaveDirectory, abs_path); | |
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 |