]> sigrok.org Git - pulseview.git/blob - pv/mainwindow.cpp
Put context bar back on the same row as the other toolbars
[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
220         // Set the title
221         setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
222                 QApplication::UnicodeUTF8));
223
224         // Setup _session events
225         connect(&_session, SIGNAL(capture_state_changed(int)), this,
226                 SLOT(capture_state_changed(int)));
227
228 }
229
230 void MainWindow::session_error(
231         const QString text, const QString info_text)
232 {
233         QMetaObject::invokeMethod(this, "show_session_error",
234                 Qt::QueuedConnection, Q_ARG(QString, text),
235                 Q_ARG(QString, info_text));
236 }
237
238 void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
239 {
240         assert(_sampling_bar);
241
242         const list<sr_dev_inst*> &devices = _device_manager.devices();
243         _sampling_bar->set_device_list(devices);
244
245         if (!selected_device && !devices.empty()) {
246                 // Fall back to the first device in the list.
247                 selected_device = devices.front();
248
249                 // Try and find the demo device and select that by default
250                 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
251                         if (strcmp(sdi->driver->name, "demo") == 0) {
252                                 selected_device = sdi;
253                         }
254         }
255
256         if (selected_device) {
257                 _sampling_bar->set_selected_device(selected_device);
258                 _session.set_device(selected_device);
259         }
260 }
261
262 void MainWindow::load_file(QString file_name)
263 {
264         const QString errorMessage(
265                 QString("Failed to load file %1").arg(file_name));
266         const QString infoMessage;
267         _session.load_file(file_name.toStdString(),
268                 boost::bind(&MainWindow::session_error, this,
269                         errorMessage, infoMessage));
270 }
271
272 void MainWindow::show_session_error(
273         const QString text, const QString info_text)
274 {
275         QMessageBox msg(this);
276         msg.setText(text);
277         msg.setInformativeText(info_text);
278         msg.setStandardButtons(QMessageBox::Ok);
279         msg.setIcon(QMessageBox::Warning);
280         msg.exec();
281 }
282
283 void MainWindow::on_actionOpen_triggered()
284 {
285         // Enumerate the file formats
286         QString filters(tr("Sigrok Sessions (*.sr)"));
287         filters.append(tr(";;All Files (*.*)"));
288
289         // Show the dialog
290         const QString file_name = QFileDialog::getOpenFileName(
291                 this, tr("Open File"), "", filters);
292         if (!file_name.isEmpty())
293                 load_file(file_name);
294 }
295
296 void MainWindow::on_actionConnect_triggered()
297 {
298         // Stop any currently running capture session
299         _session.stop_capture();
300
301         dialogs::Connect dlg(this, _device_manager);
302
303         // If the user selected a device, select it in the device list. Select the
304         // current device otherwise.
305         struct sr_dev_inst *const sdi = dlg.exec() ?
306                 dlg.get_selected_device() : _session.get_device();
307
308         update_device_list(sdi);
309 }
310
311 void MainWindow::on_actionQuit_triggered()
312 {
313         close();
314 }
315
316 void MainWindow::on_actionViewZoomIn_triggered()
317 {
318         _view->zoom(1);
319 }
320
321 void MainWindow::on_actionViewZoomOut_triggered()
322 {
323         _view->zoom(-1);
324 }
325
326 void MainWindow::on_actionViewShowCursors_triggered()
327 {
328         assert(_view);
329
330         const bool show = !_view->cursors_shown();
331         if(show)
332                 _view->centre_cursors();
333
334         _view->show_cursors(show);
335 }
336
337 void MainWindow::on_actionAbout_triggered()
338 {
339         dialogs::About dlg(this);
340         dlg.exec();
341 }
342
343 void MainWindow::device_selected()
344 {
345         _session.set_device(_sampling_bar->get_selected_device());
346 }
347
348 void MainWindow::run_stop()
349 {
350         switch(_session.get_capture_state()) {
351         case SigSession::Stopped:
352                 _session.start_capture(_sampling_bar->get_record_length(),
353                         boost::bind(&MainWindow::session_error, this,
354                                 QString("Capture failed"), _1));
355                 break;
356
357         case SigSession::AwaitingTrigger:
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_capture_state((pv::SigSession::capture_state)state);
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