2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEW_HPP
21 #define PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEW_HPP
28 #include <unordered_map>
31 #include <QAbstractScrollArea>
35 #include <pv/data/signaldata.hpp>
36 #include <pv/views/viewbase.hpp>
37 #include <pv/util.hpp>
39 #include "cursorpair.hpp"
41 #include "tracetreeitemowner.hpp"
64 class CustomAbstractScrollArea : public QAbstractScrollArea {
68 CustomAbstractScrollArea(QWidget *parent = 0);
69 void setViewportMargins(int left, int top, int right, int bottom);
70 bool viewportEvent(QEvent *event);
73 class View : public ViewBase, public TraceTreeItemOwner {
78 TraceTreeItemHExtentsChanged = 1,
79 TraceTreeItemVExtentsChanged = 2
83 static const pv::util::Timestamp MaxScale;
84 static const pv::util::Timestamp MinScale;
86 static const int MaxScrollValue;
87 static const int MaxViewAutoUpdateRate;
89 static const int ScaleUnits[3];
92 explicit View(Session &session, QWidget *parent = 0);
95 const Session& session() const;
98 * Returns the signals contained in this view.
100 std::unordered_set< std::shared_ptr<Signal> > signals() const;
102 virtual void clear_signals();
104 virtual void add_signal(const std::shared_ptr<Signal> signal);
107 virtual void clear_decode_signals();
109 virtual void add_decode_signal(std::shared_ptr<data::SignalBase> signalbase);
111 virtual void remove_decode_signal(std::shared_ptr<data::SignalBase> signalbase);
115 * Returns the view of the owner.
117 virtual View* view();
120 * Returns the view of the owner.
122 virtual const View* view() const;
124 Viewport* viewport();
126 const Viewport* viewport() const;
128 virtual void save_settings(QSettings &settings) const;
130 virtual void restore_settings(QSettings &settings);
133 * Gets a list of time markers.
135 std::vector< std::shared_ptr<TimeItem> > time_items() const;
138 * Returns the view time scale in seconds per pixel.
140 double scale() const;
143 * Returns the time offset of the left edge of the view in
146 const pv::util::Timestamp& offset() const;
149 * Returns the vertical scroll offset.
151 int owner_visual_v_offset() const;
154 * Sets the visual v-offset.
156 void set_v_offset(int offset);
159 * Returns the SI prefix to apply to the graticule time markings.
161 pv::util::SIPrefix tick_prefix() const;
164 * Returns the number of fractional digits shown for the time markings.
166 unsigned int tick_precision() const;
169 * Returns period of the graticule time markings.
171 const pv::util::Timestamp& tick_period() const;
174 * Returns the unit of time currently used.
176 util::TimeUnit time_unit() const;
179 * Returns the number of nested parents that this row item owner has.
181 unsigned int depth() const;
183 void zoom(double steps);
184 void zoom(double steps, int offset);
186 void zoom_fit(bool gui_state);
188 void zoom_one_to_one();
191 * Sets the scale and offset.
192 * @param scale The new view scale in seconds per pixel.
193 * @param offset The view time offset in seconds.
195 void set_scale_offset(double scale, const pv::util::Timestamp& offset);
197 std::set< std::shared_ptr<pv::data::SignalData> >
198 get_visible_data() const;
200 std::pair<pv::util::Timestamp, pv::util::Timestamp> get_time_extents() const;
203 * Enables or disables sticky scrolling, i.e. the view always shows
204 * the most recent samples when capturing data.
206 void enable_sticky_scrolling(bool state);
209 * Enables or disables coloured trace backgrounds. If they're not
210 * coloured then they will use alternating colors.
212 void enable_coloured_bg(bool state);
215 * Returns true if cursors are displayed. false otherwise.
217 bool cursors_shown() const;
220 * Shows or hides the cursors.
222 void show_cursors(bool show = true);
225 * Moves the cursors to a convenient position in the view.
227 void centre_cursors();
230 * Returns a reference to the pair of cursors.
232 std::shared_ptr<CursorPair> cursors() const;
235 * Adds a new flag at a specified time.
237 void add_flag(const pv::util::Timestamp& time);
240 * Removes a flag from the list.
242 void remove_flag(std::shared_ptr<Flag> flag);
245 * Gets the list of flags.
247 std::vector< std::shared_ptr<Flag> > flags() const;
249 const QPoint& hover_point() const;
251 void restack_all_trace_tree_items();
254 void hover_point_changed();
256 void selection_changed();
258 /// Emitted when the offset changed.
259 void offset_changed();
261 /// Emitted when the scale changed.
262 void scale_changed();
264 void sticky_scrolling_changed(bool state);
266 void always_zoom_to_fit_changed(bool state);
268 /// Emitted when the tick_prefix changed.
269 void tick_prefix_changed();
271 /// Emitted when the tick_precision changed.
272 void tick_precision_changed();
274 /// Emitted when the tick_period changed.
275 void tick_period_changed();
277 /// Emitted when the time_unit changed.
278 void time_unit_changed();
281 void trigger_event(util::Timestamp location);
284 void get_scroll_layout(double &length, pv::util::Timestamp &offset) const;
287 * Simultaneously sets the zoom and offset.
288 * @param scale The scale to set the view to in seconds per pixel. This
289 * value is clamped between MinScale and MaxScale.
290 * @param offset The offset of the left edge of the view in seconds.
292 void set_zoom(double scale, int offset);
295 * Find a tick spacing and number formatting that does not cause
296 * the values to collide.
298 void calculate_tick_spacing();
300 void update_scroll();
304 void set_scroll_default();
306 void update_layout();
308 TraceTreeItemOwner* find_prevalent_trace_group(
309 const std::shared_ptr<sigrok::ChannelGroup> &group,
310 const std::unordered_map<std::shared_ptr<data::SignalBase>,
311 std::shared_ptr<Signal> > &signal_map);
313 static std::vector< std::shared_ptr<Trace> >
314 extract_new_traces_for_channels(
315 const std::vector< std::shared_ptr<sigrok::Channel> > &channels,
316 const std::unordered_map<std::shared_ptr<data::SignalBase>,
317 std::shared_ptr<Signal> > &signal_map,
318 std::set< std::shared_ptr<Trace> > &add_list);
320 void determine_time_unit();
322 bool eventFilter(QObject *object, QEvent *event);
324 void resizeEvent(QResizeEvent *event);
327 void row_item_appearance_changed(bool label, bool content);
328 void time_item_appearance_changed(bool label, bool content);
330 void extents_changed(bool horz, bool vert);
334 void h_scroll_value_changed(int value);
335 void v_scroll_value_changed();
337 void signals_changed();
338 void capture_state_updated(int state);
341 void perform_delayed_view_update();
343 void process_sticky_events();
345 void on_hover_point_changed();
348 * Sets the 'offset_' member and emits the 'offset_changed'
351 void set_offset(const pv::util::Timestamp& offset);
354 * Sets the 'scale_' member and emits the 'scale_changed'
357 void set_scale(double scale);
360 * Sets the 'tick_prefix_' member and emits the 'tick_prefix_changed'
363 void set_tick_prefix(pv::util::SIPrefix tick_prefix);
366 * Sets the 'tick_precision_' member and emits the 'tick_precision_changed'
369 void set_tick_precision(unsigned tick_precision);
372 * Sets the 'tick_period_' member and emits the 'tick_period_changed'
375 void set_tick_period(const pv::util::Timestamp& tick_period);
378 * Sets the 'time_unit' member and emits the 'time_unit_changed'
381 void set_time_unit(pv::util::TimeUnit time_unit);
388 std::unordered_set< std::shared_ptr<Signal> > signals_;
391 std::vector< std::shared_ptr<DecodeTrace> > decode_traces_;
394 CustomAbstractScrollArea scrollarea_;
396 /// The view time scale in seconds per pixel.
399 /// The view time offset in seconds.
400 pv::util::Timestamp offset_;
402 bool updating_scroll_;
403 bool sticky_scrolling_;
404 bool always_zoom_to_fit_;
405 QTimer delayed_view_updater_;
407 pv::util::Timestamp tick_period_;
408 pv::util::SIPrefix tick_prefix_;
409 unsigned int tick_precision_;
410 util::TimeUnit time_unit_;
413 std::shared_ptr<CursorPair> cursors_;
415 std::list< std::shared_ptr<Flag> > flags_;
416 char next_flag_text_;
418 std::vector< std::shared_ptr<TriggerMarker> > trigger_markers_;
422 unsigned int sticky_events_;
423 QTimer lazy_event_handler_;
425 // This is true when the defaults couldn't be set due to insufficient info
426 bool scroll_needs_defaults_;
428 // A nonzero value indicates the v offset to restore. See View::resizeEvent()
431 bool size_finalized_;
434 } // namespace TraceView
438 #endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEW_HPP