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