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