Corrected video recording resolution for front camera
[harmattan/cameraplus] / src / harmattan / quillitem.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "quillitem.h"
22 #include <QuillFile>
23 #include <QUrl>
24 #include <QPainter>
25 #include <QStyleOptionGraphicsItem>
26 #include <QDir>
27
28 QuillItem::QuillItem(QDeclarativeItem *parent) :
29   QDeclarativeItem(parent), m_file(0), m_error(false) {
30
31   setFlag(QGraphicsItem::ItemHasNoContents, false);
32 }
33
34 QuillItem::~QuillItem() {
35   delete m_file; m_file = 0;
36 }
37
38 void QuillItem::initialize(const QUrl& url, const QString& mimeType) {
39   if (m_error) {
40     m_error = false;
41     emit errorChanged();
42   }
43
44   if (m_file) {
45     m_file->deleteLater();
46   }
47
48   m_file = new QuillFile(url.toLocalFile(), mimeType);
49
50   QObject::connect(m_file, SIGNAL(error(QuillError)),
51                    this, SLOT(fileError()), Qt::QueuedConnection);
52   QObject::connect(m_file, SIGNAL(imageAvailable(QuillImageList)),
53                    this, SLOT(fileLoaded()), Qt::QueuedConnection);
54   QObject::connect(m_file, SIGNAL(removed()),
55                    m_file, SLOT(deleteLater()), Qt::QueuedConnection);
56
57   if (fileError()) {
58     return;
59   }
60
61   m_file->setDisplayLevel(isLandscape() ? LANDSCAPE_PREVIEW_LEVEL
62                           : PORTRAIT_PREVIEW_LEVEL);
63
64   if (fileError()) {
65     return;
66   }
67 }
68
69 bool QuillItem::error() const {
70   return m_error;
71 }
72
73 void QuillItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
74   Q_UNUSED(widget);
75   Q_UNUSED(option);
76
77   QRectF rect = boundingRect();
78   painter->fillRect(rect, Qt::black);
79
80   if (!m_file) {
81     return;
82   }
83
84   QImage image = m_file->image(0);
85
86   if (image.isNull()) {
87     return;
88   }
89
90   QSizeF imageSize = QSizeF(image.size());
91   QSizeF widgetSize = rect.size();
92   imageSize.scale(widgetSize, Qt::KeepAspectRatio);
93
94   QPointF pos = QPointF(widgetSize.width() - imageSize.width(),
95                         widgetSize.height() - imageSize.height()) / 2;
96
97   painter->drawImage(QRectF(pos, imageSize), image, QRect(0, 0, image.width(), image.height()));
98 }
99
100 void QuillItem::fileLoaded() {
101   update();
102 }
103
104 bool QuillItem::fileError() {
105   if (!m_file) {
106     return true;
107   }
108
109   QuillError err = m_file->error();
110
111   if (err.errorCode() != QuillError::NoError) {
112     qWarning() << "Error loading file" << m_file->fileName()
113                << "Code" << err.errorCode() << "Source" << err.errorSource();
114
115     QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
116                               Q_ARG(QString, err.errorData()));
117
118     m_file->deleteLater(); m_file = 0;
119
120     if (!m_error) {
121       m_error = true;
122
123       emit errorChanged();
124     }
125
126     return true;
127   }
128
129   return false;
130 }
131
132 bool QuillItem::isLandscape() {
133   QSize size = m_file->fullImageSize();
134
135   return size.width() > size.height();
136 }