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