]>
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 <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 "devinst.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 boost::shared_ptr; | |
62 | using std::list; | |
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->setObjectName(QString::fromUtf8("actionOpen")); | |
124 | menu_file->addAction(action_open); | |
125 | ||
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 | ||
134 | menu_file->addSeparator(); | |
135 | ||
136 | QAction *const action_connect = new QAction(this); | |
137 | action_connect->setText(QApplication::translate( | |
138 | "MainWindow", "&Connect to Device...", 0, | |
139 | QApplication::UnicodeUTF8)); | |
140 | action_connect->setObjectName(QString::fromUtf8("actionConnect")); | |
141 | menu_file->addAction(action_connect); | |
142 | ||
143 | menu_file->addSeparator(); | |
144 | ||
145 | QAction *action_quit = new QAction(this); | |
146 | action_quit->setText(QApplication::translate( | |
147 | "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8)); | |
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(QApplication::translate( | |
157 | "MainWindow", "&View", 0, QApplication::UnicodeUTF8)); | |
158 | ||
159 | QAction *const action_view_zoom_in = new QAction(this); | |
160 | action_view_zoom_in->setText(QApplication::translate( | |
161 | "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8)); | |
162 | action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in", | |
163 | QIcon(":/icons/zoom-in.png"))); | |
164 | // simply using Qt::Key_Plus shows no + in the menu | |
165 | action_view_zoom_in->setShortcut(QKeySequence::ZoomIn); | |
166 | action_view_zoom_in->setObjectName( | |
167 | QString::fromUtf8("actionViewZoomIn")); | |
168 | menu_view->addAction(action_view_zoom_in); | |
169 | ||
170 | QAction *const action_view_zoom_out = new QAction(this); | |
171 | action_view_zoom_out->setText(QApplication::translate( | |
172 | "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8)); | |
173 | action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out", | |
174 | QIcon(":/icons/zoom-out.png"))); | |
175 | action_view_zoom_out->setShortcut(QKeySequence::ZoomOut); | |
176 | action_view_zoom_out->setObjectName( | |
177 | QString::fromUtf8("actionViewZoomOut")); | |
178 | menu_view->addAction(action_view_zoom_out); | |
179 | ||
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 | ||
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 | ||
201 | menu_view->addSeparator(); | |
202 | ||
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( | |
208 | QString::fromUtf8("actionViewShowCursors")); | |
209 | action_view_show_cursors->setText(QApplication::translate( | |
210 | "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8)); | |
211 | menu_view->addAction(action_view_show_cursors); | |
212 | ||
213 | // Decoders Menu | |
214 | #ifdef ENABLE_DECODE | |
215 | QMenu *const menu_decoders = new QMenu; | |
216 | menu_decoders->setTitle(QApplication::translate( | |
217 | "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8)); | |
218 | ||
219 | pv::widgets::DecoderMenu *const menu_decoders_add = | |
220 | new pv::widgets::DecoderMenu(menu_decoders); | |
221 | menu_decoders_add->setTitle(QApplication::translate( | |
222 | "MainWindow", "&Add", 0, QApplication::UnicodeUTF8)); | |
223 | connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)), | |
224 | this, SLOT(add_decoder(srd_decoder*))); | |
225 | ||
226 | menu_decoders->addMenu(menu_decoders_add); | |
227 | #endif | |
228 | ||
229 | // Help Menu | |
230 | QMenu *const menu_help = new QMenu; | |
231 | menu_help->setTitle(QApplication::translate( | |
232 | "MainWindow", "&Help", 0, QApplication::UnicodeUTF8)); | |
233 | ||
234 | QAction *const action_about = new QAction(this); | |
235 | action_about->setObjectName(QString::fromUtf8("actionAbout")); | |
236 | action_about->setText(QApplication::translate( | |
237 | "MainWindow", "&About...", 0, QApplication::UnicodeUTF8)); | |
238 | menu_help->addAction(action_about); | |
239 | ||
240 | menu_bar->addAction(menu_file->menuAction()); | |
241 | menu_bar->addAction(menu_view->menuAction()); | |
242 | #ifdef ENABLE_DECODE | |
243 | menu_bar->addAction(menu_decoders->menuAction()); | |
244 | #endif | |
245 | menu_bar->addAction(menu_help->menuAction()); | |
246 | ||
247 | setMenuBar(menu_bar); | |
248 | QMetaObject::connectSlotsByName(this); | |
249 | ||
250 | // Setup the toolbar | |
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); | |
256 | toolbar->addAction(action_view_zoom_fit); | |
257 | addToolBar(toolbar); | |
258 | ||
259 | // Setup the sampling bar | |
260 | _sampling_bar = new toolbars::SamplingBar(_session, this); | |
261 | ||
262 | // Populate the device list and select the initially selected device | |
263 | update_device_list(); | |
264 | ||
265 | connect(_sampling_bar, SIGNAL(run_stop()), this, | |
266 | SLOT(run_stop())); | |
267 | addToolBar(_sampling_bar); | |
268 | ||
269 | // Set the title | |
270 | setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0, | |
271 | QApplication::UnicodeUTF8)); | |
272 | ||
273 | // Setup _session events | |
274 | connect(&_session, SIGNAL(capture_state_changed(int)), this, | |
275 | SLOT(capture_state_changed(int))); | |
276 | ||
277 | } | |
278 | ||
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 | ||
287 | void MainWindow::update_device_list(shared_ptr<pv::DevInst> selected_device) | |
288 | { | |
289 | assert(_sampling_bar); | |
290 | ||
291 | const list< shared_ptr<DevInst> > &devices = _device_manager.devices(); | |
292 | _sampling_bar->set_device_list(devices); | |
293 | ||
294 | if (!selected_device && !devices.empty()) { | |
295 | // Fall back to the first device in the list. | |
296 | selected_device = devices.front(); | |
297 | ||
298 | // Try and find the demo device and select that by default | |
299 | BOOST_FOREACH (shared_ptr<pv::DevInst> dev_inst, devices) | |
300 | if (strcmp(dev_inst->dev_inst()->driver->name, | |
301 | "demo") == 0) { | |
302 | selected_device = dev_inst; | |
303 | } | |
304 | } | |
305 | ||
306 | if (selected_device) { | |
307 | _sampling_bar->set_selected_device(selected_device); | |
308 | _session.set_device(selected_device); | |
309 | } | |
310 | } | |
311 | ||
312 | void MainWindow::load_file(QString file_name) | |
313 | { | |
314 | const QString errorMessage( | |
315 | QString("Failed to load file %1").arg(file_name)); | |
316 | const QString infoMessage; | |
317 | _session.load_file(file_name.toStdString(), | |
318 | boost::bind(&MainWindow::session_error, this, | |
319 | errorMessage, infoMessage)); | |
320 | } | |
321 | ||
322 | void MainWindow::show_session_error( | |
323 | const QString text, const QString info_text) | |
324 | { | |
325 | QMessageBox msg(this); | |
326 | msg.setText(text); | |
327 | msg.setInformativeText(info_text); | |
328 | msg.setStandardButtons(QMessageBox::Ok); | |
329 | msg.setIcon(QMessageBox::Warning); | |
330 | msg.exec(); | |
331 | } | |
332 | ||
333 | void MainWindow::on_actionOpen_triggered() | |
334 | { | |
335 | // Show the dialog | |
336 | const QString file_name = QFileDialog::getOpenFileName( | |
337 | this, tr("Open File"), "", tr( | |
338 | "Sigrok Sessions (*.sr);;" | |
339 | "All Files (*.*)")); | |
340 | if (!file_name.isEmpty()) | |
341 | load_file(file_name); | |
342 | } | |
343 | ||
344 | void MainWindow::on_actionSaveAs_triggered() | |
345 | { | |
346 | using pv::dialogs::StoreProgress; | |
347 | ||
348 | // Stop any currently running capture session | |
349 | _session.stop_capture(); | |
350 | ||
351 | // Show the dialog | |
352 | const QString file_name = QFileDialog::getSaveFileName( | |
353 | this, tr("Save File"), "", tr("Sigrok Sessions (*.sr)")); | |
354 | ||
355 | if (file_name.isEmpty()) | |
356 | return; | |
357 | ||
358 | StoreProgress *dlg = new StoreProgress(file_name, _session, this); | |
359 | dlg->run(); | |
360 | } | |
361 | ||
362 | void MainWindow::on_actionConnect_triggered() | |
363 | { | |
364 | // Stop any currently running capture session | |
365 | _session.stop_capture(); | |
366 | ||
367 | dialogs::Connect dlg(this, _device_manager); | |
368 | ||
369 | // If the user selected a device, select it in the device list. Select the | |
370 | // current device otherwise. | |
371 | shared_ptr<DevInst> dev_inst = dlg.exec() ? | |
372 | dlg.get_selected_device() : _session.get_device(); | |
373 | ||
374 | update_device_list(dev_inst); | |
375 | } | |
376 | ||
377 | void MainWindow::on_actionQuit_triggered() | |
378 | { | |
379 | close(); | |
380 | } | |
381 | ||
382 | void MainWindow::on_actionViewZoomIn_triggered() | |
383 | { | |
384 | _view->zoom(1); | |
385 | } | |
386 | ||
387 | void MainWindow::on_actionViewZoomOut_triggered() | |
388 | { | |
389 | _view->zoom(-1); | |
390 | } | |
391 | ||
392 | void MainWindow::on_actionViewZoomFit_triggered() | |
393 | { | |
394 | _view->zoom_fit(); | |
395 | } | |
396 | ||
397 | void MainWindow::on_actionViewZoomOneToOne_triggered() | |
398 | { | |
399 | _view->zoom_one_to_one(); | |
400 | } | |
401 | ||
402 | void MainWindow::on_actionViewShowCursors_triggered() | |
403 | { | |
404 | assert(_view); | |
405 | ||
406 | const bool show = !_view->cursors_shown(); | |
407 | if(show) | |
408 | _view->centre_cursors(); | |
409 | ||
410 | _view->show_cursors(show); | |
411 | } | |
412 | ||
413 | void MainWindow::on_actionAbout_triggered() | |
414 | { | |
415 | dialogs::About dlg(this); | |
416 | dlg.exec(); | |
417 | } | |
418 | ||
419 | void MainWindow::add_decoder(srd_decoder *decoder) | |
420 | { | |
421 | #ifdef ENABLE_DECODE | |
422 | assert(decoder); | |
423 | _session.add_decoder(decoder); | |
424 | #else | |
425 | (void)decoder; | |
426 | #endif | |
427 | } | |
428 | ||
429 | void MainWindow::run_stop() | |
430 | { | |
431 | switch(_session.get_capture_state()) { | |
432 | case SigSession::Stopped: | |
433 | _session.start_capture( | |
434 | boost::bind(&MainWindow::session_error, this, | |
435 | QString("Capture failed"), _1)); | |
436 | break; | |
437 | ||
438 | case SigSession::AwaitingTrigger: | |
439 | case SigSession::Running: | |
440 | _session.stop_capture(); | |
441 | break; | |
442 | } | |
443 | } | |
444 | ||
445 | void MainWindow::capture_state_changed(int state) | |
446 | { | |
447 | _sampling_bar->set_capture_state((pv::SigSession::capture_state)state); | |
448 | } | |
449 | ||
450 | } // namespace pv |