Don't paint system background
[harmattan/cameraplus] / src / quillitem.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 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(0);
62
63   if (fileError()) {
64     return;
65   }
66 }
67
68 bool QuillItem::error() const {
69   return m_error;
70 }
71
72 void QuillItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
73   Q_UNUSED(widget);
74   Q_UNUSED(option);
75
76   QRectF rect = boundingRect();
77   painter->fillRect(rect, Qt::black);
78
79   if (!m_file) {
80     return;
81   }
82
83   QImage image = m_file->image(0);
84
85   if (image.isNull()) {
86     return;
87   }
88
89   QSizeF imageSize = QSizeF(image.size());
90   QSizeF widgetSize = rect.size();
91   imageSize.scale(widgetSize, Qt::KeepAspectRatio);
92
93   QPointF pos = QPointF(widgetSize.width() - imageSize.width(),
94                         widgetSize.height() - imageSize.height()) / 2;
95
96   painter->drawImage(QRectF(pos, imageSize), image, QRect(0, 0, image.width(), image.height()));
97 }
98
99 void QuillItem::fileLoaded() {
100   update();
101 }
102
103 bool QuillItem::fileError() {
104   if (!m_file) {
105     return true;
106   }
107
108   QuillError err = m_file->error();
109
110   if (err.errorCode() != QuillError::NoError) {
111     qWarning() << "Error loading file" << m_file->fileName()
112                << "Code" << err.errorCode() << "Source" << err.errorSource();
113
114     QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
115                               Q_ARG(QString, err.errorData()));
116
117     m_file->deleteLater(); m_file = 0;
118
119     if (!m_error) {
120       m_error = true;
121
122       emit errorChanged();
123     }
124
125     return true;
126   }
127
128   return false;
129 }