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