1 /***************************************************************************
2 * Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com) *
3 * This file is part of kdenlive. See www.kdenlive.org. *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
12 This abstract widget is a proof that abstract things sometimes *are* useful.
14 The widget expects three layers which
15 * Will be painted on top of each other on each update
16 * Are rendered in a separate thread so that the UI is not blocked
17 * Are rendered only if necessary (e.g., if a layer does not depend
18 on input images, it will not be re-rendered for incoming frames)
20 The layer order is as follows:
25 ---------------------------
30 ---------------------------
35 ---------------------------
37 Colors of Scope Widgets are defined in here (and thus don't need to be
38 re-defined in the implementation of the widget's .ui file).
40 The custom context menu already contains entries, like for enabling auto-
41 refresh. It can certainly be extended in the implementation of the widget.
43 If you intend to write an own widget inheriting from this one, please read
44 the comments on the unimplemented methods carefully. They are not only here
45 for optical amusement, but also contain important information.
48 #ifndef ABSTRACTSCOPEWIDGET_H
49 #define ABSTRACTSCOPEWIDGET_H
56 class AbstractScopeWidget : public QWidget
61 /** trackMouse enables mouse tracking; The variables m_mousePos and m_mouseWithinWidget will be set
62 if mouse tracking is enabled. See also signalMousePositionChanged(). */
63 AbstractScopeWidget(bool trackMouse = false, QWidget *parent = 0);
64 virtual ~AbstractScopeWidget(); // Must be virtual because of inheritance, to avoid memory leaks
67 enum RescaleDirection { North, Northeast, East, Southeast };
70 QPalette m_scopePalette;
72 /** Initializes widget settings (reads configuration).
73 Has to be called in the implementing object. */
76 /** Tell whether this scope has auto-refresh enabled. Required for determining whether
77 new data (e.g. an image frame) has to be delivered to this widget. */
78 bool autoRefreshEnabled();
80 ///// Unimplemented /////
82 virtual QString widgetName() const = 0;
85 static const QPen penThick;
86 static const QPen penThin;
87 static const QPen penLight;
88 static const QPen penLightDots;
89 static const QPen penLighter;
90 static const QPen penDark;
91 static const QPen penDarkDots;
92 static const QPen penBackground;
94 static const QString directions[]; // Mainly for debug output
99 /** The context menu. Feel free to add new entries in your implementation. */
102 /** Enables auto refreshing of the scope.
103 This is when fresh data is incoming.
104 Resize events always force a recalculation. */
105 QAction *m_aAutoRefresh;
107 /** Realtime rendering. Should be disabled if it is not supported.
108 Use the accelerationFactor variable passed to the render functions as a hint of
109 how many times faster the scope should be calculated. */
110 QAction *m_aRealtime;
112 /** The mouse position; Updated when the mouse enters the widget
113 AND mouse tracking has been enabled. */
115 /** Knows whether the mouse currently lies within the widget or not.
116 Can e.g. be used for drawing a HUD only when the mouse is in the widget. */
117 bool m_mouseWithinWidget;
119 /** Offset from the widget's borders */
122 /** The rect on the widget we're painting in.
123 Can be used by the implementing widget, e.g. in the render methods.
124 Is updated when necessary (size changes). */
127 /** Images storing the calculated layers. Will be used on repaint events. */
130 QImage m_imgBackground;
132 /** The acceleration factors can be accessed also by other renderer tasks,
133 e.g. to display the scope's acceleration factor in the HUD renderer. */
134 int m_accelFactorHUD;
135 int m_accelFactorScope;
136 int m_accelFactorBackground;
138 /** Reads the widget's configuration.
139 Can be extended in the implementing subclass (make sure to run readConfig as well). */
140 virtual void readConfig();
141 /** Writes the widget configuration.
142 Implementing widgets have to implement an own method and run it in their destructor. */
144 /** Identifier for the widget's configuration. */
145 QString configName();
148 ///// Unimplemented Methods /////
150 /** Where on the widget we can paint in.
151 May also update other variables, like m_scopeRect or custom ones,
152 that have to change together with the widget's size. */
153 virtual QRect scopeRect() = 0;
155 /** @brief HUD renderer. Must emit signalHUDRenderingFinished(). @see renderScope */
156 virtual QImage renderHUD(uint accelerationFactor) = 0;
157 /** @brief Scope renderer. Must emit signalScopeRenderingFinished()
158 when calculation has finished, to allow multi-threading.
159 accelerationFactor hints how much faster than usual the calculation should be accomplished, if possible. */
160 virtual QImage renderScope(uint accelerationFactor) = 0;
161 /** @brief Background renderer. Must emit signalBackgroundRenderingFinished(). @see renderScope */
162 virtual QImage renderBackground(uint accelerationFactor) = 0;
164 /** Must return true if the HUD layer depends on the input data.
165 If it does not, then it does not need to be re-calculated when
166 fresh data is incoming. */
167 virtual bool isHUDDependingOnInput() const = 0;
168 /** @see isHUDDependingOnInput() */
169 virtual bool isScopeDependingOnInput() const = 0;
170 /** @see isHUDDependingOnInput() */
171 virtual bool isBackgroundDependingOnInput() const = 0;
173 ///// Can be reimplemented /////
174 /** Calculates the acceleration factor to be used by the render thread.
175 This method can be refined in the subclass if required. */
176 virtual uint calculateAccelFactorHUD(uint oldMseconds, uint oldFactor);
177 virtual uint calculateAccelFactorScope(uint oldMseconds, uint oldFactor);
178 virtual uint calculateAccelFactorBackground(uint oldMseconds, uint oldFactor);
180 /** The Abstract Scope will try to detect the movement direction when dragging on the widget with the mouse.
181 As soon as the direction is determined it will execute this method. Can be used e.g. for re-scaling content.
182 This is just a dummy function, re-implement to add functionality. */
183 virtual void handleMouseDrag(const QPoint movement, const RescaleDirection rescaleDirection, const Qt::KeyboardModifiers rescaleModifiers);
185 ///// Reimplemented /////
187 void mouseMoveEvent(QMouseEvent *event);
188 void mousePressEvent(QMouseEvent *event);
189 void mouseReleaseEvent(QMouseEvent *event);
190 void leaveEvent(QEvent *);
191 void paintEvent(QPaintEvent *);
192 void resizeEvent(QResizeEvent *);
193 void showEvent(QShowEvent *); // Called when the widget is activated via the Menu entry
194 // void raise(); // Called only when manually calling the event -> useless
198 /** Forces an update of all layers. */
199 void forceUpdate(bool doUpdate = true);
200 void forceUpdateHUD();
201 void forceUpdateScope();
202 void forceUpdateBackground();
203 void slotAutoRefreshToggled(bool);
206 /** mseconds represent the time taken for the calculation,
207 accelerationFactor is the acceleration factor that has been used for this calculation. */
208 void signalHUDRenderingFinished(uint mseconds, uint accelerationFactor);
209 void signalScopeRenderingFinished(uint mseconds, uint accelerationFactor);
210 void signalBackgroundRenderingFinished(uint mseconds, uint accelerationFactor);
212 /** For the mouse position itself see m_mousePos.
213 To check whether the mouse has leaved the widget, see m_mouseWithinWidget.
214 This signal is typically connected to forceUpdateHUD(). */
215 void signalMousePositionChanged();
217 /** Do we need the renderer to send its frames to us? */
218 void requestAutoRefresh(bool);
222 /** Counts the number of data frames that have been rendered in the active monitor.
223 The frame number will be reset when the calculation starts for the current data set. */
224 QAtomicInt m_newHUDFrames;
225 QAtomicInt m_newScopeFrames;
226 QAtomicInt m_newBackgroundFrames;
228 /** Counts the number of updates that, unlike new frames, force a recalculation
229 of the scope, like for example a resize event. */
230 QAtomicInt m_newHUDUpdates;
231 QAtomicInt m_newScopeUpdates;
232 QAtomicInt m_newBackgroundUpdates;
234 /** The semaphores ensure that the QFutures for the HUD/Scope/Background threads cannot
235 be assigned a new thread while it is still running. (Could cause deadlocks and other
236 nasty things known from parallelism.) */
237 QSemaphore m_semaphoreHUD;
238 QSemaphore m_semaphoreScope;
239 QSemaphore m_semaphoreBackground;
241 QFuture<QImage> m_threadHUD;
242 QFuture<QImage> m_threadScope;
243 QFuture<QImage> m_threadBackground;
245 bool initialDimensionUpdateDone;
246 bool m_requestForcedUpdate;
250 QString m_widgetName;
252 void prodHUDThread();
253 void prodScopeThread();
254 void prodBackgroundThread();
256 ///// Movement detection /////
257 const int m_rescaleMinDist;
258 const float m_rescaleVerticalThreshold;
260 bool m_rescaleActive;
261 bool m_rescalePropertiesLocked;
262 bool m_rescaleFirstRescaleDone;
263 short m_rescaleScale;
264 Qt::KeyboardModifiers m_rescaleModifiers;
265 RescaleDirection m_rescaleDirection;
266 QPoint m_rescaleStartPoint;
270 void customContextMenuRequested(const QPoint &pos);
271 /** To be called when a new frame has been received.
272 The scope then decides whether and when it wants to recalculate the scope, depending
273 on whether it is currently visible and whether a calculation thread is already running. */
274 void slotRenderZoneUpdated();
275 /** The following slots are called when rendering of a component has finished. They e.g. update
276 the widget and decide whether to immediately restart the calculation thread. */
277 void slotHUDRenderingFinished(uint mseconds, uint accelerationFactor);
278 void slotScopeRenderingFinished(uint mseconds, uint accelerationFactor);
279 void slotBackgroundRenderingFinished(uint mseconds, uint accelerationFactor);
281 /** Resets the acceleration factors to 1 when realtime rendering is disabled. */
282 void slotResetRealtimeFactor(bool realtimeChecked);
286 #endif // ABSTRACTSCOPEWIDGET_H