Move qt_cam_copy_register to QtCamera
[harmattan/cameraplus] / lib / qtcamgraphicsviewfinder.cpp
index 0cd418e..6ace193 100644 (file)
@@ -1,7 +1,7 @@
 /*!
  * This file is part of CameraPlus.
  *
- * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -25,6 +25,7 @@
 #include "qtcamviewfinderrenderer.h"
 #include <QPainter>
 #include <QGraphicsSceneResizeEvent>
+#include <QMatrix4x4>
 
 class QtCamGraphicsViewfinderPrivate {
 public:
@@ -53,6 +54,7 @@ public:
   QtCamConfig *conf;
   QtCamDevice *dev;
   QtCamGraphicsViewfinder *q_ptr;
+  bool enabled;
 };
 
 QtCamGraphicsViewfinder::QtCamGraphicsViewfinder(QtCamConfig *config, QGraphicsItem *parent) :
@@ -61,6 +63,7 @@ QtCamGraphicsViewfinder::QtCamGraphicsViewfinder(QtCamConfig *config, QGraphicsI
   d_ptr->dev = 0;
   d_ptr->renderer = 0;
   d_ptr->q_ptr = this;
+  d_ptr->enabled = true;
 }
 
 QtCamGraphicsViewfinder::~QtCamGraphicsViewfinder() {
@@ -78,33 +81,6 @@ GstElement *QtCamGraphicsViewfinder::sinkElement() {
   return d_ptr->renderer->sinkElement();
 }
 
-bool QtCamGraphicsViewfinder::setDevice(QtCamDevice *device) {
-  if (device && d_ptr->dev == device) {
-    return true;
-  }
-
-  if (d_ptr->dev) {
-    qWarning() << "QtCamGraphicsViewfinder: viewfinder cannot be replaced.";
-    return false;
-  }
-
-  if (!device) {
-    qWarning() << "QtCamGraphicsViewfinder: viewfinder cannot be unset.";
-    return false;
-  }
-
-  // This is to break the loop.
-  d_ptr->dev = device;
-  if (!device->setViewfinder(this)) {
-    d_ptr->dev = 0;
-    return false;
-  }
-
-  d_ptr->resetBackend();
-
-  return true;
-}
-
 void QtCamGraphicsViewfinder::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                                    QWidget *widget) {
   Q_UNUSED(widget);
@@ -112,11 +88,21 @@ void QtCamGraphicsViewfinder::paint(QPainter *painter, const QStyleOptionGraphic
 
   painter->fillRect(boundingRect(), Qt::black);
 
-  if (!d_ptr->renderer) {
+  if (!d_ptr->renderer || !d_ptr->enabled) {
     return;
   }
 
-  d_ptr->renderer->paint(painter);
+  bool needsNativePainting = d_ptr->renderer->needsNativePainting();
+
+  if (needsNativePainting) {
+    painter->beginNativePainting();
+  }
+
+  d_ptr->renderer->paint(QMatrix4x4(painter->combinedTransform()), painter->viewport());
+
+  if (needsNativePainting) {
+    painter->endNativePainting();
+  }
 }
 
 void QtCamGraphicsViewfinder::resizeEvent(QGraphicsSceneResizeEvent *event) {
@@ -130,11 +116,15 @@ void QtCamGraphicsViewfinder::resizeEvent(QGraphicsSceneResizeEvent *event) {
 }
 
 void QtCamGraphicsViewfinder::updateRequested() {
-  update();
+  if (d_ptr->enabled) {
+    update();
+  }
 }
 
 void QtCamGraphicsViewfinder::stop() {
   d_ptr->resetBackend();
+
+  setRenderingEnabled(true);
 }
 
 QRectF QtCamGraphicsViewfinder::renderArea() const {
@@ -152,3 +142,15 @@ QSizeF QtCamGraphicsViewfinder::videoResolution() const {
 
   return d_ptr->renderer->videoResolution();
 }
+
+bool QtCamGraphicsViewfinder::isRenderingEnabled() const {
+  return d_ptr->enabled;
+}
+
+void QtCamGraphicsViewfinder::setRenderingEnabled(bool enabled) {
+  if (d_ptr->enabled != enabled) {
+    d_ptr->enabled = enabled;
+
+    emit renderingEnabledChanged();
+  }
+}