Skip to content

Commit

Permalink
Implemented StickerPackModel
Browse files Browse the repository at this point in the history
  • Loading branch information
Dax89 committed Nov 29, 2016
1 parent a3eea73 commit bff6d74
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
6 changes: 4 additions & 2 deletions LibQTelegram.pro
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ SOURCES += \
cache/database/tables/stickersetstable.cpp \
models/stickersetsmodel.cpp \
cache/database/tables/stickersetsdatatable.cpp \
quick/qquickstickerview.cpp
quick/qquickstickerview.cpp \
models/stickerpackmodel.cpp

HEADERS +=\
libqtelegram_global.h \
Expand Down Expand Up @@ -523,4 +524,5 @@ HEADERS +=\
cache/database/tables/stickersetstable.h \
models/stickersetsmodel.h \
cache/database/tables/stickersetsdatatable.h \
quick/qquickstickerview.h
quick/qquickstickerview.h \
models/stickerpackmodel.h
13 changes: 13 additions & 0 deletions cache/sticker/stickercache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ void StickerCache::populateInstalled(QList<StickerSet *> &stickerssets)
}
}

void StickerCache::populatePack(StickerSet *stickerset, QList<Document *> &stickers)
{
MessagesStickerSet* messagesstickerset = this->stickerSetData(stickerset);

if(!messagesstickerset)
{
stickers.clear();
return;
}

stickers = messagesstickerset->documents();
}

MessagesStickerSet *StickerCache::stickerSetData(StickerSet *stickerset)
{
if(!this->_stickersetsdata.contains(stickerset->id()))
Expand Down
2 changes: 2 additions & 0 deletions cache/sticker/stickercache.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define StickerCache_instance StickerCache::instance()
#define StickerCache_stickerPreview(stickerset) StickerCache::instance()->stickerPreview(stickerset)
#define StickerCache_populateInstalled(stickersets) StickerCache::instance()->populateInstalled(stickersets)
#define StickerCache_populatePack(stickerset, stickers) StickerCache::instance()->populatePack(stickerset, stickers);
#define StickerCache_populate(allstickers) StickerCache::instance()->populate(allstickers);

#include <QObject>
Expand All @@ -24,6 +25,7 @@ class StickerCache : public QObject
Document* stickerPreview(StickerSet* stickerset);
void populate(MessagesAllStickers* messageallstickers);
void populateInstalled(QList<StickerSet*>& stickerssets);
void populatePack(StickerSet* stickerset, QList<Document*>& stickers);

private:
MessagesStickerSet* stickerSetData(StickerSet* stickerset);
Expand Down
63 changes: 63 additions & 0 deletions models/stickerpackmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "stickerpackmodel.h"
#include "../cache/sticker/stickercache.h"

StickerPackModel::StickerPackModel(QObject *parent) : TelegramModel(parent), _stickerset(NULL)
{
connect(this, &StickerPackModel::stickerSetChanged, this, &StickerPackModel::titleChanged);
}

StickerSet *StickerPackModel::stickerSet() const
{
return this->_stickerset;
}

void StickerPackModel::setStickerSet(StickerSet *stickerset)
{
if(this->_stickerset == stickerset)
return;

this->_stickerset = stickerset;
this->telegramReady();
emit stickerSetChanged();
}

QString StickerPackModel::title() const
{
if(!this->_stickerset)
return QString();

return this->_stickerset->title().toString();
}

QVariant StickerPackModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid() || (index.row() >= this->_stickers.count()))
return QVariant();

Document* sticker = this->_stickers[index.row()];

if(role == StickerPackModel::ItemRole)
return QVariant::fromValue(sticker);

return QVariant();
}

int StickerPackModel::rowCount(const QModelIndex &) const
{
return this->_stickers.length();
}

QHash<int, QByteArray> StickerPackModel::roleNames() const
{
return this->initRoles();
}

void StickerPackModel::telegramReady()
{
if(!this->_telegram || !this->_stickerset)
return;

this->beginResetModel();
StickerCache_populatePack(this->_stickerset, this->_stickers);
this->endResetModel();
}
36 changes: 36 additions & 0 deletions models/stickerpackmodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef STICKERPACKMODEL_H
#define STICKERPACKMODEL_H

#include "abstract/telegrammodel.h"

class StickerPackModel : public TelegramModel
{
Q_OBJECT

Q_PROPERTY(StickerSet* stickerSet READ stickerSet WRITE setStickerSet NOTIFY stickerSetChanged)
Q_PROPERTY(QString title READ title NOTIFY titleChanged)

public:
explicit StickerPackModel(QObject *parent = 0);
StickerSet* stickerSet() const;
void setStickerSet(StickerSet* stickerset);
QString title() const;

public:
virtual QVariant data(const QModelIndex &index, int role) const;
virtual int rowCount(const QModelIndex& = QModelIndex()) const;
virtual QHash<int, QByteArray> roleNames() const;

protected:
virtual void telegramReady();

signals:
void stickerSetChanged();
void titleChanged();

private:
StickerSet* _stickerset;
QList<Document*> _stickers;
};

#endif // STICKERPACKMODEL_H
2 changes: 2 additions & 0 deletions objects/telegramqmlbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "models/messagesmodel.h"
#include "models/contactsmodel.h"
#include "models/forwarddialogsmodel.h"
#include "models/stickerpackmodel.h"
#include "models/stickersetsmodel.h"
#include "notifications/telegramnotifications.h"
#include "notifications/notificationobject.h"
Expand Down Expand Up @@ -45,6 +46,7 @@ void TelegramQmlBase::initialize(const QString &uri)
REGISTER_OBJECT(DialogsModel);
REGISTER_OBJECT(MessagesModel);
REGISTER_OBJECT(ForwardDialogsModel);
REGISTER_OBJECT(StickerPackModel);
REGISTER_OBJECT(StickerSetsModel);

// Components
Expand Down

0 comments on commit bff6d74

Please sign in to comment.