]>
Commit | Line | Data |
---|---|---|
d7bed479 | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
d7bed479 JH |
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 | ||
943edd76 MC |
21 | #include <cassert> |
22 | ||
269528f5 | 23 | #ifdef ENABLE_DECODE |
aaabd61b | 24 | #include <libsigrokdecode/libsigrokdecode.h> |
269528f5 | 25 | #endif |
30236a54 | 26 | |
85843b14 JH |
27 | #include <algorithm> |
28 | #include <iterator> | |
29 | ||
ea2dbfea JH |
30 | #include <boost/algorithm/string/join.hpp> |
31 | ||
7d5425ef JH |
32 | #include <QAction> |
33 | #include <QApplication> | |
34 | #include <QButtonGroup> | |
93f683ad | 35 | #include <QCloseEvent> |
2953961c | 36 | #include <QFileDialog> |
f2edb557 | 37 | #include <QMessageBox> |
7d5425ef JH |
38 | #include <QMenu> |
39 | #include <QMenuBar> | |
643f65f9 | 40 | #include <QSettings> |
7d5425ef JH |
41 | #include <QStatusBar> |
42 | #include <QVBoxLayout> | |
43 | #include <QWidget> | |
2953961c | 44 | |
2acdb232 | 45 | #include "mainwindow.hpp" |
107ca6d3 | 46 | |
2acdb232 | 47 | #include "devicemanager.hpp" |
d2fc6be9 SA |
48 | #include "util.hpp" |
49 | #include "data/segment.hpp" | |
da30ecb7 | 50 | #include "devices/hardwaredevice.hpp" |
dd3fd4df | 51 | #include "devices/inputfile.hpp" |
da30ecb7 | 52 | #include "devices/sessionfile.hpp" |
2acdb232 JH |
53 | #include "dialogs/about.hpp" |
54 | #include "dialogs/connect.hpp" | |
e93f5538 | 55 | #include "dialogs/inputoutputoptions.hpp" |
2acdb232 | 56 | #include "dialogs/storeprogress.hpp" |
7c657094 | 57 | #include "toolbars/mainbar.hpp" |
2acdb232 JH |
58 | #include "view/logicsignal.hpp" |
59 | #include "view/view.hpp" | |
998b89fd | 60 | #include "widgets/exportmenu.hpp" |
ed43ef2e | 61 | #include "widgets/importmenu.hpp" |
269528f5 | 62 | #ifdef ENABLE_DECODE |
2acdb232 | 63 | #include "widgets/decodermenu.hpp" |
269528f5 | 64 | #endif |
1c4c23b8 | 65 | #include "widgets/hidingmenubar.hpp" |
d7bed479 | 66 | |
30236a54 JH |
67 | #include <inttypes.h> |
68 | #include <stdint.h> | |
69 | #include <stdarg.h> | |
70 | #include <glib.h> | |
fe3a1c21 | 71 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e82fd481 | 72 | |
e3c79b07 JH |
73 | using std::cerr; |
74 | using std::endl; | |
819f4c25 | 75 | using std::list; |
6842b5fc | 76 | using std::map; |
e3c79b07 | 77 | using std::pair; |
f9abf97e | 78 | using std::shared_ptr; |
6842b5fc | 79 | using std::string; |
ea2dbfea JH |
80 | using std::vector; |
81 | ||
82 | using boost::algorithm::join; | |
30236a54 | 83 | |
e8d00928 | 84 | using sigrok::Error; |
998b89fd | 85 | using sigrok::OutputFormat; |
dd3fd4df | 86 | using sigrok::InputFormat; |
e8d00928 | 87 | |
51e77110 JH |
88 | namespace pv { |
89 | ||
b1264f56 | 90 | namespace view { |
26e3af6b | 91 | class ViewItem; |
b1264f56 JH |
92 | } |
93 | ||
643f65f9 JS |
94 | const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory"; |
95 | const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory"; | |
96 | ||
107ca6d3 | 97 | MainWindow::MainWindow(DeviceManager &device_manager, |
e3c79b07 | 98 | string open_file_name, string open_file_format, |
1d478458 | 99 | QWidget *parent) : |
107ca6d3 | 100 | QMainWindow(parent), |
8dbbc7f0 | 101 | device_manager_(device_manager), |
69654681 JH |
102 | session_(device_manager), |
103 | action_open_(new QAction(this)), | |
104 | action_save_as_(new QAction(this)), | |
d2fc6be9 | 105 | action_save_selection_as_(new QAction(this)), |
69654681 JH |
106 | action_connect_(new QAction(this)), |
107 | action_quit_(new QAction(this)), | |
108 | action_view_zoom_in_(new QAction(this)), | |
109 | action_view_zoom_out_(new QAction(this)), | |
110 | action_view_zoom_fit_(new QAction(this)), | |
111 | action_view_zoom_one_to_one_(new QAction(this)), | |
c7b03d9d | 112 | action_view_sticky_scrolling_(new QAction(this)), |
0fb9d645 | 113 | action_view_coloured_bg_(new QAction(this)), |
69654681 | 114 | action_view_show_cursors_(new QAction(this)), |
e79171dc BG |
115 | action_about_(new QAction(this)) |
116 | #ifdef ENABLE_DECODE | |
117 | , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true)) | |
19be0af1 | 118 | #endif |
d7bed479 | 119 | { |
48257a69 SA |
120 | qRegisterMetaType<util::Timestamp>("util::Timestamp"); |
121 | ||
7d5425ef | 122 | setup_ui(); |
93f683ad | 123 | restore_ui_settings(); |
e3c79b07 JH |
124 | if (open_file_name.empty()) |
125 | select_init_device(); | |
126 | else | |
127 | load_init_file(open_file_name, open_file_format); | |
7d5425ef JH |
128 | } |
129 | ||
69654681 JH |
130 | QAction* MainWindow::action_open() const |
131 | { | |
132 | return action_open_; | |
133 | } | |
134 | ||
135 | QAction* MainWindow::action_save_as() const | |
136 | { | |
137 | return action_save_as_; | |
61501398 SA |
138 | } |
139 | ||
140 | QAction* MainWindow::action_save_selection_as() const | |
141 | { | |
142 | return action_save_selection_as_; | |
69654681 JH |
143 | } |
144 | ||
145 | QAction* MainWindow::action_connect() const | |
146 | { | |
147 | return action_connect_; | |
148 | } | |
149 | ||
150 | QAction* MainWindow::action_quit() const | |
151 | { | |
152 | return action_quit_; | |
153 | } | |
154 | ||
155 | QAction* MainWindow::action_view_zoom_in() const | |
156 | { | |
157 | return action_view_zoom_in_; | |
158 | } | |
159 | ||
160 | QAction* MainWindow::action_view_zoom_out() const | |
161 | { | |
162 | return action_view_zoom_out_; | |
163 | } | |
164 | ||
165 | QAction* MainWindow::action_view_zoom_fit() const | |
166 | { | |
167 | return action_view_zoom_fit_; | |
168 | } | |
169 | ||
170 | QAction* MainWindow::action_view_zoom_one_to_one() const | |
171 | { | |
172 | return action_view_zoom_one_to_one_; | |
173 | } | |
174 | ||
c7b03d9d SA |
175 | QAction* MainWindow::action_view_sticky_scrolling() const |
176 | { | |
177 | return action_view_sticky_scrolling_; | |
178 | } | |
179 | ||
bea236d6 JH |
180 | QAction* MainWindow::action_view_coloured_bg() const |
181 | { | |
182 | return action_view_coloured_bg_; | |
183 | } | |
184 | ||
69654681 JH |
185 | QAction* MainWindow::action_view_show_cursors() const |
186 | { | |
187 | return action_view_show_cursors_; | |
188 | } | |
189 | ||
190 | QAction* MainWindow::action_about() const | |
191 | { | |
192 | return action_about_; | |
193 | } | |
194 | ||
e79171dc | 195 | #ifdef ENABLE_DECODE |
0f90452b JH |
196 | QMenu* MainWindow::menu_decoder_add() const |
197 | { | |
198 | return menu_decoders_add_; | |
199 | } | |
19be0af1 | 200 | #endif |
0f90452b | 201 | |
4e7f5ba8 JH |
202 | void MainWindow::run_stop() |
203 | { | |
2ad82c2e | 204 | switch (session_.get_capture_state()) { |
2b81ae46 | 205 | case Session::Stopped: |
4e7f5ba8 JH |
206 | session_.start_capture([&](QString message) { |
207 | session_error("Capture failed", message); }); | |
208 | break; | |
2b81ae46 JH |
209 | case Session::AwaitingTrigger: |
210 | case Session::Running: | |
4e7f5ba8 JH |
211 | session_.stop_capture(); |
212 | break; | |
213 | } | |
214 | } | |
215 | ||
da30ecb7 | 216 | void MainWindow::select_device(shared_ptr<devices::Device> device) |
51cf49fe JH |
217 | { |
218 | try { | |
859ca307 JH |
219 | if (device) |
220 | session_.set_device(device); | |
221 | else | |
222 | session_.set_default_device(); | |
2ad82c2e | 223 | } catch (const QString &e) { |
51cf49fe JH |
224 | QMessageBox msg(this); |
225 | msg.setText(e); | |
226 | msg.setInformativeText(tr("Failed to Select Device")); | |
227 | msg.setStandardButtons(QMessageBox::Ok); | |
228 | msg.setIcon(QMessageBox::Warning); | |
229 | msg.exec(); | |
230 | } | |
231 | } | |
232 | ||
7d6984a5 SA |
233 | void MainWindow::export_file(shared_ptr<OutputFormat> format, |
234 | bool selection_only) | |
998b89fd JH |
235 | { |
236 | using pv::dialogs::StoreProgress; | |
237 | ||
238 | // Stop any currently running capture session | |
239 | session_.stop_capture(); | |
240 | ||
241 | QSettings settings; | |
242 | const QString dir = settings.value(SettingSaveDirectory).toString(); | |
243 | ||
a55e7918 SA |
244 | std::pair<uint64_t, uint64_t> sample_range; |
245 | ||
246 | // Selection only? Verify that the cursors are active and fetch their values | |
247 | if (selection_only) { | |
248 | if (!view_->cursors()->enabled()) { | |
249 | show_session_error(tr("Missing Cursors"), tr("You need to set the " \ | |
250 | "cursors before you can save the data enclosed by them " \ | |
251 | "to a session file (e.g. using ALT-V - Show Cursors).")); | |
252 | return; | |
253 | } | |
254 | ||
255 | const double samplerate = session_.get_samplerate(); | |
256 | ||
257 | const pv::util::Timestamp& start_time = view_->cursors()->first()->time(); | |
258 | const pv::util::Timestamp& end_time = view_->cursors()->second()->time(); | |
259 | ||
260 | const uint64_t start_sample = start_time.convert_to<double>() * samplerate; | |
3d79f521 | 261 | const uint64_t end_sample = end_time.convert_to<double>() * samplerate; |
a55e7918 | 262 | |
3d79f521 | 263 | sample_range = std::make_pair(start_sample, end_sample); |
a55e7918 SA |
264 | } else { |
265 | sample_range = std::make_pair(0, 0); | |
266 | } | |
267 | ||
ea2dbfea JH |
268 | // Construct the filter |
269 | const vector<string> exts = format->extensions(); | |
270 | QString filter = tr("%1 files ").arg( | |
271 | QString::fromStdString(format->description())); | |
272 | ||
273 | if (exts.empty()) | |
274 | filter += "(*.*)"; | |
275 | else | |
276 | filter += QString("(*.%1);;%2 (*.*)").arg( | |
277 | QString::fromStdString(join(exts, ", *."))).arg( | |
278 | tr("All Files")); | |
279 | ||
e93f5538 | 280 | // Show the file dialog |
998b89fd | 281 | const QString file_name = QFileDialog::getSaveFileName( |
ea2dbfea | 282 | this, tr("Save File"), dir, filter); |
998b89fd JH |
283 | |
284 | if (file_name.isEmpty()) | |
285 | return; | |
286 | ||
287 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
288 | settings.setValue(SettingSaveDirectory, abs_path); | |
289 | ||
e93f5538 JH |
290 | // Show the options dialog |
291 | map<string, Glib::VariantBase> options; | |
292 | if (!format->options().empty()) { | |
293 | dialogs::InputOutputOptions dlg( | |
294 | tr("Export %1").arg(QString::fromStdString( | |
295 | format->description())), | |
296 | format->options(), this); | |
297 | if (!dlg.exec()) | |
298 | return; | |
299 | options = dlg.options(); | |
300 | } | |
301 | ||
302 | StoreProgress *dlg = new StoreProgress(file_name, format, options, | |
d2fc6be9 | 303 | sample_range, session_, this); |
998b89fd JH |
304 | dlg->run(); |
305 | } | |
306 | ||
ed43ef2e JH |
307 | void MainWindow::import_file(shared_ptr<InputFormat> format) |
308 | { | |
309 | assert(format); | |
310 | ||
311 | QSettings settings; | |
312 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
313 | ||
314 | // Construct the filter | |
315 | const vector<string> exts = format->extensions(); | |
316 | const QString filter = exts.empty() ? "" : | |
317 | tr("%1 files (*.%2)").arg( | |
318 | QString::fromStdString(format->description())).arg( | |
319 | QString::fromStdString(join(exts, ", *."))); | |
320 | ||
321 | // Show the file dialog | |
322 | const QString file_name = QFileDialog::getOpenFileName( | |
323 | this, tr("Import File"), dir, tr( | |
324 | "%1 files (*.*);;All Files (*.*)").arg( | |
325 | QString::fromStdString(format->description()))); | |
326 | ||
53d45c55 JH |
327 | if (file_name.isEmpty()) |
328 | return; | |
ed43ef2e | 329 | |
53d45c55 JH |
330 | // Show the options dialog |
331 | map<string, Glib::VariantBase> options; | |
332 | if (!format->options().empty()) { | |
333 | dialogs::InputOutputOptions dlg( | |
334 | tr("Import %1").arg(QString::fromStdString( | |
335 | format->description())), | |
336 | format->options(), this); | |
337 | if (!dlg.exec()) | |
338 | return; | |
339 | options = dlg.options(); | |
ed43ef2e | 340 | } |
53d45c55 JH |
341 | |
342 | load_file(file_name, format, options); | |
343 | ||
344 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
345 | settings.setValue(SettingOpenDirectory, abs_path); | |
ed43ef2e JH |
346 | } |
347 | ||
7d5425ef JH |
348 | void MainWindow::setup_ui() |
349 | { | |
350 | setObjectName(QString::fromUtf8("MainWindow")); | |
351 | ||
7d5425ef JH |
352 | // Set the window icon |
353 | QIcon icon; | |
4523c509 | 354 | icon.addFile(QString(":/icons/sigrok-logo-notext.svg")); |
7d5425ef JH |
355 | setWindowIcon(icon); |
356 | ||
e072efc8 | 357 | // Setup the central widget |
8dbbc7f0 JH |
358 | central_widget_ = new QWidget(this); |
359 | vertical_layout_ = new QVBoxLayout(central_widget_); | |
360 | vertical_layout_->setSpacing(6); | |
361 | vertical_layout_->setContentsMargins(0, 0, 0, 0); | |
362 | setCentralWidget(central_widget_); | |
e072efc8 | 363 | |
8dbbc7f0 | 364 | view_ = new pv::view::View(session_, this); |
b1264f56 | 365 | |
8dbbc7f0 | 366 | vertical_layout_->addWidget(view_); |
e072efc8 | 367 | |
ab1d13ee | 368 | // Setup the menu bar |
1c4c23b8 JH |
369 | pv::widgets::HidingMenuBar *const menu_bar = |
370 | new pv::widgets::HidingMenuBar(this); | |
a429590b | 371 | |
ab1d13ee | 372 | // File Menu |
ced1cc13 | 373 | QMenu *const menu_file = new QMenu; |
0a9fdca5 | 374 | menu_file->setTitle(tr("&File")); |
e072efc8 | 375 | |
69654681 JH |
376 | action_open_->setText(tr("&Open...")); |
377 | action_open_->setIcon(QIcon::fromTheme("document-open", | |
362eea96 | 378 | QIcon(":/icons/document-open.png"))); |
69654681 JH |
379 | action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); |
380 | action_open_->setObjectName(QString::fromUtf8("actionOpen")); | |
381 | menu_file->addAction(action_open_); | |
009e1503 | 382 | |
69654681 JH |
383 | action_save_as_->setText(tr("&Save As...")); |
384 | action_save_as_->setIcon(QIcon::fromTheme("document-save-as", | |
0fbda3c2 | 385 | QIcon(":/icons/document-save-as.png"))); |
69654681 JH |
386 | action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); |
387 | action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs")); | |
388 | menu_file->addAction(action_save_as_); | |
0fbda3c2 | 389 | |
d2fc6be9 SA |
390 | action_save_selection_as_->setText(tr("Save Selected &Range As...")); |
391 | action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as", | |
392 | QIcon(":/icons/document-save-as.png"))); | |
393 | action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R)); | |
394 | action_save_selection_as_->setObjectName(QString::fromUtf8("actionSaveSelectionAs")); | |
395 | menu_file->addAction(action_save_selection_as_); | |
396 | ||
ced1cc13 | 397 | menu_file->addSeparator(); |
2a032dcb | 398 | |
998b89fd JH |
399 | widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this, |
400 | device_manager_.context()); | |
401 | menu_file_export->setTitle(tr("&Export")); | |
402 | connect(menu_file_export, | |
403 | SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)), | |
404 | this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>))); | |
405 | menu_file->addAction(menu_file_export->menuAction()); | |
406 | ||
ed43ef2e JH |
407 | widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this, |
408 | device_manager_.context()); | |
409 | menu_file_import->setTitle(tr("&Import")); | |
410 | connect(menu_file_import, | |
411 | SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)), | |
412 | this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>))); | |
413 | menu_file->addAction(menu_file_import->menuAction()); | |
414 | ||
998b89fd JH |
415 | menu_file->addSeparator(); |
416 | ||
69654681 JH |
417 | action_connect_->setText(tr("&Connect to Device...")); |
418 | action_connect_->setObjectName(QString::fromUtf8("actionConnect")); | |
419 | menu_file->addAction(action_connect_); | |
9663c82b | 420 | |
ced1cc13 | 421 | menu_file->addSeparator(); |
9663c82b | 422 | |
69654681 JH |
423 | action_quit_->setText(tr("&Quit")); |
424 | action_quit_->setIcon(QIcon::fromTheme("application-exit", | |
2a032dcb | 425 | QIcon(":/icons/application-exit.png"))); |
69654681 JH |
426 | action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); |
427 | action_quit_->setObjectName(QString::fromUtf8("actionQuit")); | |
428 | menu_file->addAction(action_quit_); | |
2a032dcb | 429 | |
ab1d13ee | 430 | // View Menu |
ced1cc13 | 431 | QMenu *menu_view = new QMenu; |
0a9fdca5 | 432 | menu_view->setTitle(tr("&View")); |
ab1d13ee | 433 | |
69654681 JH |
434 | action_view_zoom_in_->setText(tr("Zoom &In")); |
435 | action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in", | |
ab1d13ee | 436 | QIcon(":/icons/zoom-in.png"))); |
d2899ad5 | 437 | // simply using Qt::Key_Plus shows no + in the menu |
69654681 JH |
438 | action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn); |
439 | action_view_zoom_in_->setObjectName( | |
ab1d13ee | 440 | QString::fromUtf8("actionViewZoomIn")); |
69654681 | 441 | menu_view->addAction(action_view_zoom_in_); |
ab1d13ee | 442 | |
69654681 JH |
443 | action_view_zoom_out_->setText(tr("Zoom &Out")); |
444 | action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out", | |
ab1d13ee | 445 | QIcon(":/icons/zoom-out.png"))); |
69654681 JH |
446 | action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut); |
447 | action_view_zoom_out_->setObjectName( | |
ab1d13ee | 448 | QString::fromUtf8("actionViewZoomOut")); |
69654681 | 449 | menu_view->addAction(action_view_zoom_out_); |
ab1d13ee | 450 | |
ce11b2ea | 451 | action_view_zoom_fit_->setCheckable(true); |
69654681 JH |
452 | action_view_zoom_fit_->setText(tr("Zoom to &Fit")); |
453 | action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit", | |
ca46b534 | 454 | QIcon(":/icons/zoom-fit.png"))); |
69654681 JH |
455 | action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F)); |
456 | action_view_zoom_fit_->setObjectName( | |
ca46b534 | 457 | QString::fromUtf8("actionViewZoomFit")); |
69654681 | 458 | menu_view->addAction(action_view_zoom_fit_); |
ca46b534 | 459 | |
1c4c23b8 | 460 | action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One")); |
69654681 | 461 | action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original", |
d1e7d82c | 462 | QIcon(":/icons/zoom-original.png"))); |
69654681 JH |
463 | action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O)); |
464 | action_view_zoom_one_to_one_->setObjectName( | |
d1e7d82c | 465 | QString::fromUtf8("actionViewZoomOneToOne")); |
69654681 | 466 | menu_view->addAction(action_view_zoom_one_to_one_); |
d1e7d82c | 467 | |
449413ab | 468 | menu_view->addSeparator(); |
c7b03d9d SA |
469 | |
470 | action_view_sticky_scrolling_->setCheckable(true); | |
471 | action_view_sticky_scrolling_->setChecked(true); | |
69282fae | 472 | action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S)); |
c7b03d9d SA |
473 | action_view_sticky_scrolling_->setObjectName( |
474 | QString::fromUtf8("actionViewStickyScrolling")); | |
69282fae | 475 | action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling")); |
c7b03d9d SA |
476 | menu_view->addAction(action_view_sticky_scrolling_); |
477 | ||
478 | view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); | |
479 | ||
ced1cc13 | 480 | menu_view->addSeparator(); |
ab1d13ee | 481 | |
0fb9d645 SA |
482 | action_view_coloured_bg_->setCheckable(true); |
483 | action_view_coloured_bg_->setChecked(true); | |
574c568d | 484 | action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B)); |
0fb9d645 SA |
485 | action_view_coloured_bg_->setObjectName( |
486 | QString::fromUtf8("actionViewColouredBg")); | |
487 | action_view_coloured_bg_->setText(tr("Use &coloured backgrounds")); | |
488 | menu_view->addAction(action_view_coloured_bg_); | |
489 | ||
574c568d SA |
490 | view_->enable_coloured_bg(action_view_coloured_bg_->isChecked()); |
491 | ||
0fb9d645 SA |
492 | menu_view->addSeparator(); |
493 | ||
69654681 JH |
494 | action_view_show_cursors_->setCheckable(true); |
495 | action_view_show_cursors_->setChecked(view_->cursors_shown()); | |
587f1d15 JH |
496 | action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors", |
497 | QIcon(":/icons/show-cursors.svg"))); | |
69654681 JH |
498 | action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C)); |
499 | action_view_show_cursors_->setObjectName( | |
ab1d13ee | 500 | QString::fromUtf8("actionViewShowCursors")); |
69654681 JH |
501 | action_view_show_cursors_->setText(tr("Show &Cursors")); |
502 | menu_view->addAction(action_view_show_cursors_); | |
a429590b | 503 | |
7dd093df | 504 | // Decoders Menu |
269528f5 | 505 | #ifdef ENABLE_DECODE |
ced1cc13 | 506 | QMenu *const menu_decoders = new QMenu; |
0a9fdca5 | 507 | menu_decoders->setTitle(tr("&Decoders")); |
7dd093df | 508 | |
0f90452b JH |
509 | menu_decoders_add_->setTitle(tr("&Add")); |
510 | connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)), | |
f0d37dab | 511 | this, SLOT(add_decoder(srd_decoder*))); |
7dd093df | 512 | |
0f90452b | 513 | menu_decoders->addMenu(menu_decoders_add_); |
269528f5 | 514 | #endif |
7dd093df | 515 | |
ab1d13ee | 516 | // Help Menu |
ced1cc13 | 517 | QMenu *const menu_help = new QMenu; |
0a9fdca5 | 518 | menu_help->setTitle(tr("&Help")); |
ab1d13ee | 519 | |
69654681 JH |
520 | action_about_->setObjectName(QString::fromUtf8("actionAbout")); |
521 | action_about_->setText(tr("&About...")); | |
522 | menu_help->addAction(action_about_); | |
7d5425ef | 523 | |
ced1cc13 JH |
524 | menu_bar->addAction(menu_file->menuAction()); |
525 | menu_bar->addAction(menu_view->menuAction()); | |
269528f5 | 526 | #ifdef ENABLE_DECODE |
ced1cc13 | 527 | menu_bar->addAction(menu_decoders->menuAction()); |
269528f5 | 528 | #endif |
ced1cc13 | 529 | menu_bar->addAction(menu_help->menuAction()); |
7d5425ef | 530 | |
ced1cc13 | 531 | setMenuBar(menu_bar); |
7d5425ef JH |
532 | QMetaObject::connectSlotsByName(this); |
533 | ||
b4543062 SA |
534 | // Also add all actions to the main window for always-enabled hotkeys |
535 | for (QAction* action : menu_bar->actions()) | |
536 | this->addAction(action); | |
537 | ||
d2f13e3e | 538 | // Setup the toolbar |
7c657094 | 539 | main_bar_ = new toolbars::MainBar(session_, *this); |
5ac961e3 JH |
540 | |
541 | // Populate the device list and select the initially selected device | |
107ca6d3 | 542 | update_device_list(); |
5ac961e3 | 543 | |
7c657094 | 544 | addToolBar(main_bar_); |
d4984fe7 | 545 | |
5ac961e3 | 546 | // Set the title |
0a9fdca5 | 547 | setWindowTitle(tr("PulseView")); |
a8d3fb2d | 548 | |
8dbbc7f0 JH |
549 | // Setup session_ events |
550 | connect(&session_, SIGNAL(capture_state_changed(int)), this, | |
6ac96c2e | 551 | SLOT(capture_state_changed(int))); |
20f81a58 JH |
552 | connect(&session_, SIGNAL(device_selected()), this, |
553 | SLOT(device_selected())); | |
48257a69 SA |
554 | connect(&session_, SIGNAL(trigger_event(util::Timestamp)), view_, |
555 | SLOT(trigger_event(util::Timestamp))); | |
c7b03d9d SA |
556 | |
557 | // Setup view_ events | |
558 | connect(view_, SIGNAL(sticky_scrolling_changed(bool)), this, | |
559 | SLOT(sticky_scrolling_changed(bool))); | |
ce11b2ea SA |
560 | connect(view_, SIGNAL(always_zoom_to_fit_changed(bool)), this, |
561 | SLOT(always_zoom_to_fit_changed(bool))); | |
48257a69 | 562 | |
d7bed479 | 563 | } |
30236a54 | 564 | |
2ad82c2e UH |
565 | void MainWindow::select_init_device() |
566 | { | |
e3c79b07 JH |
567 | QSettings settings; |
568 | map<string, string> dev_info; | |
569 | list<string> key_list; | |
570 | ||
571 | // Re-select last used device if possible. | |
572 | settings.beginGroup("Device"); | |
573 | key_list.push_back("vendor"); | |
574 | key_list.push_back("model"); | |
575 | key_list.push_back("version"); | |
576 | key_list.push_back("serial_num"); | |
577 | key_list.push_back("connection_id"); | |
578 | ||
579 | for (string key : key_list) { | |
580 | const QString k = QString::fromStdString(key); | |
581 | if (!settings.contains(k)) | |
582 | continue; | |
583 | ||
584 | const string value = settings.value(k).toString().toStdString(); | |
585 | if (!value.empty()) | |
586 | dev_info.insert(std::make_pair(key, value)); | |
587 | } | |
588 | ||
589 | const shared_ptr<devices::HardwareDevice> device = | |
590 | device_manager_.find_device_from_info(dev_info); | |
591 | select_device(device); | |
592 | update_device_list(); | |
593 | ||
594 | settings.endGroup(); | |
595 | } | |
596 | ||
597 | void MainWindow::load_init_file(const std::string &file_name, | |
2ad82c2e UH |
598 | const std::string &format) |
599 | { | |
e3c79b07 JH |
600 | shared_ptr<InputFormat> input_format; |
601 | ||
602 | if (!format.empty()) { | |
603 | const map<string, shared_ptr<InputFormat> > formats = | |
604 | device_manager_.context()->input_formats(); | |
605 | const auto iter = find_if(formats.begin(), formats.end(), | |
606 | [&](const pair<string, shared_ptr<InputFormat> > f) { | |
607 | return f.first == format; }); | |
608 | if (iter == formats.end()) { | |
609 | cerr << "Unexpected input format: " << format << endl; | |
610 | return; | |
611 | } | |
612 | ||
613 | input_format = (*iter).second; | |
614 | } | |
615 | ||
616 | load_file(QString::fromStdString(file_name), input_format); | |
617 | } | |
618 | ||
619 | ||
93f683ad SA |
620 | void MainWindow::save_ui_settings() |
621 | { | |
39eb0d45 | 622 | QSettings settings; |
93f683ad | 623 | |
6842b5fc SA |
624 | map<string, string> dev_info; |
625 | list<string> key_list; | |
626 | ||
93f683ad SA |
627 | settings.beginGroup("MainWindow"); |
628 | settings.setValue("state", saveState()); | |
629 | settings.setValue("geometry", saveGeometry()); | |
630 | settings.endGroup(); | |
6842b5fc | 631 | |
8dbbc7f0 | 632 | if (session_.device()) { |
6842b5fc SA |
633 | settings.beginGroup("Device"); |
634 | key_list.push_back("vendor"); | |
635 | key_list.push_back("model"); | |
636 | key_list.push_back("version"); | |
637 | key_list.push_back("serial_num"); | |
638 | key_list.push_back("connection_id"); | |
639 | ||
8dbbc7f0 JH |
640 | dev_info = device_manager_.get_device_info( |
641 | session_.device()); | |
6842b5fc SA |
642 | |
643 | for (string key : key_list) { | |
6842b5fc SA |
644 | if (dev_info.count(key)) |
645 | settings.setValue(QString::fromUtf8(key.c_str()), | |
646 | QString::fromUtf8(dev_info.at(key).c_str())); | |
647 | else | |
648 | settings.remove(QString::fromUtf8(key.c_str())); | |
649 | } | |
650 | ||
651 | settings.endGroup(); | |
652 | } | |
93f683ad SA |
653 | } |
654 | ||
655 | void MainWindow::restore_ui_settings() | |
656 | { | |
39eb0d45 | 657 | QSettings settings; |
93f683ad SA |
658 | |
659 | settings.beginGroup("MainWindow"); | |
660 | ||
661 | if (settings.contains("geometry")) { | |
662 | restoreGeometry(settings.value("geometry").toByteArray()); | |
663 | restoreState(settings.value("state").toByteArray()); | |
664 | } else | |
665 | resize(1000, 720); | |
666 | ||
667 | settings.endGroup(); | |
668 | } | |
669 | ||
f2edb557 JH |
670 | void MainWindow::session_error( |
671 | const QString text, const QString info_text) | |
672 | { | |
673 | QMetaObject::invokeMethod(this, "show_session_error", | |
674 | Qt::QueuedConnection, Q_ARG(QString, text), | |
675 | Q_ARG(QString, info_text)); | |
676 | } | |
677 | ||
d873f4d6 | 678 | void MainWindow::update_device_list() |
107ca6d3 | 679 | { |
079d39ea | 680 | main_bar_->update_device_list(); |
107ca6d3 JH |
681 | } |
682 | ||
dd3fd4df JH |
683 | void MainWindow::load_file(QString file_name, |
684 | std::shared_ptr<sigrok::InputFormat> format, | |
685 | const std::map<std::string, Glib::VariantBase> &options) | |
1d478458 | 686 | { |
f2edb557 JH |
687 | const QString errorMessage( |
688 | QString("Failed to load file %1").arg(file_name)); | |
ae2d1bc5 JH |
689 | |
690 | try { | |
dd3fd4df JH |
691 | if (format) |
692 | session_.set_device(shared_ptr<devices::Device>( | |
693 | new devices::InputFile( | |
694 | device_manager_.context(), | |
695 | file_name.toStdString(), | |
696 | format, options))); | |
697 | else | |
698 | session_.set_device(shared_ptr<devices::Device>( | |
699 | new devices::SessionFile( | |
700 | device_manager_.context(), | |
701 | file_name.toStdString()))); | |
2ad82c2e | 702 | } catch (Error e) { |
e8d00928 | 703 | show_session_error(tr("Failed to load ") + file_name, e.what()); |
8dbbc7f0 | 704 | session_.set_default_device(); |
6fd0b639 JH |
705 | update_device_list(); |
706 | return; | |
ae2d1bc5 JH |
707 | } |
708 | ||
a5ea63c2 JH |
709 | update_device_list(); |
710 | ||
accd9c05 | 711 | session_.start_capture([&, errorMessage](QString infoMessage) { |
6db73158 | 712 | session_error(errorMessage, infoMessage); }); |
f2edb557 JH |
713 | } |
714 | ||
e3c79b07 JH |
715 | void MainWindow::closeEvent(QCloseEvent *event) |
716 | { | |
717 | save_ui_settings(); | |
718 | event->accept(); | |
719 | } | |
720 | ||
721 | void MainWindow::keyReleaseEvent(QKeyEvent *event) | |
722 | { | |
723 | if (event->key() == Qt::Key_Alt) { | |
724 | menuBar()->setHidden(!menuBar()->isHidden()); | |
725 | menuBar()->setFocus(); | |
726 | } | |
727 | QMainWindow::keyReleaseEvent(event); | |
728 | } | |
729 | ||
f2edb557 JH |
730 | void MainWindow::show_session_error( |
731 | const QString text, const QString info_text) | |
732 | { | |
733 | QMessageBox msg(this); | |
734 | msg.setText(text); | |
735 | msg.setInformativeText(info_text); | |
736 | msg.setStandardButtons(QMessageBox::Ok); | |
737 | msg.setIcon(QMessageBox::Warning); | |
738 | msg.exec(); | |
1d478458 JH |
739 | } |
740 | ||
2953961c JH |
741 | void MainWindow::on_actionOpen_triggered() |
742 | { | |
643f65f9 JS |
743 | QSettings settings; |
744 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
745 | ||
1d43d767 | 746 | // Show the dialog |
1d478458 | 747 | const QString file_name = QFileDialog::getOpenFileName( |
643f65f9 | 748 | this, tr("Open File"), dir, tr( |
6866d5de JH |
749 | "Sigrok Sessions (*.sr);;" |
750 | "All Files (*.*)")); | |
643f65f9 JS |
751 | |
752 | if (!file_name.isEmpty()) { | |
92b139d0 | 753 | load_file(file_name); |
643f65f9 JS |
754 | |
755 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
756 | settings.setValue(SettingOpenDirectory, abs_path); | |
757 | } | |
2953961c JH |
758 | } |
759 | ||
0fbda3c2 JH |
760 | void MainWindow::on_actionSaveAs_triggered() |
761 | { | |
998b89fd | 762 | export_file(device_manager_.context()->output_formats()["srzip"]); |
0fbda3c2 JH |
763 | } |
764 | ||
d2fc6be9 SA |
765 | void MainWindow::on_actionSaveSelectionAs_triggered() |
766 | { | |
a55e7918 | 767 | export_file(device_manager_.context()->output_formats()["srzip"], true); |
d2fc6be9 SA |
768 | } |
769 | ||
9663c82b JH |
770 | void MainWindow::on_actionConnect_triggered() |
771 | { | |
b99cfc42 | 772 | // Stop any currently running capture session |
8dbbc7f0 | 773 | session_.stop_capture(); |
b99cfc42 | 774 | |
8dbbc7f0 | 775 | dialogs::Connect dlg(this, device_manager_); |
5eb0fa13 | 776 | |
dc0867ff JH |
777 | // If the user selected a device, select it in the device list. Select the |
778 | // current device otherwise. | |
d873f4d6 | 779 | if (dlg.exec()) |
51cf49fe | 780 | select_device(dlg.get_selected_device()); |
dc0867ff | 781 | |
d873f4d6 | 782 | update_device_list(); |
9663c82b JH |
783 | } |
784 | ||
2a032dcb JH |
785 | void MainWindow::on_actionQuit_triggered() |
786 | { | |
787 | close(); | |
788 | } | |
789 | ||
a429590b JH |
790 | void MainWindow::on_actionViewZoomIn_triggered() |
791 | { | |
8dbbc7f0 | 792 | view_->zoom(1); |
a429590b JH |
793 | } |
794 | ||
795 | void MainWindow::on_actionViewZoomOut_triggered() | |
796 | { | |
8dbbc7f0 | 797 | view_->zoom(-1); |
a429590b JH |
798 | } |
799 | ||
ca46b534 JH |
800 | void MainWindow::on_actionViewZoomFit_triggered() |
801 | { | |
ce11b2ea | 802 | view_->zoom_fit(action_view_zoom_fit_->isChecked()); |
ca46b534 JH |
803 | } |
804 | ||
d1e7d82c JH |
805 | void MainWindow::on_actionViewZoomOneToOne_triggered() |
806 | { | |
8dbbc7f0 | 807 | view_->zoom_one_to_one(); |
d1e7d82c JH |
808 | } |
809 | ||
c7b03d9d SA |
810 | void MainWindow::on_actionViewStickyScrolling_triggered() |
811 | { | |
812 | view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); | |
813 | } | |
814 | ||
0fb9d645 SA |
815 | void MainWindow::on_actionViewColouredBg_triggered() |
816 | { | |
574c568d | 817 | view_->enable_coloured_bg(action_view_coloured_bg_->isChecked()); |
0fb9d645 SA |
818 | } |
819 | ||
e072efc8 JH |
820 | void MainWindow::on_actionViewShowCursors_triggered() |
821 | { | |
8dbbc7f0 | 822 | assert(view_); |
b4d91e56 | 823 | |
8dbbc7f0 | 824 | const bool show = !view_->cursors_shown(); |
f3290553 | 825 | if (show) |
8dbbc7f0 | 826 | view_->centre_cursors(); |
b4d91e56 | 827 | |
8dbbc7f0 | 828 | view_->show_cursors(show); |
e072efc8 JH |
829 | } |
830 | ||
30236a54 JH |
831 | void MainWindow::on_actionAbout_triggered() |
832 | { | |
8dbbc7f0 | 833 | dialogs::About dlg(device_manager_.context(), this); |
40eb2ff4 | 834 | dlg.exec(); |
30236a54 | 835 | } |
274d4f13 | 836 | |
c7b03d9d SA |
837 | void MainWindow::sticky_scrolling_changed(bool state) |
838 | { | |
839 | action_view_sticky_scrolling_->setChecked(state); | |
840 | } | |
841 | ||
ce11b2ea SA |
842 | void MainWindow::always_zoom_to_fit_changed(bool state) |
843 | { | |
844 | action_view_zoom_fit_->setChecked(state); | |
845 | } | |
846 | ||
f0d37dab | 847 | void MainWindow::add_decoder(srd_decoder *decoder) |
7dd093df | 848 | { |
269528f5 | 849 | #ifdef ENABLE_DECODE |
f0d37dab | 850 | assert(decoder); |
8dbbc7f0 | 851 | session_.add_decoder(decoder); |
269528f5 JH |
852 | #else |
853 | (void)decoder; | |
854 | #endif | |
7dd093df JH |
855 | } |
856 | ||
6ac96c2e JH |
857 | void MainWindow::capture_state_changed(int state) |
858 | { | |
7c657094 | 859 | main_bar_->set_capture_state((pv::Session::capture_state)state); |
6ac96c2e JH |
860 | } |
861 | ||
20f81a58 JH |
862 | void MainWindow::device_selected() |
863 | { | |
864 | // Set the title to include the device/file name | |
da30ecb7 | 865 | const shared_ptr<devices::Device> device = session_.device(); |
20f81a58 JH |
866 | if (!device) |
867 | return; | |
868 | ||
3084ed4b | 869 | const string display_name = device->display_name(device_manager_); |
20f81a58 JH |
870 | setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str())); |
871 | } | |
872 | ||
51e77110 | 873 | } // namespace pv |