]> git.sesse.net Git - kdenlive/blob - src/abstractscopewidget.h
8188c16f5be498bafe8606bcc339f8a42b23dd91
[kdenlive] / src / abstractscopewidget.h
1 /***************************************************************************
2  *   Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com)      *
3  *   This file is part of kdenlive. See www.kdenlive.org.                  *
4  *                                                                         *
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  ***************************************************************************/
10
11 /**
12   This abstract widget is a proof that abstract things sometimes *are* useful.
13
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)
19
20   The layer order is as follows:
21      _____________________
22     /                     \
23    /      HUD Layer        \
24   /                         \
25   ---------------------------
26      _____________________
27     /                     \
28    /     Scope Layer       \
29   /                         \
30   ---------------------------
31      _____________________
32     /                     \
33    /   Background Layer    \
34   /                         \
35   ---------------------------
36
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).
39
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.
42
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.
46  */
47
48 #ifndef ABSTRACTSCOPEWIDGET_H
49 #define ABSTRACTSCOPEWIDGET_H
50
51 #include <QtCore>
52 #include <QWidget>
53
54 class QMenu;
55
56 class AbstractScopeWidget : public QWidget
57 {
58     Q_OBJECT
59
60 public:
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
65
66
67     enum RescaleDirection { North, Northeast, East, Southeast };
68
69
70     QPalette m_scopePalette;
71
72     /** Initializes widget settings (reads configuration).
73       Has to be called in the implementing object. */
74     virtual void init();
75
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();
79
80     ///// Unimplemented /////
81
82     virtual QString widgetName() const = 0;
83
84     ///// Variables /////
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
93     static const QString directions[]; // Mainly for debug output
94
95 protected:
96     ///// Variables /////
97
98     /** The context menu. Feel free to add new entries in your implementation. */
99     QMenu *m_menu;
100
101     /** Enables auto refreshing of the scope.
102         This is when fresh data is incoming.
103         Resize events always force a recalculation. */
104     QAction *m_aAutoRefresh;
105
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;
110
111     /** The mouse position; Updated when the mouse enters the widget
112         AND mouse tracking has been enabled. */
113     QPoint m_mousePos;
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;
117
118     /** Offset from the widget's borders */
119     const uchar offset;
120
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). */
124     QRect m_scopeRect;
125
126     /** Images storing the calculated layers. Will be used on repaint events. */
127     QImage m_imgHUD;
128     QImage m_imgScope;
129     QImage m_imgBackground;
130
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;
136
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. */
142     void writeConfig();
143     /** Identifier for the widget's configuration. */
144     QString configName();
145
146
147     ///// Unimplemented Methods /////
148
149     /** Where on the widget we can paint in.
150         May also update other variables, like m_scopeRect or custom ones,
151         that have to change together with the widget's size.  */
152     virtual QRect scopeRect() = 0;
153
154     /** @brief HUD renderer. Must emit signalHUDRenderingFinished(). @see renderScope */
155     virtual QImage renderHUD(uint accelerationFactor) = 0;
156     /** @brief Scope renderer. Must emit signalScopeRenderingFinished()
157         when calculation has finished, to allow multi-threading.
158         accelerationFactor hints how much faster than usual the calculation should be accomplished, if possible. */
159     virtual QImage renderScope(uint accelerationFactor) = 0;
160     /** @brief Background renderer. Must emit signalBackgroundRenderingFinished(). @see renderScope */
161     virtual QImage renderBackground(uint accelerationFactor) = 0;
162
163     /** Must return true if the HUD layer depends on the input data.
164         If it does not, then it does not need to be re-calculated when
165         fresh data is incoming. */
166     virtual bool isHUDDependingOnInput() const = 0;
167     /** @see isHUDDependingOnInput() */
168     virtual bool isScopeDependingOnInput() const = 0;
169     /** @see isHUDDependingOnInput() */
170     virtual bool isBackgroundDependingOnInput() const = 0;
171
172     ///// Can be reimplemented /////
173     /** Calculates the acceleration factor to be used by the render thread.
174         This method can be refined in the subclass if required. */
175     virtual uint calculateAccelFactorHUD(uint oldMseconds, uint oldFactor);
176     virtual uint calculateAccelFactorScope(uint oldMseconds, uint oldFactor);
177     virtual uint calculateAccelFactorBackground(uint oldMseconds, uint oldFactor);
178
179     /** The Abstract Scope will try to detect the movement direction when dragging on the widget with the mouse.
180         As soon as the direction is determined it will execute this method. Can be used e.g. for re-scaling content.
181         This is just a dummy function, re-implement to add functionality. */
182     virtual void handleMouseDrag(const QPoint movement, const RescaleDirection rescaleDirection, const Qt::KeyboardModifiers rescaleModifiers);
183
184     ///// Reimplemented /////
185
186     void mouseMoveEvent(QMouseEvent *event);
187     void mousePressEvent(QMouseEvent *event);
188     void mouseReleaseEvent(QMouseEvent *event);
189     void leaveEvent(QEvent *);
190     void paintEvent(QPaintEvent *);
191     void resizeEvent(QResizeEvent *);
192     void showEvent(QShowEvent *); // Called when the widget is activated via the Menu entry
193     //    void raise(); // Called only when  manually calling the event -> useless
194
195
196 protected slots:
197     /** Forces an update of all layers. */
198     void forceUpdate(bool doUpdate = true);
199     void forceUpdateHUD();
200     void forceUpdateScope();
201     void forceUpdateBackground();
202     void slotAutoRefreshToggled(bool);
203
204 signals:
205     /** mseconds represent the time taken for the calculation,
206         accelerationFactor is the acceleration factor that has been used for this calculation. */
207     void signalHUDRenderingFinished(uint mseconds, uint accelerationFactor);
208     void signalScopeRenderingFinished(uint mseconds, uint accelerationFactor);
209     void signalBackgroundRenderingFinished(uint mseconds, uint accelerationFactor);
210
211     /** For the mouse position itself see m_mousePos.
212         To check whether the mouse has leaved the widget, see m_mouseWithinWidget.
213         This signal is typically connected to forceUpdateHUD(). */
214     void signalMousePositionChanged();
215
216     /** Do we need the renderer to send its frames to us? */
217     void requestAutoRefresh(bool);
218
219 private:
220
221     /** Counts the number of data frames that have been rendered in the active monitor.
222       The frame number will be reset when the calculation starts for the current data set. */
223     QAtomicInt m_newHUDFrames;
224     QAtomicInt m_newScopeFrames;
225     QAtomicInt m_newBackgroundFrames;
226
227     /** Counts the number of updates that, unlike new frames, force a recalculation
228       of the scope, like for example a resize event. */
229     QAtomicInt m_newHUDUpdates;
230     QAtomicInt m_newScopeUpdates;
231     QAtomicInt m_newBackgroundUpdates;
232
233     /** The semaphores ensure that the QFutures for the HUD/Scope/Background threads cannot
234       be assigned a new thread while it is still running. (Could cause deadlocks and other
235       nasty things known from parallelism.) */
236     QSemaphore m_semaphoreHUD;
237     QSemaphore m_semaphoreScope;
238     QSemaphore m_semaphoreBackground;
239
240     QFuture<QImage> m_threadHUD;
241     QFuture<QImage> m_threadScope;
242     QFuture<QImage> m_threadBackground;
243
244     bool initialDimensionUpdateDone;
245     bool m_requestForcedUpdate;
246
247     QImage m_scopeImage;
248
249     QString m_widgetName;
250
251     void prodHUDThread();
252     void prodScopeThread();
253     void prodBackgroundThread();
254
255     ///// Movement detection /////
256     const int m_rescaleMinDist;
257     const float m_rescaleVerticalThreshold;
258
259     bool m_rescaleActive;
260     bool m_rescalePropertiesLocked;
261     bool m_rescaleFirstRescaleDone;
262     short m_rescaleScale;
263     Qt::KeyboardModifiers m_rescaleModifiers;
264     RescaleDirection m_rescaleDirection;
265     QPoint m_rescaleStartPoint;
266
267
268 protected slots:
269     void customContextMenuRequested(const QPoint &pos);
270     /** To be called when a new frame has been received.
271       The scope then decides whether and when it wants to recalculate the scope, depending
272       on whether it is currently visible and whether a calculation thread is already running. */
273     void slotRenderZoneUpdated();
274     /** The following slots are called when rendering of a component has finished. They e.g. update
275       the widget and decide whether to immediately restart the calculation thread. */
276     void slotHUDRenderingFinished(uint mseconds, uint accelerationFactor);
277     void slotScopeRenderingFinished(uint mseconds, uint accelerationFactor);
278     void slotBackgroundRenderingFinished(uint mseconds, uint accelerationFactor);
279
280     /** Resets the acceleration factors to 1 when realtime rendering is disabled. */
281     void slotResetRealtimeFactor(bool realtimeChecked);
282
283 };
284
285 #endif // ABSTRACTSCOPEWIDGET_H