Deleted unused pv::view::Viewport::mouseReleaseEvent handler
[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 #include <sigrokdecode.h>
22
23 #include <QAction>
24 #include <QApplication>
25 #include <QButtonGroup>
26 #include <QFileDialog>
27 #include <QMenu>
28 #include <QMenuBar>
29 #include <QStatusBar>
30 #include <QVBoxLayout>
31 #include <QWidget>
32
33 #include "mainwindow.h"
34 #include "samplingbar.h"
35 #include "dialogs/about.h"
36 #include "dialogs/connect.h"
37 #include "view/view.h"
38
39 /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
40 #define __STDC_FORMAT_MACROS
41 #include <inttypes.h>
42 #include <stdint.h>
43 #include <stdarg.h>
44 #include <glib.h>
45 #include <libsigrok/libsigrok.h>
46
47
48 namespace pv {
49
50 MainWindow::MainWindow(const char *open_file_name,
51         QWidget *parent) :
52         QMainWindow(parent)
53 {
54         setup_ui();
55         if (open_file_name) {
56                 const QString s(QString::fromUtf8(open_file_name));
57                 QMetaObject::invokeMethod(this, "load_file",
58                         Qt::QueuedConnection,
59                         Q_ARG(QString, s));
60         }
61 }
62
63 void MainWindow::setup_ui()
64 {
65         setObjectName(QString::fromUtf8("MainWindow"));
66
67         resize(1024, 768);
68
69         // Set the window icon
70         QIcon icon;
71         icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
72                 QSize(), QIcon::Normal, QIcon::Off);
73         setWindowIcon(icon);
74
75         // Setup the central widget
76         _central_widget = new QWidget(this);
77         _vertical_layout = new QVBoxLayout(_central_widget);
78         _vertical_layout->setSpacing(6);
79         _vertical_layout->setContentsMargins(0, 0, 0, 0);
80         setCentralWidget(_central_widget);
81
82         _view = new pv::view::View(_session, this);
83         _vertical_layout->addWidget(_view);
84
85         // Setup the menu bar
86         _menu_bar = new QMenuBar(this);
87         _menu_bar->setGeometry(QRect(0, 0, 400, 25));
88
89         // File Menu
90         _menu_file = new QMenu(_menu_bar);
91         _menu_file->setTitle(QApplication::translate(
92                 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
93
94         _action_open = new QAction(this);
95         _action_open->setText(QApplication::translate(
96                 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
97         _action_open->setIcon(QIcon::fromTheme("document-open",
98                 QIcon(":/icons/document-open.png")));
99         _action_open->setObjectName(QString::fromUtf8("actionOpen"));
100         _menu_file->addAction(_action_open);
101
102         _menu_file->addSeparator();
103
104         _action_connect = new QAction(this);
105         _action_connect->setText(QApplication::translate(
106                 "MainWindow", "&Connect to Device...", 0,
107                 QApplication::UnicodeUTF8));
108         _action_connect->setObjectName(QString::fromUtf8("actionConnect"));
109         _menu_file->addAction(_action_connect);
110
111         _menu_file->addSeparator();
112
113         _action_quit = new QAction(this);
114         _action_quit->setText(QApplication::translate(
115                 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
116         _action_quit->setIcon(QIcon::fromTheme("application-exit",
117                 QIcon(":/icons/application-exit.png")));
118         _action_quit->setObjectName(QString::fromUtf8("actionQuit"));
119         _menu_file->addAction(_action_quit);
120
121         // View Menu
122         _menu_view = new QMenu(_menu_bar);
123         _menu_view->setTitle(QApplication::translate(
124                 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
125
126         _action_view_zoom_in = new QAction(this);
127         _action_view_zoom_in->setText(QApplication::translate(
128                 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
129         _action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
130                 QIcon(":/icons/zoom-in.png")));
131         _action_view_zoom_in->setObjectName(
132                 QString::fromUtf8("actionViewZoomIn"));
133         _menu_view->addAction(_action_view_zoom_in);
134
135         _action_view_zoom_out = new QAction(this);
136         _action_view_zoom_out->setText(QApplication::translate(
137                 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
138         _action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
139                 QIcon(":/icons/zoom-out.png")));
140         _action_view_zoom_out->setObjectName(
141                 QString::fromUtf8("actionViewZoomOut"));
142         _menu_view->addAction(_action_view_zoom_out);
143
144         _menu_view->addSeparator();
145
146         _action_view_show_cursors = new QAction(this);
147         _action_view_show_cursors->setCheckable(true);
148         _action_view_show_cursors->setChecked(_view->cursors_shown());
149         _action_view_show_cursors->setObjectName(
150                 QString::fromUtf8("actionViewShowCursors"));
151         _action_view_show_cursors->setText(QApplication::translate(
152                 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
153         _menu_view->addAction(_action_view_show_cursors);
154
155         // Help Menu
156         _menu_help = new QMenu(_menu_bar);
157         _menu_help->setTitle(QApplication::translate(
158                 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
159
160         _action_about = new QAction(this);
161         _action_about->setObjectName(QString::fromUtf8("actionAbout"));
162         _action_about->setText(QApplication::translate(
163                 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
164         _menu_help->addAction(_action_about);
165
166         _menu_bar->addAction(_menu_file->menuAction());
167         _menu_bar->addAction(_menu_view->menuAction());
168         _menu_bar->addAction(_menu_help->menuAction());
169
170         setMenuBar(_menu_bar);
171         QMetaObject::connectSlotsByName(this);
172
173         // Setup the toolbars
174         _toolbar = new QToolBar(this);
175         _toolbar->addAction(_action_open);
176         _toolbar->addSeparator();
177         _toolbar->addAction(_action_view_zoom_in);
178         _toolbar->addAction(_action_view_zoom_out);
179         addToolBar(_toolbar);
180
181         _sampling_bar = new SamplingBar(this);
182         scan_devices();
183         connect(_sampling_bar, SIGNAL(run_stop()), this,
184                 SLOT(run_stop()));
185         addToolBar(_sampling_bar);
186
187         setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
188                 QApplication::UnicodeUTF8));
189
190         // Setup _session events
191         connect(&_session, SIGNAL(capture_state_changed(int)), this,
192                 SLOT(capture_state_changed(int)));
193
194 }
195
196 void MainWindow::scan_devices()
197 {
198         _devices.clear();
199
200         /* Scan all drivers for all devices. */
201         struct sr_dev_driver **const drivers = sr_driver_list();
202         for (struct sr_dev_driver **driver = drivers; *driver; driver++) {
203                 GSList *const devices = sr_driver_scan(*driver, NULL);
204                 for (GSList *l = devices; l; l = l->next)
205                         _devices.push_back((sr_dev_inst*)l->data);
206                 g_slist_free(devices);
207         }
208
209         assert(_sampling_bar);
210         _sampling_bar->set_device_list(_devices);
211 }
212
213 void MainWindow::load_file(QString file_name)
214 {
215         _session.load_file(file_name.toStdString());
216 }
217
218 void MainWindow::on_actionOpen_triggered()
219 {
220         const QString file_name = QFileDialog::getOpenFileName(
221                 this, tr("Open File"), "",
222                 tr("Sigrok Sessions (*.sr)"));
223         load_file(file_name);
224 }
225
226 void MainWindow::on_actionConnect_triggered()
227 {
228         dialogs::Connect dlg(this);
229         if (!dlg.exec())
230                 return;
231
232         struct sr_dev_inst *const sdi = dlg.get_selected_device();
233         if (sdi) {
234                 assert(_sampling_bar);
235
236                 _devices.push_back(sdi);
237                 _sampling_bar->set_device_list(_devices);
238                 _sampling_bar->set_selected_device(sdi);
239         }
240 }
241
242 void MainWindow::on_actionQuit_triggered()
243 {
244         close();
245 }
246
247 void MainWindow::on_actionViewZoomIn_triggered()
248 {
249         _view->zoom(1);
250 }
251
252 void MainWindow::on_actionViewZoomOut_triggered()
253 {
254         _view->zoom(-1);
255 }
256
257 void MainWindow::on_actionViewShowCursors_triggered()
258 {
259         assert(_view);
260         _view->show_cursors(_action_view_show_cursors->isChecked());
261 }
262
263 void MainWindow::on_actionAbout_triggered()
264 {
265         dialogs::About dlg(this);
266         dlg.exec();
267 }
268
269 void MainWindow::run_stop()
270 {
271         switch(_session.get_capture_state()) {
272         case SigSession::Stopped:
273                 _session.start_capture(
274                         _sampling_bar->get_selected_device(),
275                         _sampling_bar->get_record_length());
276                 break;
277
278         case SigSession::Running:
279                 _session.stop_capture();
280                 break;
281         }
282 }
283
284 void MainWindow::capture_state_changed(int state)
285 {
286         _sampling_bar->set_sampling(state != SigSession::Stopped);
287 }
288
289 } // namespace pv