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 * Toggle sticky scrolling.
211 void toggle_sticky_scrolling(void);
214 * Get current coloured_bg state. Returns true if coloured backgrounds are enabled.
216 bool get_coloured_bg(void);
219 * Enables or disables coloured trace backgrounds. If they're not
220 * coloured then they will use alternating colors.
222 void enable_coloured_bg(bool state);
225 * Toggle coloured backgrounds.
227 void toggle_coloured_bg(void);
230 * Returns true if cursors are displayed. false otherwise.
232 bool cursors_shown() const;
235 * Shows or hides the cursors.
237 void show_cursors(bool show = true);
240 * Moves the cursors to a convenient position in the view.
242 void centre_cursors();
245 * Returns a reference to the pair of cursors.
247 std::shared_ptr<CursorPair> cursors() const;
250 * Adds a new flag at a specified time.
252 void add_flag(const pv::util::Timestamp& time);
255 * Removes a flag from the list.
257 void remove_flag(std::shared_ptr<Flag> flag);
260 * Gets the list of flags.
262 std::vector< std::shared_ptr<Flag> > flags() const;
264 const QPoint& hover_point() const;
266 void restack_all_trace_tree_items();
269 void hover_point_changed();
271 void selection_changed();
273 /// Emitted when the offset changed.
274 void offset_changed();
276 /// Emitted when the scale changed.
277 void scale_changed();
279 void sticky_scrolling_changed(bool state);
281 void always_zoom_to_fit_changed(bool state);
283 /// Emitted when the tick_prefix changed.
284 void tick_prefix_changed();
286 /// Emitted when the tick_precision changed.
287 void tick_precision_changed();
289 /// Emitted when the tick_period changed.
290 void tick_period_changed();
292 /// Emitted when the time_unit changed.
293 void time_unit_changed();
296 void trigger_event(util::Timestamp location);
299 void get_scroll_layout(double &length, pv::util::Timestamp &offset) const;
302 * Simultaneously sets the zoom and offset.
303 * @param scale The scale to set the view to in seconds per pixel. This
304 * value is clamped between MinScale and MaxScale.
305 * @param offset The offset of the left edge of the view in seconds.
307 void set_zoom(double scale, int offset);
310 * Find a tick spacing and number formatting that does not cause
311 * the values to collide.
313 void calculate_tick_spacing();
315 void update_scroll();
319 void set_scroll_default();
321 void update_layout();
323 TraceTreeItemOwner* find_prevalent_trace_group(
324 const std::shared_ptr<sigrok::ChannelGroup> &group,
325 const std::unordered_map<std::shared_ptr<data::SignalBase>,
326 std::shared_ptr<Signal> > &signal_map);
328 static std::vector< std::shared_ptr<Trace> >
329 extract_new_traces_for_channels(
330 const std::vector< std::shared_ptr<sigrok::Channel> > &channels,
331 const std::unordered_map<std::shared_ptr<data::SignalBase>,
332 std::shared_ptr<Signal> > &signal_map,
333 std::set< std::shared_ptr<Trace> > &add_list);
335 void determine_time_unit();
337 bool eventFilter(QObject *object, QEvent *event);
339 void resizeEvent(QResizeEvent *event);
342 void row_item_appearance_changed(bool label, bool content);
343 void time_item_appearance_changed(bool label, bool content);
345 void extents_changed(bool horz, bool vert);
349 void h_scroll_value_changed(int value);
350 void v_scroll_value_changed();
352 void signals_changed();
353 void capture_state_updated(int state);
356 void perform_delayed_view_update();
358 void process_sticky_events();
360 void on_hover_point_changed();
363 * Sets the 'offset_' member and emits the 'offset_changed'
366 void set_offset(const pv::util::Timestamp& offset);
369 * Sets the 'scale_' member and emits the 'scale_changed'
372 void set_scale(double scale);
375 * Sets the 'tick_prefix_' member and emits the 'tick_prefix_changed'
378 void set_tick_prefix(pv::util::SIPrefix tick_prefix);
381 * Sets the 'tick_precision_' member and emits the 'tick_precision_changed'
384 void set_tick_precision(unsigned tick_precision);
387 * Sets the 'tick_period_' member and emits the 'tick_period_changed'
390 void set_tick_period(const pv::util::Timestamp& tick_period);
393 * Sets the 'time_unit' member and emits the 'time_unit_changed'
396 void set_time_unit(pv::util::TimeUnit time_unit);
403 std::unordered_set< std::shared_ptr<Signal> > signals_;
406 std::vector< std::shared_ptr<DecodeTrace> > decode_traces_;
409 CustomAbstractScrollArea scrollarea_;
411 /// The view time scale in seconds per pixel.
414 /// The view time offset in seconds.
415 pv::util::Timestamp offset_;
417 bool updating_scroll_;
418 bool sticky_scrolling_;
420 bool always_zoom_to_fit_;
421 QTimer delayed_view_updater_;
423 pv::util::Timestamp tick_period_;
424 pv::util::SIPrefix tick_prefix_;
425 unsigned int tick_precision_;
426 util::TimeUnit time_unit_;
429 std::shared_ptr<CursorPair> cursors_;
431 std::list< std::shared_ptr<Flag> > flags_;
432 char next_flag_text_;
434 std::vector< std::shared_ptr<TriggerMarker> > trigger_markers_;
438 unsigned int sticky_events_;
439 QTimer lazy_event_handler_;
441 // This is true when the defaults couldn't be set due to insufficient info
442 bool scroll_needs_defaults_;
444 // A nonzero value indicates the v offset to restore. See View::resizeEvent()
447 bool size_finalized_;
450 } // namespace TraceView
454 #endif // PULSEVIEW_PV_VIEWS_TRACEVIEW_VIEW_HPP