Commit 5cda2373 authored by Karel Slaný's avatar Karel Slaný

AccountListModel can now be instantiated from QML.

parent 59b82b25
......@@ -259,10 +259,11 @@ int main(int argc, char *argv[])
/* Create and init account model. */
globAccountsModelPtr = new (std::nothrow) AccountListModel;
if (0 == globAccountsModelPtr) {
if (Q_NULLPTR == globAccountsModelPtr) {
qCritical("Cannot create account model.");
return EXIT_FAILURE;
}
globAccountsModelPtr->setObjectName("accountListModel");
/* get main handle of appliaction and QML */
QQmlApplicationEngine engine;
......@@ -274,6 +275,7 @@ int main(int argc, char *argv[])
registerQmlTypes(uri, QML_DIALOGUES, qmlDialogues);
/* Register types into QML. */
AccountListModel::declareQML();
Dialogues::declareQML();
FileListModel::declareQML();
ListSortFilterProxyModel::declareQML();
......@@ -420,7 +422,7 @@ int main(int argc, char *argv[])
}
delete globAccountsModelPtr;
globAccountsModelPtr = 0;
globAccountsModelPtr = Q_NULLPTR;
return ret;
}
......@@ -22,6 +22,7 @@
*/
#include <QDebug>
#include <QQmlEngine> /* qmlRegisterType */
#include "src/crypto/password.h"
#include "src/crypto/wrapped.h"
......@@ -520,17 +521,31 @@ void AccountsMap::loadAccountsFromSettings(const QSettings &settings)
}
}
AccountListModel *globAccountsModelPtr = 0;
AccountListModel *globAccountsModelPtr = Q_NULLPTR;
AccountsMap AccountListModel::globAccounts;
void AccountListModel::declareQML(void)
{
qmlRegisterType<AccountListModel>("cz.nic.mobileDatovka.models", 1, 0, "AccountListModel");
qRegisterMetaType<AccountListModel>();
qRegisterMetaType<AccountListModel::Roles>();
}
AccountListModel::AccountListModel(QObject *parent)
: QAbstractListModel(parent),
m_userNames()
{
/* Used in QML. */
setObjectName("accountListModel");
/* Automatically handle signalled changes. */
connect(&globAccounts, SIGNAL(accountDataChanged(QString)),
this, SLOT(handleAccountDataChange(QString)));
}
AccountListModel::AccountListModel(const AccountListModel &model,
QObject *parent)
: QAbstractListModel(parent),
m_userNames(model.m_userNames)
{
/* Automatically handle signalled changes. */
connect(&globAccounts, SIGNAL(accountDataChanged(QString)),
this, SLOT(handleAccountDataChange(QString)));
......@@ -795,6 +810,15 @@ void AccountListModel::decryptAllPwds(const QString &pinVal)
}
}
AccountListModel *AccountListModel::fromVariant(const QVariant &modelVariant)
{
if (!modelVariant.canConvert<QObject *>()) {
return Q_NULLPTR;
}
QObject *obj = qvariant_cast<QObject *>(modelVariant);
return qobject_cast<AccountListModel *>(obj);
}
void AccountListModel::handleAccountDataChange(const QString &userName)
{
QModelIndex acntIdx(acntIndex(userName));
......
......@@ -152,13 +152,29 @@ public:
ROLE_SENT_UNREAD,
ROLE_SENT_TOTAL
};
Q_ENUM(Roles)
/* Don't forget to declare various properties to the QML system. */
static
void declareQML(void);
/*!
* @brief Constructor.
*
* @param[in] parent Pointer to parent object.
*/
explicit AccountListModel(QObject *parent = 0);
explicit AccountListModel(QObject *parent = Q_NULLPTR);
/*!
* @brief Copy constructor.
*
* @note Needed for QVariant conversion.
*
* @param[in] model Model to be copied.
* @param[in] parent Pointer to parent object.
*/
explicit AccountListModel(const AccountListModel &model,
QObject *parent = Q_NULLPTR);
/*!
* @brief Return number of rows under the given parent.
......@@ -223,7 +239,7 @@ public:
* -1 if account could not be added,
* 0 if account was added.
*/
int addAccount(const AcntData &acntData, QModelIndex *idx = 0);
int addAccount(const AcntData &acntData, QModelIndex *idx = Q_NULLPTR);
/*!
* @brief Delete account.
......@@ -270,6 +286,20 @@ public:
*/
void decryptAllPwds(const QString &pinVal);
/*!
* @brief Converts QVariant obtained from QML into model pointer.
*
* @note Some weird stuff happens in QML when passing attachment model
* directly as constant reference. Wrong constructors are called
* and no data are passed. That's because we are using a QVariant.
* This function does not allocate a new model.
*
* @param[in] modelVariant QVariant holding the model.
* @return Pointer to model if it could be acquired, Q_NULLPTR else.
*/
static
AccountListModel *fromVariant(const QVariant &modelVariant);
private slots:
/*!
* @brief This slot handles changes of account data.
......@@ -280,11 +310,15 @@ private slots:
private:
QList<QString> m_userNames; /*!<
* List of user names,
* defines the ordering of the model.
*/
* List of user names,
* defines the ordering of the model.
*/
};
/* QML passes its arguments via QVariant. */
Q_DECLARE_METATYPE(AccountListModel)
Q_DECLARE_METATYPE(AccountListModel::Roles)
/*
* The model cannot be a static global instance because of the dependencies on
* other static objects (static initialisation order fiasco).
......
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