Added copyright headers and COPYING file.
[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   static bool init = false;
34   if (!init) {
35     Quill::setPreviewLevelCount(1);
36     Quill::setPreviewSize(0, QSize(854, 480)); // TODO:
37     Quill::setMinimumPreviewSize(0, QSize(854, 480)); // TODO:
38     Quill::setThumbnailExtension("jpeg"); // TODO:
39     Quill::setThumbnailFlavorName(0, "screen");
40     Quill::setBackgroundRenderingColor(Qt::black);
41     QString tempPath(QDir::homePath() +  QDir::separator() + ".config" +
42                      QDir::separator() + "quill" + QDir::separator() + "tmp");
43     QDir().mkpath(tempPath);
44     Quill::setTemporaryFilePath(tempPath);
45     Quill::setDBusThumbnailingEnabled(true);
46     Quill::setThumbnailCreationEnabled(true);
47
48     init = true;
49   }
50 }
51
52 QuillItem::~QuillItem() {
53   delete m_file; m_file = 0;
54 }
55
56 void QuillItem::componentComplete() {
57   QDeclarativeItem::componentComplete();
58
59   recreate();
60 }
61
62 QUrl QuillItem::source() const {
63   return m_url;
64 }
65
66 void QuillItem::setSource(const QUrl& src) {
67   if (src == source()) {
68     return;
69   }
70
71   m_url = src;
72
73   if (isComponentComplete()) {
74     recreate();
75   }
76
77   emit sourceChanged();
78 }
79
80 QString QuillItem::mimeType() const {
81   return m_type;
82 }
83
84 void QuillItem::setMimeType(const QString& mime) {
85   if (mimeType() == mime) {
86     return;
87   }
88
89   m_type = mime;
90
91   if (isComponentComplete()) {
92     recreate();
93   }
94
95   emit mimeTypeChanged();
96 }
97
98 bool QuillItem::error() const {
99   return m_error;
100 }
101
102 void QuillItem::recreate() {
103   if (m_error) {
104     m_error = false;
105     emit errorChanged();
106   }
107
108   if (m_file) {
109     m_file->deleteLater();
110   }
111
112   m_file = new QuillFile(m_url.toLocalFile(), m_type);
113
114   QObject::connect(m_file, SIGNAL(error(QuillError)),
115           this, SLOT(fileError()), Qt::QueuedConnection);
116   QObject::connect(m_file, SIGNAL(imageAvailable(QuillImageList)),
117           this, SLOT(fileLoaded()), Qt::QueuedConnection);
118   QObject::connect(m_file, SIGNAL(removed()),
119                    m_file, SLOT(deleteLater()), Qt::QueuedConnection);
120
121   if (fileError()) {
122     return;
123   }
124
125   m_file->setDisplayLevel(0);
126
127   if (fileError()) {
128     return;
129   }
130 }
131
132 void QuillItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
133   Q_UNUSED(widget);
134
135   if (!m_file) {
136     return;
137   }
138
139   QImage image = m_file->image(0);
140
141   if (!image.isNull()) {
142     painter->drawImage(option->rect, image);
143   }
144 }
145
146 void QuillItem::fileLoaded() {
147   update();
148 }
149
150 bool QuillItem::fileError() {
151   if (!m_file) {
152     return true;
153   }
154
155   QuillError err = m_file->error();
156
157   if (err.errorCode() != QuillError::NoError) {
158     qWarning() << "Error loading file" << m_file->fileName()
159                << "Code" << err.errorCode() << "Source" << err.errorSource();
160
161     QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
162                               Q_ARG(QString, err.errorData()));
163     m_file->deleteLater(); m_file = 0;
164
165     if (!m_error) {
166       m_error = true;
167
168       emit errorChanged();
169     }
170
171     return true;
172   }
173
174   return false;
175 }