Krita Source Code Documentation
Loading...
Searching...
No Matches
KisStorageModel Class Reference

#include <KisStorageModel.h>

+ Inheritance diagram for KisStorageModel:

Classes

struct  Private
 

Public Types

enum  Columns {
  Id = 0 , StorageType , Location , TimeStamp ,
  PreInstalled , Active , Thumbnail , DisplayName ,
  MetaData
}
 
enum  StorageImportOption { None , Overwrite , Rename }
 

Signals

void storageDisabled (const QString &storage)
 
void storageEnabled (const QString &storage)
 
void storageResynchronized (const QString &storage, bool isBulkResynchronization)
 Emitted when an individual storage is initialized.
 
void storagesBulkSynchronizationFinished ()
 Emitted on loading when all the storages are finished initialization.
 

Public Member Functions

int columnCount (const QModelIndex &parent=QModelIndex()) const override
 
QVariant data (const QModelIndex &index, int role) const override
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
 
bool importStorage (QString filename, StorageImportOption importOption) const
 
 KisStorageModel (QObject *parent=0)
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
bool setData (const QModelIndex &index, const QVariant &value, int role) override
 Enable and disable the storage represented by index.
 
KisResourceStorageSP storageForId (const int storageId) const
 
KisResourceStorageSP storageForIndex (const QModelIndex &index) const
 
 ~KisStorageModel () override
 

Static Public Member Functions

static KisStorageModelinstance ()
 

Private Slots

void addStorage (const QString &location)
 Called whenever a storage is added.
 
void removeStorage (const QString &location)
 This is called when a storage really is deleted both from database and anywhere else.
 
void slotStoragesBulkSynchronizationFinished ()
 called when storages finished synchronization process
 

Private Member Functions

void resetQuery ()
 

Static Private Member Functions

static QImage getThumbnailFromQuery (const QSqlQuery &query)
 

Private Attributes

QScopedPointer< Privated
 

Detailed Description

KisStorageModel provides a model of all registered storages, like the folder storages, the bundle storages or the memory storages. Note that inactive storages are also part of this model.

Definition at line 24 of file KisStorageModel.h.

Member Enumeration Documentation

◆ Columns

Enumerator
Id 
StorageType 
Location 
TimeStamp 
PreInstalled 
Active 
Thumbnail 
DisplayName 
MetaData 

Definition at line 29 of file KisStorageModel.h.

◆ StorageImportOption

Enumerator
None 
Overwrite 
Rename 

Definition at line 41 of file KisStorageModel.h.

Constructor & Destructor Documentation

◆ KisStorageModel()

KisStorageModel::KisStorageModel ( QObject * parent = 0)

Definition at line 33 of file KisStorageModel.cpp.

34 : QAbstractTableModel(parent)
35 , d(new Private())
36{
41
42 resetQuery();
43}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void storageRemoved(const QString &location)
Emitted whenever a storage is removed.
void storageAdded(const QString &location)
Emitted whenever a storage is added.
void storagesBulkSynchronizationFinished()
void storageResynchronized(const QString &storage, bool isBulkResynchronization)
static KisResourceLocator * instance()
void removeStorage(const QString &location)
This is called when a storage really is deleted both from database and anywhere else.
QScopedPointer< Private > d
void addStorage(const QString &location)
Called whenever a storage is added.
void storageResynchronized(const QString &storage, bool isBulkResynchronization)
Emitted when an individual storage is initialized.
void slotStoragesBulkSynchronizationFinished()
called when storages finished synchronization process

References addStorage(), connect(), KisResourceLocator::instance(), removeStorage(), resetQuery(), slotStoragesBulkSynchronizationFinished(), KisResourceLocator::storageAdded(), KisResourceLocator::storageRemoved(), KisResourceLocator::storageResynchronized(), storageResynchronized(), and KisResourceLocator::storagesBulkSynchronizationFinished().

◆ ~KisStorageModel()

KisStorageModel::~KisStorageModel ( )
override

Definition at line 50 of file KisStorageModel.cpp.

51{
52}

Member Function Documentation

◆ addStorage

void KisStorageModel::addStorage ( const QString & location)
privateslot

Called whenever a storage is added.

Definition at line 486 of file KisStorageModel.cpp.

