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