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