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