Commit 93f62e28 authored by Camilo higuita's avatar Camilo higuita

updated models fo notes

parent b6ec8a73
...@@ -45,7 +45,9 @@ SOURCES += \ ...@@ -45,7 +45,9 @@ SOURCES += \
src/buho.cpp \ src/buho.cpp \
src/documenthandler.cpp \ src/documenthandler.cpp \
src/linker.cpp \ src/linker.cpp \
src/utils/htmlparser.cpp src/utils/htmlparser.cpp \
src/models/notesmodel.cpp \
src/models/notes.cpp
RESOURCES += \ RESOURCES += \
qml.qrc \ qml.qrc \
...@@ -74,7 +76,9 @@ HEADERS += \ ...@@ -74,7 +76,9 @@ HEADERS += \
src/utils/owl.h \ src/utils/owl.h \
src/documenthandler.h \ src/documenthandler.h \
src/linker.h \ src/linker.h \
src/utils/htmlparser.h src/utils/htmlparser.h \
src/models/notesmodel.h \
src/models/notes.h
INCLUDEPATH += \ INCLUDEPATH += \
src/utils/ \ src/utils/ \
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
#include "src/documenthandler.h" #include "src/documenthandler.h"
#include "src/linker.h" #include "src/linker.h"
#include "models/notesmodel.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
...@@ -33,7 +35,7 @@ int main(int argc, char *argv[]) ...@@ -33,7 +35,7 @@ int main(int argc, char *argv[])
QtWebView::initialize(); QtWebView::initialize();
#else #else
QApplication app(argc, argv); QApplication app(argc, argv);
// QtWebEngine::initialize(); // QtWebEngine::initialize();
#endif #endif
app.setApplicationName(OWL::App); app.setApplicationName(OWL::App);
...@@ -61,7 +63,7 @@ int main(int argc, char *argv[]) ...@@ -61,7 +63,7 @@ int main(int argc, char *argv[])
context->setContextProperty("linker", &linker); context->setContextProperty("linker", &linker);
context->setContextProperty("tag", tag); context->setContextProperty("tag", tag);
qmlRegisterType<DocumentHandler>("org.buho.editor", 1, 0, "DocumentHandler"); qmlRegisterType<DocumentHandler>("org.buho.editor", 1, 0, "DocumentHandler");
qmlRegisterType<NotesModel>("Notes", 1, 0, "NotesModel");
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty()) if (engine.rootObjects().isEmpty())
return -1; return -1;
......
...@@ -132,8 +132,7 @@ Maui.ApplicationWindow ...@@ -132,8 +132,7 @@ Maui.ApplicationWindow
id: editNote id: editNote
onNoteSaved: onNoteSaved:
{ {
if(owl.updateNote(note)) notesView.cardsView.currentItem.update(note)
notesView.cardsView.currentItem.update(note)
} }
} }
...@@ -206,8 +205,8 @@ Maui.ApplicationWindow ...@@ -206,8 +205,8 @@ Maui.ApplicationWindow
function setNote(note) function setNote(note)
{ {
var tags = owl.getNoteTags(note.id) // var tags = owl.getNoteTags(note.id)
note.tags = tags // note.tags = tags
notesView.currentNote = note notesView.currentNote = note
editNote.fill(note) editNote.fill(note)
} }
......
...@@ -132,6 +132,29 @@ bool DB::checkExistance(const QString &tableName, const QString &searchId, const ...@@ -132,6 +132,29 @@ bool DB::checkExistance(const QString &tableName, const QString &searchId, const
return false; return false;
} }
OWL::DB_LIST DB::getDBData(const QString &queryTxt)
{
OWL::DB_LIST mapList;
auto query = this->getQuery(queryTxt);
if(query.exec())
{
while(query.next())
{
OWL::DB data;
for(auto key : OWL::KEYMAP.keys())
if(query.record().indexOf(OWL::KEYMAP[key])>-1)
data.insert(key, query.value(OWL::KEYMAP[key]).toString());
mapList<< data;
}
}else qDebug()<< query.lastError()<< query.lastQuery();
return mapList;
}
QSqlQuery DB::getQuery(const QString &queryTxt) QSqlQuery DB::getQuery(const QString &queryTxt)
{ {
QSqlQuery query(queryTxt, this->m_db); QSqlQuery query(queryTxt, this->m_db);
......
...@@ -55,7 +55,7 @@ public: ...@@ -55,7 +55,7 @@ public:
/* utils*/ /* utils*/
Q_INVOKABLE bool checkExistance(const QString &tableName, const QString &searchId, const QString &search); Q_INVOKABLE bool checkExistance(const QString &tableName, const QString &searchId, const QString &search);
OWL::DB_LIST getDBData(const QString &queryTxt);
protected: protected:
QSqlQuery getQuery(const QString &queryTxt); QSqlQuery getQuery(const QString &queryTxt);
......
...@@ -25,6 +25,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. ...@@ -25,6 +25,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QDateTime> #include <QDateTime>
#include "linker.h" #include "linker.h"
#ifdef STATIC_MAUIKIT
#include "tagging.h"
#else
#include <MauiKit/tagging.h>
#endif
DBActions::DBActions(QObject *parent) : DB(parent) DBActions::DBActions(QObject *parent) : DB(parent)
{ {
qDebug() << "Getting collectionDB info from: " << OWL::CollectionDBPath; qDebug() << "Getting collectionDB info from: " << OWL::CollectionDBPath;
...@@ -36,29 +42,6 @@ DBActions::DBActions(QObject *parent) : DB(parent) ...@@ -36,29 +42,6 @@ DBActions::DBActions(QObject *parent) : DB(parent)
DBActions::~DBActions() {} DBActions::~DBActions() {}
OWL::DB_LIST DBActions::getDBData(const QString &queryTxt)
{
OWL::DB_LIST mapList;
auto query = this->getQuery(queryTxt);
if(query.exec())
{
while(query.next())
{
OWL::DB data;
for(auto key : OWL::KEYMAP.keys())
if(query.record().indexOf(OWL::KEYMAP[key])>-1)
data.insert(key, query.value(OWL::KEYMAP[key]).toString());
mapList<< data;
}
}else qDebug()<< query.lastError()<< query.lastQuery();
return mapList;
}
QVariantList DBActions::get(const QString &queryTxt) QVariantList DBActions::get(const QString &queryTxt)
{ {
QVariantList mapList; QVariantList mapList;
...@@ -82,85 +65,7 @@ QVariantList DBActions::get(const QString &queryTxt) ...@@ -82,85 +65,7 @@ QVariantList DBActions::get(const QString &queryTxt)
return mapList; return mapList;
} }
bool DBActions::insertNote(const QVariantMap &note)
{
qDebug()<<"TAGS"<< note[OWL::KEYMAP[OWL::KEY::TAG]].toStringList();
auto title = note[OWL::KEYMAP[OWL::KEY::TITLE]].toString();
auto body = note[OWL::KEYMAP[OWL::KEY::BODY]].toString();
auto color = note[OWL::KEYMAP[OWL::KEY::COLOR]].toString();
auto pin = note[OWL::KEYMAP[OWL::KEY::PIN]].toInt();
auto fav = note[OWL::KEYMAP[OWL::KEY::FAV]].toInt();
auto tags = note[OWL::KEYMAP[OWL::KEY::TAG]].toStringList();
auto id = QUuid::createUuid().toString();
QVariantMap note_map =
{
{OWL::KEYMAP[OWL::KEY::ID], id},
{OWL::KEYMAP[OWL::KEY::TITLE], title},
{OWL::KEYMAP[OWL::KEY::BODY], body},
{OWL::KEYMAP[OWL::KEY::COLOR], color},
{OWL::KEYMAP[OWL::KEY::PIN], pin},
{OWL::KEYMAP[OWL::KEY::FAV], fav},
{OWL::KEYMAP[OWL::KEY::UPDATED], QDateTime::currentDateTime().toString()},
{OWL::KEYMAP[OWL::KEY::ADD_DATE], QDateTime::currentDateTime().toString()}
};
if(this->insert(OWL::TABLEMAP[OWL::TABLE::NOTES], note_map))
{
for(auto tg : tags)
this->tag->tagAbstract(tg, OWL::TABLEMAP[OWL::TABLE::NOTES], id, color);
this->noteInserted(note_map);
return true;
}
return false;
}
bool DBActions::updateNote(const QVariantMap &note)
{
auto id = note[OWL::KEYMAP[OWL::KEY::ID]].toString();
auto title = note[OWL::KEYMAP[OWL::KEY::TITLE]].toString();
auto body = note[OWL::KEYMAP[OWL::KEY::BODY]].toString();
auto color = note[OWL::KEYMAP[OWL::KEY::COLOR]].toString();
auto pin = note[OWL::KEYMAP[OWL::KEY::PIN]].toInt();
auto fav = note[OWL::KEYMAP[OWL::KEY::FAV]].toInt();
auto tags = note[OWL::KEYMAP[OWL::KEY::TAG]].toStringList();
auto updated = note[OWL::KEYMAP[OWL::KEY::UPDATED]].toString();
QVariantMap note_map =
{
{OWL::KEYMAP[OWL::KEY::TITLE], title},
{OWL::KEYMAP[OWL::KEY::BODY], body},
{OWL::KEYMAP[OWL::KEY::COLOR], color},
{OWL::KEYMAP[OWL::KEY::PIN], pin},
{OWL::KEYMAP[OWL::KEY::FAV], fav},
{OWL::KEYMAP[OWL::KEY::UPDATED], updated}
};
for(auto tg : tags)
this->tag->tagAbstract(tg, OWL::TABLEMAP[OWL::TABLE::NOTES], id, color);
return this->update(OWL::TABLEMAP[OWL::TABLE::NOTES], note_map, {{OWL::KEYMAP[OWL::KEY::ID], id}} );
}
bool DBActions::removeNote(const QVariantMap &note)
{
qDebug()<<note;
return this->remove(OWL::TABLEMAP[OWL::TABLE::NOTES], note);
}
QVariantList DBActions::getNotes()
{
return this->get("select * from notes ORDER BY updated ASC");
}
QVariantList DBActions::getNoteTags(const QString &id)
{
return this->tag->getAbstractTags(OWL::TABLEMAP[OWL::TABLE::NOTES], id);
}
bool DBActions::insertLink(const QVariantMap &link) bool DBActions::insertLink(const QVariantMap &link)
{ {
......
...@@ -24,12 +24,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. ...@@ -24,12 +24,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QObject> #include <QObject>
#include "db.h" #include "db.h"
#ifdef STATIC_MAUIKIT class Tagging;
#include "tagging.h"
#else
#include <MauiKit/tagging.h>
#endif
class DBActions : public DB class DBActions : public DB
{ {
Q_OBJECT Q_OBJECT
...@@ -40,13 +35,6 @@ public: ...@@ -40,13 +35,6 @@ public:
Q_INVOKABLE QVariantList get(const QString &queryTxt); Q_INVOKABLE QVariantList get(const QString &queryTxt);
/*main actions*/ /*main actions*/
Q_INVOKABLE bool insertNote(const QVariantMap &note);
Q_INVOKABLE bool updateNote(const QVariantMap &note);
Q_INVOKABLE bool removeNote(const QVariantMap &note);
Q_INVOKABLE QVariantList getNotes();
Q_INVOKABLE QVariantList getNoteTags(const QString &id);
Q_INVOKABLE bool insertLink(const QVariantMap &link); Q_INVOKABLE bool insertLink(const QVariantMap &link);
Q_INVOKABLE bool updateLink(const QVariantMap &link); Q_INVOKABLE bool updateLink(const QVariantMap &link);
Q_INVOKABLE bool removeLink(const QVariantMap &link); Q_INVOKABLE bool removeLink(const QVariantMap &link);
...@@ -61,7 +49,6 @@ protected: ...@@ -61,7 +49,6 @@ protected:
void removeAbtractTags(const QString &key, const QString &lot); void removeAbtractTags(const QString &key, const QString &lot);
signals: signals:
void noteInserted(QVariantMap note);
void linkInserted(QVariantMap link); void linkInserted(QVariantMap link);
}; };
......
#include "notes.h"
#include <QUuid>
#ifdef STATIC_MAUIKIT
#include "tagging.h"
#else
#include <MauiKit/tagging.h>
#endif
Notes::Notes(QObject *parent) : DB(parent)
{
this->notes = this->getNotes();
}
OWL::DB_LIST Notes::items() const
{
return this->notes;
}
void Notes::appendItem()
{
emit preItemAppended();
emit postItemAppended();
}
void Notes::removeItem()
{
emit preItemRemoved(0);
emit postItemRemoved();
}
bool Notes::insertNote(const QVariantMap &note)
{
qDebug()<<"TAGS"<< note[OWL::KEYMAP[OWL::KEY::TAG]].toStringList();
auto title = note[OWL::KEYMAP[OWL::KEY::TITLE]].toString();
auto body = note[OWL::KEYMAP[OWL::KEY::BODY]].toString();
auto color = note[OWL::KEYMAP[OWL::KEY::COLOR]].toString();
auto pin = note[OWL::KEYMAP[OWL::KEY::PIN]].toInt();
auto fav = note[OWL::KEYMAP[OWL::KEY::FAV]].toInt();
auto tags = note[OWL::KEYMAP[OWL::KEY::TAG]].toStringList();
auto id = QUuid::createUuid().toString();
QVariantMap note_map =
{
{OWL::KEYMAP[OWL::KEY::ID], id},
{OWL::KEYMAP[OWL::KEY::TITLE], title},
{OWL::KEYMAP[OWL::KEY::BODY], body},
{OWL::KEYMAP[OWL::KEY::COLOR], color},
{OWL::KEYMAP[OWL::KEY::PIN], pin},
{OWL::KEYMAP[OWL::KEY::FAV], fav},
{OWL::KEYMAP[OWL::KEY::UPDATED], QDateTime::currentDateTime().toString()},
{OWL::KEYMAP[OWL::KEY::ADD_DATE], QDateTime::currentDateTime().toString()}
};
if(this->insert(OWL::TABLEMAP[OWL::TABLE::NOTES], note_map))
{
for(auto tg : tags)
this->tag->tagAbstract(tg, OWL::TABLEMAP[OWL::TABLE::NOTES], id, color);
this->noteInserted(note_map);
return true;
}
return false;
}
bool Notes::updateNote(const int &index, const QVariant &value, const int &role)
{
if(index < 0 || index >= notes.size())
return false;
const auto oldNote = this->notes[index][static_cast<OWL::KEY>(role)];
if(oldNote == value.toString())
return false;
this->notes[index].insert(static_cast<OWL::KEY>(role), value.toString());
const auto newNote = this->notes[index];
this->updateNote(newNote);
return true;
}
bool Notes::updateNote(const OWL::DB &note)
{
auto id = note[OWL::KEY::ID];
auto title = note[OWL::KEY::TITLE];
auto body = note[OWL::KEY::BODY];
auto color = note[OWL::KEY::COLOR];
auto pin = note[OWL::KEY::PIN].toInt();
auto fav = note[OWL::KEY::FAV].toInt();
auto tags = note[OWL::KEY::TAG].split(",", QString::SkipEmptyParts);
auto updated =note[OWL::KEY::UPDATED];
QVariantMap note_map =
{
{OWL::KEYMAP[OWL::KEY::TITLE], title},
{OWL::KEYMAP[OWL::KEY::BODY], body},
{OWL::KEYMAP[OWL::KEY::COLOR], color},
{OWL::KEYMAP[OWL::KEY::PIN], pin},
{OWL::KEYMAP[OWL::KEY::FAV], fav},
{OWL::KEYMAP[OWL::KEY::UPDATED], updated}
};
for(auto tg : tags)
this->tag->tagAbstract(tg, OWL::TABLEMAP[OWL::TABLE::NOTES], id, color);
return this->update(OWL::TABLEMAP[OWL::TABLE::NOTES], note_map, {{OWL::KEYMAP[OWL::KEY::ID], id}} );
}
bool Notes::removeNote(const QVariantMap &note)
{
qDebug()<<note;
return this->remove(OWL::TABLEMAP[OWL::TABLE::NOTES], note);
}
OWL::DB_LIST Notes::getNotes()
{
return this->getDBData("select * from notes ORDER BY updated ASC");
}
QVariantList Notes::getNoteTags(const QString &id)
{
return this->tag->getAbstractTags(OWL::TABLEMAP[OWL::TABLE::NOTES], id);
}
#ifndef NOTES_H
#define NOTES_H
#include <QObject>
#include "db/db.h"
class Tagging;
class Notes : public DB
{
Q_OBJECT
public:
explicit Notes(QObject *parent = nullptr);
OWL::DB_LIST items() const;
bool insertNote(const QVariantMap &note);
bool updateNote(const int &index, const QVariant &value, const int &role);
bool updateNote(const OWL::DB &note);
bool removeNote(const QVariantMap &note);
QVariantList getNoteTags(const QString &id);
private:
Tagging *tag;
OWL::DB_LIST notes;
OWL::DB_LIST getNotes();
signals:
void noteInserted(QVariantMap note);
void preItemAppended();
void postItemAppended();
void preItemRemoved(int index);
void postItemRemoved();
public slots:
void appendItem();
void removeItem();
};
#endif // NOTES_H
#include "notesmodel.h"
#include "owl.h"
#include "notes.h"
NotesModel::NotesModel(QObject *parent)
: QAbstractListModel(parent)
{
this->mNotes = new Notes(this);
}
int NotesModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() || !mNotes)
return 0;
return mNotes->items().size();
}
QVariant NotesModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !mNotes)
return QVariant();
return mNotes->items().at(index.row())[static_cast<OWL::KEY>(role)];
}
QVariantMap NotesModel::get(const int index)
{
QVariantMap res;
const auto note = mNotes->items().at(index);
for(auto key : note.keys())
res.insert(OWL::KEYMAP[key], note[key]);
return res;
}
bool NotesModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!mNotes)
return false;
if (mNotes->updateNote(index.row(), value, role))
{
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags NotesModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable; // FIXME: Implement me!
}
QHash<int, QByteArray> NotesModel::roleNames() const
{
QHash<int, QByteArray> names;
names[OWL::KEY::TITLE] = QString(OWL::KEYMAP[OWL::KEY::TITLE]).toUtf8();
names[OWL::KEY::BODY] = QString(OWL::KEYMAP[OWL::KEY::BODY]).toUtf8();
names[OWL::KEY::UPDATED] = QString(OWL::KEYMAP[OWL::KEY::UPDATED]).toUtf8();
names[OWL::KEY::FAV] = QString(OWL::KEYMAP[OWL::KEY::FAV]).toUtf8();
names[OWL::KEY::PIN] = QString(OWL::KEYMAP[OWL::KEY::PIN]).toUtf8();
names[OWL::KEY::COLOR] = QString(OWL::KEYMAP[OWL::KEY::COLOR]).toUtf8();
names[OWL::KEY::ID] = QString(OWL::KEYMAP[OWL::KEY::ID]).toUtf8();
names[OWL::KEY::IMAGE] = QString(OWL::KEYMAP[OWL::KEY::IMAGE]).toUtf8();
names[OWL::KEY::LINK] = QString(OWL::KEYMAP[OWL::KEY::LINK]).toUtf8();
names[OWL::KEY::PREVIEW] = QString(OWL::KEYMAP[OWL::KEY::PREVIEW]).toUtf8();
names[OWL::KEY::TAG] = QString(OWL::KEYMAP[OWL::KEY::TAG]).toUtf8();
names[OWL::KEY::URL] = QString(OWL::KEYMAP[OWL::KEY::URL]).toUtf8();
names[OWL::KEY::ADD_DATE] = QString(OWL::KEYMAP[OWL::KEY::ADD_DATE]).toUtf8();
names[OWL::KEY::URL] = QString(OWL::KEYMAP[OWL::KEY::ADD_DATE]).toUtf8();
return names;
}
Notes *NotesModel::notes() const
{
return mNotes;
}
void NotesModel::setNotes(Notes *value)
{
beginResetModel();
if(mNotes)
mNotes->disconnect(this);
mNotes = value;
if(mNotes)
{
connect(mNotes, &Notes::preItemAppended, this, [=]()
{
const int index = mNotes->items().size();
beginInsertRows(QModelIndex(), index, index);
});
connect(mNotes, &Notes::postItemAppended, this, [=]()
{
endInsertRows();
});
connect(mNotes, &Notes::preItemRemoved, this, [=](int index)
{
beginInsertRows(QModelIndex(), index, index);
});
connect(mNotes, &Notes::preItemRemoved, this, [=]()
{
endRemoveRows();
});
}
endResetModel();
}
#ifndef NOTESMODEL_H
#define NOTESMODEL_H
#include <QAbstractListModel>
#include <QList>
class Notes;
class NotesModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(Notes *notes READ notes WRITE setNotes)
public:
explicit NotesModel(QObject *parent = nullptr);
enum
{
title,
body
};
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
Q_INVOKABLE QVariantMap get(const int index);
// 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;
Notes *notes() const;
void setNotes(Notes *value);
private:
Notes *mNotes;
};
#endif // NOTESMODEL_H
...@@ -80,7 +80,7 @@ namespace OWL ...@@ -80,7 +80,7 @@ namespace OWL
{TABLE::LINKS_TAGS,"links_tags"} {TABLE::LINKS_TAGS,"links_tags"}
}; };
enum class KEY :uint8_t enum KEY :uint8_t
{ {
URL, URL,
UPDATED, UPDATED,
......
...@@ -4,7 +4,7 @@ import "../../widgets" ...@@ -4,7 +4,7 @@ import "../../widgets"
import org.kde.mauikit 1.0 as Maui import org.kde.mauikit 1.0 as Maui
import org.kde.kirigami 2.2 as Kirigami import org.kde.kirigami 2.2 as Kirigami
import "../../utils/owl.js" as O import "../../utils/owl.js" as O
import Notes 1.0
Maui.Page Maui.Page
{ {
...@@ -89,13 +89,18 @@ Maui.Page ...@@ -89,13 +89,18 @@ Maui.Page
Layout.fillWidth: true Layout.fillWidth: true
width: parent.width width: parent.width
Layout.margins: space.big Layout.margins: space.big
onItemClicked: noteClicked(cardsView.model.get(index)) onItemClicked: noteClicked(notesModel.get(index))
holder.emoji: "qrc:/Type.png" holder.emoji: "qrc:/Type.png"
holder.emojiSize: iconSizes.huge holder.emojiSize: iconSizes.huge
holder.isMask: false holder.isMask: false
holder.title : "No notes!" holder.title : "No notes!"
holder.body: "Click here to create a new note" holder.body: "Click here to create a new note"
model: NotesModel
{
id: notesModel
}
Connections Connections
{ {
target: cardsView.holder target: cardsView.holder
...@@ -113,9 +118,9 @@ Maui.Page ...@@ -113,9 +118,9 @@ Maui.Page
function populate() function populate()
{