Commit 099b003e authored by Camilo higuita's avatar Camilo higuita

continue port to new model

parent df2674c5
......@@ -49,10 +49,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <MauiKit/tagging.h>
#endif
#include "src/models/basemodel.h"
#include "src/models/baselist.h"
#include "src/models/gallery/gallery.h"
#include "src/models/albums/albums.h"
#include "./src/models/basemodel.h"
#include "./src/models/baselist.h"
#include "./src/models/gallery/gallery.h"
#include "./src/models/albums/albums.h"
#include "./src/models/folders/foldermodel.h"
#include "./src/models/folders/folders.h"
QStringList getFolderImages(const QString &path)
{
......@@ -127,7 +130,10 @@ int main(int argc, char *argv[])
auto context = engine.rootContext();
context->setContextProperty("pix", &pix);
context->setContextProperty("tag", pix.tag);
const auto dba = DBActions::getInstance();
context->setContextProperty("tag", dba->tag);
context->setContextProperty("dba", dba);
qmlRegisterUncreatableMetaObject(PIX::staticMetaObject, "PIX", 1, 0, "KEY", "Error");
......@@ -136,6 +142,8 @@ int main(int argc, char *argv[])
qmlRegisterType<BaseModel>("PixModel", 1, 0, "PixModel");
qmlRegisterType<Gallery>("GalleryList", 1, 0, "GalleryList");
qmlRegisterType<Albums>("AlbumsList", 1, 0, "AlbumsList");
qmlRegisterType<FolderModel>("FolderModel", 1, 0, "FolderModel");
qmlRegisterType<Folders>("FoldersList", 1, 0, "FoldersList");
#ifdef STATIC_KIRIGAMI
KirigamiPlugin::getInstance().registerTypes();
......
......@@ -48,7 +48,9 @@ SOURCES += main.cpp \
src/models/basemodel.cpp \
src/models/baselist.cpp \
src/models/gallery/gallery.cpp \
src/models/albums/albums.cpp
src/models/albums/albums.cpp \
src/models/folders/folders.cpp \
src/models/folders/foldermodel.cpp \
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
......@@ -75,7 +77,9 @@ HEADERS += \
src/models/basemodel.h \
src/models/baselist.h \
src/models/gallery/gallery.h \
src/models/albums/albums.h
src/models/albums/albums.h \
src/models/folders/folders.h \
src/models/folders/foldermodel.h
include(install.pri)
......
......@@ -22,15 +22,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "db.h"
#ifdef STATIC_MAUIKIT
#include "fmh.h"
#include "tagging.h"
#else
#include <MauiKit/fmh.h>
#include <MauiKit/tagging.h>
#endif
DBActions::DBActions(QObject *parent) : QObject(parent) {}
DBActions::DBActions(QObject *parent) : QObject(parent)
DBActions::~DBActions() {}
void DBActions::init()
{
qDebug() << "Getting collectionDB info from: " << PIX::CollectionDBPath;
......@@ -40,8 +41,22 @@ DBActions::DBActions(QObject *parent) : QObject(parent)
this->tag = Tagging::getInstance(PIX::App, PIX::version, "org.kde.pix", PIX::comment);
}
DBActions::~DBActions() {}
DBActions *DBActions::instance = nullptr;
DBActions *DBActions::getInstance()
{
if(!instance)
{
instance = new DBActions();
qDebug() << "getInstance(): First DBActions instance\n";
instance->init();
return instance;
} else
{
qDebug()<< "getInstance(): previous DBActions instance\n";
return instance;
}
}
bool DBActions::execQuery(const QString &queryTxt)
{
......@@ -131,6 +146,10 @@ bool DBActions::removePic(const QString &url)
bool DBActions::favPic(const QString &url, const bool &fav )
{
if(!this->db->checkExistance("images", "url", url))
if(!this->addPic(url))
return false;
PIX::DB favedPic = {{PIX::KEY::FAV, fav ? "1" : "0"}};
return this->db->update(PIX::TABLEMAP[PIX::TABLE::IMAGES], favedPic, QVariantMap({{PIX::KEYMAP[PIX::KEY::URL], url}}) );
}
......@@ -179,6 +198,17 @@ bool DBActions::cleanTags()
return false;
}
bool DBActions::addAlbum(const QString &album)
{
QVariantMap albumMap
{
{PIX::KEYMAP[PIX::KEY::ALBUM], album},
{PIX::KEYMAP[PIX::KEY::ADD_DATE], QDateTime::currentDateTime()}
};
return this->db->insert(PIX::TABLEMAP[PIX::TABLE::ALBUMS], albumMap);
}
bool DBActions::picAlbum(const QString &album, const QString &url)
{
qDebug()<<"Trying to add to album"<<album<<url;
......@@ -201,14 +231,19 @@ QVariantList DBActions::searchFor(const QStringList &queries, const QString &que
return res;
}
QVariantList DBActions::getFolders(const QString &query)
FMH::MODEL_LIST DBActions::getFolders(const QString &query)
{
QVariantList res;
FMH::MODEL_LIST res;
auto data = this->db->getDBData(query);
/*Data model keys for to be used on MauiKit Icondelegate component */
for(auto i : data)
res << FMH::getFileInfo(i[PIX::KEY::URL]);
res << FMH::getFileInfoModel(i[PIX::KEY::URL]);
return res;
}
PIX::DB_LIST DBActions::getDBData(const QString &query)
{
return this->db->getDBData(query);
}
......@@ -24,41 +24,55 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QObject>
#include "../utils/pic.h"
#ifdef STATIC_MAUIKIT
#include "fmh.h"
#else
#include <MauiKit/fmh.h>
#endif
class DB;
class Tagging;
class DBActions : public QObject
{
Q_OBJECT
public:
explicit DBActions(QObject *parent = nullptr);
~DBActions();
static DBActions *getInstance();
Tagging *tag;
bool execQuery(const QString &queryTxt);
bool insertPic(const PIX::DB &img);
Q_INVOKABLE bool addPic(const QString &url);
Q_INVOKABLE bool removePic(const QString &url);
bool addPic(const QString &url);
bool removePic(const QString &url);
/* actions on model */
Q_INVOKABLE bool favPic(const QString &url, const bool &fav);
Q_INVOKABLE bool isFav(const QString &url);
bool addTag(const QString &tag);
bool albumTag(const QString &tag, const QString &album);
bool removePicTag(const QString &tag, const QString &url);
bool removeAlbumTag(const QString &tag, const QString &album);
bool cleanTags();
Q_INVOKABLE bool addTag(const QString &tag);
Q_INVOKABLE bool albumTag(const QString &tag, const QString &album);
Q_INVOKABLE bool removePicTag(const QString &tag, const QString &url);
Q_INVOKABLE bool removeAlbumTag(const QString &tag, const QString &album);
Q_INVOKABLE bool cleanTags();
bool addAlbum(const QString &album);
bool picAlbum(const QString &album, const QString &url);
Q_INVOKABLE bool picAlbum(const QString &album, const QString &url);
QVariantList searchFor(const QStringList &queries, const QString &queryTxt);
Q_INVOKABLE QVariantList searchFor(const QStringList &queries, const QString &queryTxt);
/* utils */
Q_INVOKABLE QVariantList getFolders(const QString &query);
FMH::MODEL_LIST getFolders(const QString &query);
PIX::DB_LIST getDBData(const QString &query);
public slots:
bool favPic(const QString &url, const bool &fav);
bool isFav(const QString &url);
private:
DB *db;
static DBActions* instance;
explicit DBActions(QObject *parent = nullptr);
~DBActions();
void init();
signals:
void tagAdded(QString tag);
};
......
......@@ -32,13 +32,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "fmh.h"
#endif
class FileLoader : public DBActions
class FileLoader : public QObject
{
Q_OBJECT
public:
FileLoader() : DBActions()
FileLoader() : QObject()
{
this->dba = DBActions::getInstance();
qRegisterMetaType<PIX::DB>("PIX::DB");
qRegisterMetaType<PIX::TABLE>("PIX::TABLE");
qRegisterMetaType<QMap<PIX::TABLE, bool>>("QMap<PIX::TABLE,bool>");
......@@ -93,7 +94,7 @@ public slots:
{
if(go)
{
if(this->addPic(url))
if(this->dba->addPic(url))
newPics++;
}else break;
}
......@@ -107,11 +108,11 @@ public slots:
}
signals:
void trackReady(PIX::DB track);
void finished(int size);
void collectionSize(int size);
private:
DBActions * dba;
QThread t;
bool go = false;
bool wait = true;
......
......@@ -150,6 +150,7 @@ Maui.ApplicationWindow
}
]
content: ColumnLayout
{
id: mainPage
......@@ -286,8 +287,9 @@ Maui.ApplicationWindow
Connections
{
target: pix
// onRefreshViews: PIX.refreshViews()
onRefreshViews: PIX.refreshViews()
onViewPics: VIEWER.openExternalPics(pics, 0)
onNewPic: console.log(pic.url)
}
Connections
......
#include "albums.h"
#include "./src/db/db.h"
#include "./src/db/dbactions.h"
#ifdef STATIC_MAUIKIT
#include "fmh.h"
#include "tagging.h"
#else
#include <MauiKit/fmh.h>
#include <MauiKit/tagging.h>
#endif
Albums::Albums(QObject *parent) : BaseList(parent)
{
qDebug()<< "CREATING GALLERY LIST";
this->db = DB::getInstance();
this->tag = Tagging::getInstance(PIX::App, PIX::version, "org.kde.pix", PIX::comment);
this->tag = Tagging::getInstance(PIX::App, PIX::version, "org.kde.pix", PIX::comment);
this->dba = DBActions::getInstance();
this->sortList();
connect(this, &Albums::sortByChanged, this, &Albums::sortList);
......@@ -106,24 +101,13 @@ void Albums::setList()
{
emit this->preListChanged();
this->list = this->db->getDBData(this->query);
this->list = this->dba->getDBData(this->query);
qDebug()<< "ALBUMS LIST READY"<< list;
this->sortList();
emit this->postListChanged();
}
bool Albums::addAlbum(const QString &album)
{
QVariantMap albumMap
{
{PIX::KEYMAP[PIX::KEY::ALBUM], album},
{PIX::KEYMAP[PIX::KEY::ADD_DATE], QDateTime::currentDateTime()}
};
return this->db->insert(PIX::TABLEMAP[PIX::TABLE::ALBUMS], albumMap);
}
QVariantMap Albums::get(const int &index) const
{
if(index >= this->list.size() || index < 0)
......@@ -142,7 +126,7 @@ bool Albums::insert(const QVariantMap &pic)
{
emit this->preItemAppended();
if(this->addAlbum(pic[PIX::KEYMAP[PIX::KEY::ALBUM]].toString()))
if(this->dba->addAlbum(pic[PIX::KEYMAP[PIX::KEY::ALBUM]].toString()))
{
emit postItemAppended();
return true;
......
......@@ -5,8 +5,7 @@
#include "./src/models/baselist.h"
#include "./src/utils/pic.h"
class DB;
class Tagging;
class DBActions;
class Albums : public BaseList
{
Q_OBJECT
......@@ -24,8 +23,7 @@ public:
uint getSortBy() const;
private:
DB *db;
Tagging *tag;
DBActions *dba;
PIX::DB_LIST list;
void sortList();
void setList();
......@@ -34,7 +32,6 @@ private:
uint sort = PIX::KEY::ADD_DATE;
protected:
bool addAlbum(const QString &album);
signals:
void queryChanged();
......
......@@ -46,7 +46,7 @@ Qt::ItemFlags BaseModel::flags(const QModelIndex &index) const
QHash<int, QByteArray> BaseModel::roleNames() const
{
QHash<int, QByteArray> names;
QHash<int, QByteArray> names;
for(auto key : PIX::KEYMAP.keys())
names[key] = QString(PIX::KEYMAP[key]).toUtf8();
return names;
......@@ -107,3 +107,8 @@ void BaseModel::setList(BaseList *value)
endResetModel();
}
QVariantMap BaseModel::get(const int &index) const
{
return this->mList->get(index);
}
......@@ -32,6 +32,9 @@ private:
BaseList *mList;
signals:
void listChanged();
public slots:
QVariantMap get(const int &index) const;
};
#endif // NOTESMODEL_H
#include "foldermodel.h"
#include "folders.h"
#ifdef STATIC_MAUIKIT
#include "fmh.h"
#else
#include <MauiKit/fmh.h>
#endif
FolderModel::FolderModel(QObject *parent)
: QAbstractListModel(parent),
mList(nullptr)
{}
int FolderModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() || !mList)
return 0;
return mList->items().size();
}
QVariant FolderModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !mList)
return QVariant();
return mList->items().at(index.row())[static_cast<FMH::MODEL_KEY>(role)];
}
bool FolderModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
return false;
}
Qt::ItemFlags FolderModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable; // FIXME: Implement me!
}
QHash<int, QByteArray> FolderModel::roleNames() const
{
QHash<int, QByteArray> names;
for(auto key : FMH::MODEL_NAME.keys())
names[key] = QString(FMH::MODEL_NAME[key]).toUtf8();
return names;
}
Folders *FolderModel::getList() const
{
return this->mList;
}
void FolderModel::setList(Folders *value)
{
beginResetModel();
if(mList)
mList->disconnect(this);
mList = value;
if(mList)
{
connect(this->mList, &Folders::preItemAppended, this, [=]()
{
const int index = mList->items().size();
beginInsertRows(QModelIndex(), index, index);
});
connect(this->mList, &Folders::postItemAppended, this, [=]()
{
endInsertRows();
});
connect(this->mList, &Folders::preItemRemoved, this, [=](int index)
{
beginRemoveRows(QModelIndex(), index, index);
});
connect(this->mList, &Folders::postItemRemoved, this, [=]()
{
endRemoveRows();
});
connect(this->mList, &Folders::updateModel, this, [=](int index, QVector<int> roles)
{
emit this->dataChanged(this->index(index), this->index(index), roles);
});
connect(this->mList, &Folders::preListChanged, this, [=]()
{
beginResetModel();
});
connect(this->mList, &Folders::postListChanged, this, [=]()
{
endResetModel();
});
}
endResetModel();
}
#ifndef FOLDERMODEL_H
#define FOLDERMODEL_H
#include <QAbstractListModel>
#include <QList>
class Folders;
class FolderModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(Folders *list READ getList WRITE setList)
public:
explicit FolderModel(QObject *parent = nullptr);
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
// Editable:
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
virtual QHash<int, QByteArray> roleNames() const override;
Folders* getList() const;
void setList(Folders *value);
private:
Folders *mList;
signals:
void listChanged();
};
#endif // NOTESMODEL_H
#include "./src/models/folders/folders.h"
#include "./src/db/dbactions.h"
Folders::Folders(QObject *parent) : QObject(parent)
{
qDebug()<< "CREATING GALLERY LIST";
this->dba = DBActions::getInstance();
this->sortList();
connect(this, &Folders::sortByChanged, this, &Folders::sortList);
connect(this, &Folders::orderChanged, this, &Folders::sortList);
connect(this, &Folders::queryChanged, this, &Folders::setList);
connect(this, &Folders::sortByChanged, this, &Folders::setList);
}
void Folders::setSortBy(const uint &sort)
{
if(this->sort == sort)
return;
this->sort = sort;
emit this->sortByChanged();
}
uint Folders::getSortBy() const
{
return this->sort;
}
FMH::MODEL_LIST Folders::items() const
{
return this->list;
}
void Folders::setQuery(const QString &query)
{
if(this->query == query)
return;
this->query = query;
qDebug()<< "setting ALBUMS query"<< this->query;
emit this->queryChanged();
}
QString Folders::getQuery() const
{
return this->query;
}
void Folders::sortList()
{
const auto key = static_cast<FMH::MODEL_KEY>(this->sort);
qSort(this->list.begin(), this->list.end(), [key](const FMH::MODEL& e1, const FMH::MODEL& e2) -> bool
{
auto role = key;
switch(role)
{
case FMH::MODEL_KEY::MODIFIED:
case FMH::MODEL_KEY::DATE:
{
auto currentTime = QDateTime::currentDateTime();
auto date1 = QDateTime::fromString(e1[role], Qt::TextDate);
auto date2 = QDateTime::fromString(e2[role], Qt::TextDate);
if(date1.secsTo(currentTime) < date2.secsTo(currentTime))
return true;
break;
}
case FMH::MODEL_KEY::LABEL:
{
const auto str1 = QString(e1[role]).toLower();
const auto str2 = QString(e2[role]).toLower();
if(str1 < str2)
return true;
break;
}
default:
if(e1[role] < e2[role])
return true;
}
return false;
});
}
void Folders::setList()
{
emit this->preListChanged();
this->list = this->dba->getFolders(this->query);
// this->list =
// qDebug()<< "ALBUMS LIST READY"<< list;
this->sortList();
emit this->postListChanged();
}
QVariantMap Folders::get(const int &index) const
{
if(index >= this->list.size() || index < 0)
return QVariantMap();
const auto folder = this->list.at(index);
QVariantMap res;
for(auto key : folder.keys())
res.insert(FMH::MODEL_NAME[key], folder[key]);
return res;
}
void Folders::refresh()
{
this->setList();
}
#ifndef FOLDERS_H
#define FOLDERS_H
#include <QObject>
#include "./src/utils/pic.h"
#ifdef STATIC_MAUIKIT
#include "fmh.h"
#else
#include <MauiKit/fmh.h>
#endif
class DBActions;
class Folders : public QObject
{
Q_OBJECT
Q_PROPERTY(QString query READ getQuery WRITE setQuery NOTIFY queryChanged())
Q_PROPERTY(uint sortBy READ getSortBy WRITE setSortBy NOTIFY sortByChanged)
public:
explicit Folders(QObject *parent = nullptr);
FMH::MODEL_LIST items() const;
void setQuery(const QString &query);
QString getQuery() const;
void setSortBy(const uint &sort);
uint getSortBy() const;
private:
DBActions *dba;
FMH::MODEL_LIST list;
void sortList();
void setList();
QString query;
uint sort = FMH::MODIFIED;
protected:
signals:
void queryChanged();
void orderChanged();
void sortByChanged();
void preItemAppended();
void postItemAppended();
void preItemRemoved(int index);
void postItemRemoved();
void updateModel(int index, QVector<int> roles);
void preListChanged();
void postListChanged();
public slots:
QVariantMap get(const int &index) const;
void refresh();
};
#endif // ALBUMS_H