]> sigrok.org Git - pulseview.git/blame_incremental - pv/mainwindow.cpp
Make the first view own the toolbar instead of the main window
[pulseview.git] / pv / mainwindow.cpp
... / ...
CommitLineData
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 <cassert>
22
23#ifdef ENABLE_DECODE
24#include <libsigrokdecode/libsigrokdecode.h>
25#endif
26
27#include <algorithm>
28#include <iterator>
29
30#include <QAction>
31#include <QApplication>
32#include <QCloseEvent>
33#include <QDockWidget>
34#include <QSettings>
35#include <QWidget>
36
37#include "mainwindow.hpp"
38
39#include "devicemanager.hpp"
40#include "util.hpp"
41#include "dialogs/about.hpp"
42#include "toolbars/mainbar.hpp"
43#include "view/view.hpp"
44
45#include <stdint.h>
46#include <stdarg.h>
47#include <libsigrokcxx/libsigrokcxx.hpp>
48
49using std::list;
50using std::make_shared;
51using std::map;
52using std::shared_ptr;
53using std::string;
54
55namespace pv {
56
57namespace view {
58class ViewItem;
59}
60
61using toolbars::MainBar;
62
63MainWindow::MainWindow(DeviceManager &device_manager,
64 string open_file_name, string open_file_format,
65 QWidget *parent) :
66 QMainWindow(parent),
67 device_manager_(device_manager),
68 session_(device_manager),
69 open_file_name_(open_file_name),
70 open_file_format_(open_file_format),
71 action_view_sticky_scrolling_(new QAction(this)),
72 action_view_coloured_bg_(new QAction(this)),
73 action_about_(new QAction(this))
74{
75 qRegisterMetaType<util::Timestamp>("util::Timestamp");
76
77 setup_ui();
78 restore_ui_settings();
79}
80
81MainWindow::~MainWindow()
82{
83 for (auto entry : view_docks_) {
84
85 const std::shared_ptr<QDockWidget> dock = entry.first;
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
92 dock->setWidget(0);
93
94 const std::shared_ptr<pv::view::View> view = entry.second;
95 session_.deregister_view(view);
96 }
97}
98
99QAction* MainWindow::action_view_sticky_scrolling() const
100{
101 return action_view_sticky_scrolling_;
102}
103
104QAction* MainWindow::action_view_coloured_bg() const
105{
106 return action_view_coloured_bg_;
107}
108
109QAction* MainWindow::action_about() const
110{
111 return action_about_;
112}
113
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
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
144 if (type == pv::view::TraceView) {
145 shared_ptr<QDockWidget> dock = make_shared<QDockWidget>(title, this);
146 dock->setObjectName(title);
147 addDockWidget(Qt::TopDockWidgetArea, dock.get());
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);
154 view_docks_[dock] = v;
155 session.register_view(v);
156
157 dock_main->setCentralWidget(v.get());
158 dock->setWidget(dock_main);
159
160 dock->setFeatures(QDockWidget::DockWidgetMovable |
161 QDockWidget::DockWidgetFloatable);
162
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());
173
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());
185 }
186 }
187
188 return v;
189}
190
191void MainWindow::setup_ui()
192{
193 setObjectName(QString::fromUtf8("MainWindow"));
194
195 // Set the window icon
196 QIcon icon;
197 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
198 setWindowIcon(icon);
199
200 action_view_sticky_scrolling_->setCheckable(true);
201 action_view_sticky_scrolling_->setChecked(true);
202 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
203 action_view_sticky_scrolling_->setObjectName(
204 QString::fromUtf8("actionViewStickyScrolling"));
205 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
206
207 action_view_coloured_bg_->setCheckable(true);
208 action_view_coloured_bg_->setChecked(true);
209 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
210 action_view_coloured_bg_->setObjectName(
211 QString::fromUtf8("actionViewColouredBg"));
212 action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
213
214 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
215 action_about_->setText(tr("&About..."));
216
217 // Set up the initial view
218 shared_ptr<view::View> main_view =
219 add_view(tr("Untitled"), pv::view::TraceView, session_);
220
221 // Set the title
222 setWindowTitle(tr("PulseView"));
223}
224
225void MainWindow::save_ui_settings()
226{
227 QSettings settings;
228
229 map<string, string> dev_info;
230 list<string> key_list;
231
232 settings.beginGroup("MainWindow");
233 settings.setValue("state", saveState());
234 settings.setValue("geometry", saveGeometry());
235 settings.endGroup();
236
237 if (session_.device()) {
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
245 dev_info = device_manager_.get_device_info(
246 session_.device());
247
248 for (string key : key_list) {
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 }
258}
259
260void MainWindow::restore_ui_settings()
261{
262 QSettings settings;
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
275void MainWindow::closeEvent(QCloseEvent *event)
276{
277 save_ui_settings();
278 event->accept();
279}
280
281QMenu* MainWindow::createPopupMenu()
282{
283 return nullptr;
284}
285
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
297void MainWindow::on_actionViewStickyScrolling_triggered()
298{
299 shared_ptr<pv::view::View> view = get_active_view();
300 if (view)
301 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
302}
303
304void MainWindow::on_actionViewColouredBg_triggered()
305{
306 shared_ptr<pv::view::View> view = get_active_view();
307 if (view)
308 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
309}
310
311void MainWindow::on_actionAbout_triggered()
312{
313 dialogs::About dlg(device_manager_.context(), this);
314 dlg.exec();
315}
316
317} // namespace pv