]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
Make the first view own the toolbar instead of the main window
[pulseview.git] / pv / mainwindow.cpp
CommitLineData
d7bed479 1/*
b3f22de0 2 * This file is part of the PulseView project.
d7bed479
JH
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
943edd76
MC
21#include <cassert>
22
269528f5 23#ifdef ENABLE_DECODE
aaabd61b 24#include <libsigrokdecode/libsigrokdecode.h>
269528f5 25#endif
30236a54 26
85843b14
JH
27#include <algorithm>
28#include <iterator>
29
7d5425ef
JH
30#include <QAction>
31#include <QApplication>
93f683ad 32#include <QCloseEvent>
0f8f8c18 33#include <QDockWidget>
643f65f9 34#include <QSettings>
7d5425ef 35#include <QWidget>
2953961c 36
2acdb232 37#include "mainwindow.hpp"
107ca6d3 38
2acdb232 39#include "devicemanager.hpp"
d2fc6be9 40#include "util.hpp"
2acdb232 41#include "dialogs/about.hpp"
7c657094 42#include "toolbars/mainbar.hpp"
2acdb232 43#include "view/view.hpp"
d7bed479 44
30236a54
JH
45#include <stdint.h>
46#include <stdarg.h>
fe3a1c21 47#include <libsigrokcxx/libsigrokcxx.hpp>
e82fd481 48
819f4c25 49using std::list;
25272fee 50using std::make_shared;
6842b5fc 51using std::map;
f9abf97e 52using std::shared_ptr;
6842b5fc 53using std::string;
e8d00928 54
51e77110
JH
55namespace pv {
56
b1264f56 57namespace view {
26e3af6b 58class ViewItem;
b1264f56
JH
59}
60
0f8f8c18 61using toolbars::MainBar;
643f65f9 62
107ca6d3 63MainWindow::MainWindow(DeviceManager &device_manager,
e3c79b07 64 string open_file_name, string open_file_format,
1d478458 65 QWidget *parent) :
107ca6d3 66 QMainWindow(parent),
8dbbc7f0 67 device_manager_(device_manager),
69654681 68 session_(device_manager),
0f8f8c18
SA
69 open_file_name_(open_file_name),
70 open_file_format_(open_file_format),
c7b03d9d 71 action_view_sticky_scrolling_(new QAction(this)),
0fb9d645 72 action_view_coloured_bg_(new QAction(this)),
e79171dc 73 action_about_(new QAction(this))
d7bed479 74{
48257a69
SA
75 qRegisterMetaType<util::Timestamp>("util::Timestamp");
76
7d5425ef 77 setup_ui();
93f683ad 78 restore_ui_settings();
7d5425ef
JH
79}
80
47e9e7bb
SA
81MainWindow::~MainWindow()
82{
83 for (auto entry : view_docks_) {
0f8f8c18 84
47e9e7bb 85 const std::shared_ptr<QDockWidget> dock = entry.first;
0f8f8c18
SA
86
87 // Remove view from the dock widget's QMainWindow
88 QMainWindow *dock_main = dynamic_cast<QMainWindow*>(dock->widget());
89 dock_main->setCentralWidget(0);
90
91 // Remove the QMainWindow
47e9e7bb 92 dock->setWidget(0);
0f8f8c18 93
47e9e7bb
SA
94 const std::shared_ptr<pv::view::View> view = entry.second;
95 session_.deregister_view(view);
96 }
97}
98
c7b03d9d
SA
99QAction* MainWindow::action_view_sticky_scrolling() const
100{
101 return action_view_sticky_scrolling_;
102}
103
bea236d6
JH
104QAction* MainWindow::action_view_coloured_bg() const
105{
106 return action_view_coloured_bg_;
107}
108
69654681
JH
109QAction* MainWindow::action_about() const
110{
111 return action_about_;
112}
113
168bd8ac
SA
114shared_ptr<pv::view::View> MainWindow::get_active_view() const
115{
116 // If there's only one view, use it...
117 if (view_docks_.size() == 1)
118 return view_docks_.begin()->second;
119
120 // ...otherwise find the dock widget the widget with focus is contained in
121 QObject *w = QApplication::focusWidget();
122 QDockWidget *dock = 0;
123
124 while (w) {
125 dock = qobject_cast<QDockWidget*>(w);
126 if (dock)
127 break;
128 w = w->parent();
129 }
130
131 // Get the view contained in the dock widget
132 for (auto entry : view_docks_)
133 if (entry.first.get() == dock)
134 return entry.second;
135
136 return shared_ptr<pv::view::View>();
137}
138
7cd2b5f3
SA
139shared_ptr<pv::view::View> MainWindow::add_view(const QString &title,
140 view::ViewType type, Session &session)
141{
142 shared_ptr<pv::view::View> v;
143
0f8f8c18 144 if (type == pv::view::TraceView) {
7cd2b5f3 145 shared_ptr<QDockWidget> dock = make_shared<QDockWidget>(title, this);
7cd2b5f3
SA
146 dock->setObjectName(title);
147 addDockWidget(Qt::TopDockWidgetArea, dock.get());
0f8f8c18
SA
148
149 // Insert a QMainWindow into the dock widget to allow for a tool bar
150 QMainWindow *dock_main = new QMainWindow(dock.get());
151 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
152
153 v = make_shared<pv::view::View>(session, dock_main);
7cd2b5f3 154 view_docks_[dock] = v;
0f8f8c18
SA
155 session.register_view(v);
156
157 dock_main->setCentralWidget(v.get());
158 dock->setWidget(dock_main);
7cd2b5f3 159
d6ab7b9a
SA
160 dock->setFeatures(QDockWidget::DockWidgetMovable |
161 QDockWidget::DockWidgetFloatable);
162
7cd2b5f3
SA
163 if (type == view::TraceView) {
164 connect(&session, SIGNAL(trigger_event(util::Timestamp)), v.get(),
165 SLOT(trigger_event(util::Timestamp)));
166 connect(v.get(), SIGNAL(sticky_scrolling_changed(bool)), this,
167 SLOT(sticky_scrolling_changed(bool)));
168 connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)), this,
169 SLOT(always_zoom_to_fit_changed(bool)));
170
171 v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
172 v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
7cd2b5f3 173
0f8f8c18
SA
174 shared_ptr<MainBar> main_bar = session.main_bar();
175 if (!main_bar) {
176 main_bar = make_shared<MainBar>(session_, *this,
177 open_file_name_, open_file_format_);
178 dock_main->addToolBar(main_bar.get());
179 session.set_main_bar(main_bar);
180
181 open_file_name_.clear();
182 open_file_format_.clear();
183 }
184 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
a55e7918 185 }
e93f5538
JH
186 }
187
0f8f8c18 188 return v;
ed43ef2e
JH
189}
190
7d5425ef
JH
191void MainWindow::setup_ui()
192{
193 setObjectName(QString::fromUtf8("MainWindow"));
194
7d5425ef
JH
195 // Set the window icon
196 QIcon icon;
14f9d4a1 197 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
7d5425ef
JH
198 setWindowIcon(icon);
199
c7b03d9d
SA
200 action_view_sticky_scrolling_->setCheckable(true);
201 action_view_sticky_scrolling_->setChecked(true);
69282fae 202 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
c7b03d9d
SA
203 action_view_sticky_scrolling_->setObjectName(
204 QString::fromUtf8("actionViewStickyScrolling"));
69282fae 205 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
ab1d13ee 206
0fb9d645
SA
207 action_view_coloured_bg_->setCheckable(true);
208 action_view_coloured_bg_->setChecked(true);
574c568d 209 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
0fb9d645
SA
210 action_view_coloured_bg_->setObjectName(
211 QString::fromUtf8("actionViewColouredBg"));
212 action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
0fb9d645 213
69654681
JH
214 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
215 action_about_->setText(tr("&About..."));
7d5425ef 216
7cd2b5f3 217 // Set up the initial view
0f8f8c18
SA
218 shared_ptr<view::View> main_view =
219 add_view(tr("Untitled"), pv::view::TraceView, session_);
d4984fe7 220
5ac961e3 221 // Set the title
0a9fdca5 222 setWindowTitle(tr("PulseView"));
e3c79b07
JH
223}
224
93f683ad
SA
225void MainWindow::save_ui_settings()
226{
39eb0d45 227 QSettings settings;
93f683ad 228
6842b5fc
SA
229 map<string, string> dev_info;
230 list<string> key_list;
231
93f683ad
SA
232 settings.beginGroup("MainWindow");
233 settings.setValue("state", saveState());
234 settings.setValue("geometry", saveGeometry());
235 settings.endGroup();
6842b5fc 236
8dbbc7f0 237 if (session_.device()) {
6842b5fc
SA
238 settings.beginGroup("Device");
239 key_list.push_back("vendor");
240 key_list.push_back("model");
241 key_list.push_back("version");
242 key_list.push_back("serial_num");
243 key_list.push_back("connection_id");
244
8dbbc7f0
JH
245 dev_info = device_manager_.get_device_info(
246 session_.device());
6842b5fc
SA
247
248 for (string key : key_list) {
6842b5fc
SA
249 if (dev_info.count(key))
250 settings.setValue(QString::fromUtf8(key.c_str()),
251 QString::fromUtf8(dev_info.at(key).c_str()));
252 else
253 settings.remove(QString::fromUtf8(key.c_str()));
254 }
255
256 settings.endGroup();
257 }
93f683ad
SA
258}
259
260void MainWindow::restore_ui_settings()
261{
39eb0d45 262 QSettings settings;
93f683ad
SA
263
264 settings.beginGroup("MainWindow");
265
266 if (settings.contains("geometry")) {
267 restoreGeometry(settings.value("geometry").toByteArray());
268 restoreState(settings.value("state").toByteArray());
269 } else
270 resize(1000, 720);
271
272 settings.endGroup();
273}
274
e3c79b07
JH
275void MainWindow::closeEvent(QCloseEvent *event)
276{
277 save_ui_settings();
278 event->accept();
279}
280
d290e89f
SA
281QMenu* MainWindow::createPopupMenu()
282{
283 return nullptr;
284}
285
55547a45
SA
286bool MainWindow::restoreState(const QByteArray &state, int version)
287{
288 (void)state;
289 (void)version;
290
291 // Do nothing. We don't want Qt to handle this, or else it
292 // will try to restore all the dock widgets and create havoc.
293
294 return false;
295}
296
c7b03d9d
SA
297void MainWindow::on_actionViewStickyScrolling_triggered()
298{
168bd8ac
SA
299 shared_ptr<pv::view::View> view = get_active_view();
300 if (view)
301 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
c7b03d9d
SA
302}
303
0fb9d645
SA
304void MainWindow::on_actionViewColouredBg_triggered()
305{
168bd8ac
SA
306 shared_ptr<pv::view::View> view = get_active_view();
307 if (view)
308 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
0fb9d645
SA
309}
310
30236a54
JH
311void MainWindow::on_actionAbout_triggered()
312{
8dbbc7f0 313 dialogs::About dlg(device_manager_.context(), this);
40eb2ff4 314 dlg.exec();
30236a54 315}
274d4f13 316
51e77110 317} // namespace pv