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