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