487{
488 beginInsertRows(QModelIndex(), rowCount(), rowCount());
489 d->storages.append(location);
490 endInsertRows();
491}
int rowCount(const QModelIndex &parent=QModelIndex()) const override

References d, and rowCount().

◆ columnCount()

int KisStorageModel::columnCount ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 63 of file KisStorageModel.cpp.

64{
65 if (parent.isValid()) {
66 return 0;
67 }
68
69 return (int)MetaData;
70}
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
Definition KisForest.h:327

References MetaData.

◆ data()

QVariant KisStorageModel::data ( const QModelIndex & index,
int role ) const
override

Definition at line 116 of file KisStorageModel.cpp.

117{
118 QVariant v;
119
120 if (!index.isValid()) return v;
121 if (index.row() > rowCount()) return v;
122 if (index.column() > (int)MetaData) return v;
123
124 if (role == Qt::FontRole) {
125 return QFont();
126 }
127
128 QString location = d->storages.at(index.row());
129
130 QSqlQuery query;
131
132 bool r = query.prepare(
133 "SELECT storages.id as id\n"
134 ", storage_types.name as storage_type\n"
135 ", location\n"
136 ", timestamp\n"
137 ", pre_installed\n"
138 ", active\n"
139 "FROM storages\n"
140 ", storage_types\n"
141 "WHERE storages.storage_type_id = storage_types.id\n"
142 "AND location = :location");
143
144 if (!r) {
145 qWarning() << "Could not prepare KisStorageModel data query" << query.lastError();
146 return v;
147 }
148
149 query.bindValue(":location", location);
150
151 r = query.exec();
152
153 if (!r) {
154 qWarning() << "Could not execute KisStorageModel data query" << query.lastError() << query.boundValues();
155 return v;
156 }
157
158 if (!query.first()) {
159 qWarning() << "KisStorageModel data query did not return anything";
160 return v;
161 }
162
163 if ((role == Qt::DisplayRole || role == Qt::EditRole) && index.column() == Active) {
164 return query.value("active");
165 } else {
166 switch (role) {
167 case Qt::DisplayRole:
168 {
169 switch(index.column()) {
170 case Id:
171 return query.value("id");
172 case StorageType:
173 return query.value("storage_type");
174 case Location:
175 return query.value("location");
176 case TimeStamp:
177 return QDateTime::fromSecsSinceEpoch(query.value("timestamp").value<int>()).toString();
178 case PreInstalled:
179 return query.value("pre_installed");
180 case Active:
181 return query.value("active");
182 case Thumbnail:
183 {
184 return getThumbnailFromQuery(query);
185 }
186 case DisplayName:
187 {
188 QMap<QString, QVariant> r = KisResourceLocator::instance()->metaDataForStorage(query.value("location").toString());
189 QVariant name = query.value("location");
190 if (r.contains(KisResourceStorage::s_meta_name) && !r[KisResourceStorage::s_meta_name].toString().isNull()) {
192 }
193 else if (r.contains(KisResourceStorage::s_meta_title) && !r[KisResourceStorage::s_meta_title].toString().isNull()) {
195 }
196 return name;
197 }
198 case Qt::UserRole + MetaData:
199 {
200 QMap<QString, QVariant> r = KisResourceLocator::instance()->metaDataForStorage(query.value("location").toString());
201 return r;
202 }
203 default:
204 return v;
205 }
206 }
207 case Qt::CheckStateRole: {
208 switch (index.column()) {
209 case PreInstalled:
210 if (query.value("pre_installed").toInt() == 0) {
211 return Qt::Unchecked;
212 } else {
213 return Qt::Checked;
214 }
215 case Active:
216 if (query.value("active").toInt() == 0) {
217 return Qt::Unchecked;
218 } else {
219 return Qt::Checked;
220 }
221 default:
222 return {};
223 }
224 }
225 case Qt::DecorationRole: {
226 if (index.column() == Thumbnail) {
227 return getThumbnailFromQuery(query);
228 }
229 return {};
230 }
231 case Qt::UserRole + Id:
232 return query.value("id");
233 case Qt::UserRole + DisplayName:
234 {
235 QMap<QString, QVariant> r = KisResourceLocator::instance()->metaDataForStorage(query.value("location").toString());
236 QVariant name = query.value("location");
237 if (r.contains(KisResourceStorage::s_meta_name) && !r[KisResourceStorage::s_meta_name].toString().isNull()) {
239 }
240 else if (r.contains(KisResourceStorage::s_meta_title) && !r[KisResourceStorage::s_meta_title].toString().isNull()) {
242 }
243 return name;
244 }
245 case Qt::UserRole + StorageType:
246 return query.value("storage_type");
247 case Qt::UserRole + Location:
248 return query.value("location");
249 case Qt::UserRole + TimeStamp:
250 return query.value("timestamp");
251 case Qt::UserRole + PreInstalled:
252 return query.value("pre_installed");
253 case Qt::UserRole + Active:
254 return query.value("active");
255 case Qt::UserRole + Thumbnail:
256 return getThumbnailFromQuery(query);
257 case Qt::UserRole + MetaData:
258 {
259 QMap<QString, QVariant> r = KisResourceLocator::instance()->metaDataForStorage(query.value("location").toString());
260 return r;
261 }
262
263 default:
264 ;
265 }
266 }
267
268 return v;
269}
qreal v
QMap< QString, QVariant > metaDataForStorage(const QString &storageLocation) const
metaDataForStorage
static const QString s_meta_title
static const QString s_meta_name
static QImage getThumbnailFromQuery(const QSqlQuery &query)
const char * name(StandardAction id)

