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