Commit 1dce31e3c00a8340af160b80344d44000ca76e78
1 parent
1a51a0a3
Setting up tablemodel
Showing
6 changed files
with
230 additions
and
0 deletions
.gitignore
src/CMakeLists.txt
src/tablemodel/modeldataitem.cpp
0 → 100644
| 1 | +#include "modeldataitem.h" | |
| 2 | + | |
| 3 | +using namespace osdev::components::datamodels; | |
| 4 | + | |
| 5 | +ModelDataItem::ModelDataItem() | |
| 6 | +{ | |
| 7 | + | |
| 8 | +} | |
| 9 | + | |
| 10 | +ModelDataItem::ModelDataItem(const QHash<QString, QVariant> &data_item) | |
| 11 | + : m_itemHash() | |
| 12 | +{ | |
| 13 | + for(const auto &key : data_item.keys()) | |
| 14 | + { | |
| 15 | + m_itemHash.insert(key, data_item.value(key)); | |
| 16 | + } | |
| 17 | +} | |
| 18 | + | |
| 19 | +void ModelDataItem::setData(const QString &name, const QVariant &value) | |
| 20 | +{ | |
| 21 | + if(!name.isEmpty() && !name.isNull()) | |
| 22 | + { | |
| 23 | + m_itemHash.insert(name, value); | |
| 24 | + } | |
| 25 | +} | |
| 26 | + | |
| 27 | +QVariant ModelDataItem::getData(const QString &name) const | |
| 28 | +{ | |
| 29 | + if(!name.isEmpty() && !name.isNull() && m_itemHash.contains(name)) | |
| 30 | + { | |
| 31 | + return m_itemHash.value(name); | |
| 32 | + } | |
| 33 | + else | |
| 34 | + return QVariant(); | |
| 35 | +} | |
| 36 | + | |
| 37 | +int ModelDataItem::items() | |
| 38 | +{ | |
| 39 | + return m_itemHash.count(); | |
| 40 | +} | |
| 41 | + | |
| 42 | +QString ModelDataItem::asString() | |
| 43 | +{ | |
| 44 | + QString model_data_item; | |
| 45 | + | |
| 46 | + for(const auto &key : m_itemHash.keys()) | |
| 47 | + { | |
| 48 | + model_data_item.append(key); | |
| 49 | + model_data_item.append(" : "); | |
| 50 | + model_data_item.append( m_itemHash.value(key).toString()); | |
| 51 | + model_data_item.append(" | "); | |
| 52 | + } | |
| 53 | + return model_data_item; | |
| 54 | +} | ... | ... |
src/tablemodel/modeldataitem.h
0 → 100644
| 1 | +#pragma once | |
| 2 | + | |
| 3 | +#include <QHash> | |
| 4 | +#include <QString> | |
| 5 | +#include <QVariant> | |
| 6 | + | |
| 7 | +namespace osdev { | |
| 8 | +namespace components { | |
| 9 | +namespace datamodels { | |
| 10 | + | |
| 11 | +class ModelDataItem | |
| 12 | +{ | |
| 13 | +public: | |
| 14 | + ModelDataItem(); | |
| 15 | + ModelDataItem(const QHash<QString, QVariant> &data_item); | |
| 16 | + | |
| 17 | + void setData(const QString &name, const QVariant &value); | |
| 18 | + QVariant getData(const QString &name) const; | |
| 19 | + int items(); | |
| 20 | + QStringList getKeys() { return m_itemHash.keys(); } | |
| 21 | + | |
| 22 | + QString asString(); | |
| 23 | + | |
| 24 | +private: | |
| 25 | + QHash<QString, QVariant> m_itemHash; | |
| 26 | +}; | |
| 27 | + | |
| 28 | +} // End namespace datamodels | |
| 29 | +} // End namespace components | |
| 30 | +} // End namespace osdev | ... | ... |
src/tablemodel/tablemodel.cpp
| 1 | +#include "tablemodel.h" | |
| 2 | + | |
| 3 | +using namespace osdev::components::datamodels; | |
| 4 | + | |
| 5 | +TableModelBase::TableModelBase(QObject *parent) | |
| 6 | +{ | |
| 7 | + | |
| 8 | +} | |
| 9 | + | |
| 10 | +int TableModelBase::rowCount(const QModelIndex &parent) const | |
| 11 | +{ | |
| 12 | + | |
| 13 | +} | |
| 14 | + | |
| 15 | +int TableModelBase::columnCount(const QModelIndex &parent) const | |
| 16 | +{ | |
| 17 | + | |
| 18 | +} | |
| 19 | + | |
| 20 | +QVariant TableModelBase::headerData(int section, Qt::Orientation orientation, int role) const | |
| 21 | +{ | |
| 22 | + | |
| 23 | +} | |
| 24 | + | |
| 25 | +QVariant TableModelBase::data(const QModelIndex &index, int role) const | |
| 26 | +{ | |
| 27 | + | |
| 28 | +} | |
| 29 | + | |
| 30 | +void TableModelBase::addHeaderColumn(const QString &headerName, const QString &db_field_name) | |
| 31 | +{ | |
| 32 | + | |
| 33 | +} | |
| 34 | + | |
| 35 | +QStringList TableModelBase::getHeaderNames() | |
| 36 | +{ | |
| 37 | + | |
| 38 | +} | |
| 39 | + | |
| 40 | +QString TableModelBase::getHeaderFieldByName( const QString &headerName) const | |
| 41 | +{ | |
| 42 | + | |
| 43 | +} | |
| 44 | + | |
| 45 | +QString TableModelBase::getHeaderColumnByField( const QString &db_field_name ) const | |
| 46 | +{ | |
| 47 | + | |
| 48 | +} | |
| 49 | + | |
| 50 | +QString TableModelBase::getHeaderFieldByIndex( int index ) const | |
| 51 | +{ | |
| 52 | + | |
| 53 | +} | |
| 54 | + | |
| 55 | +QString TableModelBase::getHeaderNameByIndex( int index ) const | |
| 56 | +{ | |
| 57 | + | |
| 58 | +} | |
| 59 | + | |
| 60 | +int TableModelBase::getHeaderIndexByName( const QString &header_name ) const | |
| 61 | +{ | |
| 62 | + | |
| 63 | +} | |
| 64 | + | |
| 65 | +bool TableModelBase::setHeaderData( int section, Qt::Orientation orientation, const QVariant &value, int role) | |
| 66 | +{ | |
| 67 | + | |
| 68 | +} | |
| 69 | + | |
| 70 | +void TableModelBase::setKeyFields( const QStringList &key_fields ) | |
| 71 | +{ | |
| 72 | + | |
| 73 | +} | |
| 74 | + | |
| 75 | +// void fillList(const &DataObject &data_object); | |
| 76 | +// void addRecord(const ModelDataItem &data_item); | |
| 77 | + | |
| 78 | +void clear(); | |
| 79 | + | |
| 80 | +QString exportDataAsString(); | |
| 81 | +QStringList exportDataAsList(); | ... | ... |
src/tablemodel/tablemodel.h
| 1 | +#pragma once | |
| 2 | + | |
| 3 | +// Qt | |
| 4 | +#include <QAbstractTableModel> | |
| 5 | + | |
| 6 | +// Local | |
| 7 | +#include "modeldataitem.h" | |
| 8 | + | |
| 9 | +namespace osdev { | |
| 10 | +namespace components { | |
| 11 | +namespace datamodels { | |
| 12 | + | |
| 13 | +/*! | |
| 14 | + * \brief The TableModelBase class is the generic way of presenting a table in memory. | |
| 15 | + * It can be attached to different view-objects in a GUI. | |
| 16 | + */ | |
| 17 | +class TableModelBase : public QAbstractTableModel | |
| 18 | +{ | |
| 19 | + Q_OBJECT | |
| 20 | + | |
| 21 | +public: | |
| 22 | + TableModelBase(QObject *parent = nullptr); | |
| 23 | + int rowCount(const QModelIndex &parent = QModelIndex()) const override; | |
| 24 | + int columnCount(const QModelIndex &parent = QModelIndex()) const override; | |
| 25 | + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; | |
| 26 | + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; | |
| 27 | + | |
| 28 | + void addHeaderColumn(const QString &headerName, const QString &db_field_name = QString()); | |
| 29 | + | |
| 30 | + QStringList getHeaderNames(); | |
| 31 | + QString getHeaderFieldByName( const QString &headerName) const; | |
| 32 | + QString getHeaderColumnByField( const QString &db_field_name ) const; | |
| 33 | + QString getHeaderFieldByIndex( int index ) const; | |
| 34 | + QString getHeaderNameByIndex( int index ) const; | |
| 35 | + int getHeaderIndexByName( const QString &header_name ) const; | |
| 36 | + | |
| 37 | + bool setHeaderData( int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole ) override; | |
| 38 | + void setKeyFields( const QStringList &key_fields ); | |
| 39 | + const QStringList& getKeyFields() { return m_keyFields; } | |
| 40 | + | |
| 41 | + // void fillList(const &DataObject &data_object); | |
| 42 | + // void addRecord(const ModelDataItem &data_item); | |
| 43 | + | |
| 44 | + void clear(); | |
| 45 | + | |
| 46 | + QString exportDataAsString(); | |
| 47 | + QStringList exportDataAsList(); | |
| 48 | + | |
| 49 | +signals: | |
| 50 | + void signalHideLoading(); | |
| 51 | + | |
| 52 | +private: // Members ( Giggity! ) | |
| 53 | + QStringList m_keyFields; | |
| 54 | + QList<QPair<QString, QString>> m_headers; | |
| 55 | + QStringList m_keyList; | |
| 56 | + QHash<QString, ModelDataItem> m_qhModelData; | |
| 57 | + | |
| 58 | +}; | |
| 59 | + | |
| 60 | +} // End namespace datamodels | |
| 61 | +} // End namespace components | |
| 62 | +} // End namespace osdev | ... | ... |