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