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