refactored common test-model functions to base class
This commit is contained in:
parent
3ad5d3bd57
commit
8f170eb83b
14 changed files with 353 additions and 480 deletions
25
source/CheckableTestModel/CMakeLists.txt
Normal file
25
source/CheckableTestModel/CMakeLists.txt
Normal file
|
@ -0,0 +1,25 @@
|
|||
cmake_minimum_required(VERSION 3.6)
|
||||
|
||||
project(CheckableTestModel LANGUAGES CXX)
|
||||
|
||||
find_package(Qt5Core REQUIRED)
|
||||
|
||||
add_library(${PROJECT_NAME}
|
||||
CheckableTestModel.cpp
|
||||
)
|
||||
|
||||
set_target_properties(${PROJECT_NAME}
|
||||
PROPERTIES CXX_STANDARD 14
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
CheckableItem
|
||||
CheckableTest
|
||||
Qt5::Core
|
||||
)
|
179
source/CheckableTestModel/CheckableTestModel.cpp
Normal file
179
source/CheckableTestModel/CheckableTestModel.cpp
Normal file
|
@ -0,0 +1,179 @@
|
|||
#include "CheckableTestModel.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QDebug>
|
||||
|
||||
CheckableTestModel::CheckableTestModel(QObject *parent)
|
||||
: QAbstractTableModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
int CheckableTestModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
return m_tests.size();
|
||||
}
|
||||
|
||||
int CheckableTestModel::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
int columnCount = 0;
|
||||
|
||||
for (const auto &test : m_tests)
|
||||
{
|
||||
columnCount = std::max<int>(columnCount, test.size());
|
||||
}
|
||||
|
||||
return columnCount;
|
||||
}
|
||||
|
||||
QVariant CheckableTestModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!isValidIndex(index))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
auto &item = getItem(index);
|
||||
|
||||
if (role == Qt::DisplayRole)
|
||||
{
|
||||
return item.getText().c_str();
|
||||
}
|
||||
|
||||
if (role == Qt::CheckStateRole)
|
||||
{
|
||||
return item.isChecked() ? Qt::Checked : Qt::Unchecked;
|
||||
}
|
||||
}
|
||||
catch (std::runtime_error &e)
|
||||
{
|
||||
qDebug() << "CheckableTestModel::data" << index << e.what();
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
Qt::ItemFlags CheckableTestModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (isValidIndex(index))
|
||||
{
|
||||
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
|
||||
}
|
||||
|
||||
return Qt::NoItemFlags;
|
||||
}
|
||||
|
||||
bool CheckableTestModel::setData(
|
||||
const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (!isValidIndex(index))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (role == Qt::CheckStateRole)
|
||||
{
|
||||
auto &item = getItem(index);
|
||||
item.setState(value.toBool());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (std::runtime_error &e)
|
||||
{
|
||||
qDebug() << "CheckableTestModel::setData" << index << e.what();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QVariant CheckableTestModel::headerData(
|
||||
int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (role == Qt::DisplayRole && orientation == Qt::Vertical)
|
||||
{
|
||||
if (section < m_tests.size())
|
||||
{
|
||||
return m_tests.at(section).name();
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void CheckableTestModel::write(QJsonObject &json) const
|
||||
{
|
||||
for (const auto &test : m_tests)
|
||||
{
|
||||
QJsonArray testData;
|
||||
test.items().write(testData);
|
||||
json[test.name()] = testData;
|
||||
}
|
||||
}
|
||||
|
||||
void CheckableTestModel::read(const QJsonObject &json)
|
||||
{
|
||||
for (auto &test : m_tests)
|
||||
{
|
||||
auto testData = json[test.name()];
|
||||
if (testData.isArray())
|
||||
{
|
||||
test.items().read(testData.toArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CheckableTestModel::isValidIndex(const QModelIndex &index) const
|
||||
{
|
||||
if (index.row() < m_tests.size())
|
||||
{
|
||||
return index.column() < m_tests.at(index.row()).size();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
CheckableItems &CheckableTestModel::getItems(const QModelIndex &index)
|
||||
{
|
||||
if (index.row() < m_tests.size())
|
||||
{
|
||||
return m_tests.at(index.row()).items();
|
||||
}
|
||||
|
||||
throw std::runtime_error("invalid index");
|
||||
}
|
||||
|
||||
const CheckableItems &CheckableTestModel::getItems(
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.row() < m_tests.size())
|
||||
{
|
||||
return m_tests.at(index.row()).items();
|
||||
}
|
||||
|
||||
throw std::runtime_error("invalid index");
|
||||
}
|
||||
|
||||
CheckableItem &CheckableTestModel::getItem(const QModelIndex &index)
|
||||
{
|
||||
auto &items = getItems(index);
|
||||
if (index.column() < items.size())
|
||||
{
|
||||
return items.at(index.column());
|
||||
}
|
||||
|
||||
throw std::runtime_error("invalid index");
|
||||
}
|
||||
|
||||
const CheckableItem &CheckableTestModel::getItem(const QModelIndex &index) const
|
||||
{
|
||||
auto &items = getItems(index);
|
||||
if (index.column() < items.size())
|
||||
{
|
||||
return items.at(index.column());
|
||||
}
|
||||
|
||||
throw std::runtime_error("invalid index");
|
||||
}
|
40
source/CheckableTestModel/CheckableTestModel.h
Normal file
40
source/CheckableTestModel/CheckableTestModel.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include "CheckableTest.h"
|
||||
#include <QAbstractTableModel>
|
||||
|
||||
class CheckableTestModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
protected:
|
||||
CheckableTests m_tests;
|
||||
|
||||
public:
|
||||
CheckableTestModel(QObject *parent);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
|
||||
QVariant data(
|
||||
const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value,
|
||||
int role = Qt::EditRole) override;
|
||||
|
||||
QVariant headerData(int section, Qt::Orientation orientation,
|
||||
int role = Qt::DisplayRole) const override;
|
||||
|
||||
void write(QJsonObject &json) const;
|
||||
void read(const QJsonObject &json);
|
||||
|
||||
private:
|
||||
bool isValidIndex(const QModelIndex &index) const;
|
||||
|
||||
CheckableItems &getItems(const QModelIndex &index);
|
||||
const CheckableItems &getItems(const QModelIndex &index) const;
|
||||
|
||||
CheckableItem &getItem(const QModelIndex &index);
|
||||
const CheckableItem &getItem(const QModelIndex &index) const;
|
||||
};
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue