Changed the default configuration file path
[harmattan/cameraplus] / src / quillitem.cpp
1 #include "quillitem.h"
2 #include <QuillFile>
3 #include <QUrl>
4 #include <QPainter>
5 #include <QStyleOptionGraphicsItem>
6 #include <QDir>
7
8 QuillItem::QuillItem(QDeclarativeItem *parent) :
9   QDeclarativeItem(parent), m_file(0), m_error(false) {
10
11   setFlag(QGraphicsItem::ItemHasNoContents, false);
12
13   static bool init = false;
14   if (!init) {
15     Quill::setPreviewLevelCount(1);
16     Quill::setPreviewSize(0, QSize(854, 480)); // TODO:
17     Quill::setMinimumPreviewSize(0, QSize(854, 480)); // TODO:
18     Quill::setThumbnailExtension("jpeg"); // TODO:
19     Quill::setThumbnailFlavorName(0, "screen");
20     Quill::setBackgroundRenderingColor(Qt::black);
21     QString tempPath(QDir::homePath() +  QDir::separator() + ".config" +
22                      QDir::separator() + "quill" + QDir::separator() + "tmp");
23     QDir().mkpath(tempPath);
24     Quill::setTemporaryFilePath(tempPath);
25     Quill::setDBusThumbnailingEnabled(true);
26     Quill::setThumbnailCreationEnabled(true);
27
28     init = true;
29   }
30 }
31
32 QuillItem::~QuillItem() {
33   delete m_file; m_file = 0;
34 }
35
36 void QuillItem::componentComplete() {
37   QDeclarativeItem::componentComplete();
38
39   recreate();
40 }
41
42 QUrl QuillItem::source() const {
43   return m_url;
44 }
45
46 void QuillItem::setSource(const QUrl& src) {
47   if (src == source()) {
48     return;
49   }
50
51   m_url = src;
52
53   if (isComponentComplete()) {
54     recreate();
55   }
56
57   emit sourceChanged();
58 }
59
60 QString QuillItem::mimeType() const {
61   return m_type;
62 }
63
64 void QuillItem::setMimeType(const QString& mime) {
65   if (mimeType() == mime) {
66     return;
67   }
68
69   m_type = mime;
70
71   if (isComponentComplete()) {
72     recreate();
73   }
74
75   emit mimeTypeChanged();
76 }
77
78 bool QuillItem::error() const {
79   return m_error;
80 }
81
82 void QuillItem::recreate() {
83   if (m_error) {
84     m_error = false;
85     emit errorChanged();
86   }
87
88   if (m_file) {
89     m_file->deleteLater();
90   }
91
92   m_file = new QuillFile(m_url.toLocalFile(), m_type);
93
94   QObject::connect(m_file, SIGNAL(error(QuillError)),
95           this, SLOT(fileError()), Qt::QueuedConnection);
96   QObject::connect(m_file, SIGNAL(imageAvailable(QuillImageList)),
97           this, SLOT(fileLoaded()), Qt::QueuedConnection);
98   QObject::connect(m_file, SIGNAL(removed()),
99                    m_file, SLOT(deleteLater()), Qt::QueuedConnection);
100
101   if (fileError()) {
102     return;
103   }
104
105   m_file->setDisplayLevel(0);
106
107   if (fileError()) {
108     return;
109   }
110 }
111
112 void QuillItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
113   Q_UNUSED(widget);
114
115   if (!m_file) {
116     return;
117   }
118
119   QImage image = m_file->image(0);
120
121   if (!image.isNull()) {
122     painter->drawImage(option->rect, image);
123   }
124 }
125
126 void QuillItem::fileLoaded() {
127   update();
128 }
129
130 bool QuillItem::fileError() {
131   if (!m_file) {
132     return true;
133   }
134
135   QuillError err = m_file->error();
136
137   if (err.errorCode() != QuillError::NoError) {
138     qWarning() << "Error loading file" << m_file->fileName()
139                << "Code" << err.errorCode() << "Source" << err.errorSource();
140
141     QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
142                               Q_ARG(QString, err.errorData()));
143     m_file->deleteLater(); m_file = 0;
144
145     if (!m_error) {
146       m_error = true;
147
148       emit errorChanged();
149     }
150
151     return true;
152   }
153
154   return false;
155 }