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