References Active, d, DisplayName, getThumbnailFromQuery(), Id, KisResourceLocator::instance(), Location, MetaData, KisResourceLocator::metaDataForStorage(), PreInstalled, rowCount(), KisResourceStorage::s_meta_name, KisResourceStorage::s_meta_title, StorageType, Thumbnail, TimeStamp, and v.

◆ flags()

Qt::ItemFlags KisStorageModel::flags ( const QModelIndex & index) const
override

Definition at line 310 of file KisStorageModel.cpp.

311{
312 if (!index.isValid()) {
313 return Qt::NoItemFlags;
314 }
315 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable | Qt::ItemNeverHasChildren;
316}

◆ getThumbnailFromQuery()

QImage KisStorageModel::getThumbnailFromQuery ( const QSqlQuery & query)
staticprivate

Definition at line 72 of file KisStorageModel.cpp.

73{
74 const QString storageLocation = query.value("location").toString();
75 const QString storageType = query.value("storage_type").toString();
76 const QString storageIdAsString = query.value("id").toString();
77
78 QImage img = KisResourceThumbnailCache::instance()->originalImage(storageLocation, storageType, storageIdAsString);
79 if (!img.isNull()) {
80 return img;
81 } else {
82 const int storageId = query.value("id").toInt();
83 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(storageId >= 0, img);
84
85 bool result = false;
86 QSqlQuery thumbQuery;
87 result = thumbQuery.prepare("SELECT thumbnail FROM storages WHERE id = :id");
88 if (!result) {
89 qWarning() << "Failed to prepare query for thumbnail of" << storageId << thumbQuery.lastError();
90 return img;
91 }
92
93 thumbQuery.bindValue(":id", storageId);
94
95 result = thumbQuery.exec();
96
97 if (!result) {
98 qWarning() << "Failed to execute query for thumbnail of" << storageId << thumbQuery.lastError();
99 return img;
100 }
101
102 if (!thumbQuery.next()) {
103 qWarning() << "Failed to find thumbnail of" << storageId;
104 return img;
105 }
106
107 QByteArray ba = thumbQuery.value("thumbnail").toByteArray();
108 QBuffer buf(&ba);
109 buf.open(QBuffer::ReadOnly);
110 img.load(&buf, "PNG");
111 KisResourceThumbnailCache::instance()->insert(storageLocation, storageType, storageIdAsString, img);
112 return img;
113 }
114}
void insert(const QString &storageLocation, const QString &resourceType, const QString &filename, const QImage &image)
static KisResourceThumbnailCache * instance()
QImage originalImage(const QString &storageLocation, const QString &resourceType, const QString &filename) const
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129

References KisResourceThumbnailCache::insert(), KisResourceThumbnailCache::instance(), KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE, and KisResourceThumbnailCache::originalImage().

◆ headerData()

QVariant KisStorageModel::headerData ( int section,
Qt::Orientation orientation,
int role = Qt::DisplayRole ) const
override

Definition at line 454 of file KisStorageModel.cpp.

