]>
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 | ||
d52d8455 | 21 | #ifdef ENABLE_SIGROKDECODE |
30236a54 | 22 | #include <sigrokdecode.h> |
d52d8455 | 23 | #endif |
30236a54 | 24 | |
f2edb557 JH |
25 | #include <boost/bind.hpp> |
26 | ||
7d5425ef JH |
27 | #include <QAction> |
28 | #include <QApplication> | |
29 | #include <QButtonGroup> | |
2953961c | 30 | #include <QFileDialog> |
f2edb557 | 31 | #include <QMessageBox> |
7d5425ef JH |
32 | #include <QMenu> |
33 | #include <QMenuBar> | |
34 | #include <QStatusBar> | |
35 | #include <QVBoxLayout> | |
36 | #include <QWidget> | |
2953961c | 37 | |
d7bed479 | 38 | #include "mainwindow.h" |
acda14b8 | 39 | #include "dialogs/about.h" |
9663c82b | 40 | #include "dialogs/connect.h" |
f4c92e1c | 41 | #include "toolbars/samplingbar.h" |
4104d7f3 | 42 | #include "view/view.h" |
d7bed479 | 43 | |
30236a54 JH |
44 | /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */ |
45 | #define __STDC_FORMAT_MACROS | |
46 | #include <inttypes.h> | |
47 | #include <stdint.h> | |
48 | #include <stdarg.h> | |
49 | #include <glib.h> | |
50 | #include <libsigrok/libsigrok.h> | |
e82fd481 | 51 | |
30236a54 | 52 | |
51e77110 JH |
53 | namespace pv { |
54 | ||
1d478458 JH |
55 | MainWindow::MainWindow(const char *open_file_name, |
56 | QWidget *parent) : | |
7d5425ef | 57 | QMainWindow(parent) |
d7bed479 | 58 | { |
7d5425ef | 59 | setup_ui(); |
1d478458 JH |
60 | if (open_file_name) { |
61 | const QString s(QString::fromUtf8(open_file_name)); | |
62 | QMetaObject::invokeMethod(this, "load_file", | |
63 | Qt::QueuedConnection, | |
64 | Q_ARG(QString, s)); | |
65 | } | |
7d5425ef JH |
66 | } |
67 | ||
68 | void MainWindow::setup_ui() | |
69 | { | |
70 | setObjectName(QString::fromUtf8("MainWindow")); | |
71 | ||
72 | resize(1024, 768); | |
73 | ||
74 | // Set the window icon | |
75 | QIcon icon; | |
76 | icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"), | |
77 | QSize(), QIcon::Normal, QIcon::Off); | |
78 | setWindowIcon(icon); | |
79 | ||
e072efc8 JH |
80 | // Setup the central widget |
81 | _central_widget = new QWidget(this); | |
82 | _vertical_layout = new QVBoxLayout(_central_widget); | |
83 | _vertical_layout->setSpacing(6); | |
84 | _vertical_layout->setContentsMargins(0, 0, 0, 0); | |
85 | setCentralWidget(_central_widget); | |
86 | ||
87 | _view = new pv::view::View(_session, this); | |
88 | _vertical_layout->addWidget(_view); | |
89 | ||
ab1d13ee JH |
90 | // Setup the menu bar |
91 | _menu_bar = new QMenuBar(this); | |
92 | _menu_bar->setGeometry(QRect(0, 0, 400, 25)); | |
a429590b | 93 | |
ab1d13ee JH |
94 | // File Menu |
95 | _menu_file = new QMenu(_menu_bar); | |
96 | _menu_file->setTitle(QApplication::translate( | |
97 | "MainWindow", "&File", 0, QApplication::UnicodeUTF8)); | |
e072efc8 | 98 | |
7d5425ef | 99 | _action_open = new QAction(this); |
ab1d13ee JH |
100 | _action_open->setText(QApplication::translate( |
101 | "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8)); | |
362eea96 JH |
102 | _action_open->setIcon(QIcon::fromTheme("document-open", |
103 | QIcon(":/icons/document-open.png"))); | |
7d5425ef | 104 | _action_open->setObjectName(QString::fromUtf8("actionOpen")); |
7d5425ef | 105 | _menu_file->addAction(_action_open); |
009e1503 | 106 | |
2a032dcb JH |
107 | _menu_file->addSeparator(); |
108 | ||
9663c82b JH |
109 | _action_connect = new QAction(this); |
110 | _action_connect->setText(QApplication::translate( | |
111 | "MainWindow", "&Connect to Device...", 0, | |
112 | QApplication::UnicodeUTF8)); | |
113 | _action_connect->setObjectName(QString::fromUtf8("actionConnect")); | |
114 | _menu_file->addAction(_action_connect); | |
115 | ||
116 | _menu_file->addSeparator(); | |
117 | ||
2a032dcb JH |
118 | _action_quit = new QAction(this); |
119 | _action_quit->setText(QApplication::translate( | |
120 | "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8)); | |
121 | _action_quit->setIcon(QIcon::fromTheme("application-exit", | |
122 | QIcon(":/icons/application-exit.png"))); | |
d1bb7d7a | 123 | _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); |
2a032dcb JH |
124 | _action_quit->setObjectName(QString::fromUtf8("actionQuit")); |
125 | _menu_file->addAction(_action_quit); | |
126 | ||
ab1d13ee | 127 | // View Menu |
a429590b | 128 | _menu_view = new QMenu(_menu_bar); |
ab1d13ee JH |
129 | _menu_view->setTitle(QApplication::translate( |
130 | "MainWindow", "&View", 0, QApplication::UnicodeUTF8)); | |
131 | ||
132 | _action_view_zoom_in = new QAction(this); | |
133 | _action_view_zoom_in->setText(QApplication::translate( | |
134 | "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8)); | |
135 | _action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in", | |
136 | QIcon(":/icons/zoom-in.png"))); | |
137 | _action_view_zoom_in->setObjectName( | |
138 | QString::fromUtf8("actionViewZoomIn")); | |
a429590b | 139 | _menu_view->addAction(_action_view_zoom_in); |
ab1d13ee JH |
140 | |
141 | _action_view_zoom_out = new QAction(this); | |
142 | _action_view_zoom_out->setText(QApplication::translate( | |
143 | "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8)); | |
144 | _action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out", | |
145 | QIcon(":/icons/zoom-out.png"))); | |
146 | _action_view_zoom_out->setObjectName( | |
147 | QString::fromUtf8("actionViewZoomOut")); | |
a429590b | 148 | _menu_view->addAction(_action_view_zoom_out); |
ab1d13ee | 149 | |
e072efc8 | 150 | _menu_view->addSeparator(); |
ab1d13ee JH |
151 | |
152 | _action_view_show_cursors = new QAction(this); | |
153 | _action_view_show_cursors->setCheckable(true); | |
154 | _action_view_show_cursors->setChecked(_view->cursors_shown()); | |
4166ed6c | 155 | _action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C)); |
ab1d13ee JH |
156 | _action_view_show_cursors->setObjectName( |
157 | QString::fromUtf8("actionViewShowCursors")); | |
158 | _action_view_show_cursors->setText(QApplication::translate( | |
159 | "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8)); | |
e072efc8 | 160 | _menu_view->addAction(_action_view_show_cursors); |
a429590b | 161 | |
ab1d13ee | 162 | // Help Menu |
7d5425ef | 163 | _menu_help = new QMenu(_menu_bar); |
ab1d13ee JH |
164 | _menu_help->setTitle(QApplication::translate( |
165 | "MainWindow", "&Help", 0, QApplication::UnicodeUTF8)); | |
166 | ||
167 | _action_about = new QAction(this); | |
168 | _action_about->setObjectName(QString::fromUtf8("actionAbout")); | |
169 | _action_about->setText(QApplication::translate( | |
170 | "MainWindow", "&About...", 0, QApplication::UnicodeUTF8)); | |
7d5425ef JH |
171 | _menu_help->addAction(_action_about); |
172 | ||
173 | _menu_bar->addAction(_menu_file->menuAction()); | |
a429590b | 174 | _menu_bar->addAction(_menu_view->menuAction()); |
7d5425ef JH |
175 | _menu_bar->addAction(_menu_help->menuAction()); |
176 | ||
177 | setMenuBar(_menu_bar); | |
178 | QMetaObject::connectSlotsByName(this); | |
179 | ||
5ac961e3 | 180 | // Setup the toolbar |
0a4db787 JH |
181 | _toolbar = new QToolBar(this); |
182 | _toolbar->addAction(_action_open); | |
a429590b JH |
183 | _toolbar->addSeparator(); |
184 | _toolbar->addAction(_action_view_zoom_in); | |
185 | _toolbar->addAction(_action_view_zoom_out); | |
0a4db787 JH |
186 | addToolBar(_toolbar); |
187 | ||
5ac961e3 | 188 | // Setup the sampling bar |
f4c92e1c | 189 | _sampling_bar = new toolbars::SamplingBar(this); |
5ac961e3 JH |
190 | |
191 | // Populate the device list and select the initially selected device | |
18203d86 | 192 | scan_devices(); |
5ac961e3 JH |
193 | if(!_devices.empty()) { |
194 | struct sr_dev_inst *const initial_sdi = _devices.front(); | |
195 | _sampling_bar->set_selected_device(initial_sdi); | |
196 | _session.set_device(initial_sdi); | |
197 | } | |
198 | ||
199 | connect(_sampling_bar, SIGNAL(device_selected()), this, | |
200 | SLOT(device_selected())); | |
274d4f13 JH |
201 | connect(_sampling_bar, SIGNAL(run_stop()), this, |
202 | SLOT(run_stop())); | |
d4984fe7 JH |
203 | addToolBar(_sampling_bar); |
204 | ||
5ac961e3 | 205 | // Set the title |
a8d3fb2d JH |
206 | setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0, |
207 | QApplication::UnicodeUTF8)); | |
208 | ||
6ac96c2e JH |
209 | // Setup _session events |
210 | connect(&_session, SIGNAL(capture_state_changed(int)), this, | |
211 | SLOT(capture_state_changed(int))); | |
212 | ||
d7bed479 | 213 | } |
30236a54 | 214 | |
18203d86 JH |
215 | void MainWindow::scan_devices() |
216 | { | |
217 | _devices.clear(); | |
218 | ||
219 | /* Scan all drivers for all devices. */ | |
220 | struct sr_dev_driver **const drivers = sr_driver_list(); | |
221 | for (struct sr_dev_driver **driver = drivers; *driver; driver++) { | |
222 | GSList *const devices = sr_driver_scan(*driver, NULL); | |
223 | for (GSList *l = devices; l; l = l->next) | |
224 | _devices.push_back((sr_dev_inst*)l->data); | |
225 | g_slist_free(devices); | |
226 | } | |
227 | ||
228 | assert(_sampling_bar); | |
229 | _sampling_bar->set_device_list(_devices); | |
230 | } | |
231 | ||
f2edb557 JH |
232 | void MainWindow::session_error( |
233 | const QString text, const QString info_text) | |
234 | { | |
235 | QMetaObject::invokeMethod(this, "show_session_error", | |
236 | Qt::QueuedConnection, Q_ARG(QString, text), | |
237 | Q_ARG(QString, info_text)); | |
238 | } | |
239 | ||
1d478458 JH |
240 | void MainWindow::load_file(QString file_name) |
241 | { | |
f2edb557 JH |
242 | const QString errorMessage( |
243 | QString("Failed to load file %1").arg(file_name)); | |
244 | const QString infoMessage; | |
245 | _session.load_file(file_name.toStdString(), | |
246 | boost::bind(&MainWindow::session_error, this, | |
247 | errorMessage, infoMessage)); | |
248 | } | |
249 | ||
250 | void MainWindow::show_session_error( | |
251 | const QString text, const QString info_text) | |
252 | { | |
253 | QMessageBox msg(this); | |
254 | msg.setText(text); | |
255 | msg.setInformativeText(info_text); | |
256 | msg.setStandardButtons(QMessageBox::Ok); | |
257 | msg.setIcon(QMessageBox::Warning); | |
258 | msg.exec(); | |
1d478458 JH |
259 | } |
260 | ||
2953961c JH |
261 | void MainWindow::on_actionOpen_triggered() |
262 | { | |
1d478458 | 263 | const QString file_name = QFileDialog::getOpenFileName( |
2953961c JH |
264 | this, tr("Open File"), "", |
265 | tr("Sigrok Sessions (*.sr)")); | |
92b139d0 JH |
266 | if (!file_name.isEmpty()) |
267 | load_file(file_name); | |
2953961c JH |
268 | } |
269 | ||
9663c82b JH |
270 | void MainWindow::on_actionConnect_triggered() |
271 | { | |
272 | dialogs::Connect dlg(this); | |
793f8096 | 273 | if (!dlg.exec()) |
5eb0fa13 JH |
274 | return; |
275 | ||
276 | struct sr_dev_inst *const sdi = dlg.get_selected_device(); | |
277 | if (sdi) { | |
278 | assert(_sampling_bar); | |
279 | ||
280 | _devices.push_back(sdi); | |
281 | _sampling_bar->set_device_list(_devices); | |
282 | _sampling_bar->set_selected_device(sdi); | |
5ac961e3 JH |
283 | |
284 | _session.set_device(sdi); | |
5eb0fa13 | 285 | } |
9663c82b JH |
286 | } |
287 | ||
2a032dcb JH |
288 | void MainWindow::on_actionQuit_triggered() |
289 | { | |
290 | close(); | |
291 | } | |
292 | ||
a429590b JH |
293 | void MainWindow::on_actionViewZoomIn_triggered() |
294 | { | |
295 | _view->zoom(1); | |
296 | } | |
297 | ||
298 | void MainWindow::on_actionViewZoomOut_triggered() | |
299 | { | |
300 | _view->zoom(-1); | |
301 | } | |
302 | ||
e072efc8 JH |
303 | void MainWindow::on_actionViewShowCursors_triggered() |
304 | { | |
305 | assert(_view); | |
b4d91e56 JH |
306 | |
307 | const bool show = !_view->cursors_shown(); | |
308 | if(show) | |
309 | _view->centre_cursors(); | |
310 | ||
311 | _view->show_cursors(show); | |
e072efc8 JH |
312 | } |
313 | ||
30236a54 JH |
314 | void MainWindow::on_actionAbout_triggered() |
315 | { | |
acda14b8 | 316 | dialogs::About dlg(this); |
40eb2ff4 | 317 | dlg.exec(); |
30236a54 | 318 | } |
274d4f13 | 319 | |
5ac961e3 JH |
320 | void MainWindow::device_selected() |
321 | { | |
322 | _session.set_device(_sampling_bar->get_selected_device()); | |
323 | } | |
324 | ||
274d4f13 JH |
325 | void MainWindow::run_stop() |
326 | { | |
5b7cf66c JH |
327 | switch(_session.get_capture_state()) { |
328 | case SigSession::Stopped: | |
d64d1596 | 329 | _session.start_capture(_sampling_bar->get_record_length(), |
f2edb557 JH |
330 | boost::bind(&MainWindow::session_error, this, |
331 | QString("Capture failed"), _1)); | |
5b7cf66c JH |
332 | break; |
333 | ||
334 | case SigSession::Running: | |
335 | _session.stop_capture(); | |
336 | break; | |
337 | } | |
274d4f13 | 338 | } |
51e77110 | 339 | |
6ac96c2e JH |
340 | void MainWindow::capture_state_changed(int state) |
341 | { | |
342 | _sampling_bar->set_sampling(state != SigSession::Stopped); | |
343 | } | |
344 | ||
51e77110 | 345 | } // namespace pv |