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