455{
456 QVariant v = QVariant();
457 if (role != Qt::DisplayRole) {
458 return v;
459 }
460 if (orientation == Qt::Horizontal) {
461 switch(section) {
462 case Id:
463 return i18n("Id");
464 case StorageType:
465 return i18n("Type");
466 case Location:
467 return i18n("Location");
468 case TimeStamp:
469 return i18n("Creation Date");
470 case PreInstalled:
471 return i18n("Preinstalled");
472 case Active:
473 return i18n("Active");
474 case Thumbnail:
475 return i18n("Thumbnail");
476 case DisplayName:
477 return i18n("Name");
478 default:
479 v = QString::number(section);
480 }
481 return v;
482 }
483 return QAbstractTableModel::headerData(section, orientation, role);
484}

References Active, DisplayName, Id, Location, PreInstalled, StorageType, Thumbnail, TimeStamp, and v.

◆ importStorage()

bool KisStorageModel::importStorage ( QString filename,
StorageImportOption importOption ) const

Definition at line 420 of file KisStorageModel.cpp.

421{
422 // 1. Copy the bundle/storage to the resource folder
423 QFileInfo oldFileInfo(filename);
424 QString newDir = KoResourcePaths::getAppDataLocation();
425 QString newName = oldFileInfo.fileName();
426 QString newLocation = newDir + '/' + newName;
427
428 QFileInfo newFileInfo(newLocation);
429 if (newFileInfo.exists()) {
430 if (importOption == Overwrite) {
431 //QFile::remove(newLocation);
432 return false;
433 } else if (importOption == Rename) {
434 newName = findUnusedName(newDir, newName);
435 newLocation = newDir + '/' + newName;
436 newFileInfo = QFileInfo(newLocation);
437 } else { // importOption == None
438 return false;
439 }
440 }
441 QFile::copy(filename, newLocation);
442
443 // 2. Add the bundle as a storage/update database
445 KIS_ASSERT(!storage.isNull());
446 if (storage.isNull()) { return false; }
447 if (!KisResourceLocator::instance()->addStorage(newLocation, storage)) {
448 qWarning() << "Could not add bundle to the storages" << newLocation;
449 return false;
450 }
451 return true;
452}
QString findUnusedName(QString location, QString filename)
static QString getAppDataLocation()
#define KIS_ASSERT(cond)
Definition kis_assert.h:33

References addStorage(), findUnusedName(), KoResourcePaths::getAppDataLocation(), KisResourceLocator::instance(), KIS_ASSERT, Overwrite, and Rename.

◆ instance()

KisStorageModel * KisStorageModel::instance ( )
static

Definition at line 45 of file KisStorageModel.cpp.

46{
47 return s_instance;
48}

◆ removeStorage

void KisStorageModel::removeStorage ( const QString & location)
privateslot

This is called when a storage really is deleted both from database and anywhere else.

Definition at line 493 of file KisStorageModel.cpp.

494{
495 int row = d->storages.indexOf(QFileInfo(location).fileName());
496 beginRemoveRows(QModelIndex(), row, row);
497 d->storages.removeAt(row);
498 endRemoveRows();
499}

References d.

◆ resetQuery()

void KisStorageModel::resetQuery ( )
private

Definition at line 510 of file KisStorageModel.cpp.

511{
512 QSqlQuery query;
513
514 bool r = query.prepare(
515 "SELECT location\n"
516 "FROM storages\n"
517 "ORDER BY id");
518 if (!r) {
519 qWarning() << "Could not prepare KisStorageModel query" << query.lastError();
520 }
521
522 r = query.exec();
523
524 if (!r) {
525 qWarning() << "Could not execute KisStorageModel query" << query.lastError();
526 }
527
528 d->storages.clear();
529 while (query.next()) {
530 d->storages << query.value(0).toString();
531 }
532}

References d.

◆ rowCount()

int KisStorageModel::rowCount ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 54 of file KisStorageModel.cpp.

55{
56 if (parent.isValid()) {
57 return 0;
58 }
59 return d->storages.size();
60
61}

References d.

◆ setData()

bool KisStorageModel::setData ( const QModelIndex & index,
const QVariant & value,
int role )
override

Enable and disable the storage represented by index.

Definition at line 271 of file KisStorageModel.cpp.

