decodetrace.cpp: Drop unused "using std::all_of".
[pulseview.git] / pv / views / trace / standardbar.cpp
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
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <QAction>
22 #include <QMessageBox>
23
24 #include "standardbar.hpp"
25 #include "view.hpp"
26
27 #include <pv/mainwindow.hpp>
28
29 using pv::views::trace::View;
30
31 namespace pv {
32 namespace views {
33
34 namespace trace {
35
36 StandardBar::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)),
45         action_view_show_cursors_(new QAction(this)),
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)),
50         segment_selector_(new QSpinBox(this))
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"));
72         action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit-best",
73                 QIcon(":/icons/zoom-fit-best.png")));
74         action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
75         connect(action_view_zoom_fit_, SIGNAL(triggered(bool)),
76                 this, SLOT(on_actionViewZoomFit_triggered(bool)));
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);
86         action_view_show_cursors_->setIcon(QIcon(":/icons/show-cursors.svg"));
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
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
113         segment_selector_->setMinimum(1);
114         segment_selector_->hide();
115
116         connect(&session_, SIGNAL(new_segment(int)),
117                 this, SLOT(on_new_segment(int)));
118
119         connect(&session_, SIGNAL(segment_completed(int)),
120                 view_, SLOT(on_segment_completed(int)));
121
122         connect(segment_selector_, SIGNAL(valueChanged(int)),
123                 this, SLOT(on_segment_selected(int)));
124         connect(view_, SIGNAL(segment_changed(int)),
125                 this, SLOT(on_segment_changed(int)));
126
127         connect(this, SIGNAL(segment_selected(int)),
128                 view_, SLOT(on_segment_changed(int)));
129
130         connect(view_, SIGNAL(segment_display_mode_changed(int, bool)),
131                 this, SLOT(on_segment_display_mode_changed(int, bool)));
132
133         connect(view_, SIGNAL(always_zoom_to_fit_changed(bool)),
134                 this, SLOT(on_always_zoom_to_fit_changed(bool)));
135
136         if (add_default_widgets)
137                 add_toolbar_widgets();
138 }
139
140 Session &StandardBar::session() const
141 {
142         return session_;
143 }
144
145 void StandardBar::add_toolbar_widgets()
146 {
147         // Setup the toolbar
148         addAction(action_view_zoom_in_);
149         addAction(action_view_zoom_out_);
150         addAction(action_view_zoom_fit_);
151         addAction(action_view_zoom_one_to_one_);
152         addSeparator();
153         addAction(action_view_show_cursors_);
154         multi_segment_actions_.push_back(addSeparator());
155         multi_segment_actions_.push_back(addWidget(segment_display_mode_selector_));
156         multi_segment_actions_.push_back(addWidget(segment_selector_));
157         addSeparator();
158
159         // Hide the multi-segment UI until we know that there are multiple segments
160         show_multi_segment_ui(false);
161 }
162
163 void StandardBar::show_multi_segment_ui(const bool state)
164 {
165         for (QAction* action : multi_segment_actions_)
166                 action->setVisible(state);
167
168         on_segment_display_mode_changed(view_->segment_display_mode(),
169                 view_->segment_is_selectable());
170 }
171
172 QAction* StandardBar::action_view_zoom_in() const
173 {
174         return action_view_zoom_in_;
175 }
176
177 QAction* StandardBar::action_view_zoom_out() const
178 {
179         return action_view_zoom_out_;
180 }
181
182 QAction* StandardBar::action_view_zoom_fit() const
183 {
184         return action_view_zoom_fit_;
185 }
186
187 QAction* StandardBar::action_view_zoom_one_to_one() const
188 {
189         return action_view_zoom_one_to_one_;
190 }
191
192 QAction* StandardBar::action_view_show_cursors() const
193 {
194         return action_view_show_cursors_;
195 }
196
197 void StandardBar::on_actionViewZoomIn_triggered()
198 {
199         view_->zoom(1);
200 }
201
202 void StandardBar::on_actionViewZoomOut_triggered()
203 {
204         view_->zoom(-1);
205 }
206
207 void StandardBar::on_actionViewZoomFit_triggered(bool checked)
208 {
209         view_->zoom_fit(checked);
210 }
211
212 void StandardBar::on_actionViewZoomOneToOne_triggered()
213 {
214         view_->zoom_one_to_one();
215 }
216
217 void StandardBar::on_actionViewShowCursors_triggered()
218 {
219         const bool show = !view_->cursors_shown();
220         if (show)
221                 view_->centre_cursors();
222
223         view_->show_cursors(show);
224 }
225
226 void StandardBar::on_actionSDMLast_triggered()
227 {
228         view_->set_segment_display_mode(Trace::ShowLastSegmentOnly);
229 }
230
231 void StandardBar::on_actionSDMLastComplete_triggered()
232 {
233         view_->set_segment_display_mode(Trace::ShowLastCompleteSegmentOnly);
234 }
235
236 void StandardBar::on_actionSDMSingle_triggered()
237 {
238         view_->set_segment_display_mode(Trace::ShowSingleSegmentOnly);
239 }
240
241 void StandardBar::on_always_zoom_to_fit_changed(bool state)
242 {
243         action_view_zoom_fit_->setChecked(state);
244 }
245
246 void StandardBar::on_new_segment(int new_segment_id)
247 {
248         if (new_segment_id > 0) {
249                 show_multi_segment_ui(true);
250                 segment_selector_->setMaximum(new_segment_id + 1);
251         } else
252                 show_multi_segment_ui(false);
253 }
254
255 void StandardBar::on_segment_changed(int segment_id)
256 {
257         // We need to adjust the value by 1 because internally, segments
258         // start at 0 while they start with 1 for the spinbox
259         const uint32_t ui_segment_id = segment_id + 1;
260
261         // This is called when the current segment was changed
262         // by other parts of the UI, e.g. the view itself
263
264         // Make sure our value isn't limited by a too low maximum
265         // Note: this can happen if on_segment_changed() is called before
266         // on_new_segment()
267         if ((uint32_t)segment_selector_->maximum() < ui_segment_id)
268                 segment_selector_->setMaximum(ui_segment_id);
269
270         segment_selector_->setValue(ui_segment_id);
271 }
272
273 void StandardBar::on_segment_selected(int ui_segment_id)
274 {
275         // We need to adjust the value by 1 because internally, segments
276         // start at 0 while they start with 1 for the spinbox
277         const uint32_t segment_id = ui_segment_id - 1;
278
279         // This is called when the user selected a segment using the spin box
280         // or when the value of the spinbox was assigned a new value. Since we
281         // only care about the former, we filter out the latter:
282         if (segment_id == view_->current_segment())
283                 return;
284
285         // No matter which segment display mode we were in, we now show a single segment
286         if (view_->segment_display_mode() != Trace::ShowSingleSegmentOnly)
287                 on_actionSDMSingle_triggered();
288
289         segment_selected(segment_id);
290 }
291
292 void StandardBar::on_segment_display_mode_changed(int mode, bool segment_selectable)
293 {
294         segment_selector_->setReadOnly(!segment_selectable);
295
296         switch ((Trace::SegmentDisplayMode)mode) {
297         case Trace::ShowLastSegmentOnly:
298                 segment_display_mode_selector_->setDefaultAction(action_sdm_last_);
299                 break;
300         case Trace::ShowLastCompleteSegmentOnly:
301                 segment_display_mode_selector_->setDefaultAction(action_sdm_last_complete_);
302                 break;
303         case Trace::ShowSingleSegmentOnly:
304                 segment_display_mode_selector_->setDefaultAction(action_sdm_single_);
305                 break;
306         default:
307                 break;
308         }
309 }
310
311 } // namespace trace
312 } // namespace views
313 } // namespace pv