]>
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"))); | |
121 | _action_quit->setObjectName(QString::fromUtf8("actionQuit")); | |
122 | _menu_file->addAction(_action_quit); | |
123 | ||
ab1d13ee | 124 | // View Menu |
a429590b | 125 | _menu_view = new QMenu(_menu_bar); |
ab1d13ee JH |
126 | _menu_view->setTitle(QApplication::translate( |
127 | "MainWindow", "&View", 0, QApplication::UnicodeUTF8)); | |
128 | ||
129 | _action_view_zoom_in = new QAction(this); | |
130 | _action_view_zoom_in->setText(QApplication::translate( | |
131 | "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8)); | |
132 | _action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in", | |
133 | QIcon(":/icons/zoom-in.png"))); | |
134 | _action_view_zoom_in->setObjectName( | |
135 | QString::fromUtf8("actionViewZoomIn")); | |
a429590b | 136 | _menu_view->addAction(_action_view_zoom_in); |
ab1d13ee JH |
137 | |
138 | _action_view_zoom_out = new QAction(this); | |
139 | _action_view_zoom_out->setText(QApplication::translate( | |
140 | "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8)); | |
141 | _action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out", | |
142 | QIcon(":/icons/zoom-out.png"))); | |
143 | _action_view_zoom_out->setObjectName( | |
144 | QString::fromUtf8("actionViewZoomOut")); | |
a429590b | 145 | _menu_view->addAction(_action_view_zoom_out); |
ab1d13ee | 146 | |
e072efc8 | 147 | _menu_view->addSeparator(); |
ab1d13ee JH |
148 | |
149 | _action_view_show_cursors = new QAction(this); | |
150 | _action_view_show_cursors->setCheckable(true); | |
151 | _action_view_show_cursors->setChecked(_view->cursors_shown()); | |
152 | _action_view_show_cursors->setObjectName( | |
153 | QString::fromUtf8("actionViewShowCursors")); | |
154 | _action_view_show_cursors->setText(QApplication::translate( | |
155 | "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8)); | |
e072efc8 | 156 | _menu_view->addAction(_action_view_show_cursors); |
a429590b | 157 | |
ab1d13ee | 158 | // Help Menu |
7d5425ef | 159 | _menu_help = new QMenu(_menu_bar); |
ab1d13ee JH |
160 | _menu_help->setTitle(QApplication::translate( |
161 | "MainWindow", "&Help", 0, QApplication::UnicodeUTF8)); | |
162 | ||
163 | _action_about = new QAction(this); | |
164 | _action_about->setObjectName(QString::fromUtf8("actionAbout")); | |
165 | _action_about->setText(QApplication::translate( | |
166 | "MainWindow", "&About...", 0, QApplication::UnicodeUTF8)); | |
7d5425ef JH |
167 | _menu_help->addAction(_action_about); |
168 | ||
169 | _menu_bar->addAction(_menu_file->menuAction()); | |
a429590b | 170 | _menu_bar->addAction(_menu_view->menuAction()); |
7d5425ef JH |
171 | _menu_bar->addAction(_menu_help->menuAction()); |
172 | ||
173 | setMenuBar(_menu_bar); | |
174 | QMetaObject::connectSlotsByName(this); | |
175 | ||
176 | // Setup the toolbars | |
0a4db787 JH |
177 | _toolbar = new QToolBar(this); |
178 | _toolbar->addAction(_action_open); | |
a429590b JH |
179 | _toolbar->addSeparator(); |
180 | _toolbar->addAction(_action_view_zoom_in); | |
181 | _toolbar->addAction(_action_view_zoom_out); | |
0a4db787 JH |
182 | addToolBar(_toolbar); |
183 | ||
f4c92e1c | 184 | _sampling_bar = new toolbars::SamplingBar(this); |
18203d86 | 185 | scan_devices(); |
274d4f13 JH |
186 | connect(_sampling_bar, SIGNAL(run_stop()), this, |
187 | SLOT(run_stop())); | |
d4984fe7 JH |
188 | addToolBar(_sampling_bar); |
189 | ||
a8d3fb2d JH |
190 | setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0, |
191 | QApplication::UnicodeUTF8)); | |
192 | ||
6ac96c2e JH |
193 | // Setup _session events |
194 | connect(&_session, SIGNAL(capture_state_changed(int)), this, | |
195 | SLOT(capture_state_changed(int))); | |
196 | ||
d7bed479 | 197 | } |
30236a54 | 198 | |
18203d86 JH |
199 | void MainWindow::scan_devices() |
200 | { | |
201 | _devices.clear(); | |
202 | ||
203 | /* Scan all drivers for all devices. */ | |
204 | struct sr_dev_driver **const drivers = sr_driver_list(); | |
205 | for (struct sr_dev_driver **driver = drivers; *driver; driver++) { | |
206 | GSList *const devices = sr_driver_scan(*driver, NULL); | |
207 | for (GSList *l = devices; l; l = l->next) | |
208 | _devices.push_back((sr_dev_inst*)l->data); | |
209 | g_slist_free(devices); | |
210 | } | |
211 | ||
212 | assert(_sampling_bar); | |
213 | _sampling_bar->set_device_list(_devices); | |
214 | } | |
215 | ||
f2edb557 JH |
216 | void MainWindow::session_error( |
217 | const QString text, const QString info_text) | |
218 | { | |
219 | QMetaObject::invokeMethod(this, "show_session_error", | |
220 | Qt::QueuedConnection, Q_ARG(QString, text), | |
221 | Q_ARG(QString, info_text)); | |
222 | } | |
223 | ||
1d478458 JH |
224 | void MainWindow::load_file(QString file_name) |
225 | { | |
f2edb557 JH |
226 | const QString errorMessage( |
227 | QString("Failed to load file %1").arg(file_name)); | |
228 | const QString infoMessage; | |
229 | _session.load_file(file_name.toStdString(), | |
230 | boost::bind(&MainWindow::session_error, this, | |
231 | errorMessage, infoMessage)); | |
232 | } | |
233 | ||
234 | void MainWindow::show_session_error( | |
235 | const QString text, const QString info_text) | |
236 | { | |
237 | QMessageBox msg(this); | |
238 | msg.setText(text); | |
239 | msg.setInformativeText(info_text); | |
240 | msg.setStandardButtons(QMessageBox::Ok); | |
241 | msg.setIcon(QMessageBox::Warning); | |
242 | msg.exec(); | |
1d478458 JH |
243 | } |
244 | ||
2953961c JH |
245 | void MainWindow::on_actionOpen_triggered() |
246 | { | |
1d478458 | 247 | const QString file_name = QFileDialog::getOpenFileName( |
2953961c JH |
248 | this, tr("Open File"), "", |
249 | tr("Sigrok Sessions (*.sr)")); | |
1d478458 | 250 | load_file(file_name); |
2953961c JH |
251 | } |
252 | ||
9663c82b JH |
253 | void MainWindow::on_actionConnect_triggered() |
254 | { | |
255 | dialogs::Connect dlg(this); | |
793f8096 | 256 | if (!dlg.exec()) |
5eb0fa13 JH |
257 | return; |
258 | ||
259 | struct sr_dev_inst *const sdi = dlg.get_selected_device(); | |
260 | if (sdi) { | |
261 | assert(_sampling_bar); | |
262 | ||
263 | _devices.push_back(sdi); | |
264 | _sampling_bar->set_device_list(_devices); | |
265 | _sampling_bar->set_selected_device(sdi); | |
266 | } | |
9663c82b JH |
267 | } |
268 | ||
2a032dcb JH |
269 | void MainWindow::on_actionQuit_triggered() |
270 | { | |
271 | close(); | |
272 | } | |
273 | ||
a429590b JH |
274 | void MainWindow::on_actionViewZoomIn_triggered() |
275 | { | |
276 | _view->zoom(1); | |
277 | } | |
278 | ||
279 | void MainWindow::on_actionViewZoomOut_triggered() | |
280 | { | |
281 | _view->zoom(-1); | |
282 | } | |
283 | ||
e072efc8 JH |
284 | void MainWindow::on_actionViewShowCursors_triggered() |
285 | { | |
286 | assert(_view); | |
287 | _view->show_cursors(_action_view_show_cursors->isChecked()); | |
288 | } | |
289 | ||
30236a54 JH |
290 | void MainWindow::on_actionAbout_triggered() |
291 | { | |
acda14b8 | 292 | dialogs::About dlg(this); |
40eb2ff4 | 293 | dlg.exec(); |
30236a54 | 294 | } |
274d4f13 JH |
295 | |
296 | void MainWindow::run_stop() | |
297 | { | |
5b7cf66c JH |
298 | switch(_session.get_capture_state()) { |
299 | case SigSession::Stopped: | |
300 | _session.start_capture( | |
301 | _sampling_bar->get_selected_device(), | |
f2edb557 JH |
302 | _sampling_bar->get_record_length(), |
303 | boost::bind(&MainWindow::session_error, this, | |
304 | QString("Capture failed"), _1)); | |
5b7cf66c JH |
305 | break; |
306 | ||
307 | case SigSession::Running: | |
308 | _session.stop_capture(); | |
309 | break; | |
310 | } | |
274d4f13 | 311 | } |
51e77110 | 312 | |
6ac96c2e JH |
313 | void MainWindow::capture_state_changed(int state) |
314 | { | |
315 | _sampling_bar->set_sampling(state != SigSession::Stopped); | |
316 | } | |
317 | ||
51e77110 | 318 | } // namespace pv |