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