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 AbstractScopeWidget(bool trackMouse = false, QWidget *parent = 0);
62 virtual ~AbstractScopeWidget(); // Must be virtual because of inheritance, to avoid memory leaks
63 QPalette m_scopePalette;
65 /** Initializes widget settings (reads configuration).
66 Has to be called in the implementing object. */
69 /** Does this scope have auto-refresh enabled */
70 bool autoRefreshEnabled();
72 ///// Unimplemented /////
74 virtual QString widgetName() const = 0;
77 static const QPen penThick;
78 static const QPen penThin;
79 static const QPen penLight;
80 static const QPen penLightDots;
81 static const QPen penDark;
82 static const QPen penDarkDots;
87 /** The context menu. Feel free to add new entries in your implementation. */
90 /** Enables auto refreshing of the scope.
91 This is when fresh data is incoming.
92 Resize events always force a recalculation. */
93 QAction *m_aAutoRefresh;
95 /** Realtime rendering. Should be disabled if it is not supported.
96 Use the accelerationFactor variable passed to the render functions as a hint of
97 how many times faster the scope should be calculated. */
100 /** The mouse position; Updated when the mouse enters the widget
101 AND mouse tracking has been enabled. */
103 /** Knows whether the mouse currently lies within the widget or not.
104 Can e.g. be used for drawing a HUD only when the mouse is in the widget. */
105 bool m_mouseWithinWidget;
107 /** Offset from the widget's borders */
110 /** The rect on the widget we're painting in.
111 Can be used by the implementing widget, e.g. in the render methods.
112 Is updated when necessary (size changes). */
115 /** Images storing the calculated layers. Will be used on repaint events. */
118 QImage m_imgBackground;
120 /** The acceleration factors can be accessed also by other renderer tasks,
121 e.g. to display the scope's acceleration factor in the HUD renderer. */
122 int m_accelFactorHUD;
123 int m_accelFactorScope;
124 int m_accelFactorBackground;
126 /** Reads the widget's configuration.
127 Can be extended in the implementing subclass (make sure to run readConfig as well). */
128 virtual void readConfig();
129 /** Writes the widget configuration.
130 Implementing widgets have to implement an own method and run it in their destructor. */
132 /** Identifier for the widget's configuration. */
133 QString configName();
136 ///// Unimplemented Methods /////
138 /** Where on the widget we can paint in.
139 May also update other variables that depend on the widget's size. */
140 virtual QRect scopeRect() = 0;
142 /** @brief HUD renderer. Must emit signalHUDRenderingFinished(). @see renderScope */
143 virtual QImage renderHUD(uint accelerationFactor) = 0;
144 /** @brief Scope renderer. Must emit signalScopeRenderingFinished()
145 when calculation has finished, to allow multi-threading.
146 accelerationFactor hints how much faster than usual the calculation should be accomplished, if possible. */
147 virtual QImage renderScope(uint accelerationFactor) = 0;
148 /** @brief Background renderer. Must emit signalBackgroundRenderingFinished(). @see renderScope */
149 virtual QImage renderBackground(uint accelerationFactor) = 0;
151 /** Must return true if the HUD layer depends on the input data.
152 If it does not, then it does not need to be re-calculated when
153 fresh data is incoming. */
154 virtual bool isHUDDependingOnInput() const = 0;
155 /** @see isHUDDependingOnInput() */
156 virtual bool isScopeDependingOnInput() const = 0;
157 /** @see isHUDDependingOnInput() */
158 virtual bool isBackgroundDependingOnInput() const = 0;
160 ///// Can be reimplemented /////
161 /** Calculates the acceleration factor to be used by the render thread.
162 This method can be refined in the subclass if required. */
163 virtual uint calculateAccelFactorHUD(uint oldMseconds, uint oldFactor);
164 virtual uint calculateAccelFactorScope(uint oldMseconds, uint oldFactor);
165 virtual uint calculateAccelFactorBackground(uint oldMseconds, uint oldFactor);
167 ///// Reimplemented /////
169 void mouseMoveEvent(QMouseEvent *);
170 void leaveEvent(QEvent *);
171 void mouseReleaseEvent(QMouseEvent *);
172 void paintEvent(QPaintEvent *);
173 void resizeEvent(QResizeEvent *);
174 void showEvent(QShowEvent *); // Called when the widget is activated via the Menu entry
175 // void raise(); // Called only when manually calling the event -> useless
179 /** Forces an update of all layers. */
180 void forceUpdate(bool doUpdate = true);
181 void forceUpdateHUD();
182 void forceUpdateScope();
183 void forceUpdateBackground();
184 virtual void slotAutoRefreshToggled(bool);
187 /** mseconds represent the time taken for the calculation,
188 accelerationFactor is the acceleration factor that has been used for this calculation. */
189 void signalHUDRenderingFinished(uint mseconds, uint accelerationFactor);
190 void signalScopeRenderingFinished(uint mseconds, uint accelerationFactor);
191 void signalBackgroundRenderingFinished(uint mseconds, uint accelerationFactor);
193 /** For the mouse position itself see m_mousePos.
194 To check whether the mouse has leaved the widget, see m_mouseWithinWidget. */
195 void signalMousePositionChanged();
197 /** Do we need the renderer to send its frames to us? */
198 void requestAutoRefresh(bool);
202 /** Counts the number of data frames that have been rendered in the active monitor.
203 The frame number will be reset when the calculation starts for the current data set. */
204 QAtomicInt m_newHUDFrames;
205 QAtomicInt m_newScopeFrames;
206 QAtomicInt m_newBackgroundFrames;
208 /** Counts the number of updates that, unlike new frames, force a recalculation
209 of the scope, like for example a resize event. */
210 QAtomicInt m_newHUDUpdates;
211 QAtomicInt m_newScopeUpdates;
212 QAtomicInt m_newBackgroundUpdates;
214 /** The semaphores ensure that the QFutures for the HUD/Scope/Background threads cannot
215 be assigned a new thread while it is still running. (Could cause deadlocks and other
216 nasty things known from parallelism.) */
217 QSemaphore m_semaphoreHUD;
218 QSemaphore m_semaphoreScope;
219 QSemaphore m_semaphoreBackground;
221 QFuture<QImage> m_threadHUD;
222 QFuture<QImage> m_threadScope;
223 QFuture<QImage> m_threadBackground;
225 bool initialDimensionUpdateDone;
226 bool m_requestForcedUpdate;
230 QString m_widgetName;
232 void prodHUDThread();
233 void prodScopeThread();
234 void prodBackgroundThread();
237 void customContextMenuRequested(const QPoint &pos);
238 /** To be called when a new frame has been received.
239 The scope then decides whether and when it wants to recalculate the scope, depending
240 on whether it is currently visible and whether a calculation thread is already running. */
241 void slotRenderZoneUpdated();
242 void slotRenderZoneUpdated(QImage);//TODO remove
243 /** The following slots are called when rendering of a component has finished. They e.g. update
244 the widget and decide whether to immediately restart the calculation thread. */
245 void slotHUDRenderingFinished(uint mseconds, uint accelerationFactor);
246 void slotScopeRenderingFinished(uint mseconds, uint accelerationFactor);
247 void slotBackgroundRenderingFinished(uint mseconds, uint accelerationFactor);
249 /** Resets the acceleration factors to 1 when realtime rendering is disabled. */
250 void slotResetRealtimeFactor(bool realtimeChecked);
254 #endif // ABSTRACTSCOPEWIDGET_H