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