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