]> sigrok.org Git - pulseview.git/blob - pv/mainwindow.cpp
Initial working context bar
[pulseview.git] / pv / mainwindow.cpp
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 <libsigrokdecode/libsigrokdecode.h>
23 #endif
24
25 #include <boost/bind.hpp>
26 #include <boost/foreach.hpp>
27
28 #include <QAction>
29 #include <QApplication>
30 #include <QButtonGroup>
31 #include <QFileDialog>
32 #include <QMessageBox>
33 #include <QMenu>
34 #include <QMenuBar>
35 #include <QStatusBar>
36 #include <QVBoxLayout>
37 #include <QWidget>
38
39 #include "mainwindow.h"
40
41 #include "devicemanager.h"
42 #include "dialogs/about.h"
43 #include "dialogs/connect.h"
44 #include "toolbars/contextbar.h"
45 #include "toolbars/samplingbar.h"
46 #include "view/view.h"
47
48 /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
49 #define __STDC_FORMAT_MACROS
50 #include <inttypes.h>
51 #include <stdint.h>
52 #include <stdarg.h>
53 #include <glib.h>
54 #include <libsigrok/libsigrok.h>
55
56 using namespace boost;
57 using namespace std;
58
59 namespace pv {
60
61 namespace view {
62 class SelectableItem;
63 }
64
65 MainWindow::MainWindow(DeviceManager &device_manager,
66         const char *open_file_name,
67         QWidget *parent) :
68         QMainWindow(parent),
69         _device_manager(device_manager),
70         _session(device_manager)
71 {
72         setup_ui();
73         if (open_file_name) {
74                 const QString s(QString::fromUtf8(open_file_name));
75                 QMetaObject::invokeMethod(this, "load_file",
76                         Qt::QueuedConnection,
77                         Q_ARG(QString, s));
78         }
79 }
80
81 void MainWindow::setup_ui()
82 {
83         setObjectName(QString::fromUtf8("MainWindow"));
84
85         resize(1024, 768);
86
87         // Set the window icon
88         QIcon icon;
89         icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
90                 QSize(), QIcon::Normal, QIcon::Off);
91         setWindowIcon(icon);
92
93         // Setup the central widget
94         _central_widget = new QWidget(this);
95         _vertical_layout = new QVBoxLayout(_central_widget);
96         _vertical_layout->setSpacing(6);
97         _vertical_layout->setContentsMargins(0, 0, 0, 0);
98         setCentralWidget(_central_widget);
99
100         _view = new pv::view::View(_session, this);
101         connect(_view, SIGNAL(selection_changed()), this,
102                 SLOT(view_selection_changed()));
103
104         _vertical_layout->addWidget(_view);
105
106         // Setup the menu bar
107         _menu_bar = new QMenuBar(this);
108         _menu_bar->setGeometry(QRect(0, 0, 400, 25));
109
110         // File Menu
111         _menu_file = new QMenu(_menu_bar);
112         _menu_file->setTitle(QApplication::translate(
113                 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
114
115         _action_open = new QAction(this);
116         _action_open->setText(QApplication::translate(
117                 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
118         _action_open->setIcon(QIcon::fromTheme("document-open",
119                 QIcon(":/icons/document-open.png")));
120         _action_open->setObjectName(QString::fromUtf8("actionOpen"));
121         _menu_file->addAction(_action_open);
122
123         _menu_file->addSeparator();
124
125         _action_connect = new QAction(this);
126         _action_connect->setText(QApplication::translate(
127                 "MainWindow", "&Connect to Device...", 0,
128                 QApplication::UnicodeUTF8));
129         _action_connect->setObjectName(QString::fromUtf8("actionConnect"));
130         _menu_file->addAction(_action_connect);
131
132         _menu_file->addSeparator();
133
134         _action_quit = new QAction(this);
135         _action_quit->setText(QApplication::translate(
136                 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
137         _action_quit->setIcon(QIcon::fromTheme("application-exit",
138                 QIcon(":/icons/application-exit.png")));
139         _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
140         _action_quit->setObjectName(QString::fromUtf8("actionQuit"));
141         _menu_file->addAction(_action_quit);
142
143         // View Menu
144         _menu_view = new QMenu(_menu_bar);
145         _menu_view->setTitle(QApplication::translate(
146                 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
147
148         _action_view_zoom_in = new QAction(this);
149         _action_view_zoom_in->setText(QApplication::translate(
150                 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
151         _action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
152                 QIcon(":/icons/zoom-in.png")));
153         _action_view_zoom_in->setObjectName(
154                 QString::fromUtf8("actionViewZoomIn"));
155         _menu_view->addAction(_action_view_zoom_in);
156
157         _action_view_zoom_out = new QAction(this);
158         _action_view_zoom_out->setText(QApplication::translate(
159                 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
160         _action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
161                 QIcon(":/icons/zoom-out.png")));
162         _action_view_zoom_out->setObjectName(
163                 QString::fromUtf8("actionViewZoomOut"));
164         _menu_view->addAction(_action_view_zoom_out);
165
166         _menu_view->addSeparator();
167
168         _action_view_show_cursors = new QAction(this);
169         _action_view_show_cursors->setCheckable(true);
170         _action_view_show_cursors->setChecked(_view->cursors_shown());
171         _action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
172         _action_view_show_cursors->setObjectName(
173                 QString::fromUtf8("actionViewShowCursors"));
174         _action_view_show_cursors->setText(QApplication::translate(
175                 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
176         _menu_view->addAction(_action_view_show_cursors);
177
178         // Help Menu
179         _menu_help = new QMenu(_menu_bar);
180         _menu_help->setTitle(QApplication::translate(
181                 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
182
183         _action_about = new QAction(this);
184         _action_about->setObjectName(QString::fromUtf8("actionAbout"));
185         _action_about->setText(QApplication::translate(
186                 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
187         _menu_help->addAction(_action_about);
188
189         _menu_bar->addAction(_menu_file->menuAction());
190         _menu_bar->addAction(_menu_view->menuAction());
191         _menu_bar->addAction(_menu_help->menuAction());
192
193         setMenuBar(_menu_bar);
194         QMetaObject::connectSlotsByName(this);
195
196         // Setup the toolbar
197         _toolbar = new QToolBar(tr("Main Toolbar"), this);
198         _toolbar->addAction(_action_open);
199         _toolbar->addSeparator();
200         _toolbar->addAction(_action_view_zoom_in);
201         _toolbar->addAction(_action_view_zoom_out);
202         addToolBar(_toolbar);
203
204         // Setup the sampling bar
205         _sampling_bar = new toolbars::SamplingBar(this);
206
207         // Populate the device list and select the initially selected device
208         update_device_list();
209
210         connect(_sampling_bar, SIGNAL(device_selected()), this,
211                 SLOT(device_selected()));
212         connect(_sampling_bar, SIGNAL(run_stop()), this,
213                 SLOT(run_stop()));
214         addToolBar(_sampling_bar);
215
216         // Setup the context bar
217         _context_bar = new toolbars::ContextBar(this);
218         addToolBar(_context_bar);
219         insertToolBarBreak(_context_bar);
220
221         // Set the title
222         setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
223                 QApplication::UnicodeUTF8));
224
225         // Setup _session events
226         connect(&_session, SIGNAL(capture_state_changed(int)), this,
227                 SLOT(capture_state_changed(int)));
228
229 }
230
231 void MainWindow::session_error(
232         const QString text, const QString info_text)
233 {
234         QMetaObject::invokeMethod(this, "show_session_error",
235                 Qt::QueuedConnection, Q_ARG(QString, text),
236                 Q_ARG(QString, info_text));
237 }
238
239 void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
240 {
241         assert(_sampling_bar);
242
243         const list<sr_dev_inst*> &devices = _device_manager.devices();
244         _sampling_bar->set_device_list(devices);
245
246         if (!selected_device && !devices.empty()) {
247                 // Fall back to the first device in the list.
248                 selected_device = devices.front();
249
250                 // Try and find the demo device and select that by default
251                 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
252                         if (strcmp(sdi->driver->name, "demo") == 0) {
253                                 selected_device = sdi;
254                         }
255         }
256
257         if (selected_device) {
258                 _sampling_bar->set_selected_device(selected_device);
259                 _session.set_device(selected_device);
260         }
261 }
262
263 void MainWindow::load_file(QString file_name)
264 {
265         const QString errorMessage(
266                 QString("Failed to load file %1").arg(file_name));
267         const QString infoMessage;
268         _session.load_file(file_name.toStdString(),
269                 boost::bind(&MainWindow::session_error, this,
270                         errorMessage, infoMessage));
271 }
272
273 void MainWindow::show_session_error(
274         const QString text, const QString info_text)
275 {
276         QMessageBox msg(this);
277         msg.setText(text);
278         msg.setInformativeText(info_text);
279         msg.setStandardButtons(QMessageBox::Ok);
280         msg.setIcon(QMessageBox::Warning);
281         msg.exec();
282 }
283
284 void MainWindow::on_actionOpen_triggered()
285 {
286         // Enumerate the file formats
287         QString filters(tr("Sigrok Sessions (*.sr)"));
288         filters.append(tr(";;All Files (*.*)"));
289
290         // Show the dialog
291         const QString file_name = QFileDialog::getOpenFileName(
292                 this, tr("Open File"), "", filters);
293         if (!file_name.isEmpty())
294                 load_file(file_name);
295 }
296
297 void MainWindow::on_actionConnect_triggered()
298 {
299         // Stop any currently running capture session
300         _session.stop_capture();
301
302         dialogs::Connect dlg(this, _device_manager);
303
304         // If the user selected a device, select it in the device list. Select the
305         // current device otherwise.
306         struct sr_dev_inst *const sdi = dlg.exec() ?
307                 dlg.get_selected_device() : _session.get_device();
308
309         update_device_list(sdi);
310 }
311
312 void MainWindow::on_actionQuit_triggered()
313 {
314         close();
315 }
316
317 void MainWindow::on_actionViewZoomIn_triggered()
318 {
319         _view->zoom(1);
320 }
321
322 void MainWindow::on_actionViewZoomOut_triggered()
323 {
324         _view->zoom(-1);
325 }
326
327 void MainWindow::on_actionViewShowCursors_triggered()
328 {
329         assert(_view);
330
331         const bool show = !_view->cursors_shown();
332         if(show)
333                 _view->centre_cursors();
334
335         _view->show_cursors(show);
336 }
337
338 void MainWindow::on_actionAbout_triggered()
339 {
340         dialogs::About dlg(this);
341         dlg.exec();
342 }
343
344 void MainWindow::device_selected()
345 {
346         _session.set_device(_sampling_bar->get_selected_device());
347 }
348
349 void MainWindow::run_stop()
350 {
351         switch(_session.get_capture_state()) {
352         case SigSession::Stopped:
353                 _session.start_capture(_sampling_bar->get_record_length(),
354                         boost::bind(&MainWindow::session_error, this,
355                                 QString("Capture failed"), _1));
356                 break;
357
358         case SigSession::Running:
359                 _session.stop_capture();
360                 break;
361         }
362 }
363
364 void MainWindow::capture_state_changed(int state)
365 {
366         _sampling_bar->set_sampling(state != SigSession::Stopped);
367 }
368
369 void MainWindow::view_selection_changed()
370 {
371         assert(_context_bar);
372
373         const list<weak_ptr<pv::view::SelectableItem> > items(
374                 _view->selected_items());
375         _context_bar->set_selected_items(items);
376 }
377
378 } // namespace pv