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 ***************************************************************************/
11 #include "qtconcurrentrun.h"
13 #include "abstractscopewidget.h"
20 #include <QMouseEvent>
23 // Uncomment for Scope debugging.
30 const int REALTIME_FPS = 30;
32 const QColor light(250, 238, 226, 255);
33 const QColor dark(40, 40, 39, 255);
34 const QColor dark2(25, 25, 23, 255);
36 const QPen AbstractScopeWidget::penThick(QBrush(QColor(250, 250, 250)), 2, Qt::SolidLine);
37 const QPen AbstractScopeWidget::penThin(QBrush(QColor(250, 250, 250)), 1, Qt::SolidLine);
38 const QPen AbstractScopeWidget::penLight(QBrush(QColor(200, 200, 250, 150)), 1, Qt::SolidLine);
39 const QPen AbstractScopeWidget::penLightDots(QBrush(QColor(200, 200, 250, 150)), 1, Qt::DotLine);
40 const QPen AbstractScopeWidget::penDark(QBrush(QColor(0, 0, 20, 250)), 1, Qt::SolidLine);
41 const QPen AbstractScopeWidget::penDarkDots(QBrush(QColor(0, 0, 20, 250)), 1, Qt::DotLine);
43 const QString AbstractScopeWidget::directions[] = {"North", "Northeast", "East", "Southeast"};
45 AbstractScopeWidget::AbstractScopeWidget(bool trackMouse, QWidget *parent) :
48 m_mouseWithinWidget(false),
51 m_accelFactorScope(1),
52 m_accelFactorBackground(1),
55 m_semaphoreBackground(1),
56 initialDimensionUpdateDone(false),
57 m_requestForcedUpdate(false),
59 m_rescaleVerticalThreshold(2.0f)
62 m_scopePalette = QPalette();
63 m_scopePalette.setBrush(QPalette::Window, QBrush(dark2));
64 m_scopePalette.setBrush(QPalette::Base, QBrush(dark));
65 m_scopePalette.setBrush(QPalette::Button, QBrush(dark));
66 m_scopePalette.setBrush(QPalette::Text, QBrush(light));
67 m_scopePalette.setBrush(QPalette::WindowText, QBrush(light));
68 m_scopePalette.setBrush(QPalette::ButtonText, QBrush(light));
69 this->setPalette(m_scopePalette);
70 this->setAutoFillBackground(true);
72 m_aAutoRefresh = new QAction(i18n("Auto Refresh"), this);
73 m_aAutoRefresh->setCheckable(true);
74 m_aRealtime = new QAction(i18n("Realtime (with precision loss)"), this);
75 m_aRealtime->setCheckable(true);
77 m_menu = new QMenu(this);
78 m_menu->setPalette(m_scopePalette);
79 m_menu->addAction(m_aAutoRefresh);
80 m_menu->addAction(m_aRealtime);
82 this->setContextMenuPolicy(Qt::CustomContextMenu);
85 b &= connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
87 b &= connect(this, SIGNAL(signalHUDRenderingFinished(uint, uint)), this, SLOT(slotHUDRenderingFinished(uint, uint)));
88 b &= connect(this, SIGNAL(signalScopeRenderingFinished(uint, uint)), this, SLOT(slotScopeRenderingFinished(uint, uint)));
89 b &= connect(this, SIGNAL(signalBackgroundRenderingFinished(uint, uint)), this, SLOT(slotBackgroundRenderingFinished(uint, uint)));
90 b &= connect(m_aRealtime, SIGNAL(toggled(bool)), this, SLOT(slotResetRealtimeFactor(bool)));
91 b &= connect(m_aAutoRefresh, SIGNAL(toggled(bool)), this, SLOT(slotAutoRefreshToggled(bool)));
94 // Enable mouse tracking if desired.
95 // Causes the mouseMoved signal to be emitted when the mouse moves inside the
96 // widget, even when no mouse button is pressed.
97 this->setMouseTracking(trackMouse);
100 AbstractScopeWidget::~AbstractScopeWidget()
105 delete m_aAutoRefresh;
109 void AbstractScopeWidget::init()
111 m_widgetName = widgetName();
115 void AbstractScopeWidget::readConfig()
117 KSharedConfigPtr config = KGlobal::config();
118 KConfigGroup scopeConfig(config, configName());
119 m_aAutoRefresh->setChecked(scopeConfig.readEntry("autoRefresh", true));
120 m_aRealtime->setChecked(scopeConfig.readEntry("realtime", false));
124 void AbstractScopeWidget::writeConfig()
126 KSharedConfigPtr config = KGlobal::config();
127 KConfigGroup scopeConfig(config, configName());
128 scopeConfig.writeEntry("autoRefresh", m_aAutoRefresh->isChecked());
129 scopeConfig.writeEntry("realtime", m_aRealtime->isChecked());
133 QString AbstractScopeWidget::configName()
135 return "Scope_" + m_widgetName;
138 void AbstractScopeWidget::prodHUDThread()
140 if (this->visibleRegion().isEmpty()) {
142 qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating HUD.";
145 if (m_semaphoreHUD.tryAcquire(1)) {
146 Q_ASSERT(!m_threadHUD.isRunning());
148 m_newHUDFrames.fetchAndStoreRelaxed(0);
149 m_newHUDUpdates.fetchAndStoreRelaxed(0);
150 m_threadHUD = QtConcurrent::run(this, &AbstractScopeWidget::renderHUD, m_accelFactorHUD);
152 qDebug() << "HUD thread started in " << m_widgetName;
158 qDebug() << "HUD semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadHUD.isRunning();
164 void AbstractScopeWidget::prodScopeThread()
166 // Only start a new thread if the scope is actually visible
167 // and not hidden by another widget on the stack and if user want the scope to update.
168 if (this->visibleRegion().isEmpty() || (!m_aAutoRefresh->isChecked() && !m_requestForcedUpdate)) {
170 qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating scope.";
173 // Try to acquire the semaphore. This must only succeed if m_threadScope is not running
174 // anymore. Therefore the semaphore must NOT be released before m_threadScope ends.
175 // If acquiring the semaphore fails, the thread is still running.
176 if (m_semaphoreScope.tryAcquire(1)) {
177 Q_ASSERT(!m_threadScope.isRunning());
179 m_newScopeFrames.fetchAndStoreRelaxed(0);
180 m_newScopeUpdates.fetchAndStoreRelaxed(0);
182 Q_ASSERT(m_accelFactorScope > 0);
184 // See http://doc.qt.nokia.com/latest/qtconcurrentrun.html#run about
185 // running member functions in a thread
186 m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope, m_accelFactorScope);
187 m_requestForcedUpdate = false;
190 qDebug() << "Scope thread started in " << m_widgetName;
195 qDebug() << "Scope semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadScope.isRunning();
200 void AbstractScopeWidget::prodBackgroundThread()
202 if (this->visibleRegion().isEmpty()) {
204 qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating background.";
207 if (m_semaphoreBackground.tryAcquire(1)) {
208 Q_ASSERT(!m_threadBackground.isRunning());
210 m_newBackgroundFrames.fetchAndStoreRelaxed(0);
211 m_newBackgroundUpdates.fetchAndStoreRelaxed(0);
212 m_threadBackground = QtConcurrent::run(this, &AbstractScopeWidget::renderBackground, m_accelFactorBackground);
215 qDebug() << "Background thread started in " << m_widgetName;
220 qDebug() << "Background semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadBackground.isRunning();
226 void AbstractScopeWidget::forceUpdate(bool doUpdate)
229 qDebug() << "Forced update called in " << widgetName() << ". Arg: " << doUpdate;
235 m_requestForcedUpdate = true;
236 m_newHUDUpdates.fetchAndAddRelaxed(1);
237 m_newScopeUpdates.fetchAndAddRelaxed(1);
238 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
241 prodBackgroundThread();
243 void AbstractScopeWidget::forceUpdateHUD()
245 m_newHUDUpdates.fetchAndAddRelaxed(1);
249 void AbstractScopeWidget::forceUpdateScope()
251 m_newScopeUpdates.fetchAndAddRelaxed(1);
252 m_requestForcedUpdate = true;
256 void AbstractScopeWidget::forceUpdateBackground()
258 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
259 prodBackgroundThread();
267 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
269 // Update the dimension of the available rect for painting
270 m_scopeRect = scopeRect();
273 QWidget::resizeEvent(event);
276 void AbstractScopeWidget::showEvent(QShowEvent *event)
278 QWidget::showEvent(event);
279 m_scopeRect = scopeRect();
282 void AbstractScopeWidget::paintEvent(QPaintEvent *)
284 QPainter davinci(this);
285 davinci.drawImage(m_scopeRect.topLeft(), m_imgBackground);
286 davinci.drawImage(m_scopeRect.topLeft(), m_imgScope);
287 davinci.drawImage(m_scopeRect.topLeft(), m_imgHUD);
290 void AbstractScopeWidget::mousePressEvent(QMouseEvent *event)
292 if (event->button() == Qt::LeftButton) {
293 // Rescaling mode starts
294 m_rescaleActive = true;
295 m_rescalePropertiesLocked = false;
296 m_rescaleFirstRescaleDone = false;
297 m_rescaleStartPoint = event->pos();
298 m_rescaleModifiers = event->modifiers();
301 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
303 m_rescaleActive = false;
304 m_rescalePropertiesLocked = false;
306 if (!m_aAutoRefresh->isChecked()) {
307 m_requestForcedUpdate = true;
311 prodBackgroundThread();
312 QWidget::mouseReleaseEvent(event);
314 void AbstractScopeWidget::mouseMoveEvent(QMouseEvent *event)
316 m_mousePos = event->pos();
317 m_mouseWithinWidget = true;
318 emit signalMousePositionChanged();
320 QPoint movement = event->pos()-m_rescaleStartPoint;
322 if (m_rescaleActive) {
323 if (m_rescalePropertiesLocked) {
324 // Direction is known, now adjust parameters
326 // Reset the starting point to make the next moveEvent relative to the current one
327 m_rescaleStartPoint = event->pos();
330 if (!m_rescaleFirstRescaleDone) {
331 // We have just learned the desired direction; Normalize the movement to one pixel
332 // to avoid a jump by m_rescaleMinDist
334 if (movement.x() != 0) {
335 movement.setX(movement.x() / abs(movement.x()));
337 if (movement.y() != 0) {
338 movement.setY(movement.y() / abs(movement.y()));
341 m_rescaleFirstRescaleDone = true;
344 handleMouseDrag(movement, m_rescaleDirection, m_rescaleModifiers);
349 // Detect the movement direction here.
350 // This algorithm relies on the aspect ratio of dy/dx (size and signum).
351 if (movement.manhattanLength() > m_rescaleMinDist) {
352 float diff = ((float) movement.y())/movement.x();
354 if (abs(diff) > m_rescaleVerticalThreshold || movement.x() == 0) {
355 m_rescaleDirection = North;
356 } else if (abs(diff) < 1/m_rescaleVerticalThreshold) {
357 m_rescaleDirection = East;
358 } else if (diff < 0) {
359 m_rescaleDirection = Northeast;
361 m_rescaleDirection = Southeast;
364 qDebug() << "Diff is " << diff << "; chose " << directions[m_rescaleDirection] << " as direction";
366 m_rescalePropertiesLocked = true;
372 void AbstractScopeWidget::leaveEvent(QEvent *)
374 m_mouseWithinWidget = false;
375 emit signalMousePositionChanged();
378 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
380 m_menu->exec(this->mapToGlobal(pos));
383 uint AbstractScopeWidget::calculateAccelFactorHUD(uint oldMseconds, uint)
385 return ceil((float)oldMseconds*REALTIME_FPS / 1000);
387 uint AbstractScopeWidget::calculateAccelFactorScope(uint oldMseconds, uint)
389 return ceil((float)oldMseconds*REALTIME_FPS / 1000);
391 uint AbstractScopeWidget::calculateAccelFactorBackground(uint oldMseconds, uint)
393 return ceil((float)oldMseconds*REALTIME_FPS / 1000);
399 void AbstractScopeWidget::slotHUDRenderingFinished(uint mseconds, uint oldFactor)
402 qDebug() << "HUD rendering has finished in " << mseconds << " ms, waiting for termination in " << m_widgetName;
404 m_threadHUD.waitForFinished();
405 m_imgHUD = m_threadHUD.result();
407 m_semaphoreHUD.release(1);
411 if (m_aRealtime->isChecked()) {
412 accel = calculateAccelFactorHUD(mseconds, oldFactor);
413 if (m_accelFactorHUD < 1) {
416 m_accelFactorHUD = accel;
419 if ((m_newHUDFrames > 0 && m_aAutoRefresh->isChecked()) || m_newHUDUpdates > 0) {
421 qDebug() << "Trying to start a new HUD thread for " << m_widgetName
422 << ". New frames/updates: " << m_newHUDFrames << "/" << m_newHUDUpdates;
428 void AbstractScopeWidget::slotScopeRenderingFinished(uint mseconds, uint oldFactor)
430 // The signal can be received before the thread has really finished. So we
431 // need to wait until it has really finished before starting a new thread.
433 qDebug() << "Scope rendering has finished in " << mseconds << " ms, waiting for termination in " << m_widgetName;
435 m_threadScope.waitForFinished();
436 m_imgScope = m_threadScope.result();
438 // The scope thread has finished. Now we can release the semaphore, allowing a new thread.
439 // See prodScopeThread where the semaphore is acquired again.
440 m_semaphoreScope.release(1);
443 // Calculate the acceleration factor hint to get «realtime» updates.
445 if (m_aRealtime->isChecked()) {
446 accel = calculateAccelFactorScope(mseconds, oldFactor);
448 // If mseconds happens to be 0.
451 // Don't directly calculate with m_accelFactorScope as we are dealing with concurrency.
452 // If m_accelFactorScope is set to 0 at the wrong moment, who knows what might happen
453 // then :) Therefore use a local variable.
454 m_accelFactorScope = accel;
457 if ((m_newScopeFrames > 0 && m_aAutoRefresh->isChecked()) || m_newScopeUpdates > 0) {
459 qDebug() << "Trying to start a new scope thread for " << m_widgetName
460 << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
466 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint mseconds, uint oldFactor)
469 qDebug() << "Background rendering has finished in " << mseconds << " ms, waiting for termination in " << m_widgetName;
471 m_threadBackground.waitForFinished();
472 m_imgBackground = m_threadBackground.result();
474 m_semaphoreBackground.release(1);
478 if (m_aRealtime->isChecked()) {
479 accel = calculateAccelFactorBackground(mseconds, oldFactor);
480 if (m_accelFactorBackground < 1) {
483 m_accelFactorBackground = accel;
486 if ((m_newBackgroundFrames > 0 && m_aAutoRefresh->isChecked()) || m_newBackgroundUpdates > 0) {
488 qDebug() << "Trying to start a new background thread for " << m_widgetName
489 << ". New frames/updates: " << m_newBackgroundFrames << "/" << m_newBackgroundUpdates;
491 prodBackgroundThread();;
495 void AbstractScopeWidget::slotRenderZoneUpdated()
497 m_newHUDFrames.fetchAndAddRelaxed(1);
498 m_newScopeFrames.fetchAndAddRelaxed(1);
499 m_newBackgroundFrames.fetchAndAddRelaxed(1);
502 qDebug() << "Monitor incoming at " << widgetName() << ". New frames total HUD/Scope/Background: " << m_newHUDFrames
503 << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
506 if (this->visibleRegion().isEmpty()) {
508 qDebug() << "Scope of widget " << m_widgetName << " is not at the top, not rendering.";
511 if (m_aAutoRefresh->isChecked()) {
514 prodBackgroundThread();
519 void AbstractScopeWidget::slotResetRealtimeFactor(bool realtimeChecked)
521 if (!realtimeChecked) {
522 m_accelFactorHUD = 1;
523 m_accelFactorScope = 1;
524 m_accelFactorBackground = 1;
528 bool AbstractScopeWidget::autoRefreshEnabled()
530 return m_aAutoRefresh->isChecked();
533 void AbstractScopeWidget::slotAutoRefreshToggled(bool autoRefresh)
536 qDebug() << "Auto-refresh switched to " << autoRefresh << " in " << widgetName()
537 << " (Visible: " << isVisible() << "/" << this->visibleRegion().isEmpty() << ")";
540 // Notify listeners whether we accept new frames now
541 emit requestAutoRefresh(autoRefresh);
543 // TODO only if depends on input
546 m_requestForcedUpdate = true;
550 void AbstractScopeWidget::handleMouseDrag(const QPoint, const RescaleDirection, const Qt::KeyboardModifiers) { }