]>
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 | #include <sigrokdecode.h> | |
22 | ||
23 | #include <boost/bind.hpp> | |
24 | ||
25 | #include <QAction> | |
26 | #include <QApplication> | |
27 | #include <QButtonGroup> | |
28 | #include <QFileDialog> | |
29 | #include <QMessageBox> | |
30 | #include <QMenu> | |
31 | #include <QMenuBar> | |
32 | #include <QStatusBar> | |
33 | #include <QVBoxLayout> | |
34 | #include <QWidget> | |
35 | ||
36 | #include "mainwindow.h" | |
37 | #include "dialogs/about.h" | |
38 | #include "dialogs/connect.h" | |
39 | #include "toolbars/samplingbar.h" | |
40 | #include "view/view.h" | |
41 | ||
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> | |
49 | ||
50 | ||
51 | namespace pv { | |
52 | ||
53 | MainWindow::MainWindow(const char *open_file_name, | |
54 | QWidget *parent) : | |
55 | QMainWindow(parent) | |
56 | { | |
57 | setup_ui(); | |
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 | } | |
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 | ||
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 | ||
88 | // Setup the menu bar | |
89 | _menu_bar = new QMenuBar(this); | |
90 | _menu_bar->setGeometry(QRect(0, 0, 400, 25)); | |
91 | ||
92 | // File Menu | |
93 | _menu_file = new QMenu(_menu_bar); | |
94 | _menu_file->setTitle(QApplication::translate( | |
95 | "MainWindow", "&File", 0, QApplication::UnicodeUTF8)); | |
96 | ||
97 | _action_open = new QAction(this); | |
98 | _action_open->setText(QApplication::translate( | |
99 | "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8)); | |
100 | _action_open->setIcon(QIcon::fromTheme("document-open", | |
101 | QIcon(":/icons/document-open.png"))); | |
102 | _action_open->setObjectName(QString::fromUtf8("actionOpen")); | |
103 | _menu_file->addAction(_action_open); | |
104 | ||
105 | _menu_file->addSeparator(); | |
106 | ||
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 | ||
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 | ||
124 | // View Menu | |
125 | _menu_view = new QMenu(_menu_bar); | |
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")); | |
136 | _menu_view->addAction(_action_view_zoom_in); | |
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")); | |
145 | _menu_view->addAction(_action_view_zoom_out); | |
146 | ||
147 | _menu_view->addSeparator(); | |
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)); | |
156 | _menu_view->addAction(_action_view_show_cursors); | |
157 | ||
158 | // Help Menu | |
159 | _menu_help = new QMenu(_menu_bar); | |
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)); | |
167 | _menu_help->addAction(_action_about); | |
168 | ||
169 | _menu_bar->addAction(_menu_file->menuAction()); | |
170 | _menu_bar->addAction(_menu_view->menuAction()); | |
171 | _menu_bar->addAction(_menu_help->menuAction()); | |
172 | ||
173 | setMenuBar(_menu_bar); | |
174 | QMetaObject::connectSlotsByName(this); | |
175 | ||
176 | // Setup the toolbars | |
177 | _toolbar = new QToolBar(this); | |
178 | _toolbar->addAction(_action_open); | |
179 | _toolbar->addSeparator(); | |
180 | _toolbar->addAction(_action_view_zoom_in); | |
181 | _toolbar->addAction(_action_view_zoom_out); | |
182 | addToolBar(_toolbar); | |
183 | ||
184 | _sampling_bar = new toolbars::SamplingBar(this); | |
185 | scan_devices(); | |
186 | connect(_sampling_bar, SIGNAL(run_stop()), this, | |
187 | SLOT(run_stop())); | |
188 | addToolBar(_sampling_bar); | |
189 | ||
190 | setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0, | |
191 | QApplication::UnicodeUTF8)); | |
192 | ||
193 | // Setup _session events | |
194 | connect(&_session, SIGNAL(capture_state_changed(int)), this, | |
195 | SLOT(capture_state_changed(int))); | |
196 | ||
197 | } | |
198 | ||
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 | ||
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 | ||
224 | void MainWindow::load_file(QString file_name) | |
225 | { | |
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(); | |
243 | } | |
244 | ||
245 | void MainWindow::on_actionOpen_triggered() | |
246 | { | |
247 | const QString file_name = QFileDialog::getOpenFileName( | |
248 | this, tr("Open File"), "", | |
249 | tr("Sigrok Sessions (*.sr)")); | |
250 | load_file(file_name); | |
251 | } | |
252 | ||
253 | void MainWindow::on_actionConnect_triggered() | |
254 | { | |
255 | dialogs::Connect dlg(this); | |
256 | if (!dlg.exec()) | |
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 | } | |
267 | } | |
268 | ||
269 | void MainWindow::on_actionQuit_triggered() | |
270 | { | |
271 | close(); | |
272 | } | |
273 | ||
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 | ||
284 | void MainWindow::on_actionViewShowCursors_triggered() | |
285 | { | |
286 | assert(_view); | |
287 | _view->show_cursors(_action_view_show_cursors->isChecked()); | |
288 | } | |
289 | ||
290 | void MainWindow::on_actionAbout_triggered() | |
291 | { | |
292 | dialogs::About dlg(this); | |
293 | dlg.exec(); | |
294 | } | |
295 | ||
296 | void MainWindow::run_stop() | |
297 | { | |
298 | switch(_session.get_capture_state()) { | |
299 | case SigSession::Stopped: | |
300 | _session.start_capture( | |
301 | _sampling_bar->get_selected_device(), | |
302 | _sampling_bar->get_record_length(), | |
303 | boost::bind(&MainWindow::session_error, this, | |
304 | QString("Capture failed"), _1)); | |
305 | break; | |
306 | ||
307 | case SigSession::Running: | |
308 | _session.stop_capture(); | |
309 | break; | |
310 | } | |
311 | } | |
312 | ||
313 | void MainWindow::capture_state_changed(int state) | |
314 | { | |
315 | _sampling_bar->set_sampling(state != SigSession::Stopped); | |
316 | } | |
317 | ||
318 | } // namespace pv |