]> sigrok.org Git - pulseview.git/blame - pv/views/trace/standardbar.cpp
Add segment display mode icons
[pulseview.git] / pv / views / trace / standardbar.cpp
CommitLineData
e0ba4f6f
SA
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2016 Soeren Apel <soeren@apelpie.net>
5 * Copyright (C) 2012-2015 Joel Holdsworth <joel@airwebreathe.org.uk>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
efdec55a 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
e0ba4f6f
SA
19 */
20
21#include <QAction>
22#include <QMessageBox>
23
24#include "standardbar.hpp"
1573bf16 25#include "view.hpp"
e0ba4f6f
SA
26
27#include <pv/mainwindow.hpp>
e0ba4f6f 28
f23c4692 29using pv::views::trace::View;
e0ba4f6f
SA
30
31namespace pv {
32namespace views {
33
34namespace trace {
35
36StandardBar::StandardBar(Session &session, QWidget *parent,
37 View *view, bool add_default_widgets) :
38 QToolBar("Standard Trace View Toolbar", parent),
39 session_(session),
40 view_(view),
41 action_view_zoom_in_(new QAction(this)),
42 action_view_zoom_out_(new QAction(this)),
43 action_view_zoom_fit_(new QAction(this)),
44 action_view_zoom_one_to_one_(new QAction(this)),
7f965464
SA
45 action_view_show_cursors_(new QAction(this)),
46 segment_selector_(new QSpinBox(this))
e0ba4f6f
SA
47{
48 setObjectName(QString::fromUtf8("StandardBar"));
49
50 // Actions
51 action_view_zoom_in_->setText(tr("Zoom &In"));
52 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
53 QIcon(":/icons/zoom-in.png")));
54 // simply using Qt::Key_Plus shows no + in the menu
55 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
56 connect(action_view_zoom_in_, SIGNAL(triggered(bool)),
57 this, SLOT(on_actionViewZoomIn_triggered()));
58
59 action_view_zoom_out_->setText(tr("Zoom &Out"));
60 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
61 QIcon(":/icons/zoom-out.png")));
62 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
63 connect(action_view_zoom_out_, SIGNAL(triggered(bool)),
64 this, SLOT(on_actionViewZoomOut_triggered()));
65
66 action_view_zoom_fit_->setCheckable(true);
67 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
3432032f
UH
68 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit-best",
69 QIcon(":/icons/zoom-fit-best.png")));
e0ba4f6f
SA
70 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
71 connect(action_view_zoom_fit_, SIGNAL(triggered(bool)),
dfe1bf82 72 this, SLOT(on_actionViewZoomFit_triggered(bool)));
e0ba4f6f
SA
73
74 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
75 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
76 QIcon(":/icons/zoom-original.png")));
77 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
78 connect(action_view_zoom_one_to_one_, SIGNAL(triggered(bool)),
79 this, SLOT(on_actionViewZoomOneToOne_triggered()));
80
81 action_view_show_cursors_->setCheckable(true);
4d8130bb 82 action_view_show_cursors_->setIcon(QIcon(":/icons/show-cursors.svg"));
e0ba4f6f
SA
83 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
84 connect(action_view_show_cursors_, SIGNAL(triggered(bool)),
85 this, SLOT(on_actionViewShowCursors_triggered()));
86 action_view_show_cursors_->setText(tr("Show &Cursors"));
87
7f965464 88 segment_selector_->setMinimum(1);
526c8c00 89 segment_selector_->hide();
4e86ec70
SA
90 connect(&session_, SIGNAL(new_segment(int)),
91 this, SLOT(on_new_segment(int)));
341d9a79 92
558ad6ce
SA
93 connect(&session_, SIGNAL(segment_completed(int)),
94 view_, SLOT(on_segment_completed(int)));
95
526c8c00 96 connect(segment_selector_, SIGNAL(valueChanged(int)),
341d9a79 97 this, SLOT(on_segment_selected(int)));
7daebd05
SA
98 connect(view_, SIGNAL(segment_changed(int)),
99 this, SLOT(on_segment_changed(int)));
341d9a79
SA
100
101 connect(this, SIGNAL(segment_selected(int)),
102 view_, SLOT(on_segment_changed(int)));
103
7daebd05
SA
104 connect(view_, SIGNAL(segment_display_mode_changed(bool)),
105 this, SLOT(on_segment_display_mode_changed(bool)));
7f965464 106
dfe1bf82
SA
107 connect(view_, SIGNAL(always_zoom_to_fit_changed(bool)),
108 this, SLOT(on_always_zoom_to_fit_changed(bool)));
109
e0ba4f6f
SA
110 if (add_default_widgets)
111 add_toolbar_widgets();
112}
113
1887da21 114Session &StandardBar::session() const
e0ba4f6f
SA
115{
116 return session_;
117}
118
119void StandardBar::add_toolbar_widgets()
120{
121 // Setup the toolbar
122 addAction(action_view_zoom_in_);
123 addAction(action_view_zoom_out_);
124 addAction(action_view_zoom_fit_);
125 addAction(action_view_zoom_one_to_one_);
126 addSeparator();
127 addAction(action_view_show_cursors_);
7f965464
SA
128 multi_segment_actions_.push_back(addSeparator());
129 multi_segment_actions_.push_back(addWidget(segment_selector_));
130 addSeparator();
131
132 // Hide the multi-segment UI until we know that there are multiple segments
133 show_multi_segment_ui(false);
134}
135
136void StandardBar::show_multi_segment_ui(const bool state)
137{
138 for (QAction* action : multi_segment_actions_)
139 action->setVisible(state);
7daebd05
SA
140
141 on_segment_display_mode_changed(view_->segment_is_selectable());
e0ba4f6f
SA
142}
143
144QAction* StandardBar::action_view_zoom_in() const
145{
146 return action_view_zoom_in_;
147}
148
149QAction* StandardBar::action_view_zoom_out() const
150{
151 return action_view_zoom_out_;
152}
153
154QAction* StandardBar::action_view_zoom_fit() const
155{
156 return action_view_zoom_fit_;
157}
158
159QAction* StandardBar::action_view_zoom_one_to_one() const
160{
161 return action_view_zoom_one_to_one_;
162}
163
164QAction* StandardBar::action_view_show_cursors() const
165{
166 return action_view_show_cursors_;
167}
168
169void StandardBar::on_actionViewZoomIn_triggered()
170{
171 view_->zoom(1);
172}
173
174void StandardBar::on_actionViewZoomOut_triggered()
175{
176 view_->zoom(-1);
177}
178
dfe1bf82 179void StandardBar::on_actionViewZoomFit_triggered(bool checked)
e0ba4f6f 180{
dfe1bf82 181 view_->zoom_fit(checked);
e0ba4f6f
SA
182}
183
184void StandardBar::on_actionViewZoomOneToOne_triggered()
185{
186 view_->zoom_one_to_one();
187}
188
189void StandardBar::on_actionViewShowCursors_triggered()
190{
191 const bool show = !view_->cursors_shown();
192 if (show)
193 view_->centre_cursors();
194
195 view_->show_cursors(show);
196}
197
198void StandardBar::on_always_zoom_to_fit_changed(bool state)
199{
200 action_view_zoom_fit_->setChecked(state);
201}
202
4e86ec70 203void StandardBar::on_new_segment(int new_segment_id)
7f965464 204{
558ad6ce 205 if (new_segment_id > 0) {
7f965464 206 show_multi_segment_ui(true);
341d9a79 207 segment_selector_->setMaximum(new_segment_id + 1);
7f965464
SA
208 } else
209 show_multi_segment_ui(false);
210}
211
7daebd05
SA
212void StandardBar::on_segment_changed(int segment_id)
213{
214 // This is called when the current segment was changed
215 // by other parts of the UI, e.g. the view itself
341d9a79
SA
216
217 // We need to adjust the value by 1 because internally, segments
218 // start at 0 while they start with 1 for the spinbox
219 segment_selector_->setValue(segment_id + 1);
220
221 segment_selected(segment_id);
222}
223
224void StandardBar::on_segment_selected(int ui_segment_id)
225{
226 // This is called when the user selected a segment using the spin box
227
228 // We need to adjust the value by 1 because internally, segments
229 // start at 0 while they start with 1 for the spinbox
230 segment_selected(ui_segment_id - 1);
7daebd05
SA
231}
232
233void StandardBar::on_segment_display_mode_changed(bool segment_selectable)
234{
235 segment_selector_->setReadOnly(!segment_selectable);
236}
237
e0ba4f6f
SA
238} // namespace trace
239} // namespace views
240} // namespace pv