]> sigrok.org Git - pulseview.git/blame - pv/views/trace/standardbar.cpp
Update cursor action's state whenever the cursor state changes
[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 45 action_view_show_cursors_(new QAction(this)),
89914a86
SA
46 segment_display_mode_selector_(new QToolButton(this)),
47 action_sdm_last_(new QAction(this)),
48 action_sdm_last_complete_(new QAction(this)),
49 action_sdm_single_(new QAction(this)),
7f965464 50 segment_selector_(new QSpinBox(this))
e0ba4f6f
SA
51{
52 setObjectName(QString::fromUtf8("StandardBar"));
53
54 // Actions
55 action_view_zoom_in_->setText(tr("Zoom &In"));
56 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
57 QIcon(":/icons/zoom-in.png")));
58 // simply using Qt::Key_Plus shows no + in the menu
59 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
60 connect(action_view_zoom_in_, SIGNAL(triggered(bool)),
61 this, SLOT(on_actionViewZoomIn_triggered()));
62
63 action_view_zoom_out_->setText(tr("Zoom &Out"));
64 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
65 QIcon(":/icons/zoom-out.png")));
66 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
67 connect(action_view_zoom_out_, SIGNAL(triggered(bool)),
68 this, SLOT(on_actionViewZoomOut_triggered()));
69
70 action_view_zoom_fit_->setCheckable(true);
71 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
3432032f
UH
72 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit-best",
73 QIcon(":/icons/zoom-fit-best.png")));
e0ba4f6f
SA
74 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
75 connect(action_view_zoom_fit_, SIGNAL(triggered(bool)),
dfe1bf82 76 this, SLOT(on_actionViewZoomFit_triggered(bool)));
e0ba4f6f
SA
77
78 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
79 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
80 QIcon(":/icons/zoom-original.png")));
81 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
82 connect(action_view_zoom_one_to_one_, SIGNAL(triggered(bool)),
83 this, SLOT(on_actionViewZoomOneToOne_triggered()));
84
85 action_view_show_cursors_->setCheckable(true);
4d8130bb 86 action_view_show_cursors_->setIcon(QIcon(":/icons/show-cursors.svg"));
e0ba4f6f
SA
87 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
88 connect(action_view_show_cursors_, SIGNAL(triggered(bool)),
89 this, SLOT(on_actionViewShowCursors_triggered()));
90 action_view_show_cursors_->setText(tr("Show &Cursors"));
91
89914a86
SA
92 action_sdm_last_->setIcon(QIcon(":/icons/view-displaymode-last_segment.svg"));
93 action_sdm_last_->setText(tr("Display last segment only"));
94 connect(action_sdm_last_, SIGNAL(triggered(bool)),
95 this, SLOT(on_actionSDMLast_triggered()));
96
97 action_sdm_last_complete_->setIcon(QIcon(":/icons/view-displaymode-last_complete_segment.svg"));
98 action_sdm_last_complete_->setText(tr("Display last complete segment only"));
99 connect(action_sdm_last_complete_, SIGNAL(triggered(bool)),
100 this, SLOT(on_actionSDMLastComplete_triggered()));
101
102 action_sdm_single_->setIcon(QIcon(":/icons/view-displaymode-single_segment.svg"));
103 action_sdm_single_->setText(tr("Display a single segment"));
104 connect(action_view_show_cursors_, SIGNAL(triggered(bool)),
105 this, SLOT(on_actionSDMSingle_triggered()));
106
107 segment_display_mode_selector_->addAction(action_sdm_last_);
108 segment_display_mode_selector_->addAction(action_sdm_last_complete_);
109 segment_display_mode_selector_->addAction(action_sdm_single_);
110 segment_display_mode_selector_->setPopupMode(QToolButton::InstantPopup);
111 segment_display_mode_selector_->hide();
112
7f965464 113 segment_selector_->setMinimum(1);
526c8c00 114 segment_selector_->hide();
89914a86 115
4e86ec70
SA
116 connect(&session_, SIGNAL(new_segment(int)),
117 this, SLOT(on_new_segment(int)));
341d9a79 118
558ad6ce
SA
119 connect(&session_, SIGNAL(segment_completed(int)),
120 view_, SLOT(on_segment_completed(int)));
121
526c8c00 122 connect(segment_selector_, SIGNAL(valueChanged(int)),
341d9a79 123 this, SLOT(on_segment_selected(int)));
7daebd05
SA
124 connect(view_, SIGNAL(segment_changed(int)),
125 this, SLOT(on_segment_changed(int)));
341d9a79
SA
126
127 connect(this, SIGNAL(segment_selected(int)),
128 view_, SLOT(on_segment_changed(int)));
129
89914a86
SA
130 connect(view_, SIGNAL(segment_display_mode_changed(int, bool)),
131 this, SLOT(on_segment_display_mode_changed(int, bool)));
7f965464 132
dfe1bf82
SA
133 connect(view_, SIGNAL(always_zoom_to_fit_changed(bool)),
134 this, SLOT(on_always_zoom_to_fit_changed(bool)));
135
e4e5a958
SA
136 connect(view_, SIGNAL(cursor_state_changed(bool)),
137 this, SLOT(on_cursor_state_changed(bool)));
138
e0ba4f6f
SA
139 if (add_default_widgets)
140 add_toolbar_widgets();
141}
142
1887da21 143Session &StandardBar::session() const
e0ba4f6f
SA
144{
145 return session_;
146}
147
148void StandardBar::add_toolbar_widgets()
149{
150 // Setup the toolbar
151 addAction(action_view_zoom_in_);
152 addAction(action_view_zoom_out_);
153 addAction(action_view_zoom_fit_);
154 addAction(action_view_zoom_one_to_one_);
155 addSeparator();
156 addAction(action_view_show_cursors_);
7f965464 157 multi_segment_actions_.push_back(addSeparator());
89914a86 158 multi_segment_actions_.push_back(addWidget(segment_display_mode_selector_));
7f965464
SA
159 multi_segment_actions_.push_back(addWidget(segment_selector_));
160 addSeparator();
161
162 // Hide the multi-segment UI until we know that there are multiple segments
163 show_multi_segment_ui(false);
164}
165
166void StandardBar::show_multi_segment_ui(const bool state)
167{
168 for (QAction* action : multi_segment_actions_)
169 action->setVisible(state);
7daebd05 170
89914a86
SA
171 on_segment_display_mode_changed(view_->segment_display_mode(),
172 view_->segment_is_selectable());
e0ba4f6f
SA
173}
174
175QAction* StandardBar::action_view_zoom_in() const
176{
177 return action_view_zoom_in_;
178}
179
180QAction* StandardBar::action_view_zoom_out() const
181{
182 return action_view_zoom_out_;
183}
184
185QAction* StandardBar::action_view_zoom_fit() const
186{
187 return action_view_zoom_fit_;
188}
189
190QAction* StandardBar::action_view_zoom_one_to_one() const
191{
192 return action_view_zoom_one_to_one_;
193}
194
195QAction* StandardBar::action_view_show_cursors() const
196{
197 return action_view_show_cursors_;
198}
199
200void StandardBar::on_actionViewZoomIn_triggered()
201{
202 view_->zoom(1);
203}
204
205void StandardBar::on_actionViewZoomOut_triggered()
206{
207 view_->zoom(-1);
208}
209
dfe1bf82 210void StandardBar::on_actionViewZoomFit_triggered(bool checked)
e0ba4f6f 211{
dfe1bf82 212 view_->zoom_fit(checked);
e0ba4f6f
SA
213}
214
215void StandardBar::on_actionViewZoomOneToOne_triggered()
216{
217 view_->zoom_one_to_one();
218}
219
220void StandardBar::on_actionViewShowCursors_triggered()
221{
e4e5a958
SA
222 const bool show = action_view_show_cursors_->isChecked();
223
e0ba4f6f
SA
224 if (show)
225 view_->centre_cursors();
226
227 view_->show_cursors(show);
228}
229
89914a86
SA
230void StandardBar::on_actionSDMLast_triggered()
231{
232 view_->set_segment_display_mode(Trace::ShowLastSegmentOnly);
233}
234
235void StandardBar::on_actionSDMLastComplete_triggered()
236{
237 view_->set_segment_display_mode(Trace::ShowLastCompleteSegmentOnly);
238}
239
240void StandardBar::on_actionSDMSingle_triggered()
241{
242 view_->set_segment_display_mode(Trace::ShowSingleSegmentOnly);
243}
244
e0ba4f6f
SA
245void StandardBar::on_always_zoom_to_fit_changed(bool state)
246{
247 action_view_zoom_fit_->setChecked(state);
248}
249
4e86ec70 250void StandardBar::on_new_segment(int new_segment_id)
7f965464 251{
558ad6ce 252 if (new_segment_id > 0) {
7f965464 253 show_multi_segment_ui(true);
341d9a79 254 segment_selector_->setMaximum(new_segment_id + 1);
7f965464
SA
255 } else
256 show_multi_segment_ui(false);
257}
258
7daebd05
SA
259void StandardBar::on_segment_changed(int segment_id)
260{
89914a86
SA
261 // We need to adjust the value by 1 because internally, segments
262 // start at 0 while they start with 1 for the spinbox
263 const uint32_t ui_segment_id = segment_id + 1;
264
7daebd05
SA
265 // This is called when the current segment was changed
266 // by other parts of the UI, e.g. the view itself
341d9a79 267
89914a86
SA
268 // Make sure our value isn't limited by a too low maximum
269 // Note: this can happen if on_segment_changed() is called before
270 // on_new_segment()
271 if ((uint32_t)segment_selector_->maximum() < ui_segment_id)
272 segment_selector_->setMaximum(ui_segment_id);
341d9a79 273
89914a86 274 segment_selector_->setValue(ui_segment_id);
341d9a79
SA
275}
276
277void StandardBar::on_segment_selected(int ui_segment_id)
278{
341d9a79
SA
279 // We need to adjust the value by 1 because internally, segments
280 // start at 0 while they start with 1 for the spinbox
89914a86
SA
281 const uint32_t segment_id = ui_segment_id - 1;
282
283 // This is called when the user selected a segment using the spin box
284 // or when the value of the spinbox was assigned a new value. Since we
285 // only care about the former, we filter out the latter:
286 if (segment_id == view_->current_segment())
287 return;
288
289 // No matter which segment display mode we were in, we now show a single segment
290 if (view_->segment_display_mode() != Trace::ShowSingleSegmentOnly)
291 on_actionSDMSingle_triggered();
292
293 segment_selected(segment_id);
7daebd05
SA
294}
295
89914a86 296void StandardBar::on_segment_display_mode_changed(int mode, bool segment_selectable)
7daebd05
SA
297{
298 segment_selector_->setReadOnly(!segment_selectable);
89914a86
SA
299
300 switch ((Trace::SegmentDisplayMode)mode) {
301 case Trace::ShowLastSegmentOnly:
302 segment_display_mode_selector_->setDefaultAction(action_sdm_last_);
303 break;
304 case Trace::ShowLastCompleteSegmentOnly:
305 segment_display_mode_selector_->setDefaultAction(action_sdm_last_complete_);
306 break;
307 case Trace::ShowSingleSegmentOnly:
308 segment_display_mode_selector_->setDefaultAction(action_sdm_single_);
309 break;
310 default:
311 break;
312 }
7daebd05
SA
313}
314
e4e5a958
SA
315void StandardBar::on_cursor_state_changed(bool show)
316{
317 action_view_show_cursors_->setChecked(show);
318}
319
e0ba4f6f
SA
320} // namespace trace
321} // namespace views
322} // namespace pv