Commit cce30611 authored by Camilo higuita's avatar Camilo higuita

models now set

parent e0733c99
......@@ -28,10 +28,16 @@ set(CMAKE_AUTORCC ON)
set(union_SRCS
src/main.cpp
src/models/baselist.cpp
src/models/basemodel.cpp
src/models/contacts/contactsmodel.cpp
)
set(union_HDRS
src/union.h
src/models/baselist.h
src/models/basemodel.h
src/models/contacts/contactsmodel.h
)
set(union_ASSETS
......
#include "baselist.h"
BaseList::BaseList(QObject *parent) : QObject(parent)
{
}
#ifndef BASELIST_H
#define BASELIST_H
#ifdef STATIC_MAUIKIT
#include "fm.h"
#include "fmh.h"
#else
#include <MauiKit/fm.h>
#include <MauiKit/fmh.h>
#endif
class BaseList : public QObject
{
Q_OBJECT
public:
explicit BaseList(QObject *parent = nullptr);
//* To be overrided *//
virtual FMH::MODEL_LIST items() const {return FMH::MODEL_LIST({{}});}
protected:
signals:
void preItemAppended();
void postItemAppended();
void preItemAppendedAt(int index);
void preItemRemoved(int index);
void postItemRemoved();
void updateModel(int index, QVector<int> roles);
void preListChanged();
void postListChanged();
public slots:
virtual QVariantMap get(const int &index) const
{
Q_UNUSED(index);
return QVariantMap();
}
virtual bool update(const int &index, const QVariant &value, const int &role)
{
Q_UNUSED(index);
Q_UNUSED(value);
Q_UNUSED(role);
return false;
}
virtual bool update(const QVariantMap &data, const int &index)
{
Q_UNUSED(index);
Q_UNUSED(data);
return false;
}
virtual bool update(const FMH::MODEL &data)
{
Q_UNUSED(data);
return false;
}
virtual bool insert(const QVariantMap &map)
{
Q_UNUSED(map);
return false;
}
virtual bool remove(const int &index)
{
Q_UNUSED(index);
return false;
}
};
#endif // BASELIST_H
#include "basemodel.h"
#include "baselist.h"
BaseModel::BaseModel(QObject *parent)
: QAbstractListModel(parent),
mList(nullptr)
{}
int BaseModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() || !mList)
return 0;
return mList->items().size();
}
QVariant BaseModel::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 BaseModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!mList)
return false;
if (mList->update(index.row(), value, role))
{
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags BaseModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable; // FIXME: Implement me!
}
QHash<int, QByteArray> BaseModel::roleNames() const
{
QHash<int, QByteArray> names;
for(auto key : FMH::MODEL_NAME.keys())
names[key] = QString(FMH::MODEL_NAME[key]).toUtf8();
return names;
}
BaseList *BaseModel::getList() const
{
return this->mList;
}
void BaseModel::setList(BaseList *value)
{
beginResetModel();
if(mList)
mList->disconnect(this);
mList = value;
if(mList)
{
connect(this->mList, &BaseList::preItemAppended, this, [=]()
{
const int index = mList->items().size();
beginInsertRows(QModelIndex(), index, index);
});
connect(this->mList, &BaseList::postItemAppended, this, [=]()
{
endInsertRows();
});
connect(this->mList, &BaseList::preItemAppendedAt, this, [=](int index)
{
beginInsertRows(QModelIndex(), index, index);
});
connect(this->mList, &BaseList::preItemRemoved, this, [=](int index)
{
beginRemoveRows(QModelIndex(), index, index);
});
connect(this->mList, &BaseList::postItemRemoved, this, [=]()
{
endRemoveRows();
});
connect(this->mList, &BaseList::updateModel, this, [=](int index, QVector<int> roles)
{
emit this->dataChanged(this->index(index), this->index(index), roles);
});
connect(this->mList, &BaseList::preListChanged, this, [=]()
{
beginResetModel();
});
connect(this->mList, &BaseList::postListChanged, this, [=]()
{
endResetModel();
});
}
endResetModel();
}
QVariantMap BaseModel::get(const int &index) const
{
return this->mList->get(index);
}
#ifndef BASEMODEL_H
#define BASEMODEL_H
#include <QAbstractListModel>
#include <QList>
class BaseList;
class BaseModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(BaseList *list READ getList WRITE setList)
public:
explicit BaseModel(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;
BaseList* getList() const;
void setList(BaseList *value);
private:
BaseList *mList;
signals:
void listChanged();
public slots:
QVariantMap get(const int &index) const;
};
#endif // NOTESMODEL_H
#include "contactsmodel.h"
ContactsModel::ContactsModel(QObject *parent) : BaseList(parent)
{
connect(this, &ContactsModel::queryChanged, this, &ContactsModel::setList);
}
FMH::MODEL_LIST ContactsModel::items() const
{
return this->list;
}
void ContactsModel::setQuery(const QString &query)
{
if(this->query == query)
return;
this->query = query;
qDebug()<< "setting query"<< this->query;
emit this->queryChanged();
}
QString ContactsModel::getQuery() const
{
return this->query;
}
void ContactsModel::setSortBy(const SORTBY &sort)
{
if(this->sort == sort)
return;
this->sort = sort;
this->preListChanged();
this->sortList();
this->postListChanged();
emit this->sortByChanged();
}
ContactsModel::SORTBY ContactsModel::getSortBy() const
{
return this->sort;
}
void ContactsModel::sortList()
{
if(this->sort == ContactsModel::SORTBY::NONE)
return;
const auto key = static_cast<FMH::MODEL_KEY>(this->sort);
qDebug()<< "SORTING LIST BY"<< 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::RELEASEDATE:
case FMH::MODEL_KEY::RATE:
case FMH::MODEL_KEY::FAV:
case FMH::MODEL_KEY::COUNT:
{
if(e1[role].toInt() > e2[role].toInt())
return true;
break;
}
case FMH::MODEL_KEY::TRACK:
{
if(e1[role].toInt() < e2[role].toInt())
return true;
break;
}
case FMH::MODEL_KEY::ADDDATE:
{
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::TITLE:
case FMH::MODEL_KEY::ARTIST:
case FMH::MODEL_KEY::ALBUM:
case FMH::MODEL_KEY::FORMAT:
{
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 ContactsModel::setList()
{
emit this->preListChanged();
qDebug()<< "my LIST" ;
this->sortList();
emit this->postListChanged();
}
QVariantMap ContactsModel::get(const int &index) const
{
if(index >= this->list.size() || index < 0)
return QVariantMap();
QVariantMap res;
const auto item = this->list.at(index);
for(auto key : item.keys())
res.insert(FMH::MODEL_NAME[key], item[key]);
return res;
}
void ContactsModel::append(const QVariantMap &item)
{
if(item.isEmpty())
return;
emit this->preItemAppended();
FMH::MODEL model;
for(auto key : item.keys())
model.insert(FMH::MODEL_NAME_KEY[key], item[key].toString());
qDebug() << "Appending item to list" << item;
this->list << model;
qDebug()<< this->list;
emit this->postItemAppended();
}
void ContactsModel::append(const QVariantMap &item, const int &at)
{
if(item.isEmpty())
return;
if(at > this->list.size() || at < 0)
return;
qDebug()<< "trying to append at" << at << item["title"];
emit this->preItemAppendedAt(at);
FMH::MODEL model;
for(auto key : item.keys())
model.insert(FMH::MODEL_NAME_KEY[key], item[key].toString());
this->list.insert(at, model);
emit this->postItemAppended();
}
void ContactsModel::appendQuery(const QString &query)
{
if(query.isEmpty() || query == this->query)
return;
this->query = query;
emit this->preListChanged();
emit this->postListChanged();
}
void ContactsModel::clear()
{
emit this->preListChanged();
this->list.clear();
emit this->postListChanged();
}
#ifndef CONTACTSMODEL_H
#define CONTACTSMODEL_H
#include <QObject>
#include "../baselist.h"
class ContactsModel : public BaseList
{
Q_OBJECT
Q_PROPERTY(QString query READ getQuery WRITE setQuery NOTIFY queryChanged())
Q_PROPERTY(ContactsModel::SORTBY sortBy READ getSortBy WRITE setSortBy NOTIFY sortByChanged)
public:
enum SORTBY : uint_fast8_t
{
ADDDATE = FMH::MODEL_KEY::ADDDATE,
RELEASEDATE = FMH::MODEL_KEY::RELEASEDATE,
FORMAT = FMH::MODEL_KEY::FORMAT,
ARTIST = FMH::MODEL_KEY::ARTIST,
TITLE = FMH::MODEL_KEY::TITLE,
ALBUM = FMH::MODEL_KEY::ALBUM,
RATE = FMH::MODEL_KEY::RATE,
FAV = FMH::MODEL_KEY::FAV,
TRACK = FMH::MODEL_KEY::TRACK,
COUNT = FMH::MODEL_KEY::COUNT,
NONE
}; Q_ENUM(SORTBY)
explicit ContactsModel(QObject *parent = nullptr);
FMH::MODEL_LIST items() const override;
void setQuery(const QString &query);
QString getQuery() const;
void setSortBy(const ContactsModel::SORTBY &sort);
ContactsModel::SORTBY getSortBy() const;
private:
FMH::MODEL_LIST list;
void sortList();
void setList();
QString query;
ContactsModel::SORTBY sort = ContactsModel::SORTBY::ADDDATE;
signals:
void queryChanged();
void sortByChanged();
public slots:
QVariantMap get(const int &index) const override;
void append(const QVariantMap &item);
void append(const QVariantMap &item, const int &at);
void appendQuery(const QString &query);
void clear();
};
#endif // CONTACTSMODEL_H
......@@ -16,6 +16,14 @@ Maui.Dialog
property var contact : ({})
acceptButton.text: qsTr("Edit")
rejectButton.visible: false
onAccepted: _editContactDialog.open()
EditContactDialog
{
id: _editContactDialog
contact: control.contact
}
ColumnLayout
{
......
......@@ -70,7 +70,11 @@ Maui.Page
Connections
{
target: _delegate
onClicked: _contactDialog.show(_listModel.get(index))
onClicked:
{
_listView.currentIndex = index
_contactDialog.show(_listModel.get(index))
}
}
}
}
......
......@@ -34,6 +34,9 @@ linux:unix:!android {
SOURCES += \
$$PWD/src/main.cpp \
src/models/baselist.cpp \
src/models/basemodel.cpp \
src/models/contacts/contactsmodel.cpp
RESOURCES += \
$$PWD/src/qml.qrc \
......@@ -43,3 +46,9 @@ qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
src/union.h \
src/models/baselist.h \
src/models/basemodel.h \
src/models/contacts/contactsmodel.h
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment