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 Note: Widgets deriving from this class should connect slotActiveMonitorChanged
44 to the appropriate signal.
46 If you intend to write an own widget inheriting from this one, please read
47 the comments on the unimplemented methods carefully. They are not only here
48 for optical amusement, but also contain important information.
51 #ifndef ABSTRACTSCOPEWIDGET_H
52 #define ABSTRACTSCOPEWIDGET_H
62 class AbstractScopeWidget : public QWidget
67 AbstractScopeWidget(Monitor *projMonitor, Monitor *clipMonitor, bool trackMouse = false, QWidget *parent = 0);
68 virtual ~AbstractScopeWidget(); // Must be virtual because of inheritance, to avoid memory leaks
69 QPalette m_scopePalette;
71 /** Initializes widget settings (reads configuration).
72 Has to be called in the implementing object. */
75 /** Does this scope have auto-refresh enabled */
76 bool autoRefreshEnabled();
78 ///// Unimplemented /////
80 virtual QString widgetName() const = 0;
83 static const QPen penThick;
84 static const QPen penThin;
85 static const QPen penLight;
86 static const QPen penLightDots;
87 static const QPen penDark;
88 static const QPen penDarkDots;
93 Monitor *m_projMonitor;
94 Monitor *m_clipMonitor;
95 Render *m_activeRender;
98 /** The context menu. Feel free to add new entries in your implementation. */
101 /** Enables auto refreshing of the scope.
102 This is when a new frame is shown on the active monitor.
103 Resize events always force a recalculation. */
104 QAction *m_aAutoRefresh;
106 /** Realtime rendering. Should be disabled if it is not supported.
107 Use the accelerationFactor variable passed to the render functions as a hint of
108 how many times faster the scope should be calculated. */
109 QAction *m_aRealtime;
111 /** The mouse position; Updated when the mouse enters the widget
112 AND mouse tracking has been enabled. */
114 /** Knows whether the mouse currently lies within the widget or not.
115 Can e.g. be used for drawing a HUD only when the mouse is in the widget. */
116 bool m_mouseWithinWidget;
118 /** Offset from the widget's borders */
121 /** The rect on the widget we're painting in.
122 Can be used by the implementing widget, e.g. in the render methods.
123 Is updated when necessary (size changes). */
126 /** Images storing the calculated layers. Will be used on repaint events. */
129 QImage m_imgBackground;
131 /** The acceleration factors can be accessed also by other renderer tasks,
132 e.g. to display the scope's acceleration factor in the HUD renderer. */
133 int m_accelFactorHUD;
134 int m_accelFactorScope;
135 int m_accelFactorBackground;
137 /** Reads the widget's configuration.
138 Can be extended in the implementing subclass (make sure to run readConfig as well). */
139 virtual void readConfig();
140 /** Writes the widget configuration.
141 Implementing widgets have to implement an own method and run it in their destructor. */
143 /** Identifier for the widget's configuration. */
144 QString configName();
147 ///// Unimplemented Methods /////
149 /** Where on the widget we can paint in.
150 May also update other variables that depend on the widget's size. */
151 virtual QRect scopeRect() = 0;
153 /** @brief HUD renderer. Must emit signalHUDRenderingFinished(). @see renderScope */
154 virtual QImage renderHUD(uint accelerationFactor) = 0;
155 /** @brief Scope renderer. Must emit signalScopeRenderingFinished()
156 when calculation has finished, to allow multi-threading.
157 accelerationFactor hints how much faster than usual the calculation should be accomplished, if possible. */
158 virtual QImage renderScope(uint accelerationFactor) = 0;
159 /** @brief Background renderer. Must emit signalBackgroundRenderingFinished(). @see renderScope */
160 virtual QImage renderBackground(uint accelerationFactor) = 0;
162 /** Must return true if the HUD layer depends on the input monitor.
163 If it does not, then it does not need to be re-calculated when
164 a new frame from the monitor is incoming. */
165 virtual bool isHUDDependingOnInput() const = 0;
166 /** @see isHUDDependingOnInput() */
167 virtual bool isScopeDependingOnInput() const = 0;
168 /** @see isHUDDependingOnInput() */
169 virtual bool isBackgroundDependingOnInput() const = 0;
171 ///// Can be reimplemented /////
172 /** Calculates the acceleration factor to be used by the render thread.
173 This method can be refined in the subclass if required. */
174 virtual uint calculateAccelFactorHUD(uint oldMseconds, uint oldFactor);
175 virtual uint calculateAccelFactorScope(uint oldMseconds, uint oldFactor);
176 virtual uint calculateAccelFactorBackground(uint oldMseconds, uint oldFactor);
178 ///// Reimplemented /////
180 void mouseMoveEvent(QMouseEvent *);
181 void leaveEvent(QEvent *);
182 void mouseReleaseEvent(QMouseEvent *);
183 void paintEvent(QPaintEvent *);
184 void resizeEvent(QResizeEvent *);
185 void showEvent(QShowEvent *); // Called when the widget is activated via the Menu entry
186 // void raise(); // Called only when manually calling the event -> useless
190 /** Forces an update of all layers. */
191 void forceUpdate(bool doUpdate = true);
192 void forceUpdateHUD();
193 void forceUpdateScope();
194 void forceUpdateBackground();
195 void slotAutoRefreshToggled(bool);
198 /** mseconds represent the time taken for the calculation,
199 accelerationFactor is the acceleration factor that has been used for this calculation. */
200 void signalHUDRenderingFinished(uint mseconds, uint accelerationFactor);
201 void signalScopeRenderingFinished(uint mseconds, uint accelerationFactor);
202 void signalBackgroundRenderingFinished(uint mseconds, uint accelerationFactor);
204 /** For the mouse position itself see m_mousePos.
205 To check whether the mouse has leaved the widget, see m_mouseWithinWidget. */
206 void signalMousePositionChanged();
208 /** Do we need the renderer to send its frames to us? */
209 void requestAutoRefresh(bool);
213 /** Counts the number of frames that have been rendered in the active monitor.
214 The frame number will be reset when the calculation starts for the current frame. */
215 QAtomicInt m_newHUDFrames;
216 QAtomicInt m_newScopeFrames;
217 QAtomicInt m_newBackgroundFrames;
219 /** Counts the number of updates that, unlike new frames, force a recalculation
220 of the scope, like for example a resize event. */
221 QAtomicInt m_newHUDUpdates;
222 QAtomicInt m_newScopeUpdates;
223 QAtomicInt m_newBackgroundUpdates;
225 /** The semaphores ensure that the QFutures for the HUD/Scope/Background threads cannot
226 be assigned a new thread while it is still running. (Could cause deadlocks and other
227 nasty things known from parallelism.) */
228 QSemaphore m_semaphoreHUD;
229 QSemaphore m_semaphoreScope;
230 QSemaphore m_semaphoreBackground;
232 QFuture<QImage> m_threadHUD;
233 QFuture<QImage> m_threadScope;
234 QFuture<QImage> m_threadBackground;
236 bool initialDimensionUpdateDone;
237 bool m_requestForcedUpdate;
241 QString m_widgetName;
243 void prodHUDThread();
244 void prodScopeThread();
245 void prodBackgroundThread();
248 /** @brief Must be called when the active monitor has shown a new frame.
249 This slot must be connected in the implementing class, it is *not*
250 done in this abstract class. */
251 void slotActiveMonitorChanged(bool isClipMonitor);
254 void customContextMenuRequested(const QPoint &pos);
255 /** To be called when a new frame has been received.
256 The scope then decides whether and when it wants to recalculate the scope, depending
257 on whether it is currently visible and whether a calculation thread is already running. */
258 void slotRenderZoneUpdated();
259 void slotRenderZoneUpdated(QImage);
260 /** The following slots are called when rendering of a component has finished. They e.g. update
261 the widget and decide whether to immediately restart the calculation thread. */
262 void slotHUDRenderingFinished(uint mseconds, uint accelerationFactor);
263 void slotScopeRenderingFinished(uint mseconds, uint accelerationFactor);
264 void slotBackgroundRenderingFinished(uint mseconds, uint accelerationFactor);
266 /** Resets the acceleration factors to 1 when realtime rendering is disabled. */
267 void slotResetRealtimeFactor(bool realtimeChecked);
271 #endif // ABSTRACTSCOPEWIDGET_H