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, QWidget *parent = 0);
68 virtual ~AbstractScopeWidget(); // Must be virtual because of inheritance, to avoid memory leaks
69 QPalette m_scopePalette;
71 ///// Unimplemented /////
73 virtual QString widgetName() const = 0;
78 Monitor *m_projMonitor;
79 Monitor *m_clipMonitor;
80 Render *m_activeRender;
83 /** The context menu. Feel free to add new entries in your implementation. */
86 /** Enables auto refreshing of the scope.
87 This is when a new frame is shown on the active monitor.
88 Resize events always force a recalculation. */
89 QAction *m_aAutoRefresh;
91 /** Realtime rendering. Should be disabled if it is not supported.
92 Use the accelerationFactor variable passed to the render functions as a hint of
93 how many times faster the scope should be calculated. */
97 /** Offset from the widget's borders */
100 /** The rect on the widget we're painting in. */
103 /** Images storing the calculated layers. Will be used on repaint events. */
106 QImage m_imgBackground;
108 /** The acceleration factors can be accessed also by other renderer tasks,
109 e.g. to display the scope's acceleration factor in the HUD renderer. */
110 int m_accelFactorHUD;
111 int m_accelFactorScope;
112 int m_accelFactorBackground;
115 ///// Unimplemented Methods /////
117 /** Where on the widget we can paint in.
118 May also update other variables that depend on the widget's size. */
119 virtual QRect scopeRect() = 0;
121 /** @brief HUD renderer. Must emit signalHUDRenderingFinished(). @see renderScope */
122 virtual QImage renderHUD(uint accelerationFactor) = 0;
123 /** @brief Scope renderer. Must emit signalScopeRenderingFinished()
124 when calculation has finished, to allow multi-threading.
125 accelerationFactor hints how much faster than usual the calculation should be accomplished, if possible. */
126 virtual QImage renderScope(uint accelerationFactor) = 0;
127 /** @brief Background renderer. Must emit signalBackgroundRenderingFinished(). @see renderScope */
128 virtual QImage renderBackground(uint accelerationFactor) = 0;
130 /** Must return true if the HUD layer depends on the input monitor.
131 If it does not, then it does not need to be re-calculated when
132 a new frame from the monitor is incoming. */
133 virtual bool isHUDDependingOnInput() const = 0;
134 /** @see isHUDDependingOnInput() */
135 virtual bool isScopeDependingOnInput() const = 0;
136 /** @see isHUDDependingOnInput() */
137 virtual bool isBackgroundDependingOnInput() const = 0;
139 ///// Can be reimplemented /////
140 /** Calculates the acceleration factor to be used by the render thread.
141 This method can be refined in the subclass if required. */
142 virtual uint calculateAccelFactorHUD(uint oldMseconds, uint oldFactor);
143 virtual uint calculateAccelFactorScope(uint oldMseconds, uint oldFactor);
144 virtual uint calculateAccelFactorBackground(uint oldMseconds, uint oldFactor);
146 ///// Reimplemented /////
148 void mouseReleaseEvent(QMouseEvent *);
149 void paintEvent(QPaintEvent *);
151 void resizeEvent(QResizeEvent *);
154 /** Forces an update of all layers. */
156 void forceUpdateHUD();
157 void forceUpdateScope();
158 void forceUpdateBackground();
159 void slotAutoRefreshToggled(bool);
162 /** mseconds represent the time taken for the calculation,
163 accelerationFactor the acceleration factor that has been used. */
164 void signalHUDRenderingFinished(uint mseconds, uint accelerationFactor);
165 void signalScopeRenderingFinished(uint mseconds, uint accelerationFactor);
166 void signalBackgroundRenderingFinished(uint mseconds, uint accelerationFactor);
170 /** Counts the number of frames that have been rendered in the active monitor.
171 The frame number will be reset when the calculation starts for the current frame. */
172 QAtomicInt m_newHUDFrames;
173 QAtomicInt m_newScopeFrames;
174 QAtomicInt m_newBackgroundFrames;
176 /** Counts the number of updates that, unlike new frames, force a recalculation
177 of the scope, like for example a resize event. */
178 QAtomicInt m_newHUDUpdates;
179 QAtomicInt m_newScopeUpdates;
180 QAtomicInt m_newBackgroundUpdates;
182 /** The semaphores ensure that the QFutures for the HUD/Scope/Background threads cannot
183 be assigned a new thread while it is still running. (Could cause deadlocks and other
184 nasty things known from parallelism. */
185 QSemaphore m_semaphoreHUD;
186 QSemaphore m_semaphoreScope;
187 QSemaphore m_semaphoreBackground;
189 QFuture<QImage> m_threadHUD;
190 QFuture<QImage> m_threadScope;
191 QFuture<QImage> m_threadBackground;
193 bool initialDimensionUpdateDone;
194 void prodHUDThread();
195 void prodScopeThread();
196 void prodBackgroundThread();
200 /** @brief Must be called when the active monitor has shown a new frame.
201 This slot must be connected in the implementing class, it is *not*
202 done in this abstract class. */
203 void slotActiveMonitorChanged(bool isClipMonitor);
204 void customContextMenuRequested(const QPoint &pos);
205 void slotRenderZoneUpdated();
206 void slotHUDRenderingFinished(uint mseconds, uint accelerationFactor);
207 void slotScopeRenderingFinished(uint mseconds, uint accelerationFactor);
208 void slotBackgroundRenderingFinished(uint mseconds, uint accelerationFactor);
210 /** Resets the acceleration factors to 1 when realtime rendering is disabled. */
211 void slotResetRealtimeFactor(bool realtimeChecked);
215 #endif // ABSTRACTSCOPEWIDGET_H