Commit df2674c5 authored by Camilo higuita's avatar Camilo higuita

start moving to pix own custom models instead of qml listmodel

parent c2d8bb33
......@@ -16,19 +16,36 @@ include(ECMAddAppIcon)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTORCC ON)
add_executable(pix
assets.qrc
set(pix_SRCS
main.cpp
src/pix.cpp
src/db/db.cpp
src/db/db.h
src/db/dbactions.cpp
src/db/dbactions.h
src/db/dbactions.cpp
src/models/basemodel.cpp
src/models/baselist.cpp
)
set(pix_HDRS
src/pix.h
src/db/fileloader.h
src/pix.cpp
src/db/db.h
src/db/dbactions.h
src/utils/pic.h
src/models/basemodel.h
src/models/baselist.h
)
set(pix_ASSETS
src/qml.qrc
assets.qrc
)
add_executable(pix
${pix_SRCS}
${pix_HDRS}
${pix_ASSETS}
)
if (ANDROID)
find_package(Qt5 REQUIRED COMPONENTS AndroidExtras)
......
......@@ -42,20 +42,25 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#endif
#ifdef STATIC_MAUIKIT
#include "fm.h"
#include "fmh.h"
#include "tagging.h"
#else
#include <MauiKit/fm.h>
#include <MauiKit/fmh.h>
#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"
QStringList getFolderImages(const QString &path)
{
QStringList urls;
if (QFileInfo(path).isDir())
{
QDirIterator it(path, PIX::formats, QDir::Files, QDirIterator::Subdirectories);
QDirIterator it(path, FMH::FILTER_LIST[FMH::FILTER_TYPE::IMAGE], QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
urls << it.next();
......@@ -124,6 +129,14 @@ int main(int argc, char *argv[])
context->setContextProperty("pix", &pix);
context->setContextProperty("tag", pix.tag);
qmlRegisterUncreatableMetaObject(PIX::staticMetaObject, "PIX", 1, 0, "KEY", "Error");
qmlRegisterUncreatableType<BaseList>("BaseList", 1, 0, "BaseList", QStringLiteral("BaseList should not be created in QML"));
qmlRegisterType<BaseModel>("PixModel", 1, 0, "PixModel");
qmlRegisterType<Gallery>("GalleryList", 1, 0, "GalleryList");
qmlRegisterType<Albums>("AlbumsList", 1, 0, "AlbumsList");
#ifdef STATIC_KIRIGAMI
KirigamiPlugin::getInstance().registerTypes();
#endif
......
......@@ -44,7 +44,11 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp \
src/pix.cpp \
src/db/db.cpp \
src/db/dbactions.cpp
src/db/dbactions.cpp \
src/models/basemodel.cpp \
src/models/baselist.cpp \
src/models/gallery/gallery.cpp \
src/models/albums/albums.cpp
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
......@@ -67,7 +71,11 @@ HEADERS += \
src/db/fileloader.h \
src/db/db.h \
src/db/dbactions.h \
src/utils/pic.h
src/utils/pic.h \
src/models/basemodel.h \
src/models/baselist.h \
src/models/gallery/gallery.h \
src/models/albums/albums.h
include(install.pri)
......
......@@ -9,7 +9,7 @@ var Query = {
allTags : "select * from tags",
tagPics_: "select i.* from images i inner join images_tags it on it.url = i.url where it.tag = \"%1\"",
allAlbums : "select * from albums order by strftime(\"%s\", addDate) desc",
allAlbums : "select * from albums",
allAlbumPics_ : "select distinct i.* from images i inner join images_tags it on it.url = i.url inner join albums_tags at on at.tag = it.tag where at.album = \"%1\" union select i.* from images_albums ia inner join images i on i.url = ia.url where ia.album = \"%1\"",
albumPics_ : "select i.* from images_albums ia inner join images i on i.url = ia.url where ia.album = \"%1\" order by strftime(\"%s\", ia.addDate) desc",
albumPicsTags_ : "select i.* from images i inner join images_tags it on it.url = i.url inner join albums_tags at on at.tag = it.tag where at.album = \"%1\"",
......
......@@ -24,6 +24,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QStringList>
#include <QSqlQuery>
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
#include <MauiKit/fmh.h>
#else
#include "fmh.h"
#endif
DB::DB(QObject *parent) : QObject(parent)
{
QDir collectionDBPath_dir(PIX::CollectionDBPath);
......@@ -31,7 +37,7 @@ DB::DB(QObject *parent) : QObject(parent)
collectionDBPath_dir.mkpath(".");
this->name = QUuid::createUuid().toString();
if(!PIX::fileExists(PIX::CollectionDBPath + PIX::DBName))
if(!FMH::fileExists(PIX::CollectionDBPath + PIX::DBName))
{
this->openDB(this->name);
qDebug()<<"Collection doesn't exists, trying to create it" << PIX::CollectionDBPath + PIX::DBName;
......@@ -44,6 +50,37 @@ DB::~DB()
this->m_db.close();
}
void DB::init()
{
QDir collectionDBPath_dir(PIX::CollectionDBPath);
if (!collectionDBPath_dir.exists())
collectionDBPath_dir.mkpath(".");
this->name = QUuid::createUuid().toString();
if(!FMH::fileExists(PIX::CollectionDBPath + PIX::DBName))
{
this->openDB(this->name);
qDebug()<<"Collection doesn't exists, trying to create it" << PIX::CollectionDBPath + PIX::DBName;
this->prepareCollectionDB();
}else this->openDB(this->name);
}
DB *DB::instance = nullptr;
DB *DB::getInstance()
{
if(!instance)
{
instance = new DB();
qDebug() << "getInstance(): First DB instance\n";
instance->init();
return instance;
} else
{
qDebug()<< "getInstance(): previous DB instance\n";
return instance;
}
}
void DB::openDB(const QString &name)
{
if(!QSqlDatabase::contains(name))
......@@ -216,7 +253,7 @@ bool DB::remove(const QString &tableName, const PIX::DB &removeData)
}
QString strValues;
auto i = 0;
auto i = 0;
for (auto key : removeData.keys())
{
strValues.append(QString("%1 = \"%2\"").arg(PIX::KEYMAP[key], removeData[key]));
......@@ -231,3 +268,66 @@ bool DB::remove(const QString &tableName, const PIX::DB &removeData)
return this->getQuery(sqlQueryString).exec();
}
PIX::DB_LIST DB::getDBData(const QString &queryTxt)
{
PIX::DB_LIST mapList;
auto query = this->getQuery(queryTxt);
if(query.exec())
{
while(query.next())
{
PIX::DB data;
for(auto key : PIX::KEYMAP.keys())
if(query.record().indexOf(PIX::KEYMAP[key])>-1)
data.insert(key, query.value(PIX::KEYMAP[key]).toString());
const auto url = data[PIX::KEY::URL];
if(!url.isEmpty())
{
if(FMH::fileExists(url))
mapList<< data;
// else
// removePic(data[PIX::KEY::URL]);
}else mapList<< data;
}
}else qDebug()<< query.lastError()<< query.lastQuery();
return mapList;
}
QVariantList DB::get(const QString &queryTxt)
{
QVariantList mapList;
auto query = this->getQuery(queryTxt);
if(query.exec())
{
while(query.next())
{
QVariantMap data;
for(auto key : PIX::KEYMAP.keys())
if(query.record().indexOf(PIX::KEYMAP[key])>-1)
data[PIX::KEYMAP[key]] = query.value(PIX::KEYMAP[key]).toString();
auto url = data[PIX::KEYMAP[PIX::KEY::URL]].toString();
// if(!url.isEmpty())
// {
// if(FMH::fileExists(url))
// mapList<< data;
// else
// removePic(data[PIX::KEYMAP[PIX::KEY::URL]].toString());
// }else
mapList<< data;
}
}else qDebug()<< query.lastError()<< query.lastQuery();
return mapList;
}
......@@ -40,34 +40,37 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
class DB : public QObject
{
Q_OBJECT
private:
QString name;
QSqlDatabase m_db;
public:
explicit DB(QObject *parent = nullptr);
~ DB();
Q_OBJECT
private:
QString name;
QSqlDatabase m_db;
static DB* instance;
explicit DB(QObject *parent = nullptr);
~ DB();
void openDB(const QString &name);
public:
static DB *getInstance();
/*basic public actions*/
void prepareCollectionDB() const;
/* utils*/
Q_INVOKABLE bool checkExistance(const QString &tableName, const QString &searchId, const QString &search);
/* utils*/
Q_INVOKABLE bool checkExistance(const QString &tableName, const QString &searchId, const QString &search);
QSqlQuery getQuery(const QString &queryTxt);
bool insert(const QString &tableName, const QVariantMap &insertData);
bool update(const QString &tableName, const PIX::DB &updateData, const QVariantMap &where);
bool update(const QString &table, const QString &column, const QVariant &newValue, const QVariant &op, const QString &id);
bool remove(const QString &tableName, const PIX::DB &removeData);
PIX::DB_LIST getDBData(const QString &queryTxt);
QVariantList get(const QString &queryTxt);
protected:
QSqlQuery getQuery(const QString &queryTxt);
protected:
void init();
void openDB(const QString &name);
void prepareCollectionDB() const;
bool insert(const QString &tableName, const QVariantMap &insertData);
bool update(const QString &tableName, const PIX::DB &updateData, const QVariantMap &where);
bool update(const QString &table, const QString &column, const QVariant &newValue, const QVariant &op, const QString &id);
bool remove(const QString &tableName, const PIX::DB &removeData);
signals:
signals:
public slots:
public slots:
};
#endif // DB_H
......@@ -19,89 +19,33 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "dbactions.h"
#include "db.h"
#ifdef STATIC_MAUIKIT
#include "fm.h"
#include "fmh.h"
#include "tagging.h"
#else
#include <MauiKit/fm.h>
#include <MauiKit/fmh.h>
#include <MauiKit/tagging.h>
#endif
DBActions::DBActions(QObject *parent) : DB(parent)
DBActions::DBActions(QObject *parent) : QObject(parent)
{
qDebug() << "Getting collectionDB info from: " << PIX::CollectionDBPath;
qDebug()<< "Starting DBActions";
this->db = DB::getInstance();
this->tag = Tagging::getInstance(PIX::App, PIX::version, "org.kde.pix", PIX::comment);
}
DBActions::~DBActions()
{
}
PIX::DB_LIST DBActions::getDBData(const QString &queryTxt)
{
PIX::DB_LIST mapList;
auto query = this->getQuery(queryTxt);
if(query.exec())
{
while(query.next())
{
PIX::DB data;
for(auto key : PIX::KEYMAP.keys())
if(query.record().indexOf(PIX::KEYMAP[key])>-1)
data.insert(key, query.value(PIX::KEYMAP[key]).toString());
mapList<< data;
}
}else qDebug()<< query.lastError()<< query.lastQuery();
return mapList;
}
QVariantList DBActions::get(const QString &queryTxt)
{
QVariantList mapList;
auto query = this->getQuery(queryTxt);
if(query.exec())
{
while(query.next())
{
QVariantMap data;
for(auto key : PIX::KEYMAP.keys())
if(query.record().indexOf(PIX::KEYMAP[key])>-1)
data[PIX::KEYMAP[key]] = query.value(PIX::KEYMAP[key]).toString();
auto url =data[PIX::KEYMAP[PIX::KEY::URL]].toString();
if(!url.isEmpty())
{
if(PIX::fileExists(url))
mapList<< data;
else
removePic(data[PIX::KEYMAP[PIX::KEY::URL]].toString());
}else
mapList<< data;
}
DBActions::~DBActions() {}
}else qDebug()<< query.lastError()<< query.lastQuery();
return mapList;
}
bool DBActions::execQuery(const QString &queryTxt)
{
auto query = this->getQuery(queryTxt);
auto query = this->db->getQuery(queryTxt);
return query.exec();
}
......@@ -121,7 +65,7 @@ bool DBActions::insertPic(const PIX::DB &img)
qDebug()<< "writting to db: "<<title<<url;
/* first needs to insert album and artist*/
QVariantMap sourceMap {{PIX::KEYMAP[PIX::KEY::URL],sourceUrl}};
insert(PIX::TABLEMAP[PIX::TABLE::SOURCES], sourceMap);
this->db->insert(PIX::TABLEMAP[PIX::TABLE::SOURCES], sourceMap);
QVariantMap imgMap {{PIX::KEYMAP[PIX::KEY::URL], url},
......@@ -134,13 +78,13 @@ bool DBActions::insertPic(const PIX::DB &img)
{PIX::KEYMAP[PIX::KEY::PIC_DATE], picDate},
{PIX::KEYMAP[PIX::KEY::PLACE], place},
{PIX::KEYMAP[PIX::KEY::ADD_DATE], QDateTime::currentDateTime()}};
return insert(PIX::TABLEMAP[PIX::TABLE::IMAGES], imgMap);
return this->db->insert(PIX::TABLEMAP[PIX::TABLE::IMAGES], imgMap);
}
bool DBActions::addPic(const QString &url)
{
if(!this->checkExistance(PIX::TABLEMAP[PIX::TABLE::IMAGES], PIX::KEYMAP[PIX::KEY::URL], url))
if(!this->db->checkExistance(PIX::TABLEMAP[PIX::TABLE::IMAGES], PIX::KEYMAP[PIX::KEY::URL], url))
{
QFileInfo info(url);
auto title = info.baseName();
......@@ -168,17 +112,17 @@ bool DBActions::addPic(const QString &url)
bool DBActions::removePic(const QString &url)
{
auto queryTxt = QString("DELETE FROM images WHERE url = \"%1\"").arg(url);
auto query = this->getQuery(queryTxt);
auto query = this->db->getQuery(queryTxt);
if(query.exec())
{
queryTxt = QString("DELETE FROM images_tags WHERE url = \"%1\"").arg(url);
this->getQuery(queryTxt).exec();
this->db->getQuery(queryTxt).exec();
queryTxt = QString("DELETE FROM images_albums WHERE url = \"%1\"").arg(url);
this->getQuery(queryTxt).exec();
this->db->getQuery(queryTxt).exec();
queryTxt = QString("DELETE FROM images_notes WHERE url = \"%1\"").arg(url);
this->getQuery(queryTxt).exec();
this->db->getQuery(queryTxt).exec();
return true;
}
......@@ -188,12 +132,12 @@ bool DBActions::removePic(const QString &url)
bool DBActions::favPic(const QString &url, const bool &fav )
{
PIX::DB favedPic = {{PIX::KEY::FAV, fav ? "1" : "0"}};
return this->update(PIX::TABLEMAP[PIX::TABLE::IMAGES], favedPic, QVariantMap({{PIX::KEYMAP[PIX::KEY::URL], url}}) );
return this->db->update(PIX::TABLEMAP[PIX::TABLE::IMAGES], favedPic, QVariantMap({{PIX::KEYMAP[PIX::KEY::URL], url}}) );
}
bool DBActions::isFav(const QString &url)
{
auto data = this->getDBData(QString("select * from images where url = '%1'").arg(url));
auto data = this->db->getDBData(QString("select * from images where url = '%1'").arg(url));
if (data.isEmpty()) return false;
......@@ -221,13 +165,13 @@ bool DBActions::albumTag(const QString &tag, const QString &album)
bool DBActions::removePicTag(const QString &tag, const QString &url)
{
PIX::DB tagMap {{PIX::KEY::URL, url}, {PIX::KEY::TAG, tag}};
return this->remove(PIX::TABLEMAP[PIX::TABLE::IMAGES_TAGS], tagMap);
return this->db->remove(PIX::TABLEMAP[PIX::TABLE::IMAGES_TAGS], tagMap);
}
bool DBActions::removeAlbumTag(const QString &tag, const QString &album)
{
PIX::DB tagMap {{PIX::KEY::TAG, tag}, {PIX::KEY::ALBUM, album}};
return this->remove(PIX::TABLEMAP[PIX::TABLE::ALBUMS_TAGS], tagMap);
return this->db->remove(PIX::TABLEMAP[PIX::TABLE::ALBUMS_TAGS], tagMap);
}
bool DBActions::cleanTags()
......@@ -235,35 +179,24 @@ 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->insert(PIX::TABLEMAP[PIX::TABLE::ALBUMS], albumMap);
}
bool DBActions::picAlbum(const QString &album, const QString &url)
{
qDebug()<<"Trying to add to album"<<album<<url;
this->addAlbum(album);
// this->addAlbum(album);
QVariantMap albumPic
{
{PIX::KEYMAP[PIX::KEY::URL], url},
{PIX::KEYMAP[PIX::KEY::ALBUM], album},
{PIX::KEYMAP[PIX::KEY::ADD_DATE], QDateTime::currentDateTime()}
};
return this->insert(PIX::TABLEMAP[PIX::TABLE::IMAGES_ALBUMS], albumPic);
return this->db->insert(PIX::TABLEMAP[PIX::TABLE::IMAGES_ALBUMS], albumPic);
}
QVariantList DBActions::searchFor(const QStringList &queries, const QString &queryTxt)
{
QVariantList res;
for(auto query : queries)
res << get(PIX::getQuery("searchFor_").arg(query));
res << this->db->get(PIX::getQuery("searchFor_").arg(query));
return res;
}
......@@ -271,21 +204,11 @@ QVariantList DBActions::searchFor(const QStringList &queries, const QString &que
QVariantList DBActions::getFolders(const QString &query)
{
QVariantList res;
auto data = this->getDBData(query);
auto data = this->db->getDBData(query);
/*Data model keys for to be used on MauiKit Icondelegate component */
for(auto i : data)
{
res << FM::getDirInfo(i[PIX::KEY::URL], FMH::PATHTYPE_NAME[FMH::PATHTYPE_KEY::PLACES_PATH]);
// QVariantMap {
// {PIX::KEYMAP[PIX::KEY::URL], i[PIX::KEY::URL]},
// {"label", QFileInfo(i[PIX::KEY::URL]).baseName()},
// {"mime", "inode/directory"},
// {"icon", "folder"},
// {"path", i[PIX::KEY::URL]}
// };
}
return res;
res << FMH::getFileInfo(i[PIX::KEY::URL]);
return res;
}
......@@ -22,12 +22,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define DBACTIONS_H
#include <QObject>
#include "db.h"
#include "../utils/pic.h"
class DB;
class Tagging;
class DBActions : public DB
class DBActions : public QObject
{
Q_OBJECT
public:
......@@ -35,9 +34,6 @@ public:
~DBActions();
Tagging *tag;
PIX::DB_LIST getDBData(const QString &queryTxt);
bool execQuery(const QString &queryTxt);
bool insertPic(const PIX::DB &img);
......@@ -54,16 +50,14 @@ public:
Q_INVOKABLE bool removeAlbumTag(const QString &tag, const QString &album);
Q_INVOKABLE bool cleanTags();
Q_INVOKABLE bool addAlbum(const QString &album);
Q_INVOKABLE bool picAlbum(const QString &album, const QString &url);
Q_INVOKABLE QVariantList searchFor(const QStringList &queries, const QString &queryTxt);
/* utils */
Q_INVOKABLE QVariantList getFolders(const QString &query);
Q_INVOKABLE QVariantList get(const QString &queryTxt);
private:
DB *db;
signals:
void tagAdded(QString tag);
......
......@@ -26,6 +26,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QFileInfo>
#include "dbactions.h"
#if (defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID))
#include <MauiKit/fmh.h>
#else
#include "fmh.h"
#endif
class FileLoader : public DBActions
{
Q_OBJECT
......@@ -71,7 +77,7 @@ public slots:
for(auto path : paths)
if (QFileInfo(path).isDir())
{
QDirIterator it(path, PIX::formats, QDir::Files, QDirIterator::Subdirectories);
QDirIterator it(path, FMH::FILTER_LIST[FMH::FILTER_TYPE::IMAGE], QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
urls << it.next();
......
......@@ -52,6 +52,8 @@ Maui.ApplicationWindow
// altToolBars: true
about.appDescription: qsTr("Pix is an image gallery manager made for Maui. Pix is a convergent and multiplatform app that works under Android and GNU Linux distros.")
about.appIcon: "qrc:/img/assets/pix.svg"
property alias dialog : dialogLoader.item
/*READONLY PROPS*/
readonly property var views : ({
viewer: 0,
......@@ -95,7 +97,11 @@ Maui.ApplicationWindow
Maui.MenuItem
{
text: "Sources"
onTriggered: fmDialog.show()
onTriggered:
{
dialogLoader.sourceComponent= fmDialogComponent
dialog.show()
}
}
]
......@@ -188,7 +194,6 @@ Maui.ApplicationWindow
{
id: searchView
}
}
Maui.SelectionBar
......@@ -202,7 +207,6 @@ Maui.ApplicationWindow
visible: selectionList.count > 0 && currentView !== views.viewer
onIconClicked: picMenu.showMultiple(selectedPaths)
onExitClicked: clear()
}
}
......@@ -211,40 +215,78 @@ Maui.ApplicationWindow
id: picMenu
onFavClicked: VIEWER.fav(urls)
onRemoveClicked: PIX.removePic(urls)
onShareClicked: isAndroid ? Maui.Android.shareDialog(urls) : shareDialog.show(urls)
onAddClicked: albumsDialog.show(urls)
onTagsClicked: tagsDialog.show(urls)
onShareClicked:
{
if(isAndroid)
Maui.Android.shareDialog(urls)
else
{
dialogLoader.sourceComponent = shareDialogComponent
dialog.show(urls)
}
}
onAddClicked:
{
dialogLoader.sourceComponent = albumsDialogComponent
dialog.show(urls)
}
onTagsClicked:
{
dialogLoader.sourceComponent = tagsDialogComponent
dialog.show(urls)
}
onShowFolderClicked: pix.showInFolder(urls)
}
Maui.ShareDialog
Component
{
id: shareDialog
id: shareDialogComponent
Maui.ShareDialog
{
id: shareDialog
}
}
AlbumsDialog
Component
{
id: albumsDialog
id: albumsDialogComponent
AlbumsDialog
{
id: albumsDialog
}
}
TagsDialog
Component
{
id: tagsDialog
forAlbum: false
onTagsAdded: addTagsToPic(urls, tags)
id: tagsDialogComponent
TagsDialog
{
id: tagsDialog
forAlbum: false
onTagsAdded: addTagsToPic(urls, tags)
}
}
Maui.FileDialog
Component
{
id: fmDialog
onlyDirs: true
id: fmDialogComponent
Maui.FileDialog
{
id: fmDialog
onlyDirs: true
}