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