]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
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 | ||
21 | #ifdef ENABLE_SIGROKDECODE | |
22 | #include <sigrokdecode.h> | |
23 | #endif | |
24 | ||
25 | #include <boost/bind.hpp> | |
26 | ||
27 | #include <QAction> | |
28 | #include <QApplication> | |
29 | #include <QButtonGroup> | |
30 | #include <QFileDialog> | |
31 | #include <QMessageBox> | |
32 | #include <QMenu> | |
33 | #include <QMenuBar> | |
34 | #include <QStatusBar> | |
35 | #include <QVBoxLayout> | |
36 | #include <QWidget> | |
37 | ||
38 | #include "mainwindow.h" | |
39 | #include "dialogs/about.h" | |
40 | #include "dialogs/connect.h" | |
41 | #include "toolbars/samplingbar.h" | |
42 | #include "view/view.h" | |
43 | ||
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> | |
51 | ||
52 | ||
53 | namespace pv { | |
54 | ||
55 | MainWindow::MainWindow(const char *open_file_name, | |
56 | QWidget *parent) : | |
57 | QMainWindow(parent) | |
58 | { | |
59 | setup_ui(); | |
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 | } | |
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 | ||
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 | ||
90 | // Setup the menu bar | |
91 | _menu_bar = new QMenuBar(this); | |
92 | _menu_bar->setGeometry(QRect(0, 0, 400, 25)); | |
93 | ||
94 | // File Menu | |
95 | _menu_file = new QMenu(_menu_bar); | |
96 | _menu_file->setTitle(QApplication::translate( | |
97 | "MainWindow", "&File", 0, QApplication::UnicodeUTF8)); | |
98 | ||
99 | _action_open = new QAction(this); | |
100 | _action_open->setText(QApplication::translate( | |
101 | "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8)); | |
102 | _action_open->setIcon(QIcon::fromTheme("document-open", | |
103 | QIcon(":/icons/document-open.png"))); | |
104 | _action_open->setObjectName(QString::fromUtf8("actionOpen")); | |
105 | _menu_file->addAction(_action_open); | |
106 | ||
107 | _menu_file->addSeparator(); | |
108 | ||
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 | ||
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"))); | |
123 | _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); | |
124 | _action_quit->setObjectName(QString::fromUtf8("actionQuit")); | |
125 | _menu_file->addAction(_action_quit); | |
126 | ||
127 | // View Menu | |
128 | _menu_view = new QMenu(_menu_bar); | |
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")); | |
139 | _menu_view->addAction(_action_view_zoom_in); | |
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")); | |
148 | _menu_view->addAction(_action_view_zoom_out); | |
149 | ||
150 | _menu_view->addSeparator(); | |
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()); | |
155 | _action_view_show_cursors->setObjectName( | |
156 | QString::fromUtf8("actionViewShowCursors")); | |
157 | _action_view_show_cursors->setText(QApplication::translate( | |
158 | "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8)); | |
159 | _menu_view->addAction(_action_view_show_cursors); | |
160 | ||
161 | // Help Menu | |
162 | _menu_help = new QMenu(_menu_bar); | |
163 | _menu_help->setTitle(QApplication::translate( | |
164 | "MainWindow", "&Help", 0, QApplication::UnicodeUTF8)); | |
165 | ||
166 | _action_about = new QAction(this); | |
167 | _action_about->setObjectName(QString::fromUtf8("actionAbout")); | |
168 | _action_about->setText(QApplication::translate( | |
169 | "MainWindow", "&About...", 0, QApplication::UnicodeUTF8)); | |
170 | _menu_help->addAction(_action_about); | |
171 | ||
172 | _menu_bar->addAction(_menu_file->menuAction()); | |
173 | _menu_bar->addAction(_menu_view->menuAction()); | |
174 | _menu_bar->addAction(_menu_help->menuAction()); | |
175 | ||
176 | setMenuBar(_menu_bar); | |
177 | QMetaObject::connectSlotsByName(this); | |
178 | ||
179 | // Setup the toolbars | |
180 | _toolbar = new QToolBar(this); | |
181 | _toolbar->addAction(_action_open); | |
182 | _toolbar->addSeparator(); | |
183 | _toolbar->addAction(_action_view_zoom_in); | |
184 | _toolbar->addAction(_action_view_zoom_out); | |
185 | addToolBar(_toolbar); | |
186 | ||
187 | _sampling_bar = new toolbars::SamplingBar(this); | |
188 | scan_devices(); | |
189 | connect(_sampling_bar, SIGNAL(run_stop()), this, | |
190 | SLOT(run_stop())); | |
191 | addToolBar(_sampling_bar); | |
192 | ||
193 | setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0, | |
194 | QApplication::UnicodeUTF8)); | |
195 | ||
196 | // Setup _session events | |
197 | connect(&_session, SIGNAL(capture_state_changed(int)), this, | |
198 | SLOT(capture_state_changed(int))); | |
199 | ||
200 | } | |
201 | ||
202 | void MainWindow::scan_devices() | |
203 | { | |
204 | _devices.clear(); | |
205 | ||
206 | /* Scan all drivers for all devices. */ | |
207 | struct sr_dev_driver **const drivers = sr_driver_list(); | |
208 | for (struct sr_dev_driver **driver = drivers; *driver; driver++) { | |
209 | GSList *const devices = sr_driver_scan(*driver, NULL); | |
210 | for (GSList *l = devices; l; l = l->next) | |
211 | _devices.push_back((sr_dev_inst*)l->data); | |
212 | g_slist_free(devices); | |
213 | } | |
214 | ||
215 | assert(_sampling_bar); | |
216 | _sampling_bar->set_device_list(_devices); | |
217 | } | |
218 | ||
219 | void MainWindow::session_error( | |
220 | const QString text, const QString info_text) | |
221 | { | |
222 | QMetaObject::invokeMethod(this, "show_session_error", | |
223 | Qt::QueuedConnection, Q_ARG(QString, text), | |
224 | Q_ARG(QString, info_text)); | |
225 | } | |
226 | ||
227 | void MainWindow::load_file(QString file_name) | |
228 | { | |
229 | const QString errorMessage( | |
230 | QString("Failed to load file %1").arg(file_name)); | |
231 | const QString infoMessage; | |
232 | _session.load_file(file_name.toStdString(), | |
233 | boost::bind(&MainWindow::session_error, this, | |
234 | errorMessage, infoMessage)); | |
235 | } | |
236 | ||
237 | void MainWindow::show_session_error( | |
238 | const QString text, const QString info_text) | |
239 | { | |
240 | QMessageBox msg(this); | |
241 | msg.setText(text); | |
242 | msg.setInformativeText(info_text); | |
243 | msg.setStandardButtons(QMessageBox::Ok); | |
244 | msg.setIcon(QMessageBox::Warning); | |
245 | msg.exec(); | |
246 | } | |
247 | ||
248 | void MainWindow::on_actionOpen_triggered() | |
249 | { | |
250 | const QString file_name = QFileDialog::getOpenFileName( | |
251 | this, tr("Open File"), "", | |
252 | tr("Sigrok Sessions (*.sr)")); | |
253 | load_file(file_name); | |
254 | } | |
255 | ||
256 | void MainWindow::on_actionConnect_triggered() | |
257 | { | |
258 | dialogs::Connect dlg(this); | |
259 | if (!dlg.exec()) | |
260 | return; | |
261 | ||
262 | struct sr_dev_inst *const sdi = dlg.get_selected_device(); | |
263 | if (sdi) { | |
264 | assert(_sampling_bar); | |
265 | ||
266 | _devices.push_back(sdi); | |
267 | _sampling_bar->set_device_list(_devices); | |
268 | _sampling_bar->set_selected_device(sdi); | |
269 | } | |
270 | } | |
271 | ||
272 | void MainWindow::on_actionQuit_triggered() | |
273 | { | |
274 | close(); | |
275 | } | |
276 | ||
277 | void MainWindow::on_actionViewZoomIn_triggered() | |
278 | { | |
279 | _view->zoom(1); | |
280 | } | |
281 | ||
282 | void MainWindow::on_actionViewZoomOut_triggered() | |
283 | { | |
284 | _view->zoom(-1); | |
285 | } | |
286 | ||
287 | void MainWindow::on_actionViewShowCursors_triggered() | |
288 | { | |
289 | assert(_view); | |
290 | _view->show_cursors(_action_view_show_cursors->isChecked()); | |
291 | } | |
292 | ||
293 | void MainWindow::on_actionAbout_triggered() | |
294 | { | |
295 | dialogs::About dlg(this); | |
296 | dlg.exec(); | |
297 | } | |
298 | ||
299 | void MainWindow::run_stop() | |
300 | { | |
301 | switch(_session.get_capture_state()) { | |
302 | case SigSession::Stopped: | |
303 | _session.start_capture( | |
304 | _sampling_bar->get_selected_device(), | |
305 | _sampling_bar->get_record_length(), | |
306 | boost::bind(&MainWindow::session_error, this, | |
307 | QString("Capture failed"), _1)); | |
308 | break; | |
309 | ||
310 | case SigSession::Running: | |
311 | _session.stop_capture(); | |
312 | break; | |
313 | } | |
314 | } | |
315 | ||
316 | void MainWindow::capture_state_changed(int state) | |
317 | { | |
318 | _sampling_bar->set_sampling(state != SigSession::Stopped); | |
319 | } | |
320 | ||
321 | } // namespace pv |