File indexing completed on 2024-05-12 15:54:50

0001 /*
0002  * SPDX-FileCopyrightText: (C) 2017 Atul Sharma <atulsharma406@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "imagelistmodel.h"
0008 #include "imagestorage.h"
0009 #include "roles.h"
0010 
0011 #include <QDebug>
0012 #include <QMimeDatabase>
0013 
0014 ImageListModel::ImageListModel(QObject *parent)
0015     : OpenFileModel({}, parent)
0016 {
0017     connect(this, &ImageListModel::locationGroupChanged, this, &ImageListModel::slotLocationGroupChanged);
0018     connect(this, &ImageListModel::timeGroupChanged, this, &ImageListModel::slotTimeGroupChanged);
0019     connect(this, &ImageListModel::queryChanged, this, &ImageListModel::slotResetModel);
0020 
0021     connect(ImageStorage::instance(), &ImageStorage::storageModified, this, &ImageListModel::slotResetModel);
0022 }
0023 
0024 ImageListModel::~ImageListModel()
0025 {
0026 }
0027 
0028 void ImageListModel::slotLocationGroupChanged()
0029 {
0030     if (m_locationGroup != -1) {
0031         m_locations = ImageStorage::instance()->locations(static_cast<Types::LocationGroup>(m_locationGroup));
0032         m_queryType = Types::LocationQuery;
0033     }
0034 }
0035 
0036 void ImageListModel::slotTimeGroupChanged()
0037 {
0038     if (m_timeGroup != -1) {
0039         m_times = ImageStorage::instance()->timeTypes(static_cast<Types::TimeGroup>(m_timeGroup));
0040         m_queryType = Types::TimeQuery;
0041     }
0042 }
0043 
0044 void ImageListModel::slotResetModel()
0045 {
0046     beginResetModel();
0047     if (m_queryType == Types::LocationQuery) {
0048         m_images = ImageStorage::instance()->imagesForLocation(m_query, static_cast<Types::LocationGroup>(m_locationGroup));
0049     } else if (m_queryType == Types::TimeQuery) {
0050         m_images = ImageStorage::instance()->imagesForTime(m_query, static_cast<Types::TimeGroup>(m_timeGroup));
0051     }
0052     endResetModel();
0053 }
0054 
0055 Types::LocationGroup ImageListModel::locationGroup() const
0056 {
0057     return m_locationGroup;
0058 }
0059 
0060 void ImageListModel::setLocationGroup(const Types::LocationGroup &group)
0061 {
0062     m_locationGroup = group;
0063     emit locationGroupChanged();
0064 }
0065 
0066 Types::TimeGroup ImageListModel::timeGroup() const
0067 {
0068     return m_timeGroup;
0069 }
0070 
0071 void ImageListModel::setTimeGroup(const Types::TimeGroup &group)
0072 {
0073     m_timeGroup = group;
0074     emit timeGroupChanged();
0075 }
0076 
0077 Types::QueryType ImageListModel::queryType() const
0078 {
0079     return m_queryType;
0080 }
0081 
0082 void ImageListModel::setQueryType(const Types::QueryType &type)
0083 {
0084     m_queryType = type;
0085 }
0086 
0087 QByteArray ImageListModel::query() const
0088 {
0089     return m_query;
0090 }
0091 void ImageListModel::setQuery(const QByteArray &statement)
0092 {
0093     m_query = statement;
0094     emit queryChanged();
0095 }
0096 
0097 QByteArray ImageListModel::queryForIndex(const int &index)
0098 {
0099     if (m_queryType == Types::LocationQuery) {
0100         return m_locations.at(index).first;
0101     } else if (m_queryType == Types::TimeQuery) {
0102         return m_times.at(index).first;
0103     }
0104     return QByteArray();
0105 }
0106 
0107 #include "moc_imagelistmodel.cpp"