Initial commit based on GenericQtClient v0.3.0
This commit is contained in:
80
libs/GenericCore/model/commands/edititemcommand.cpp
Normal file
80
libs/GenericCore/model/commands/edititemcommand.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include "edititemcommand.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "../metadata.h"
|
||||
#include "../tablemodel.h"
|
||||
|
||||
EditItemCommand::EditItemCommand(TableModel* model,
|
||||
const QModelIndex& index,
|
||||
QMap<int, QVariant>& changedValues,
|
||||
QUndoCommand* parent)
|
||||
: QUndoCommand(parent)
|
||||
, m_model(model)
|
||||
, m_row(index.row()) {
|
||||
qInfo() << "New EditCommand...";
|
||||
QString commandText;
|
||||
|
||||
if (changedValues.size() == 1) {
|
||||
qDebug() << "Only one value to change. Using more specific command text...";
|
||||
const int role = changedValues.firstKey();
|
||||
const QVariant value = changedValues.first();
|
||||
QString roleName = model->roleNames().value(role);
|
||||
|
||||
if (USER_FACING_ROLES.contains(role)) {
|
||||
commandText = QString("Setting '%1' of item '%2' to '%3'")
|
||||
.arg(roleName)
|
||||
.arg(index.data(DEFAULT_ROLE).toString())
|
||||
.arg(value.toString());
|
||||
} else if (role == IdRole) {
|
||||
commandText = QString("Setting '%1' of item '%2' to '%3'")
|
||||
.arg(roleName)
|
||||
.arg(index.data(DEFAULT_ROLE).toString())
|
||||
.arg(value.toString());
|
||||
} else {
|
||||
qWarning() << "Role didn't match! Using a generic command text...";
|
||||
commandText = QString("Edit item '%1'").arg(index.data(DEFAULT_ROLE).toString());
|
||||
}
|
||||
} else {
|
||||
qDebug() << "More than one value to change. Using a generic command text...";
|
||||
commandText = QString("Edit item '%1'").arg(index.data(DEFAULT_ROLE).toString());
|
||||
}
|
||||
setText(commandText);
|
||||
|
||||
m_newValues = changedValues;
|
||||
|
||||
/// storing old values for undo step
|
||||
m_oldValues = getOldValues(index, changedValues);
|
||||
}
|
||||
|
||||
void EditItemCommand::undo() {
|
||||
qDebug() << "Undoing the EditCommand...";
|
||||
m_model->execEditItemData(m_row, m_oldValues);
|
||||
}
|
||||
|
||||
void EditItemCommand::redo() {
|
||||
qDebug() << "(Re-)doing the EditCommand...";
|
||||
m_model->execEditItemData(m_row, m_newValues);
|
||||
}
|
||||
|
||||
const QMap<int, QVariant> EditItemCommand::getOldValues(
|
||||
const QModelIndex& index,
|
||||
const QMap<int, QVariant>& changedValues) const {
|
||||
QMap<int, QVariant> result;
|
||||
QMap<int, QVariant>::const_iterator i;
|
||||
for (i = changedValues.constBegin(); i != changedValues.constEnd(); ++i) {
|
||||
const int role = i.key();
|
||||
const QVariant newValue = i.value();
|
||||
const QVariant oldValue = index.data(role);
|
||||
// TODO check if role is a editable role?
|
||||
if (oldValue != newValue) {
|
||||
qDebug() << "oldValue:" << oldValue << "!= newValue:" << newValue;
|
||||
result.insert(role, oldValue);
|
||||
} else {
|
||||
qInfo() << "oldValue is already the same as newValue:" << oldValue;
|
||||
}
|
||||
}
|
||||
// QVariant oldModifiedDate = index.data(ModifiedDateUTCRole);
|
||||
// result.insert(ModifiedDateUTCRole, oldModifiedDate);
|
||||
return result;
|
||||
}
|
||||
33
libs/GenericCore/model/commands/edititemcommand.h
Normal file
33
libs/GenericCore/model/commands/edititemcommand.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef EDITITEMCOMMAND_H
|
||||
#define EDITITEMCOMMAND_H
|
||||
|
||||
#include <QMap>
|
||||
#include <QUndoCommand>
|
||||
|
||||
class TableModel;
|
||||
|
||||
class EditItemCommand : public QUndoCommand {
|
||||
public:
|
||||
// TODO don't use simple pointer to model
|
||||
/// Using simple pointer to model because there was a crash when closing the application with an
|
||||
/// unclean undo stack
|
||||
EditItemCommand(TableModel* model,
|
||||
const QModelIndex& index,
|
||||
QMap<int, QVariant>& changedValues,
|
||||
QUndoCommand* parent = nullptr);
|
||||
/// QUndoCommand interface
|
||||
void undo();
|
||||
void redo();
|
||||
|
||||
private:
|
||||
TableModel* m_model = nullptr;
|
||||
const int m_row;
|
||||
QMap<int, QVariant> m_oldValues;
|
||||
QMap<int, QVariant> m_newValues;
|
||||
|
||||
/// private functions
|
||||
const QMap<int, QVariant> getOldValues(const QModelIndex& index,
|
||||
const QMap<int, QVariant>& changedValues) const;
|
||||
};
|
||||
|
||||
#endif // EDITITEMCOMMAND_H
|
||||
33
libs/GenericCore/model/commands/insertrowscommand.cpp
Normal file
33
libs/GenericCore/model/commands/insertrowscommand.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "insertrowscommand.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "../tablemodel.h"
|
||||
|
||||
InsertRowsCommand::InsertRowsCommand(TableModel* model,
|
||||
int startRow,
|
||||
QList<ModelItemValues> valueList,
|
||||
QUndoCommand* parent)
|
||||
: QUndoCommand(parent)
|
||||
, m_tableModel(model)
|
||||
, m_startRow(startRow)
|
||||
, m_valueList(valueList) {
|
||||
qInfo() << "New InsertCommand...";
|
||||
const QString commandText =
|
||||
QString("inserting %1 item(s) on row %2").arg(valueList.length()).arg(startRow);
|
||||
setText(commandText);
|
||||
}
|
||||
|
||||
void InsertRowsCommand::undo() {
|
||||
qDebug() << "Undoing the InsertCommand...";
|
||||
if (m_tableModel) {
|
||||
m_tableModel->execRemoveItems(m_startRow, m_valueList.length());
|
||||
}
|
||||
}
|
||||
|
||||
void InsertRowsCommand::redo() {
|
||||
qDebug() << "(Re-)doing the InsertCommand...";
|
||||
if (m_tableModel) {
|
||||
m_tableModel->execInsertItems(m_startRow, m_valueList);
|
||||
}
|
||||
}
|
||||
30
libs/GenericCore/model/commands/insertrowscommand.h
Normal file
30
libs/GenericCore/model/commands/insertrowscommand.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef INSERTROWSCOMMAND_H
|
||||
#define INSERTROWSCOMMAND_H
|
||||
|
||||
#include <QUndoCommand>
|
||||
|
||||
typedef QMap<int, QVariant> ModelItemValues;
|
||||
|
||||
class TableModel;
|
||||
|
||||
class InsertRowsCommand : public QUndoCommand {
|
||||
public:
|
||||
// TODO don't use simple pointer to model
|
||||
/// Using simple pointer to model because there was a crash when closing the application with an
|
||||
/// unclean undo stack
|
||||
InsertRowsCommand(TableModel* model,
|
||||
int startRow,
|
||||
QList<ModelItemValues> valueList,
|
||||
QUndoCommand* parent = nullptr);
|
||||
|
||||
/// QUndoCommand interface
|
||||
void undo() override;
|
||||
void redo() override;
|
||||
|
||||
private:
|
||||
TableModel* m_tableModel;
|
||||
const int m_startRow;
|
||||
const QList<ModelItemValues> m_valueList;
|
||||
};
|
||||
|
||||
#endif // INSERTROWSCOMMAND_H
|
||||
41
libs/GenericCore/model/commands/removerowscommand.cpp
Normal file
41
libs/GenericCore/model/commands/removerowscommand.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include "removerowscommand.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "../tablemodel.h"
|
||||
|
||||
RemoveRowsCommand::RemoveRowsCommand(TableModel* model,
|
||||
const int startRow,
|
||||
const int nRows,
|
||||
QUndoCommand* parent)
|
||||
: QUndoCommand(parent)
|
||||
, m_tableModel(model)
|
||||
, m_startRow(startRow) {
|
||||
qInfo() << "New RemoveCommand...";
|
||||
const QString commandText =
|
||||
QString("removing %1 item(s) on position %2").arg(nRows).arg(startRow);
|
||||
setText(commandText);
|
||||
|
||||
for (int row = 0; row < nRows; ++row) {
|
||||
const int rowPosition = startRow + row;
|
||||
QModelIndex index = m_tableModel->index(rowPosition, 0);
|
||||
|
||||
ModelItemValues values = m_tableModel->getItemValues(index);
|
||||
|
||||
m_valueList.append(values);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveRowsCommand::undo() {
|
||||
qDebug() << "Undoing the RemoveCommand...";
|
||||
if (m_tableModel) {
|
||||
m_tableModel->execInsertItems(m_startRow, m_valueList);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveRowsCommand::redo() {
|
||||
qDebug() << "(Re-)doing the RemoveCommand...";
|
||||
if (m_tableModel) {
|
||||
m_tableModel->execRemoveItems(m_startRow, m_valueList.length());
|
||||
}
|
||||
}
|
||||
30
libs/GenericCore/model/commands/removerowscommand.h
Normal file
30
libs/GenericCore/model/commands/removerowscommand.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef REMOVEROWSCOMMAND_H
|
||||
#define REMOVEROWSCOMMAND_H
|
||||
|
||||
#include <QUndoCommand>
|
||||
|
||||
class TableModel;
|
||||
|
||||
typedef QMap<int, QVariant> ModelItemValues;
|
||||
|
||||
class RemoveRowsCommand : public QUndoCommand {
|
||||
public:
|
||||
// TODO don't use simple pointer to model
|
||||
/// Using simple pointer to model because there was a crash when closing the application with an
|
||||
/// unclean undo stack
|
||||
RemoveRowsCommand(TableModel* model,
|
||||
const int startRow,
|
||||
const int nRows,
|
||||
QUndoCommand* parent = nullptr);
|
||||
|
||||
/// QUndoCommand interface
|
||||
void undo() override;
|
||||
void redo() override;
|
||||
|
||||
private:
|
||||
TableModel* m_tableModel;
|
||||
const int m_startRow;
|
||||
QList<ModelItemValues> m_valueList;
|
||||
};
|
||||
|
||||
#endif // REMOVEROWSCOMMAND_H
|
||||
105
libs/GenericCore/model/generalsortfiltermodel.cpp
Normal file
105
libs/GenericCore/model/generalsortfiltermodel.cpp
Normal file
@ -0,0 +1,105 @@
|
||||
#include "generalsortfiltermodel.h"
|
||||
#include "metadata.h"
|
||||
|
||||
#include <QItemSelection>
|
||||
|
||||
GeneralSortFilterModel::GeneralSortFilterModel(std::shared_ptr<TableModel> sourceModel)
|
||||
: QSortFilterProxyModel{sourceModel.get()}
|
||||
, m_tableModel(sourceModel) {
|
||||
setSourceModel(sourceModel.get());
|
||||
|
||||
m_collator.setNumericMode(true);
|
||||
}
|
||||
|
||||
QItemSelection GeneralSortFilterModel::findItems(const QString& text) const {
|
||||
QItemSelection result;
|
||||
|
||||
for (int i = 0; i < rowCount(); ++i) {
|
||||
const QModelIndex localIndex = index(i, 0);
|
||||
if (indexContainsText(localIndex, text)) {
|
||||
result.select(localIndex, index(i, columnCount() - 1)); /// select entire row
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QString GeneralSortFilterModel::getUuid(const QModelIndex& itemIndex) const {
|
||||
return data(itemIndex, IdRole).toString();
|
||||
}
|
||||
|
||||
QByteArray GeneralSortFilterModel::jsonDataForServer(const QModelIndex& proxyIndex) {
|
||||
const QModelIndex sourceIndex = mapToSource(proxyIndex);
|
||||
return m_tableModel->jsonDataForServer(sourceIndex);
|
||||
}
|
||||
|
||||
void GeneralSortFilterModel::appendItems(const QByteArray& jsonDoc) {
|
||||
m_tableModel->appendItems(jsonDoc);
|
||||
}
|
||||
|
||||
bool GeneralSortFilterModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
|
||||
const QModelIndex proxyIndex = index(firstRow, 0, parentIndex);
|
||||
const QModelIndex sourceIndex = mapToSource(proxyIndex);
|
||||
return m_tableModel->removeRows(sourceIndex.row(), nRows, sourceIndex.parent());
|
||||
}
|
||||
|
||||
bool GeneralSortFilterModel::lessThan(const QModelIndex& source_left,
|
||||
const QModelIndex& source_right) const {
|
||||
if (source_left.column() != source_right.column()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QAbstractItemModel* localSourceModel = sourceModel();
|
||||
const int role = GET_ROLE_FOR_COLUMN(source_left.column());
|
||||
const QVariant leftData = localSourceModel->data(source_left);
|
||||
const QVariant rightData = localSourceModel->data(source_right);
|
||||
|
||||
const bool isText = STRING_ROLES.contains(role);
|
||||
if (isText) {
|
||||
const QString leftString = leftData.toString();
|
||||
const QString rightString = rightData.toString();
|
||||
return m_collator.compare(leftString, rightString) > 0;
|
||||
}
|
||||
const bool isInt = INT_ROLES.contains(role);
|
||||
if (isInt) {
|
||||
const int leftInt = leftData.toInt();
|
||||
const int rightInt = rightData.toInt();
|
||||
return leftInt > rightInt;
|
||||
}
|
||||
const bool isDouble = DOUBLE_ROLES.contains(role);
|
||||
if (isDouble) {
|
||||
const int leftInt = leftData.toDouble();
|
||||
const int rightInt = rightData.toDouble();
|
||||
return leftInt > rightInt;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GeneralSortFilterModel::indexContainsText(const QModelIndex& index,
|
||||
const QString& text) const {
|
||||
// iterate over USER_FACING_ROLES and call roleDataContainsText(...);
|
||||
// ...for each until text is found or no more roles to check;
|
||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||
while (i.hasNext()) {
|
||||
const UserRoles role = i.next();
|
||||
const bool isTextFound = roleDataContainsText(index, role, text);
|
||||
if (isTextFound) {
|
||||
// qInfo() << "Text is found in role:" << role;
|
||||
return true;
|
||||
// } else {
|
||||
// qDebug() << QString("Can't find text in role %1. Continuing search...").arg(role);
|
||||
}
|
||||
}
|
||||
// qWarning() << "Text not found in any role. Returning false...";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GeneralSortFilterModel::roleDataContainsText(const QModelIndex& index,
|
||||
const int role,
|
||||
const QString& text) const {
|
||||
const QString dataString = index.data(role).toString();
|
||||
if (dataString.contains(text, Qt::CaseInsensitive)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
39
libs/GenericCore/model/generalsortfiltermodel.h
Normal file
39
libs/GenericCore/model/generalsortfiltermodel.h
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef GENERALSORTFILTERMODEL_H
|
||||
#define GENERALSORTFILTERMODEL_H
|
||||
|
||||
#include <QCollator>
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
#include "tablemodel.h"
|
||||
|
||||
class GeneralSortFilterModel : public QSortFilterProxyModel {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit GeneralSortFilterModel(std::shared_ptr<TableModel> sourceModel = nullptr);
|
||||
|
||||
/** Returns a QItemSelection with all ModelIndexes which contain the given text.
|
||||
* @brief Returns QItemSelection containing all successful ModelIndex results
|
||||
* @param text To search for
|
||||
* @return QItemSelection containing all successful ModelIndex results
|
||||
*/
|
||||
QItemSelection findItems(const QString& text) const;
|
||||
QString getUuid(const QModelIndex& itemIndex) const;
|
||||
|
||||
QByteArray jsonDataForServer(const QModelIndex& proxyIndex);
|
||||
|
||||
public slots:
|
||||
void appendItems(const QByteArray& jsonDoc);
|
||||
bool removeRows(int firstRow, int nRows, const QModelIndex& parentIndex = QModelIndex()) override;
|
||||
/// QSortFilterProxyModel interface
|
||||
protected:
|
||||
bool lessThan(const QModelIndex& sourceLeft, const QModelIndex& sourceRight) const override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<TableModel> m_tableModel;
|
||||
QCollator m_collator; /// for sorting
|
||||
|
||||
bool indexContainsText(const QModelIndex& index, const QString& text) const;
|
||||
bool roleDataContainsText(const QModelIndex& index, const int role, const QString& text) const;
|
||||
};
|
||||
|
||||
#endif // GENERALSORTFILTERMODEL_H
|
||||
77
libs/GenericCore/model/metadata.h
Normal file
77
libs/GenericCore/model/metadata.h
Normal file
@ -0,0 +1,77 @@
|
||||
#ifndef METADATA_H
|
||||
#define METADATA_H
|
||||
|
||||
#include <QDebug>
|
||||
#include <QHash>
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
|
||||
// TODO add namespace
|
||||
|
||||
/// model data
|
||||
enum UserRoles {
|
||||
NameRole = Qt::UserRole + 1,
|
||||
DescriptionRole,
|
||||
InfoRole,
|
||||
AmountRole,
|
||||
FactorRole,
|
||||
/// Non user facing
|
||||
IdRole,
|
||||
/// read only roles
|
||||
ToStringRole,
|
||||
ToJsonRole
|
||||
};
|
||||
|
||||
static UserRoles DEFAULT_ROLE = NameRole;
|
||||
// TODO ?rename USER_FACING_ROLES -> MAIN_ROLES ?
|
||||
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, AmountRole,
|
||||
FactorRole};
|
||||
static QHash<int, QByteArray> ROLE_NAMES = {
|
||||
{NameRole, "name"}, {DescriptionRole, "description"}, {InfoRole, "info"},
|
||||
{AmountRole, "amount"}, {FactorRole, "factor"}, {ToStringRole, "ToString"},
|
||||
{IdRole, "id"}};
|
||||
static QList<UserRoles> STRING_ROLES = {NameRole, DescriptionRole, InfoRole, IdRole};
|
||||
static QList<UserRoles> INT_ROLES = {AmountRole};
|
||||
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
|
||||
|
||||
/// JSON keys
|
||||
static const QString ITEMS_KEY_STRING = "items";
|
||||
static const QString ITEM_KEY_STRING = "item";
|
||||
|
||||
/// file naming
|
||||
static const QString ITEMS_FILE_NAME = ITEMS_KEY_STRING + ".json";
|
||||
|
||||
/// functions
|
||||
static int GET_ROLE_FOR_COLUMN(const int column) {
|
||||
switch (column) {
|
||||
case 0:
|
||||
return NameRole;
|
||||
break;
|
||||
case 1:
|
||||
return DescriptionRole;
|
||||
break;
|
||||
case 2:
|
||||
return InfoRole;
|
||||
break;
|
||||
case 3:
|
||||
return AmountRole;
|
||||
break;
|
||||
case 4:
|
||||
return FactorRole;
|
||||
break;
|
||||
default:
|
||||
qWarning() << QString("No role found for column %1! Returning 'NameRole'...").arg(column);
|
||||
return NameRole;
|
||||
break;
|
||||
}
|
||||
}
|
||||
static QList<QString> GET_HEADER_NAMES() {
|
||||
QList<QString> result;
|
||||
for (const UserRoles& role : USER_FACING_ROLES) {
|
||||
const QString headerName = ROLE_NAMES.value(role);
|
||||
result.append(headerName);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif // METADATA_H
|
||||
94
libs/GenericCore/model/modelitem.cpp
Normal file
94
libs/GenericCore/model/modelitem.cpp
Normal file
@ -0,0 +1,94 @@
|
||||
#include "modelitem.h"
|
||||
|
||||
#include "metadata.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
|
||||
ModelItem::ModelItem(const ModelItemValues values)
|
||||
: m_values(values) {}
|
||||
|
||||
QVariant ModelItem::data(int role) const { return m_values.value(role); }
|
||||
|
||||
bool ModelItem::setData(const QVariant& value, int role) {
|
||||
bool valueChanged = false;
|
||||
if (m_values.contains(role)) {
|
||||
if (m_values.value(role) != value) {
|
||||
valueChanged = true;
|
||||
}
|
||||
}
|
||||
m_values[role] = value;
|
||||
|
||||
return valueChanged;
|
||||
}
|
||||
|
||||
bool ModelItem::setItemData(const QMap<int, QVariant>& changedValues) {
|
||||
bool valueChanged = false;
|
||||
|
||||
QMap<int, QVariant>::const_iterator citer = changedValues.constBegin();
|
||||
|
||||
while (citer != changedValues.constEnd()) {
|
||||
const int role = citer.key();
|
||||
const QVariant value = citer.value();
|
||||
|
||||
if (m_values.contains(role)) {
|
||||
if (m_values.value(role) != value) {
|
||||
valueChanged = true;
|
||||
}
|
||||
}
|
||||
m_values[role] = value;
|
||||
|
||||
citer++;
|
||||
}
|
||||
|
||||
return valueChanged;
|
||||
}
|
||||
|
||||
QString ModelItem::toString() const {
|
||||
QString result;
|
||||
|
||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||
while (i.hasNext()) {
|
||||
const UserRoles role = i.next();
|
||||
const QString roleName = ROLE_NAMES.value(role);
|
||||
const QVariant value = data(role);
|
||||
// result.append(value.toString());
|
||||
result.append(QString("%1: %2\n").arg(roleName, data(role).toString()));
|
||||
}
|
||||
|
||||
const UserRoles idRole = IdRole;
|
||||
QVariant idValue = data(idRole);
|
||||
if (!idValue.isNull()) {
|
||||
const QString idRoleName = ROLE_NAMES.value(idRole);
|
||||
result.append(QString("%1: %2\n").arg(idRoleName, idValue.toString()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QJsonObject ModelItem::toJsonObject() const {
|
||||
QJsonObject itemObject;
|
||||
// TODO add UUID and dates (entry, modification, end)
|
||||
const UserRoles idRole = IdRole;
|
||||
QVariant idValue = data(idRole);
|
||||
if (!idValue.isNull()) {
|
||||
const QString idRoleName = ROLE_NAMES.value(idRole);
|
||||
itemObject.insert(idRoleName, idValue.toString());
|
||||
}
|
||||
|
||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||
while (i.hasNext()) {
|
||||
const UserRoles role = i.next();
|
||||
const QString roleName = ROLE_NAMES.value(role);
|
||||
const QVariant value = data(role);
|
||||
if (STRING_ROLES.contains(role)) {
|
||||
itemObject.insert(roleName, value.toString());
|
||||
} else if (INT_ROLES.contains(role)) {
|
||||
itemObject.insert(roleName, value.toInt());
|
||||
} else if (DOUBLE_ROLES.contains(role)) {
|
||||
itemObject.insert(roleName, value.toDouble());
|
||||
} else {
|
||||
qCritical() << QString("Cant find data type of role %1!!!").arg(role);
|
||||
}
|
||||
}
|
||||
return itemObject;
|
||||
}
|
||||
24
libs/GenericCore/model/modelitem.h
Normal file
24
libs/GenericCore/model/modelitem.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef MODELITEM_H
|
||||
#define MODELITEM_H
|
||||
|
||||
#include <QVariant>
|
||||
|
||||
typedef QMap<int, QVariant> ModelItemValues;
|
||||
|
||||
class ModelItem {
|
||||
public:
|
||||
ModelItem(const ModelItemValues values);
|
||||
|
||||
QVariant data(int role) const;
|
||||
bool setData(const QVariant& value, int role);
|
||||
// TODO change return value to list of changed roles
|
||||
bool setItemData(const QMap<int, QVariant>& changedValues);
|
||||
|
||||
QString toString() const;
|
||||
QJsonObject toJsonObject() const;
|
||||
|
||||
private:
|
||||
ModelItemValues m_values;
|
||||
};
|
||||
|
||||
#endif // MODELITEM_H
|
||||
390
libs/GenericCore/model/tablemodel.cpp
Normal file
390
libs/GenericCore/model/tablemodel.cpp
Normal file
@ -0,0 +1,390 @@
|
||||
#include "tablemodel.h"
|
||||
|
||||
#include "../formats/jsonparser.h"
|
||||
#include "commands/edititemcommand.h"
|
||||
#include "commands/insertrowscommand.h"
|
||||
#include "commands/removerowscommand.h"
|
||||
#include "metadata.h"
|
||||
#include "modelitem.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
QByteArray TableModel::generateExampleItems() {
|
||||
QJsonDocument doc = QJsonDocument();
|
||||
QJsonObject rootObject;
|
||||
QJsonArray array;
|
||||
|
||||
// TODO use JsonParser for the item generation
|
||||
for (int row = 0; row < 5; ++row) {
|
||||
QJsonObject itemObject;
|
||||
// itemObject.insert("uuid", m_uuid.toString());
|
||||
// itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate));
|
||||
itemObject.insert(ROLE_NAMES.value(NameRole), QString("Item %1").arg(row));
|
||||
itemObject.insert(ROLE_NAMES.value(DescriptionRole), QString("This is item %1").arg(row));
|
||||
itemObject.insert(ROLE_NAMES.value(InfoRole), QString("Info of item %1").arg(row));
|
||||
itemObject.insert(ROLE_NAMES.value(AmountRole), row);
|
||||
itemObject.insert(ROLE_NAMES.value(FactorRole), row * 1.1);
|
||||
|
||||
array.append(itemObject);
|
||||
}
|
||||
rootObject.insert(ITEMS_KEY_STRING, array);
|
||||
|
||||
doc.setObject(rootObject);
|
||||
return doc.toJson();
|
||||
}
|
||||
|
||||
TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
|
||||
: QAbstractTableModel{parent}
|
||||
, m_undoStack(undoStack) {}
|
||||
|
||||
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
|
||||
if (!index.isValid()) {
|
||||
return QAbstractTableModel::flags(index);
|
||||
}
|
||||
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> TableModel::roleNames() const { return ROLE_NAMES; }
|
||||
|
||||
int TableModel::rowCount(const QModelIndex& parent) const {
|
||||
if (parent.isValid()) {
|
||||
return 0; /// no children
|
||||
}
|
||||
return m_items.size();
|
||||
}
|
||||
|
||||
int TableModel::columnCount(const QModelIndex& parent) const {
|
||||
if (parent.isValid()) {
|
||||
return 0; /// no children
|
||||
}
|
||||
return USER_FACING_ROLES.size();
|
||||
}
|
||||
|
||||
QVariant TableModel::data(const QModelIndex& index, int role) const {
|
||||
const int row = index.row();
|
||||
const int column = index.column();
|
||||
|
||||
if (!index.isValid()) {
|
||||
return QVariant();
|
||||
}
|
||||
if (row >= rowCount(QModelIndex()) || column >= columnCount(QModelIndex())) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
int roleForColumn = GET_ROLE_FOR_COLUMN(column);
|
||||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
case Qt::EditRole:
|
||||
return m_items.at(row)->data(roleForColumn);
|
||||
case NameRole:
|
||||
case DescriptionRole:
|
||||
case InfoRole:
|
||||
case AmountRole:
|
||||
case FactorRole:
|
||||
case IdRole:
|
||||
return m_items.at(row)->data(role);
|
||||
case ToStringRole:
|
||||
return m_items.at(row)->toString();
|
||||
case ToJsonRole:
|
||||
return m_items.at(row)->toJsonObject();
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
||||
if (role == Qt::DisplayRole) {
|
||||
if (orientation == Qt::Horizontal) {
|
||||
const int columnRole = GET_ROLE_FOR_COLUMN(section);
|
||||
const QString headerName = ROLE_NAMES.value(columnRole);
|
||||
return QString("%1").arg(headerName);
|
||||
} else {
|
||||
return QString("%1").arg(section);
|
||||
}
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
||||
if (role == Qt::EditRole && checkIndex(index)) {
|
||||
const int column = index.column();
|
||||
const int roleForColumn = GET_ROLE_FOR_COLUMN(column);
|
||||
return setItemData(index, {{roleForColumn, value}});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TableModel::setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) {
|
||||
if (!checkIndex(index)) {
|
||||
return false;
|
||||
}
|
||||
// if (isRoleReadOnly(roleForColumn)) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
QMap<int, QVariant> changedValues = onlyChangedValues(index, roles);
|
||||
if (changedValues.size() > 0) {
|
||||
EditItemCommand* editCommand = new EditItemCommand(this, index, changedValues);
|
||||
m_undoStack->push(editCommand);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ModelItemValues TableModel::getItemValues(const QModelIndex& index) const {
|
||||
ModelItemValues values;
|
||||
|
||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||
while (i.hasNext()) {
|
||||
const UserRoles role = i.next();
|
||||
values.insert(role, data(index, role));
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
QJsonDocument TableModel::getAllItemsAsJsonDoc() const {
|
||||
QJsonDocument doc = QJsonDocument();
|
||||
QJsonObject rootObject;
|
||||
QJsonArray array;
|
||||
|
||||
foreach (shared_ptr<ModelItem> item, m_items) {
|
||||
QJsonObject itemObject = item->toJsonObject();
|
||||
array.append(itemObject);
|
||||
}
|
||||
rootObject.insert(ITEMS_KEY_STRING, array);
|
||||
|
||||
doc.setObject(rootObject);
|
||||
return doc;
|
||||
}
|
||||
|
||||
QList<QStringList> TableModel::getItemsAsStringLists() const {
|
||||
QList<QStringList> result;
|
||||
foreach (shared_ptr<ModelItem> item, m_items) {
|
||||
QStringList valueList;
|
||||
for (int column = 0; column < columnCount(); ++column) {
|
||||
QString value = item->data(GET_ROLE_FOR_COLUMN(column)).toString();
|
||||
valueList.append(value);
|
||||
}
|
||||
result.append(valueList);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// TODO use item selection as parameter to wrap multiple items into JSON data structure
|
||||
QByteArray TableModel::jsonDataForServer(const QModelIndex& currentIndex) const {
|
||||
const QJsonObject itemObject = data(currentIndex, ToJsonRole).toJsonObject();
|
||||
QJsonObject rootObject;
|
||||
rootObject.insert(ITEM_KEY_STRING, itemObject);
|
||||
const QJsonDocument jsonDoc(rootObject);
|
||||
return jsonDoc.toJson(QJsonDocument::Compact);
|
||||
}
|
||||
|
||||
QString TableModel::updateItemsFromJson(const QByteArray& jsonData) {
|
||||
const QList<ModelItemValues> valueList = JsonParser::toItemValuesList(jsonData, ITEM_KEY_STRING);
|
||||
int nChangedItems = 0;
|
||||
for (const ModelItemValues& itemValues : valueList) {
|
||||
bool updateHappened = updateItem(itemValues);
|
||||
if (updateHappened) {
|
||||
nChangedItems++;
|
||||
}
|
||||
}
|
||||
return QString("Found and updated %1 item(s).").arg(nChangedItems);
|
||||
}
|
||||
|
||||
bool TableModel::updateItem(const ModelItemValues& itemValues) {
|
||||
QModelIndex foundIndex = searchItemIndex(itemValues);
|
||||
|
||||
qDebug() << "Search done!";
|
||||
if (foundIndex == QModelIndex()) {
|
||||
const QString errorMessage = "No matching item found!";
|
||||
qWarning() << errorMessage;
|
||||
return false;
|
||||
} else {
|
||||
qInfo() << "Item found!";
|
||||
/// update existing item
|
||||
setItemData(foundIndex, itemValues);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
|
||||
if (parentIndex != QModelIndex()) {
|
||||
qWarning() << "Removing of child rows is not supported yet!";
|
||||
return false;
|
||||
}
|
||||
|
||||
const int lastRow = firstRow + nRows - 1;
|
||||
if (firstRow < 0 || lastRow >= m_items.size()) {
|
||||
qWarning() << "Trying to remove rows is out of bounds!";
|
||||
return false;
|
||||
}
|
||||
|
||||
RemoveRowsCommand* removeCommand = new RemoveRowsCommand(this, firstRow, nRows);
|
||||
m_undoStack->push(removeCommand);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TableModel::appendItems(const QByteArray& jsonDoc) { insertItems(-1, jsonDoc, QModelIndex()); }
|
||||
|
||||
void TableModel::insertItems(int startPosition,
|
||||
const QByteArray& jsonDoc,
|
||||
const QModelIndex& parentIndex) {
|
||||
const QList<ModelItemValues> valueList = JsonParser::toItemValuesList(jsonDoc, ITEMS_KEY_STRING);
|
||||
|
||||
if (valueList.empty()) {
|
||||
/// don't try inserting if no values to insert
|
||||
qDebug() << "No items found in JSON document. Not adding anything...";
|
||||
return;
|
||||
}
|
||||
|
||||
insertItems(startPosition, valueList, parentIndex);
|
||||
}
|
||||
|
||||
void TableModel::insertItems(int startPosition,
|
||||
const QList<ModelItemValues>& itemValuesList,
|
||||
const QModelIndex& parentIndex) {
|
||||
qInfo() << "Inserting item(s) into model...";
|
||||
if (parentIndex != QModelIndex()) {
|
||||
qWarning()
|
||||
<< "Using invalid parent index (no child support for now)! Using root index as parent...";
|
||||
}
|
||||
if (startPosition == -1 || startPosition > m_items.size()) {
|
||||
/// Appending item(s)
|
||||
startPosition = m_items.size();
|
||||
}
|
||||
|
||||
InsertRowsCommand* insertCommand = new InsertRowsCommand(this, startPosition, itemValuesList);
|
||||
m_undoStack->push(insertCommand);
|
||||
}
|
||||
|
||||
void TableModel::execInsertItems(const int firstRow, const QList<ModelItemValues> valueList) {
|
||||
const int nRows = valueList.size();
|
||||
qDebug() << "Inserting" << nRows << "items...";
|
||||
|
||||
const int lastRow = firstRow + nRows - 1;
|
||||
beginInsertRows(QModelIndex(), firstRow, lastRow);
|
||||
for (int row = 0; row < nRows; ++row) {
|
||||
const int rowPosition = firstRow + row;
|
||||
shared_ptr<ModelItem> item = make_unique<ModelItem>(valueList.at(row));
|
||||
m_items.insert(rowPosition, std::move(item));
|
||||
}
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void TableModel::execRemoveItems(const int firstRow, const int nRows) {
|
||||
const int lastRow = firstRow + nRows - 1;
|
||||
beginRemoveRows(QModelIndex(), firstRow, lastRow);
|
||||
m_items.remove(firstRow, nRows);
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
void TableModel::execEditItemData(const int row, const QMap<int, QVariant>& changedValues) {
|
||||
shared_ptr<ModelItem> item = m_items.at(row);
|
||||
bool isDataChanged = item->setItemData(changedValues);
|
||||
|
||||
if (isDataChanged) {
|
||||
/// FIXME due to the mapping from roles to the DisplayRole of different columns the complete row
|
||||
/// is getting notified about (potential) data changes; dataChanged should be called only for
|
||||
/// the affected columns
|
||||
const QModelIndex firstIndex = this->index(row, 0);
|
||||
const QModelIndex lastIndex = this->index(row, USER_FACING_ROLES.size() - 1);
|
||||
QList<int> roles = changedValues.keys();
|
||||
roles.insert(0, Qt::DisplayRole);
|
||||
emit dataChanged(firstIndex, lastIndex, roles.toVector());
|
||||
}
|
||||
}
|
||||
|
||||
QMap<int, QVariant> TableModel::onlyChangedValues(const QModelIndex& index,
|
||||
const QMap<int, QVariant>& roleValueMap) const {
|
||||
QMap<int, QVariant> result;
|
||||
QMap<int, QVariant>::const_iterator i;
|
||||
for (i = roleValueMap.constBegin(); i != roleValueMap.constEnd(); ++i) {
|
||||
const int role = i.key();
|
||||
const QVariant newValue = i.value();
|
||||
const QVariant oldValue = index.data(role);
|
||||
// TODO check if role is a editable role?
|
||||
if (oldValue != newValue) {
|
||||
bool emptyValueIsEqualToZero = isEmptyValueEqualToZero(role);
|
||||
if (emptyValueIsEqualToZero && oldValue == QVariant() && newValue == 0) {
|
||||
qDebug() << "oldValue:" << oldValue << "& newValue:" << newValue
|
||||
<< "mean the same. Ignoring...";
|
||||
continue;
|
||||
}
|
||||
qDebug() << "oldValue:" << oldValue << "!= newValue:" << newValue;
|
||||
result.insert(role, newValue);
|
||||
} else {
|
||||
qInfo() << "oldValue is already the same as newValue:" << oldValue << "-> ignoring...";
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool TableModel::isEmptyValueEqualToZero(const int role) const {
|
||||
if (INT_ROLES.contains(role)) {
|
||||
return true;
|
||||
} else if (DOUBLE_ROLES.contains(role)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QModelIndex TableModel::searchItemIndex(const ModelItemValues givenItemValues) const {
|
||||
// iterate over indexes to search item : see searchItem(...);
|
||||
// for (const shared_ptr<ModelItem>& item : m_items) {
|
||||
for (int row = 0; row < rowCount(); ++row) {
|
||||
qDebug() << "Processing item at row" << row << "...";
|
||||
QModelIndex itemIndex = index(row, 0);
|
||||
if (isItemEqualToItemValues(itemIndex, givenItemValues)) {
|
||||
qInfo() << "Found item at row" << row << "! Returning its index...";
|
||||
return itemIndex;
|
||||
}
|
||||
}
|
||||
qDebug() << "No matching item found. Returning empty pointer...";
|
||||
return {};
|
||||
}
|
||||
|
||||
bool TableModel::isItemEqualToItemValues(const QModelIndex& itemIndex,
|
||||
const ModelItemValues givenItemValues) const {
|
||||
/// do both have a UUID?
|
||||
QVariant idOfItem = data(itemIndex, IdRole);
|
||||
QVariant given = givenItemValues.value(IdRole);
|
||||
if (idOfItem.isValid() && given.isValid()) {
|
||||
/// are the UUIDs the same?
|
||||
if (idOfItem.toString() == given.toString()) {
|
||||
qInfo() << "UUIDs are the same.";
|
||||
return true;
|
||||
} else {
|
||||
qDebug() << "UUIDs are NOT the same.";
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
/// are all other values the same? (for now only USER_FACING_ROLES are checked)
|
||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||
while (i.hasNext()) {
|
||||
const UserRoles role = i.next();
|
||||
const QString roleName = ROLE_NAMES.value(role);
|
||||
const QVariant valueOfItem = data(itemIndex, role);
|
||||
const QVariant givenValue = givenItemValues.value(role);
|
||||
if (STRING_ROLES.contains(role)) {
|
||||
if (valueOfItem.toString() != givenValue.toString()) {
|
||||
return false;
|
||||
}
|
||||
} else if (INT_ROLES.contains(role)) {
|
||||
if (valueOfItem.toInt() != givenValue.toInt()) {
|
||||
return false;
|
||||
}
|
||||
} else if (DOUBLE_ROLES.contains(role)) {
|
||||
if (valueOfItem.toDouble() != givenValue.toDouble()) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
qCritical() << QString("Can't find data type for role %1!!!").arg(role);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
79
libs/GenericCore/model/tablemodel.h
Normal file
79
libs/GenericCore/model/tablemodel.h
Normal file
@ -0,0 +1,79 @@
|
||||
#ifndef TABLEMODEL_H
|
||||
#define TABLEMODEL_H
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
|
||||
class QUndoStack;
|
||||
class ModelItem;
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef QMap<int, QVariant> ModelItemValues;
|
||||
|
||||
class TableModel : public QAbstractTableModel {
|
||||
Q_OBJECT
|
||||
|
||||
friend class InsertRowsCommand;
|
||||
friend class RemoveRowsCommand;
|
||||
friend class EditItemCommand;
|
||||
|
||||
public:
|
||||
static QByteArray generateExampleItems();
|
||||
|
||||
explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr);
|
||||
|
||||
/// QAbstractItemModel interface
|
||||
Qt::ItemFlags flags(const QModelIndex& index) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex& index, int role) const override;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||
|
||||
bool setData(const QModelIndex& index, const QVariant& value, int role) override;
|
||||
bool setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) override;
|
||||
|
||||
ModelItemValues getItemValues(const QModelIndex& index) const;
|
||||
QJsonDocument getAllItemsAsJsonDoc() const;
|
||||
QList<QStringList> getItemsAsStringLists() const;
|
||||
|
||||
QByteArray jsonDataForServer(const QModelIndex& currentIndex) const;
|
||||
|
||||
QString updateItemsFromJson(const QByteArray& jsonData);
|
||||
bool updateItem(const ModelItemValues& itemValues);
|
||||
|
||||
public slots:
|
||||
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
||||
// override;
|
||||
bool removeRows(int firstRow, int nRows, const QModelIndex& parentIndex = QModelIndex()) override;
|
||||
void appendItems(const QByteArray& jsonDoc);
|
||||
void insertItems(int startPosition,
|
||||
const QByteArray& jsonDoc,
|
||||
const QModelIndex& parentIndex = QModelIndex());
|
||||
void insertItems(int startPosition,
|
||||
const QList<ModelItemValues>& itemValuesList,
|
||||
const QModelIndex& parentIndex = QModelIndex());
|
||||
|
||||
private:
|
||||
/// *** members ***
|
||||
// TODO shared_ptr -> unique_ptr
|
||||
QList<shared_ptr<ModelItem>> m_items;
|
||||
QUndoStack* m_undoStack;
|
||||
|
||||
/// *** functions ***
|
||||
/// undo/redo functions
|
||||
void execInsertItems(const int firstRow, const QList<ModelItemValues> valueList);
|
||||
void execRemoveItems(const int firstRow, const int nRows);
|
||||
void execEditItemData(const int row, const QMap<int, QVariant>& changedValues);
|
||||
|
||||
/// misc functions
|
||||
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
|
||||
const QMap<int, QVariant>& roleValueMap) const;
|
||||
bool isEmptyValueEqualToZero(const int role) const;
|
||||
QModelIndex searchItemIndex(const ModelItemValues givenItemValues) const;
|
||||
bool isItemEqualToItemValues(const QModelIndex& itemIndex,
|
||||
const ModelItemValues givenItemValues) const;
|
||||
};
|
||||
|
||||
#endif // TABLEMODEL_H
|
||||
Reference in New Issue
Block a user