From 431c257dd29e2e3d8878db200f0de4d452bffe92 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 30 Dec 2016 12:02:12 +0100 Subject: Encapsulate completer and selector. --- framework/domain/composercontroller.cpp | 88 ++++++++++++++++++--------------- framework/domain/composercontroller.h | 67 ++++++++++++++++++++----- 2 files changed, 102 insertions(+), 53 deletions(-) (limited to 'framework/domain') diff --git a/framework/domain/composercontroller.cpp b/framework/domain/composercontroller.cpp index 7129e342..57d386c6 100644 --- a/framework/domain/composercontroller.cpp +++ b/framework/domain/composercontroller.cpp @@ -47,35 +47,65 @@ ComposerController::ComposerController(QObject *parent) : QObject(parent) QQmlEngine::setObjectOwnership(&mContext, QQmlEngine::CppOwnership); } -QString ComposerController::recepientSearchString() const -{ - return QString(); -} Kube::Context* ComposerController::mailContext() { return &mContext; } -void ComposerController::setRecepientSearchString(const QString &s) -{ - if (auto model = static_cast(recepientAutocompletionModel())) { - model->setFilter(s); +class RecipientCompleter : public Completer { +public: + RecipientCompleter() : Completer(new RecipientAutocompletionModel) + { } -} -QAbstractItemModel *ComposerController::identityModel() const + void setSearchString(const QString &s) { + static_cast(model())->setFilter(s); + Completer::setSearchString(s); + } +}; + +Completer *ComposerController::recipientCompleter() const { - static auto model = new IdentitiesModel(); - QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership); - return model; + static auto selector = new RecipientCompleter(); + QQmlEngine::setObjectOwnership(selector, QQmlEngine::CppOwnership); + return selector; } -QAbstractItemModel *ComposerController::recepientAutocompletionModel() const +class IdentitySelector : public Selector { +public: + IdentitySelector(ComposerContext &context) : Selector(new IdentitiesModel), mContext(context) + { + } + + void setCurrent(const QModelIndex &index) Q_DECL_OVERRIDE + { + if (index.isValid()) { + auto currentAccountId = index.data(IdentitiesModel::AccountId).toByteArray(); + + KMime::Types::Mailbox mb; + mb.setName(index.data(IdentitiesModel::Username).toString()); + mb.setAddress(index.data(IdentitiesModel::Address).toString().toUtf8()); + SinkLog() << "Setting current identity: " << mb.prettyAddress() << "Account: " << currentAccountId; + + mContext.setProperty("identity", QVariant::fromValue(mb)); + mContext.setProperty("accountId", QVariant::fromValue(currentAccountId)); + } else { + SinkWarning() << "No valid identity for index: " << index; + mContext.setProperty("identity", QVariant{}); + mContext.setProperty("accountId", QVariant{}); + } + + } +private: + ComposerContext &mContext; +}; + +Selector *ComposerController::identitySelector() const { - static auto model = new RecipientAutocompletionModel(); - QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership); - return model; + static auto selector = new IdentitySelector(*const_cast(&mContext)); + QQmlEngine::setObjectOwnership(selector, QQmlEngine::CppOwnership); + return selector; } void ComposerController::setMessage(const KMime::Message::Ptr &msg) @@ -113,7 +143,7 @@ void ComposerController::loadMessage(const QVariant &message, bool loadAsDraft) void ComposerController::recordForAutocompletion(const QByteArray &addrSpec, const QByteArray &displayName) { - if (auto model = static_cast(recepientAutocompletionModel())) { + if (auto model = static_cast(recipientCompleter()->model())) { model->addEntry(addrSpec, displayName); } } @@ -194,25 +224,3 @@ Kube::Action* ComposerController::sendAction() })); return action; } - -void ComposerController::setCurrentIdentityIndex(int index) -{ - m_currentAccountIndex = index; - auto currentIndex = identityModel()->index(m_currentAccountIndex, 0); - if (currentIndex.isValid()) { - auto currentAccountId = currentIndex.data(IdentitiesModel::AccountId).toByteArray(); - KMime::Types::Mailbox mb; - mb.setName(currentIndex.data(IdentitiesModel::Username).toString()); - mb.setAddress(currentIndex.data(IdentitiesModel::Address).toString().toUtf8()); - SinkLog() << "Setting current identity: " << mb.prettyAddress() << "Account: " << currentAccountId; - mContext.setProperty("identity", QVariant::fromValue(mb)); - mContext.setProperty("accountId", QVariant::fromValue(currentAccountId)); - } else { - SinkWarning() << "No valid identity for index: " << index << " out of available in model: " << identityModel()->rowCount(); - } -} - -int ComposerController::currentIdentityIndex() const -{ - return m_currentAccountIndex; -} diff --git a/framework/domain/composercontroller.h b/framework/domain/composercontroller.h index 11da517e..3e701ed1 100644 --- a/framework/domain/composercontroller.h +++ b/framework/domain/composercontroller.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -43,15 +44,62 @@ class ComposerContext : public Kube::Context { KUBE_CONTEXT_PROPERTY(QString, Body, body) }; +class Completer : public QObject { + Q_OBJECT + Q_PROPERTY (QAbstractItemModel* model READ model CONSTANT) + Q_PROPERTY (QString searchString WRITE setSearchString READ searchString) + +public: + Completer(QAbstractItemModel *model) : mModel{model} + { + QQmlEngine::setObjectOwnership(mModel, QQmlEngine::CppOwnership); + } + QAbstractItemModel *model() { return mModel; } + virtual void setSearchString(const QString &s) { mSearchString = s; } + QString searchString() const { return mSearchString; } + +private: + QAbstractItemModel *mModel = nullptr; + QString mSearchString; +}; + +/** + * Exposes a model and maintains a current index selection. + */ +class Selector : public QObject { + Q_OBJECT + Q_PROPERTY (int currentIndex READ currentIndex WRITE setCurrentIndex) + Q_PROPERTY (QAbstractItemModel* model READ model CONSTANT) + +public: + Selector(QAbstractItemModel *model) : mModel{model} + { + QQmlEngine::setObjectOwnership(mModel, QQmlEngine::CppOwnership); + } + + virtual QAbstractItemModel *model() { return mModel; } + + void setCurrentIndex(int i) { + mCurrentIndex = i; + Q_ASSERT(mModel); + setCurrent(mModel->index(mCurrentIndex, 0)); + } + + int currentIndex() { return mCurrentIndex; } + + virtual void setCurrent(const QModelIndex &) = 0; +private: + QAbstractItemModel *mModel = nullptr; + int mCurrentIndex = 0; +}; + class ComposerController : public QObject { Q_OBJECT Q_PROPERTY (Kube::Context* mailContext READ mailContext CONSTANT) - Q_PROPERTY (int currentIdentityIndex READ currentIdentityIndex WRITE setCurrentIdentityIndex) - Q_PROPERTY (QString recepientSearchString READ recepientSearchString WRITE setRecepientSearchString) - Q_PROPERTY (QAbstractItemModel* recepientAutocompletionModel READ recepientAutocompletionModel CONSTANT) - Q_PROPERTY (QAbstractItemModel* identityModel READ identityModel CONSTANT) + Q_PROPERTY (Completer* recipientCompleter READ recipientCompleter CONSTANT) + Q_PROPERTY (Selector* identitySelector READ identitySelector CONSTANT) Q_PROPERTY (Kube::Action* sendAction READ sendAction) Q_PROPERTY (Kube::Action* saveAsDraftAction READ saveAsDraftAction) @@ -61,20 +109,14 @@ public: Kube::Context* mailContext(); - QString recepientSearchString() const; - void setRecepientSearchString(const QString &body); - - QAbstractItemModel *identityModel() const; - QAbstractItemModel *recepientAutocompletionModel() const; + Completer *recipientCompleter() const; + Selector *identitySelector() const; Q_INVOKABLE void loadMessage(const QVariant &draft, bool loadAsDraft); Kube::Action* sendAction(); Kube::Action* saveAsDraftAction(); - void setCurrentIdentityIndex(int index); - int currentIdentityIndex() const; - public slots: void clear(); @@ -86,6 +128,5 @@ private: void recordForAutocompletion(const QByteArray &addrSpec, const QByteArray &displayName); void setMessage(const QSharedPointer &msg); - int m_currentAccountIndex = -1; ComposerContext mContext; }; -- cgit v1.2.3