diff --git a/qmlUiKirigami/main.qml b/qmlUiKirigami/main.qml --- a/qmlUiKirigami/main.qml +++ b/qmlUiKirigami/main.qml @@ -97,53 +97,53 @@ Koko.SortModel { id: imageTimeModelYear sourceModel: Koko.ImageTimeModel { - group: Koko.ImageTimeModel.Year + group: Koko.Groups.Year } sortRoleName: "date" } Koko.SortModel { id: imageTimeModelMonth sourceModel: Koko.ImageTimeModel { - group: Koko.ImageTimeModel.Month + group: Koko.Groups.Month } sortRoleName: "date" } Koko.SortModel { id: imageTimeModelWeek sourceModel: Koko.ImageTimeModel { - group: Koko.ImageTimeModel.Week + group: Koko.Groups.Week } sortRoleName: "date" } Koko.SortModel { id: imageTimeModelDay sourceModel: Koko.ImageTimeModel { - group: Koko.ImageTimeModel.Day + group: Koko.Groups.Day } sortRoleName: "date" } Koko.SortModel { id: imageLocationModelCountry sourceModel: Koko.ImageLocationModel { - group: Koko.ImageLocationModel.Country + group: Koko.Groups.Country } } Koko.SortModel { id: imageLocationModelState sourceModel: Koko.ImageLocationModel { - group: Koko.ImageLocationModel.State + group: Koko.Groups.State } } Koko.SortModel { id: imageLocationModelCity sourceModel: Koko.ImageLocationModel { - group: Koko.ImageLocationModel.City + group: Koko.Groups.City } } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -65,6 +65,7 @@ imagelistmodel.cpp types.cpp roles.cpp + groups.cpp ) add_library (kokoqmlplugin SHARED ${qml_plugin_SRCS}) diff --git a/src/groups.h b/src/groups.h new file mode 100644 --- /dev/null +++ b/src/groups.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2017 Atul Sharma + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + */ + +#ifndef GROUPS_H +#define GROUPS_H + +#include + +class Groups: public QObject +{ + Q_OBJECT + Q_ENUMS(TimeGroup) + Q_ENUMS(LocationGroup) +public: + enum TimeGroup { + Year, + Month, + Week, + Day + }; + + enum LocationGroup { + Country, + State, + City + }; + + Groups(QObject *parent = 0); + ~Groups(); +}; + +#endif diff --git a/src/groups.cpp b/src/groups.cpp new file mode 100644 --- /dev/null +++ b/src/groups.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2017 Atul Sharma + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + * + */ + +#include "groups.h" + +Groups::Groups(QObject* parent) +{ +} + +Groups::~Groups() +{ +} + +#include "moc_groups.cpp" diff --git a/src/imagelocationmodel.h b/src/imagelocationmodel.h --- a/src/imagelocationmodel.h +++ b/src/imagelocationmodel.h @@ -25,35 +25,30 @@ #include #include +#include "groups.h" + class ImageLocationModel : public QAbstractListModel { Q_OBJECT - Q_PROPERTY(LocationGroup group READ group WRITE setGroup NOTIFY groupChanged) + Q_PROPERTY(Groups::LocationGroup group READ group WRITE setGroup NOTIFY groupChanged) public: explicit ImageLocationModel(QObject* parent = 0); virtual QHash< int, QByteArray > roleNames() const; virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; virtual int rowCount(const QModelIndex& parent = QModelIndex()) const; - enum LocationGroup { - Country, - State, - City - }; - Q_ENUMS(LocationGroup) - - LocationGroup group() const; - void setGroup(LocationGroup group); + Groups::LocationGroup group() const; + void setGroup(Groups::LocationGroup group); signals: void groupChanged(); private slots: void slotPopulate(); private: - LocationGroup m_group; + Groups::LocationGroup m_group; QList > m_locations; }; diff --git a/src/imagelocationmodel.cpp b/src/imagelocationmodel.cpp --- a/src/imagelocationmodel.cpp +++ b/src/imagelocationmodel.cpp @@ -28,15 +28,15 @@ ImageLocationModel::ImageLocationModel(QObject* parent) : QAbstractListModel(parent) - , m_group(ImageLocationModel::City) + , m_group(Groups::LocationGroup::City) { connect(ImageStorage::instance(), SIGNAL(storageModified()), this, SLOT(slotPopulate())); } void ImageLocationModel::slotPopulate() { beginResetModel(); - m_locations = ImageStorage::instance()->locations(static_cast(m_group)); + m_locations = ImageStorage::instance()->locations(static_cast(m_group)); endResetModel(); } @@ -66,17 +66,17 @@ return display; case Roles::FilesRole: { - auto group = static_cast(m_group); + auto group = static_cast(m_group); return ImageStorage::instance()->imagesForLocation(key, group); } case Roles::FileCountRole: { - auto group = static_cast(m_group); + auto group = static_cast(m_group); return ImageStorage::instance()->imagesForLocation(key, group).size(); } case Roles::ImageUrlRole: { - auto group = static_cast(m_group); + auto group = static_cast(m_group); return ImageStorage::instance()->imageForLocation(key, group); } @@ -98,17 +98,17 @@ return m_locations.size(); } -void ImageLocationModel::setGroup(ImageLocationModel::LocationGroup group) +void ImageLocationModel::setGroup(Groups::LocationGroup group) { beginResetModel(); m_group = group; - m_locations = ImageStorage::instance()->locations(static_cast(group)); + m_locations = ImageStorage::instance()->locations(static_cast(group)); endResetModel(); emit groupChanged(); } -ImageLocationModel::LocationGroup ImageLocationModel::group() const +Groups::LocationGroup ImageLocationModel::group() const { return m_group; } diff --git a/src/imagestorage.h b/src/imagestorage.h --- a/src/imagestorage.h +++ b/src/imagestorage.h @@ -29,6 +29,7 @@ #include #include +#include "groups.h" #include "koko_export.h" struct ImageInfo { @@ -50,26 +51,15 @@ static ImageStorage* instance(); - enum LocationGroup { - Country, - State, - City - }; - QList< QPair > locations(LocationGroup loca); - QStringList imagesForLocation(const QByteArray& name, LocationGroup loc); - QString imageForLocation(const QByteArray& name, LocationGroup loc); - - enum TimeGroup { - Year, - Month, - Week, - Day - }; - QList< QPair > timeGroups(TimeGroup group); - QStringList imagesForTime(const QByteArray& name, TimeGroup group); - QString imageForTime(const QByteArray& name, TimeGroup group); - - QDate dateForKey(const QByteArray& key, TimeGroup group); + QList< QPair > locations(Groups::LocationGroup loca); + QStringList imagesForLocation(const QByteArray& name, Groups::LocationGroup loc); + QString imageForLocation(const QByteArray& name, Groups::LocationGroup loc); + + QList< QPair > timeGroups(Groups::TimeGroup group); + QStringList imagesForTime(const QByteArray& name, Groups::TimeGroup group); + QString imageForTime(const QByteArray& name, Groups::TimeGroup group); + + QDate dateForKey(const QByteArray& key, Groups::TimeGroup group); /** * Fetch all the images ordered by descending date time. diff --git a/src/imagestorage.cpp b/src/imagestorage.cpp --- a/src/imagestorage.cpp +++ b/src/imagestorage.cpp @@ -180,12 +180,12 @@ emit storageModified(); } -QList > ImageStorage::locations(ImageStorage::LocationGroup loca) +QList > ImageStorage::locations(Groups::LocationGroup loca) { QMutexLocker lock(&m_mutex); QList< QPair > list; - if (loca == Country) { + if (loca == Groups::LocationGroup::Country) { QSqlQuery query; query.prepare("SELECT DISTINCT country from locations"); @@ -200,7 +200,7 @@ } return list; } - else if (loca == State) { + else if (loca == Groups::LocationGroup::State) { QSqlQuery query; query.prepare("SELECT DISTINCT country, state from locations"); @@ -223,7 +223,7 @@ } return list; } - else if (loca == City) { + else if (loca == Groups::LocationGroup::City) { QSqlQuery query; query.prepare("SELECT DISTINCT country, state, city from locations"); @@ -256,15 +256,15 @@ return list; } -QStringList ImageStorage::imagesForLocation(const QByteArray& name, ImageStorage::LocationGroup loc) +QStringList ImageStorage::imagesForLocation(const QByteArray& name, Groups::LocationGroup loc) { QMutexLocker lock(&m_mutex); QSqlQuery query; - if (loc == Country) { + if (loc == Groups::LocationGroup::Country) { query.prepare("SELECT DISTINCT url from files, locations where country = ? AND files.location = locations.id"); query.addBindValue(QString::fromUtf8(name)); } - else if (loc == State) { + else if (loc == Groups::LocationGroup::State) { QDataStream st(name); QString country; @@ -275,7 +275,7 @@ query.addBindValue(country); query.addBindValue(state); } - else if (loc == City) { + else if (loc == Groups::LocationGroup::City) { QDataStream st(name); QString country; @@ -300,15 +300,15 @@ return files; } -QString ImageStorage::imageForLocation(const QByteArray& name, ImageStorage::LocationGroup loc) +QString ImageStorage::imageForLocation(const QByteArray& name, Groups::LocationGroup loc) { QMutexLocker lock(&m_mutex); QSqlQuery query; - if (loc == Country) { + if (loc == Groups::LocationGroup::Country) { query.prepare("SELECT DISTINCT url from files, locations where country = ? AND files.location = locations.id"); query.addBindValue(QString::fromUtf8(name)); } - else if (loc == State) { + else if (loc == Groups::LocationGroup::State) { QDataStream st(name); QString country; @@ -319,7 +319,7 @@ query.addBindValue(country); query.addBindValue(state); } - else if (loc == City) { + else if (loc == Groups::LocationGroup::City) { QDataStream st(name); QString country; @@ -343,14 +343,13 @@ return QString(); } - -QList > ImageStorage::timeGroups(ImageStorage::TimeGroup group) +QList > ImageStorage::timeGroups(Groups::TimeGroup group) { QMutexLocker lock(&m_mutex); QList< QPair > list; QSqlQuery query; - if (group == Year) { + if (group == Groups::TimeGroup::Year) { query.prepare("SELECT DISTINCT strftime('%Y', dateTime) from files"); if (!query.exec()) { qDebug() << group << query.lastError(); @@ -363,7 +362,7 @@ } return list; } - else if (group == Month) { + else if (group == Groups::TimeGroup::Month) { query.prepare("SELECT DISTINCT strftime('%Y', dateTime), strftime('%m', dateTime) from files"); if (!query.exec()) { qDebug() << group << query.lastError(); @@ -385,7 +384,7 @@ } return list; } - else if (group == Week) { + else if (group == Groups::TimeGroup::Week) { query.prepare("SELECT DISTINCT strftime('%Y', dateTime), strftime('%m', dateTime), strftime('%W', dateTime) from files"); if (!query.exec()) { qDebug() << group << query.lastError(); @@ -407,7 +406,7 @@ } return list; } - else if (group == Day) { + else if (group == Groups::TimeGroup::Day) { query.prepare("SELECT DISTINCT date(dateTime) from files"); if (!query.exec()) { qDebug() << group << query.lastError(); @@ -429,15 +428,15 @@ return list; } -QStringList ImageStorage::imagesForTime(const QByteArray& name, ImageStorage::TimeGroup group) +QStringList ImageStorage::imagesForTime(const QByteArray& name, Groups::TimeGroup group) { QMutexLocker lock(&m_mutex); QSqlQuery query; - if (group == Year) { + if (group == Groups::TimeGroup::Year) { query.prepare("SELECT DISTINCT url from files where strftime('%Y', dateTime) = ?"); query.addBindValue(QString::fromUtf8(name)); } - else if (group == Month) { + else if (group == Groups::TimeGroup::Month) { QDataStream stream(name); QString year; QString month; @@ -447,7 +446,7 @@ query.addBindValue(year); query.addBindValue(month); } - else if (group == Week) { + else if (group == Groups::TimeGroup::Week) { QDataStream stream(name); QString year; QString week; @@ -457,7 +456,7 @@ query.addBindValue(year); query.addBindValue(week); } - else if (group == Day) { + else if (group == Groups::TimeGroup::Day) { QDate date = QDate::fromString(QString::fromUtf8(name), Qt::ISODate); query.prepare("SELECT DISTINCT url from files where date(dateTime) = ?"); @@ -478,17 +477,17 @@ return files; } -QString ImageStorage::imageForTime(const QByteArray& name, ImageStorage::TimeGroup group) +QString ImageStorage::imageForTime(const QByteArray& name, Groups::TimeGroup group) { QMutexLocker lock(&m_mutex); Q_ASSERT(!name.isEmpty()); QSqlQuery query; - if (group == Year) { + if (group == Groups::TimeGroup::Year) { query.prepare("SELECT DISTINCT url from files where strftime('%Y', dateTime) = ? LIMIT 1"); query.addBindValue(QString::fromUtf8(name)); } - else if (group == Month) { + else if (group == Groups::TimeGroup::Month) { QDataStream stream(name); QString year; QString month; @@ -498,7 +497,7 @@ query.addBindValue(year); query.addBindValue(month); } - else if (group == Week) { + else if (group == Groups::TimeGroup::Week) { QDataStream stream(name); QString year; QString week; @@ -508,7 +507,7 @@ query.addBindValue(year); query.addBindValue(week); } - else if (group == Day) { + else if (group == Groups::TimeGroup::Day) { QDate date = QDate::fromString(QString::fromUtf8(name), Qt::ISODate); query.prepare("SELECT DISTINCT url from files where date(dateTime) = ? LIMIT 1"); @@ -528,31 +527,31 @@ return QString(); } -QDate ImageStorage::dateForKey(const QByteArray& name, ImageStorage::TimeGroup group) +QDate ImageStorage::dateForKey(const QByteArray& key, Groups::TimeGroup group) { - if (group == Year) { - return QDate(name.toInt(), 1, 1); + if (group == Groups::TimeGroup::Year) { + return QDate(key.toInt(), 1, 1); } - else if (group == Month) { - QDataStream stream(name); + else if (group == Groups::TimeGroup::Month) { + QDataStream stream(key); QString year; QString month; stream >> year >> month; return QDate(year.toInt(), month.toInt(), 1); } - else if (group == Week) { - QDataStream stream(name); + else if (group == Groups::TimeGroup::Week) { + QDataStream stream(key); QString year; QString week; stream >> year >> week; int month = week.toInt() / 4; int day = week.toInt() % 4; return QDate(year.toInt(), month, day); } - else if (group == Day) { - return QDate::fromString(QString::fromUtf8(name), Qt::ISODate); + else if (group == Groups::TimeGroup::Day) { + return QDate::fromString(QString::fromUtf8(key), Qt::ISODate); } Q_ASSERT(0); diff --git a/src/imagetimemodel.h b/src/imagetimemodel.h --- a/src/imagetimemodel.h +++ b/src/imagetimemodel.h @@ -24,36 +24,30 @@ #include #include +#include "groups.h" + class ImageTimeModel : public QAbstractListModel { Q_OBJECT - Q_PROPERTY(TimeGroup group READ group WRITE setGroup NOTIFY groupChanged) + Q_PROPERTY(Groups::TimeGroup group READ group WRITE setGroup NOTIFY groupChanged) public: explicit ImageTimeModel(QObject* parent = 0); virtual QHash< int, QByteArray > roleNames() const; virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; virtual int rowCount(const QModelIndex& parent = QModelIndex()) const; - enum TimeGroup { - Year, - Month, - Week, - Day - }; - Q_ENUMS(TimeGroup) - - TimeGroup group() const; - void setGroup(TimeGroup group); + Groups::TimeGroup group() const; + void setGroup(Groups::TimeGroup group); signals: void groupChanged(); private slots: void slotPopulate(); private: - TimeGroup m_group; + Groups::TimeGroup m_group; QList< QPair > m_times; }; diff --git a/src/imagetimemodel.cpp b/src/imagetimemodel.cpp --- a/src/imagetimemodel.cpp +++ b/src/imagetimemodel.cpp @@ -28,15 +28,15 @@ ImageTimeModel::ImageTimeModel(QObject* parent) : QAbstractListModel(parent) - , m_group(ImageTimeModel::Day) + , m_group(Groups::TimeGroup::Day) { connect(ImageStorage::instance(), SIGNAL(storageModified()), this, SLOT(slotPopulate())); } void ImageTimeModel::slotPopulate() { beginResetModel(); - auto tg = static_cast(m_group); + auto tg = static_cast(m_group); m_times = ImageStorage::instance()->timeGroups(tg); endResetModel(); } @@ -68,22 +68,22 @@ return display; case Roles::FilesRole: { - auto tg = static_cast(m_group); + auto tg = static_cast(m_group); return ImageStorage::instance()->imagesForTime(key, tg); } case Roles::FileCountRole: { - auto tg = static_cast(m_group); + auto tg = static_cast(m_group); return ImageStorage::instance()->imagesForTime(key, tg).size(); } case Roles::ImageUrlRole: { - auto tg = static_cast(m_group); + auto tg = static_cast(m_group); return ImageStorage::instance()->imageForTime(key, tg); } case Roles::DateRole: { - auto tg = static_cast(m_group); + auto tg = static_cast(m_group); return ImageStorage::instance()->dateForKey(key, tg); } @@ -105,17 +105,17 @@ return m_times.size(); } -ImageTimeModel::TimeGroup ImageTimeModel::group() const +Groups::TimeGroup ImageTimeModel::group() const { return m_group; } -void ImageTimeModel::setGroup(ImageTimeModel::TimeGroup group) +void ImageTimeModel::setGroup(Groups::TimeGroup group) { beginResetModel(); m_group = group; - auto tg = static_cast(m_group); + auto tg = static_cast(m_group); m_times = ImageStorage::instance()->timeGroups(tg); endResetModel(); diff --git a/src/qmlplugins.cpp b/src/qmlplugins.cpp --- a/src/qmlplugins.cpp +++ b/src/qmlplugins.cpp @@ -30,6 +30,7 @@ #include "fileinfo.h" #include "imagelistmodel.h" #include "types.h" +#include "groups.h" #include @@ -49,4 +50,5 @@ qmlRegisterType (uri, 0, 1, "FileInfo"); qmlRegisterType (uri, 0, 1, "ImageListModel"); qmlRegisterUncreatableType(uri, 0, 1, "Types", "Cannot instantiate the Types class"); + qmlRegisterUncreatableType(uri, 0, 1, "Groups", "Cannot instantiate the Groups class"); }