2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
22 #include <libsigrokdecode/libsigrokdecode.h>
25 #include <boost/bind.hpp>
26 #include <boost/foreach.hpp>
32 #include <QApplication>
33 #include <QButtonGroup>
34 #include <QFileDialog>
35 #include <QMessageBox>
39 #include <QVBoxLayout>
42 #include "mainwindow.h"
44 #include "devicemanager.h"
45 #include "device/device.h"
46 #include "dialogs/about.h"
47 #include "dialogs/connect.h"
48 #include "dialogs/storeprogress.h"
49 #include "toolbars/samplingbar.h"
50 #include "view/logicsignal.h"
51 #include "view/view.h"
53 #include "widgets/decodermenu.h"
56 /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
57 #define __STDC_FORMAT_MACROS
62 #include <libsigrok/libsigrok.h>
64 using boost::shared_ptr;
73 MainWindow::MainWindow(DeviceManager &device_manager,
74 const char *open_file_name,
77 _device_manager(device_manager),
78 _session(device_manager)
82 const QString s(QString::fromUtf8(open_file_name));
83 QMetaObject::invokeMethod(this, "load_file",
89 void MainWindow::setup_ui()
91 setObjectName(QString::fromUtf8("MainWindow"));
95 // Set the window icon
97 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
98 QSize(), QIcon::Normal, QIcon::Off);
101 // Setup the central widget
102 _central_widget = new QWidget(this);
103 _vertical_layout = new QVBoxLayout(_central_widget);
104 _vertical_layout->setSpacing(6);
105 _vertical_layout->setContentsMargins(0, 0, 0, 0);
106 setCentralWidget(_central_widget);
108 _view = new pv::view::View(_session, this);
110 _vertical_layout->addWidget(_view);
112 // Setup the menu bar
113 QMenuBar *const menu_bar = new QMenuBar(this);
114 menu_bar->setGeometry(QRect(0, 0, 400, 25));
117 QMenu *const menu_file = new QMenu;
118 menu_file->setTitle(QApplication::translate(
119 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
121 QAction *const action_open = new QAction(this);
122 action_open->setText(QApplication::translate(
123 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
124 action_open->setIcon(QIcon::fromTheme("document-open",
125 QIcon(":/icons/document-open.png")));
126 action_open->setObjectName(QString::fromUtf8("actionOpen"));
127 menu_file->addAction(action_open);
129 QAction *const action_save_as = new QAction(this);
130 action_save_as->setText(QApplication::translate(
131 "MainWindow", "&Save As...", 0, QApplication::UnicodeUTF8));
132 action_save_as->setIcon(QIcon::fromTheme("document-save-as",
133 QIcon(":/icons/document-save-as.png")));
134 action_save_as->setObjectName(QString::fromUtf8("actionSaveAs"));
135 menu_file->addAction(action_save_as);
137 menu_file->addSeparator();
139 QAction *const action_connect = new QAction(this);
140 action_connect->setText(QApplication::translate(
141 "MainWindow", "&Connect to Device...", 0,
142 QApplication::UnicodeUTF8));
143 action_connect->setObjectName(QString::fromUtf8("actionConnect"));
144 menu_file->addAction(action_connect);
146 menu_file->addSeparator();
148 QAction *action_quit = new QAction(this);
149 action_quit->setText(QApplication::translate(
150 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
151 action_quit->setIcon(QIcon::fromTheme("application-exit",
152 QIcon(":/icons/application-exit.png")));
153 action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
154 action_quit->setObjectName(QString::fromUtf8("actionQuit"));
155 menu_file->addAction(action_quit);
158 QMenu *menu_view = new QMenu;
159 menu_view->setTitle(QApplication::translate(
160 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
162 QAction *const action_view_zoom_in = new QAction(this);
163 action_view_zoom_in->setText(QApplication::translate(
164 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
165 action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
166 QIcon(":/icons/zoom-in.png")));
167 // simply using Qt::Key_Plus shows no + in the menu
168 action_view_zoom_in->setShortcut(QKeySequence::ZoomIn);
169 action_view_zoom_in->setObjectName(
170 QString::fromUtf8("actionViewZoomIn"));
171 menu_view->addAction(action_view_zoom_in);
173 QAction *const action_view_zoom_out = new QAction(this);
174 action_view_zoom_out->setText(QApplication::translate(
175 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
176 action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
177 QIcon(":/icons/zoom-out.png")));
178 action_view_zoom_out->setShortcut(QKeySequence::ZoomOut);
179 action_view_zoom_out->setObjectName(
180 QString::fromUtf8("actionViewZoomOut"));
181 menu_view->addAction(action_view_zoom_out);
183 QAction *const action_view_zoom_fit = new QAction(this);
184 action_view_zoom_fit->setText(QApplication::translate(
185 "MainWindow", "Zoom to &Fit", 0, QApplication::UnicodeUTF8));
186 action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit",
187 QIcon(":/icons/zoom-fit.png")));
188 action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F));
189 action_view_zoom_fit->setObjectName(
190 QString::fromUtf8("actionViewZoomFit"));
191 menu_view->addAction(action_view_zoom_fit);
193 QAction *const action_view_zoom_one_to_one = new QAction(this);
194 action_view_zoom_one_to_one->setText(QApplication::translate(
195 "MainWindow", "Zoom to &One-to-One", 0,
196 QApplication::UnicodeUTF8));
197 action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original",
198 QIcon(":/icons/zoom-original.png")));
199 action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O));
200 action_view_zoom_one_to_one->setObjectName(
201 QString::fromUtf8("actionViewZoomOneToOne"));
202 menu_view->addAction(action_view_zoom_one_to_one);
204 menu_view->addSeparator();
206 QAction *action_view_show_cursors = new QAction(this);
207 action_view_show_cursors->setCheckable(true);
208 action_view_show_cursors->setChecked(_view->cursors_shown());
209 action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
210 action_view_show_cursors->setObjectName(
211 QString::fromUtf8("actionViewShowCursors"));
212 action_view_show_cursors->setText(QApplication::translate(
213 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
214 menu_view->addAction(action_view_show_cursors);
218 QMenu *const menu_decoders = new QMenu;
219 menu_decoders->setTitle(QApplication::translate(
220 "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8));
222 pv::widgets::DecoderMenu *const menu_decoders_add =
223 new pv::widgets::DecoderMenu(menu_decoders);
224 menu_decoders_add->setTitle(QApplication::translate(
225 "MainWindow", "&Add", 0, QApplication::UnicodeUTF8));
226 connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
227 this, SLOT(add_decoder(srd_decoder*)));
229 menu_decoders->addMenu(menu_decoders_add);
233 QMenu *const menu_help = new QMenu;
234 menu_help->setTitle(QApplication::translate(
235 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
237 QAction *const action_about = new QAction(this);
238 action_about->setObjectName(QString::fromUtf8("actionAbout"));
239 action_about->setText(QApplication::translate(
240 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
241 menu_help->addAction(action_about);
243 menu_bar->addAction(menu_file->menuAction());
244 menu_bar->addAction(menu_view->menuAction());
246 menu_bar->addAction(menu_decoders->menuAction());
248 menu_bar->addAction(menu_help->menuAction());
250 setMenuBar(menu_bar);
251 QMetaObject::connectSlotsByName(this);
254 QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this);
255 toolbar->addAction(action_open);
256 toolbar->addSeparator();
257 toolbar->addAction(action_view_zoom_in);
258 toolbar->addAction(action_view_zoom_out);
259 toolbar->addAction(action_view_zoom_fit);
262 // Setup the sampling bar
263 _sampling_bar = new toolbars::SamplingBar(_session, this);
265 // Populate the device list and select the initially selected device
266 update_device_list();
268 connect(_sampling_bar, SIGNAL(run_stop()), this,
270 addToolBar(_sampling_bar);
273 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
274 QApplication::UnicodeUTF8));
276 // Setup _session events
277 connect(&_session, SIGNAL(capture_state_changed(int)), this,
278 SLOT(capture_state_changed(int)));
282 void MainWindow::session_error(
283 const QString text, const QString info_text)
285 QMetaObject::invokeMethod(this, "show_session_error",
286 Qt::QueuedConnection, Q_ARG(QString, text),
287 Q_ARG(QString, info_text));
290 void MainWindow::update_device_list(
291 shared_ptr<pv::device::DevInst> selected_device)
293 assert(_sampling_bar);
295 list< shared_ptr<device::DevInst> > devices;
296 std::copy(_device_manager.devices().begin(),
297 _device_manager.devices().end(), std::back_inserter(devices));
299 _sampling_bar->set_device_list(devices);
301 if (!selected_device && !devices.empty()) {
302 // Fall back to the first device in the list.
303 selected_device = devices.front();
305 // Try and find the demo device and select that by default
306 BOOST_FOREACH (shared_ptr<pv::device::DevInst> dev_inst, devices)
307 if (strcmp(dev_inst->dev_inst()->driver->name,
309 selected_device = dev_inst;
313 if (selected_device) {
314 _sampling_bar->set_selected_device(selected_device);
315 _session.set_device(selected_device);
319 void MainWindow::load_file(QString file_name)
321 const QString errorMessage(
322 QString("Failed to load file %1").arg(file_name));
323 const QString infoMessage;
324 _session.load_file(file_name.toStdString(),
325 boost::bind(&MainWindow::session_error, this,
326 errorMessage, infoMessage));
329 void MainWindow::show_session_error(
330 const QString text, const QString info_text)
332 QMessageBox msg(this);
334 msg.setInformativeText(info_text);
335 msg.setStandardButtons(QMessageBox::Ok);
336 msg.setIcon(QMessageBox::Warning);
340 void MainWindow::on_actionOpen_triggered()
343 const QString file_name = QFileDialog::getOpenFileName(
344 this, tr("Open File"), "", tr(
345 "Sigrok Sessions (*.sr);;"
347 if (!file_name.isEmpty())
348 load_file(file_name);
351 void MainWindow::on_actionSaveAs_triggered()
353 using pv::dialogs::StoreProgress;
355 // Stop any currently running capture session
356 _session.stop_capture();
359 const QString file_name = QFileDialog::getSaveFileName(
360 this, tr("Save File"), "", tr("Sigrok Sessions (*.sr)"));
362 if (file_name.isEmpty())
365 StoreProgress *dlg = new StoreProgress(file_name, _session, this);
369 void MainWindow::on_actionConnect_triggered()
371 // Stop any currently running capture session
372 _session.stop_capture();
374 dialogs::Connect dlg(this, _device_manager);
376 // If the user selected a device, select it in the device list. Select the
377 // current device otherwise.
378 shared_ptr<device::DevInst> dev_inst = dlg.exec() ?
379 dlg.get_selected_device() : _session.get_device();
381 update_device_list(dev_inst);
384 void MainWindow::on_actionQuit_triggered()
389 void MainWindow::on_actionViewZoomIn_triggered()
394 void MainWindow::on_actionViewZoomOut_triggered()
399 void MainWindow::on_actionViewZoomFit_triggered()
404 void MainWindow::on_actionViewZoomOneToOne_triggered()
406 _view->zoom_one_to_one();
409 void MainWindow::on_actionViewShowCursors_triggered()
413 const bool show = !_view->cursors_shown();
415 _view->centre_cursors();
417 _view->show_cursors(show);
420 void MainWindow::on_actionAbout_triggered()
422 dialogs::About dlg(this);
426 void MainWindow::add_decoder(srd_decoder *decoder)
430 _session.add_decoder(decoder);
436 void MainWindow::run_stop()
438 switch(_session.get_capture_state()) {
439 case SigSession::Stopped:
440 _session.start_capture(
441 boost::bind(&MainWindow::session_error, this,
442 QString("Capture failed"), _1));
445 case SigSession::AwaitingTrigger:
446 case SigSession::Running:
447 _session.stop_capture();
452 void MainWindow::capture_state_changed(int state)
454 _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);