272{
273 if (index.isValid()) {
274
275 if (role == Qt::CheckStateRole) {
276 QSqlQuery query;
277 bool r = query.prepare("UPDATE storages\n"
278 "SET active = :active\n"
279 "WHERE id = :id\n");
280 query.bindValue(":active", value);
281 query.bindValue(":id", index.data(Qt::UserRole + Id));
282
283 if (!r) {
284 qWarning() << "Could not prepare KisStorageModel update query" << query.lastError();
285 return false;
286 }
287
288 r = query.exec();
289
290 if (!r) {
291 qWarning() << "Could not execute KisStorageModel update query" << query.lastError();
292 return false;
293 }
294
295 }
296
297 Q_EMIT dataChanged(index, index, {role});
298
299 if (value.toBool()) {
300 Q_EMIT storageEnabled(data(index, Qt::UserRole + Location).toString());
301 }
302 else {
303 Q_EMIT storageDisabled(data(index, Qt::UserRole + Location).toString());
304 }
305
306 }
307 return true;
308}
float value(const T *src, size_t ch)
void storageDisabled(const QString &storage)
void storageEnabled(const QString &storage)
QVariant data(const QModelIndex &index, int role) const override
QString toString(const QString &value)

References data(), Id, Location, storageDisabled(), storageEnabled(), and value().

◆ slotStoragesBulkSynchronizationFinished

void KisStorageModel::slotStoragesBulkSynchronizationFinished ( )
privateslot

called when storages finished synchronization process

Definition at line 501 of file KisStorageModel.cpp.

502{
503 beginResetModel();
504 resetQuery();
505 endResetModel();
506
508}
void storagesBulkSynchronizationFinished()
Emitted on loading when all the storages are finished initialization.

References resetQuery(), and storagesBulkSynchronizationFinished().

◆ storageDisabled

void KisStorageModel::storageDisabled ( const QString & storage)
signal

◆ storageEnabled

void KisStorageModel::storageEnabled ( const QString & storage)
signal

◆ storageForId()

KisResourceStorageSP KisStorageModel::storageForId ( const int storageId) const

Definition at line 330 of file KisStorageModel.cpp.

331{
332 QSqlQuery query;
333
334 bool r = query.prepare("SELECT location\n"
335 "FROM storages\n"
336 "WHERE storages.id = :storageId");
337
338 if (!r) {
339 qWarning() << "Could not prepare KisStorageModel data query" << query.lastError();
340 return 0;
341 }
342
343 query.bindValue(":storageId", storageId);
344
345 r = query.exec();
346
347 if (!r) {
348 qWarning() << "Could not execute KisStorageModel data query" << query.lastError() << query.boundValues();
349 return 0;
350 }
351
352 if (!query.first()) {
353 qWarning() << "KisStorageModel data query did not return anything";
354 return 0;
355 }
356
357 return KisResourceLocator::instance()->storageByLocation(KisResourceLocator::instance()->makeStorageLocationAbsolute(query.value("location").toString()));
358}
KisResourceStorageSP storageByLocation(const QString &location) const

References KisResourceLocator::instance(), and KisResourceLocator::storageByLocation().

◆ storageForIndex()

KisResourceStorageSP KisStorageModel::storageForIndex ( const QModelIndex & index) const

Definition at line 318 of file KisStorageModel.cpp.

319{
320
321 if (!index.isValid()) return 0;
322 if (index.row() > rowCount()) return 0;
323 if (index.column() > (int)MetaData) return 0;
324
325 QString location = d->storages.at(index.row());
326
327 return KisResourceLocator::instance()->storageByLocation(KisResourceLocator::instance()->makeStorageLocationAbsolute(location));
328}

References d, KisResourceLocator::instance(), MetaData, rowCount(), and KisResourceLocator::storageByLocation().

◆ storageResynchronized

void KisStorageModel::storageResynchronized ( const QString & storage,
bool isBulkResynchronization )
signal

Emitted when an individual storage is initialized.

◆ storagesBulkSynchronizationFinished

void KisStorageModel::storagesBulkSynchronizationFinished ( )
signal

Emitted on loading when all the storages are finished initialization.

Member Data Documentation

◆ d

QScopedPointer<Private> KisStorageModel::d
private

Definition at line 95 of file KisStorageModel.h.


The documentation for this class was generated from the following files: