Commit 41a21c62 authored by Karel Slaný's avatar Karel Slaný Committed by Martin Straka

Added GovFormListModel.

parent 9531f0e9
......@@ -91,6 +91,7 @@ SOURCES += \
src/accounts.cpp \
src/auxiliaries/attachment_helper.cpp \
src/auxiliaries/email_helper.cpp \
src/datovka_shared/gov_services/model/gov_form_list_model.cpp \
src/datovka_shared/gov_services/model/gov_service_list_model.cpp \
src/datovka_shared/gov_services/service/gov_mv_crr_vbh.cpp \
src/datovka_shared/gov_services/service/gov_mv_ir_vp.cpp \
......@@ -210,6 +211,7 @@ HEADERS += \
src/auxiliaries/attachment_helper.h \
src/auxiliaries/email_helper.h \
src/common.h \
src/datovka_shared/gov_services/model/gov_form_list_model.h \
src/datovka_shared/gov_services/model/gov_service_list_model.h \
src/datovka_shared/gov_services/service/gov_mv_crr_vbh.h \
src/datovka_shared/gov_services/service/gov_mv_ir_vp.h \
......
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#include <QQmlEngine> /* qmlRegisterType */
#include "src/datovka_shared/gov_services/model/gov_form_list_model.h"
void GovFormListModel::declareQML(void)
{
qmlRegisterType<GovFormListModel>("cz.nic.mobileDatovka.models", 1, 0, "GovFormListModel");
qRegisterMetaType<GovFormListModel>();
}
GovFormListModel::GovFormListModel(QObject *parent)
: QAbstractListModel(parent),
m_requiredInputList()
{
}
GovFormListModel::GovFormListModel(const GovFormListModel &model,
QObject *parent)
: QAbstractListModel(parent),
m_requiredInputList(model.m_requiredInputList)
{
}
int GovFormListModel::rowCount(const QModelIndex &parent) const
{
return !parent.isValid() ? m_requiredInputList.size() : 0;
}
QHash<int, QByteArray> GovFormListModel::roleNames(void) const
{
static QHash<int, QByteArray> roles;
if (roles.isEmpty()) {
roles[ROLE_GOV_SRVC_KEY] = "gsKey";
roles[ROLE_GOV_SRVC_VAL] = "gsVal";
roles[ROLE_GOV_SRVC_DESCR] = "gsDescr";
roles[ROLE_GOV_SRVC_PLACEHOLD] = "gsPlacehold";
roles[ROLE_GOV_SRVC_MANDATORY] = "gsMandatory";
}
return roles;
}
QVariant GovFormListModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
if (Q_UNLIKELY((row < 0) || (row >= m_requiredInputList.size()))) {
Q_ASSERT(0);
return QVariant();
}
const RequiredInput &ri(m_requiredInputList[row]);
switch (role) {
case ROLE_GOV_SRVC_KEY:
return ri.key;
break;
case ROLE_GOV_SRVC_VAL:
return ri.val;
break;
case ROLE_GOV_SRVC_DESCR:
return ri.descr;
break;
case ROLE_GOV_SRVC_PLACEHOLD:
return ri.placeholder;
break;
case ROLE_GOV_SRVC_MANDATORY:
return ri.mandatory;
break;
default:
/* Do nothing. */
break;
}
return QVariant();
}
const QList<RequiredInput> &GovFormListModel::allEntries(void) const
{
return m_requiredInputList;
}
Qt::ItemFlags GovFormListModel::flags(const QModelIndex &index) const
{
return QAbstractListModel::flags(index);
}
void GovFormListModel::appendEntry(const RequiredInput &entry)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_requiredInputList.append(entry);
endInsertRows();
}
void GovFormListModel::clearAll(void)
{
beginResetModel();
m_requiredInputList.clear();
endResetModel();
}
GovFormListModel *GovFormListModel::fromVariant(const QVariant &modelVariant)
{
if (!modelVariant.canConvert<QObject *>()) {
return Q_NULLPTR;
}
QObject *obj = qvariant_cast<QObject *>(modelVariant);
return qobject_cast<GovFormListModel *>(obj);
}
/*
* Copyright (C) 2014-2018 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#pragma once
#include <QAbstractListModel>
#include <QList>
#include <QString>
#include "src/datovka_shared/gov_services/service/gov_service.h"
/*!
* @brief Holds form data for gov services.
*/
class GovFormListModel : public QAbstractListModel {
Q_OBJECT
public:
/*!
* @brief Roles which this model supports.
*/
enum Roles {
ROLE_GOV_SRVC_KEY = Qt::UserRole,
ROLE_GOV_SRVC_VAL,
ROLE_GOV_SRVC_DESCR,
ROLE_GOV_SRVC_PLACEHOLD,
ROLE_GOV_SRVC_MANDATORY
};
/* 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 GovFormListModel(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 GovFormListModel(const GovFormListModel &model,
QObject *parent = Q_NULLPTR);
/*!
* @brief Return number of rows under the given parent.
*
* @param[in] parent Parent node index.
* @return Number of rows.
*/
virtual
int rowCount(const QModelIndex &parent = QModelIndex()) const
Q_DECL_OVERRIDE;
/*!
* @brief Returns the model's role names.
*
* @return Model's role names.
*/
virtual
QHash<int, QByteArray> roleNames(void) const Q_DECL_OVERRIDE;
/*!
* @brief Return data stored in given location under given role.
*
* @param[in] index Index specifying the item.
* @param[in] role Data role.
* @return Data from model.
*/
virtual
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
/*!
* @brief Return list of all entries.
*
* @return List of all entries.
*/
const QList<RequiredInput> &allEntries(void) const;
/*!
* @brief Returns item flags for given index.
*
* @brief[in] index Index specifying the item.
* @return Item flags.
*/
virtual
Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
/*!
* @brief Appends entry line to model.
*
* @param[in] entry Entry to append to model data.
*/
void appendEntry(const RequiredInput &entry);
/*!
* @brief Clears the model.
*/
Q_INVOKABLE
void clearAll(void);
/*!
* @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
GovFormListModel *fromVariant(const QVariant &modelVariant);
private:
QList<RequiredInput> m_requiredInputList; /*!< Form data description. */
};
/* QML passes its arguments via QVariant. */
Q_DECLARE_METATYPE(GovFormListModel)
......@@ -117,25 +117,26 @@ QHash<int, QByteArray> GovServiceListModel::roleNames(void) const
QVariant GovServiceListModel::data(const QModelIndex &index, int role) const
{
if ((index.row() < 0) || (index.row() >= m_services.size())) {
int row = index.row();
if (Q_UNLIKELY((row < 0) || (row >= m_services.size()))) {
Q_ASSERT(0);
return QVariant();
}
const Entry &s(m_services[index.row()]);
const Entry &e(m_services[row]);
switch (role) {
case ROLE_GOV_SRVC_INTERN_ID:
return s.srvcInternId();
return e.srvcInternId();
break;
case ROLE_GOV_SRVC_FULL_NAME:
return s.srvcFullName();
return e.srvcFullName();
break;
case ROLE_GOV_SRVC_INST_NAME:
return s.instName();
return e.instName();
break;
case ROLE_GOV_SRVC_BOXID:
return s.srvcBoxId();
return e.srvcBoxId();
break;
default:
/* Do nothing. */
......@@ -145,15 +146,10 @@ QVariant GovServiceListModel::data(const QModelIndex &index, int role) const
return QVariant();
}
QList<GovServiceListModel::Entry> GovServiceListModel::allEntries(void) const
const QList<GovServiceListModel::Entry> &GovServiceListModel::allEntries(
void) const
{
QList<Entry> entries;
foreach (const Entry &e, m_services) {
entries.append(e);
}
return entries;
return m_services;
}
Qt::ItemFlags GovServiceListModel::flags(const QModelIndex &index) const
......
......@@ -26,10 +26,16 @@
#include <QAbstractListModel>
#include <QString>
/*!
* @brief Holds information about available gov services.
*/
class GovServiceListModel : public QAbstractListModel {
Q_OBJECT
public:
/*!
* @brief Model entry.
*/
class Entry {
public:
Entry(const Entry &sme);
......@@ -116,7 +122,7 @@ public:
*
* @return List of all entries.
*/
QList<Entry> allEntries(void) const;
const QList<Entry> &allEntries(void) const;
/*!
* @brief Returns item flags for given index.
......
......@@ -37,6 +37,7 @@
#include "src/accounts.h"
#include "src/datovka_shared/crypto/crypto_trusted_certs.h"
#include "src/datovka_shared/gov_services/model/gov_form_list_model.h"
#include "src/datovka_shared/gov_services/model/gov_service_list_model.h"
#include "src/datovka_shared/io/records_management_db.h"
#include "src/datovka_shared/localisation/localisation.h"
......@@ -485,6 +486,7 @@ int main(int argc, char *argv[])
Dialogues::declareQML();
FileListModel::declareQML();
Files::declareQML();
GovFormListModel::declareQML();
GovServiceListModel::declareQML();
InteractionFilesystem::declareQML();
ListSortFilterProxyModel::declareQML();
......
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