File indexing completed on 2024-05-12 04:21:06

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         emit queryTypeChanged();
0034     }
0035 }
0036 
0037 void ImageListModel::slotTimeGroupChanged()
0038 {
0039     if (m_timeGroup != -1) {
0040         m_times = ImageStorage::instance()->timeTypes(static_cast<Types::TimeGroup>(m_timeGroup));
0041         m_queryType = Types::TimeQuery;
0042         emit queryTypeChanged();
0043     }
0044 }
0045 
0046 void ImageListModel::slotResetModel()
0047 {
0048     beginResetModel();
0049     if (m_queryType == Types::LocationQuery) {
0050         m_images = ImageStorage::instance()->imagesForLocation(m_query, static_cast<Types::LocationGroup>(m_locationGroup));
0051     } else if (m_queryType == Types::TimeQuery) {
0052         m_images = ImageStorage::instance()->imagesForTime(m_query, static_cast<Types::TimeGroup>(m_timeGroup));
0053     }
0054     endResetModel();
0055 }
0056 
0057 Types::LocationGroup ImageListModel::locationGroup() const
0058 {
0059     return m_locationGroup;
0060 }
0061 
0062 void ImageListModel::setLocationGroup(const Types::LocationGroup &group)
0063 {
0064     m_locationGroup = group;
0065     emit locationGroupChanged();
0066 }
0067 
0068 Types::TimeGroup ImageListModel::timeGroup() const
0069 {
0070     return m_timeGroup;
0071 }
0072 
0073 void ImageListModel::setTimeGroup(const Types::TimeGroup &group)
0074 {
0075     m_timeGroup = group;
0076     emit timeGroupChanged();
0077 }
0078 
0079 Types::QueryType ImageListModel::queryType() const
0080 {
0081     return m_queryType;
0082 }
0083 
0084 void ImageListModel::setQueryType(const Types::QueryType &type)
0085 {
0086     m_queryType = type;
0087     emit queryTypeChanged();
0088 }
0089 
0090 QByteArray ImageListModel::query() const
0091 {
0092     return m_query;
0093 }
0094 void ImageListModel::setQuery(const QByteArray &statement)
0095 {
0096     m_query = statement;
0097     emit queryChanged();
0098 }
0099 
0100 QByteArray ImageListModel::queryForIndex(const int &index)
0101 {
0102     if (m_queryType == Types::LocationQuery) {
0103         return m_locations.at(index).first;
0104     } else if (m_queryType == Types::TimeQuery) {
0105         return m_times.at(index).first;
0106     }
0107     return QByteArray();
0108 }
0109 
0110 #include "moc_imagelistmodel.cpp"