]> sigrok.org Git - pulseview.git/blob - pv/views/trace/standardbar.cpp
6363e2ec6fcc9717e92dfc3a327713836d5b93eb
[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_selector_(new QSpinBox(this))
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"));
68         action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit-best",
69                 QIcon(":/icons/zoom-fit-best.png")));
70         action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
71         connect(action_view_zoom_fit_, SIGNAL(triggered(bool)),
72                 this, SLOT(on_actionViewZoomFit_triggered(bool)));
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);
82         action_view_show_cursors_->setIcon(QIcon(":/icons/show-cursors.svg"));
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
88         segment_selector_->setMinimum(1);
89         segment_selector_->hide();
90         connect(&session_, SIGNAL(new_segment(int)),
91                 this, SLOT(on_new_segment(int)));
92         connect(segment_selector_, SIGNAL(valueChanged(int)),
93                 view_, SLOT(on_segment_changed(int)));
94         connect(view_, SIGNAL(segment_changed(int)),
95                 this, SLOT(on_segment_changed(int)));
96         connect(view_, SIGNAL(segment_display_mode_changed(bool)),
97                 this, SLOT(on_segment_display_mode_changed(bool)));
98
99         connect(view_, SIGNAL(always_zoom_to_fit_changed(bool)),
100                 this, SLOT(on_always_zoom_to_fit_changed(bool)));
101
102         if (add_default_widgets)
103                 add_toolbar_widgets();
104 }
105
106 Session &StandardBar::session() const
107 {
108         return session_;
109 }
110
111 void StandardBar::add_toolbar_widgets()
112 {
113         // Setup the toolbar
114         addAction(action_view_zoom_in_);
115         addAction(action_view_zoom_out_);
116         addAction(action_view_zoom_fit_);
117         addAction(action_view_zoom_one_to_one_);
118         addSeparator();
119         addAction(action_view_show_cursors_);
120         multi_segment_actions_.push_back(addSeparator());
121         multi_segment_actions_.push_back(addWidget(segment_selector_));
122         addSeparator();
123
124         // Hide the multi-segment UI until we know that there are multiple segments
125         show_multi_segment_ui(false);
126 }
127
128 void StandardBar::show_multi_segment_ui(const bool state)
129 {
130         for (QAction* action : multi_segment_actions_)
131                 action->setVisible(state);
132
133         on_segment_display_mode_changed(view_->segment_is_selectable());
134 }
135
136 QAction* StandardBar::action_view_zoom_in() const
137 {
138         return action_view_zoom_in_;
139 }
140
141 QAction* StandardBar::action_view_zoom_out() const
142 {
143         return action_view_zoom_out_;
144 }
145
146 QAction* StandardBar::action_view_zoom_fit() const
147 {
148         return action_view_zoom_fit_;
149 }
150
151 QAction* StandardBar::action_view_zoom_one_to_one() const
152 {
153         return action_view_zoom_one_to_one_;
154 }
155
156 QAction* StandardBar::action_view_show_cursors() const
157 {
158         return action_view_show_cursors_;
159 }
160
161 void StandardBar::on_actionViewZoomIn_triggered()
162 {
163         view_->zoom(1);
164 }
165
166 void StandardBar::on_actionViewZoomOut_triggered()
167 {
168         view_->zoom(-1);
169 }
170
171 void StandardBar::on_actionViewZoomFit_triggered(bool checked)
172 {
173         view_->zoom_fit(checked);
174 }
175
176 void StandardBar::on_actionViewZoomOneToOne_triggered()
177 {
178         view_->zoom_one_to_one();
179 }
180
181 void StandardBar::on_actionViewShowCursors_triggered()
182 {
183         const bool show = !view_->cursors_shown();
184         if (show)
185                 view_->centre_cursors();
186
187         view_->show_cursors(show);
188 }
189
190 void StandardBar::on_always_zoom_to_fit_changed(bool state)
191 {
192         action_view_zoom_fit_->setChecked(state);
193 }
194
195 void StandardBar::on_new_segment(int new_segment_id)
196 {
197         if (new_segment_id > 1) {
198                 show_multi_segment_ui(true);
199                 segment_selector_->setMaximum(new_segment_id);
200         } else
201                 show_multi_segment_ui(false);
202 }
203
204 void StandardBar::on_segment_changed(int segment_id)
205 {
206         // This is called when the current segment was changed
207         // by other parts of the UI, e.g. the view itself
208         segment_selector_->setValue(segment_id);
209 }
210
211 void StandardBar::on_segment_display_mode_changed(bool segment_selectable)
212 {
213         segment_selector_->setReadOnly(!segment_selectable);
214 }
215
216 } // namespace trace
217 } // namespace views
218 } // namespace pv