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