Commit f21c1d31 authored by Karel Slaný's avatar Karel Slaný

Enhanced DataboxListModel.

parent 8033f45a
......@@ -26,8 +26,27 @@
#include "src/models/databoxmodel.h"
void DataboxModelEntry::declareQML(void)
{
qmlRegisterType<DataboxModelEntry>("cz.nic.mobileDatovka.modelEntries", 1, 0, "DataboxModelEntry");
qRegisterMetaType<DataboxModelEntry>();
}
DataboxModelEntry::DataboxModelEntry(QObject *parent)
: QObject(parent),
m_dbID(),
m_dbType(),
m_dbName(),
m_dbAddress(),
m_dbIC(),
m_dbEffectiveOVM(),
m_dbSendOptions()
{
}
DataboxModelEntry::DataboxModelEntry(const DataboxModelEntry &dme)
: m_dbID(dme.m_dbID),
: QObject(Q_NULLPTR),
m_dbID(dme.m_dbID),
m_dbType(dme.m_dbType),
m_dbName(dme.m_dbName),
m_dbAddress(dme.m_dbAddress),
......@@ -40,7 +59,8 @@ DataboxModelEntry::DataboxModelEntry(const DataboxModelEntry &dme)
DataboxModelEntry::DataboxModelEntry(const QString &dbID, const QString &dbType,
const QString &dbName, const QString &dbAddress, const QString &dbIC,
const QString &dbEffectiveOVM, const QString &dbSendOptions)
: m_dbID(dbID),
: QObject(Q_NULLPTR),
m_dbID(dbID),
m_dbType(dbType),
m_dbName(dbName),
m_dbAddress(dbAddress),
......@@ -50,6 +70,19 @@ DataboxModelEntry::DataboxModelEntry(const QString &dbID, const QString &dbType,
{
}
DataboxModelEntry &DataboxModelEntry::operator=(const DataboxModelEntry &entry)
{
m_dbID = entry.m_dbID;
m_dbType = entry.m_dbType;
m_dbName = entry.m_dbName;
m_dbAddress = entry.m_dbAddress;
m_dbIC = entry.m_dbIC;
m_dbEffectiveOVM = entry.m_dbEffectiveOVM;
m_dbSendOptions = entry.m_dbSendOptions;
return *this;
}
QString DataboxModelEntry::dbID(void) const
{
return m_dbID;
......@@ -120,6 +153,15 @@ void DataboxModelEntry::setDbSendOptions(const QString &dbSendOptions)
m_dbSendOptions = dbSendOptions;
}
DataboxModelEntry *DataboxModelEntry::fromVariant(const QVariant &entryVariant)
{
if (!entryVariant.canConvert<QObject *>()) {
return Q_NULLPTR;
}
QObject *obj = qvariant_cast<QObject *>(entryVariant);
return qobject_cast<DataboxModelEntry *>(obj);
}
void DataboxListModel::declareQML(void)
{
qmlRegisterType<DataboxListModel>("cz.nic.mobileDatovka.models", 1, 0, "DataboxListModel");
......@@ -129,14 +171,16 @@ void DataboxListModel::declareQML(void)
DataboxListModel::DataboxListModel(QObject *parent)
: QAbstractListModel(parent),
m_databoxes()
m_databoxes(),
m_selected()
{
}
DataboxListModel::DataboxListModel(const DataboxListModel &model,
QObject *parent)
: QAbstractListModel(parent),
m_databoxes(model.m_databoxes)
m_databoxes(model.m_databoxes),
m_selected(model.m_selected)
{
}
......@@ -156,6 +200,7 @@ QHash<int, QByteArray> DataboxListModel::roleNames(void) const
roles[ROLE_DB_IC] = "rDbIc";
roles[ROLE_DB_EFFECTIVE_OVM] = "rDbEffectiveOvm";
roles[ROLE_DB_SEND_OPTION] = "rDbSendOption";
roles[ROLE_DB_SELECTED] = "rDbSelected";
}
return roles;
}
......@@ -190,6 +235,9 @@ QVariant DataboxListModel::data(const QModelIndex &index, int role) const
case ROLE_DB_SEND_OPTION:
return databox.dbSendOptions();
break;
case ROLE_DB_SELECTED:
return m_selected.at(index.row());
break;
default:
/* Do nothing. */
break;
......@@ -198,11 +246,63 @@ QVariant DataboxListModel::data(const QModelIndex &index, int role) const
return QVariant();
}
void DataboxListModel::addEntry(const DataboxModelEntry &data)
bool DataboxListModel::addEntry(const DataboxModelEntry &entry)
{
if (Q_UNLIKELY(entry.dbID().isEmpty())) {
return false;
}
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_databoxes.append(data);
m_databoxes.append(entry);
m_selected.append(false);
endInsertRows();
return true;
}
bool DataboxListModel::addEntry(const QVariant &entryVariant)
{
DataboxModelEntry *entry = DataboxModelEntry::fromVariant(entryVariant);
if (Q_NULLPTR == entry) {
return false;
}
return addEntry(*entry);
}
DataboxModelEntry *DataboxListModel::entry(const QString &boxId) const
{
if (Q_UNLIKELY(boxId.isEmpty())) {
Q_ASSERT(0);
return new (std::nothrow) DataboxModelEntry;
}
foreach (const DataboxModelEntry &entry, m_databoxes) {
if (entry.dbID() == boxId) {
return new (std::nothrow) DataboxModelEntry(entry);
}
}
return new (std::nothrow) DataboxModelEntry;
}
bool DataboxListModel::selectEntry(const QString &boxId, bool selected)
{
if (Q_UNLIKELY(boxId.isEmpty())) {
return false;
}
for (int i = 0; i < m_databoxes.size(); ++i) {
if (boxId == m_databoxes.at(i).dbID()) {
m_selected[i] = selected;
emit dataChanged(QAbstractListModel::index(i, 0),
QAbstractListModel::index(i, 0));
return true;
}
}
return false;
}
int DataboxListModel::setQuery(QSqlQuery &query, const QString &dbId,
......@@ -218,6 +318,7 @@ int DataboxListModel::setQuery(QSqlQuery &query, const QString &dbId,
if (!isAppend) {
m_databoxes.clear();
m_selected.clear();
}
query.first();
......@@ -228,6 +329,7 @@ int DataboxListModel::setQuery(QSqlQuery &query, const QString &dbId,
query.value(0).toString(), QString(),
query.value(1).toString(), query.value(2).toString(),
QString(), QString(), QString()));
m_selected.append(false);
databoxCnt++;
}
query.next();
......@@ -247,6 +349,7 @@ void DataboxListModel::clearAll(void)
{
beginResetModel();
m_databoxes.clear();
m_selected.clear();
endResetModel();
}
......
......@@ -25,30 +25,92 @@
#define _DATABOXMODEL_H_
#include <QAbstractListModel>
#include <QObject>
#include <QSqlQuery>
class DataboxModelEntry {
class DataboxModelEntry : public QObject {
Q_OBJECT
Q_PROPERTY(QString dbID READ dbID WRITE setDbID NOTIFY dbIDChanged)
Q_PROPERTY(QString dbType READ dbType WRITE setDbType NOTIFY dbTypeChanged)
Q_PROPERTY(QString dbName READ dbName WRITE setDbName NOTIFY dbNameChanged)
Q_PROPERTY(QString dbAddress READ dbAddress WRITE setDbAddress NOTIFY dbAddressChanged)
Q_PROPERTY(QString dbIC READ dbIC WRITE setDbIC NOTIFY dbICChanged)
Q_PROPERTY(QString dbEffectiveOVM READ dbEffectiveOVM WRITE setDbEffectiveOVM NOTIFY dbEffectiveOVMChanged)
Q_PROPERTY(QString dbSendOptions READ dbSendOptions WRITE setDbSendOptions NOTIFY dbSendOptionsChanged)
public:
/* Don't forget to declare various properties to the QML system. */
static
void declareQML(void);
/*!
* @brief Constructor.
*
* @param[in] parent Parent object.
*/
explicit DataboxModelEntry(QObject *parent = Q_NULLPTR);
/*!
* @brief Copy constructor.
*
* @note Needed for QVariant conversion.
*
* @param[in] dme Entry to be copied.
*/
DataboxModelEntry(const DataboxModelEntry &dme);
DataboxModelEntry(const QString &dbID, const QString &dbType,
const QString &dbName, const QString &dbAddress, const QString &dbIC,
const QString &dbEffectiveOVM, const QString &dbSendOptions);
/*!
* @brief Assignment operator.
*
* @param[in] entry Source entry.
* @return Reference to itself.
*/
DataboxModelEntry &operator=(const DataboxModelEntry &entry);
QString dbID(void) const;
void setDbID(const QString &dbID);
QString dbType(void) const;
void setDbType(const QString &dbType);
QString dbName(void) const;
void setDbName(const QString &dbName);
QString dbAddress(void) const;
void setDbAddress(const QString &dbAddress);
QString dbIC(void) const;
void setDbIC(const QString &dbIC);
QString dbEffectiveOVM(void) const;
void setDbEffectiveOVM(const QString &dbEffectiveOVM);
QString dbSendOptions(void) const;
void setDbSendOptions(const QString &dbSendOptions);
/*!
* @brief Converts QVariant obtained from QML into model pointer.
*
* @param[in] entryVariant Variant containing model entry object.
* @return Pointer to model entry object or Q_NULLPTR if variant
* does not contain such object.
*/
static
DataboxModelEntry *fromVariant(const QVariant &entryVariant);
signals:
void dbIDChanged(const QString &newDbID);
void dbTypeChanged(const QString &newDbType);
void dbNameChanged(const QString *newDbName);
void dbAddressChanged(const QString &newDbAddress);
void dbICChanged(const QString &newDbIC);
void dbEffectiveOVMChanged(const QString &newDbEffectiveOVM);
void dbSendOptionsChanged(const QString &newDbSendOptions);
private:
QString m_dbID; /*!< Databox identifier. */
QString m_dbType; /*!< Databox type. */
......@@ -59,6 +121,9 @@ private:
QString m_dbSendOptions; /*!< Flag send otption (see IDSD documentation). */
};
/* QML passes its arguments via QVariant. */
Q_DECLARE_METATYPE(DataboxModelEntry)
class DataboxListModel : public QAbstractListModel {
Q_OBJECT
......@@ -73,7 +138,8 @@ public:
ROLE_DB_ADDRESS,
ROLE_DB_IC,
ROLE_DB_EFFECTIVE_OVM,
ROLE_DB_SEND_OPTION
ROLE_DB_SEND_OPTION,
ROLE_DB_SELECTED /* TODO -- See whether ItemSelectionModel can be used. */
};
Q_ENUM(Roles)
......@@ -130,10 +196,39 @@ public:
/*!
* @brief Add data-box entry into model.
*
* @param[in] data Data-box entry data.
* @param[in] role Data role.
* @param[in] entry Data-box entry data.
* @return True if data were added.
*/
bool addEntry(const DataboxModelEntry &entry);
/*!
* @brief Add data-box entry into model.
*
* @param[in] entryVariant Data-box entry data in a QVariant.
* @return True if data were added.
*/
Q_INVOKABLE
bool addEntry(const QVariant &entryVariant);
/*!
* @brief Return entry at given row.
*
* @note http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#data-ownership
*
* @param[in] boxId Data box identifier.
*/
Q_INVOKABLE
DataboxModelEntry *entry(const QString &boxId) const;
/*!
* @brief Assign selection state to box entry.
*
* @param[in] boxId Data box identifier.
* @param[in] selected Selection state.
* @return True if selection state was successfully set.
*/
void addEntry(const DataboxModelEntry &data);
Q_INVOKABLE
bool selectEntry(const QString &boxId, bool selected);
/*!
* @brief Sets the content of the model according to the supplied query.
......@@ -175,7 +270,8 @@ public:
DataboxListModel *fromVariant(const QVariant &modelVariant);
private:
QList<DataboxModelEntry> m_databoxes; /*!< List of messages stored. */
QList<DataboxModelEntry> m_databoxes; /*!< List of box entries. */
QList<bool> m_selected; /*!< Holds information whether entry is selected. */
};
/* QML passes its arguments via QVariant. */
......
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