Print subtests 2, 3 and 4

This commit is contained in:
Michael Mandl 2018-12-08 21:02:35 +01:00
parent e2237110cf
commit 49b52f1dbc
21 changed files with 223 additions and 201 deletions

View file

@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.6)
project(CheckableTestModel LANGUAGES CXX)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
add_library(${PROJECT_NAME}
CheckableTestModel.cpp
@ -22,4 +23,5 @@ target_link_libraries(${PROJECT_NAME}
CheckableItem
CheckableTest
Qt5::Core
Qt5::Gui
)

View file

@ -2,16 +2,17 @@
#include <QJsonArray>
#include <QSize>
#include <QTextTable>
#include <QDebug>
CheckableTestModel::CheckableTestModel(QObject *parent)
: QAbstractTableModel(parent)
: QAbstractTableModel(parent)
{
}
int CheckableTestModel::rowCount(const QModelIndex &) const
{
return static_cast<int>(m_tests.size());
return static_cast<int>(m_tests.size());
}
int CheckableTestModel::columnCount(const QModelIndex &) const
@ -20,7 +21,7 @@ int CheckableTestModel::columnCount(const QModelIndex &) const
for (const auto &test : m_tests)
{
columnCount = std::max(columnCount, static_cast<int>(test.size()));
columnCount = std::max(columnCount, static_cast<int>(test.size()));
}
return columnCount;
@ -37,17 +38,17 @@ QVariant CheckableTestModel::data(const QModelIndex &index, int role) const
{
auto &item = getItem(index);
switch (role)
{
case Qt::DisplayRole:
{
return item.getText().c_str();
}
case Qt::CheckStateRole:
{
return item.isChecked() ? Qt::Checked : Qt::Unchecked;
}
}
switch (role)
{
case Qt::DisplayRole:
{
return item.getText().c_str();
}
case Qt::CheckStateRole:
{
return item.isChecked() ? Qt::Checked : Qt::Unchecked;
}
}
}
catch (std::runtime_error &e)
{
@ -67,8 +68,7 @@ Qt::ItemFlags CheckableTestModel::flags(const QModelIndex &index) const
return Qt::NoItemFlags;
}
bool CheckableTestModel::setData(
const QModelIndex &index, const QVariant &value, int role)
bool CheckableTestModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!isValidIndex(index))
{
@ -93,8 +93,7 @@ bool CheckableTestModel::setData(
return false;
}
QVariant CheckableTestModel::headerData(
int section, Qt::Orientation orientation, int role) const
QVariant CheckableTestModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Vertical)
{
@ -104,7 +103,7 @@ QVariant CheckableTestModel::headerData(
}
}
return QAbstractTableModel::headerData(section, orientation, role);
return QAbstractTableModel::headerData(section, orientation, role);
}
bool CheckableTestModel::isValidIndex(const QModelIndex &index) const
@ -117,6 +116,129 @@ bool CheckableTestModel::isValidIndex(const QModelIndex &index) const
return false;
}
void CheckableTestModel::printTo(QTextCursor &cursor) const
{
QTextCharFormat headerFormat;
headerFormat.setFontPointSize(10);
cursor.insertBlock();
cursor.insertText("\n");
cursor.insertText(getName().c_str(), headerFormat);
printTableTo(cursor);
cursor.movePosition(QTextCursor::End);
cursor.insertBlock();
cursor.insertText("\n");
printSummaryTo(cursor);
cursor.movePosition(QTextCursor::End);
}
void CheckableTestModel::printTableTo(QTextCursor &cursor) const
{
QTextTableFormat tableFormat;
tableFormat.setCellPadding(2);
tableFormat.setCellSpacing(0);
tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 20),
QTextLength(QTextLength::PercentageLength, 9),
QTextLength(QTextLength::PercentageLength, 9),
QTextLength(QTextLength::PercentageLength, 9),
QTextLength(QTextLength::PercentageLength, 9),
QTextLength(QTextLength::PercentageLength, 9),
QTextLength(QTextLength::PercentageLength, 9),
QTextLength(QTextLength::PercentageLength, 9),
QTextLength(QTextLength::PercentageLength, 9),
QTextLength(QTextLength::PercentageLength, 1),
QTextLength(QTextLength::PercentageLength, 3),
QTextLength(QTextLength::PercentageLength, 1),
QTextLength(QTextLength::PercentageLength, 3)});
QTextTable *table = cursor.insertTable(m_tests.size() * 2, 13, tableFormat);
const char *emptyBox = "\u2610";
//const char *checkBox = "\u2611";
const char *checkBox = "x";
auto insertText = [&table](int row, int column, const QString &text) {
auto cell = table->cellAt(row, column);
auto textCursor = cell.firstCursorPosition();
auto blockFormat = textCursor.blockFormat();
blockFormat.setAlignment(Qt::AlignCenter);
textCursor.setBlockFormat(blockFormat);
auto cellFormat = cell.format();
cellFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle);
cell.setFormat(cellFormat);
auto charFormat = textCursor.charFormat();
charFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle);
charFormat.setFontPointSize(8);
textCursor.setCharFormat(charFormat);
textCursor.insertText(text);
};
int currentRow = 0;
for (const auto &test : m_tests)
{
table->mergeCells(currentRow, 0, 2, 1);
int currentColumn = 0;
insertText(currentRow, currentColumn, test.name());
currentColumn++;
for (const auto &item : test.items())
{
insertText(currentRow, currentColumn, item.getText().c_str());
insertText(currentRow + 1, currentColumn, item.isChecked() ? checkBox : emptyBox);
currentColumn++;
}
insertText(currentRow + 1, 12, QString::number(test.getPoints()));
currentRow += 2;
}
}
void CheckableTestModel::printSummaryTo(QTextCursor &cursor) const
{
QTextTableFormat tableFormat;
tableFormat.setCellPadding(2);
tableFormat.setCellSpacing(0);
tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 76),
QTextLength(QTextLength::PercentageLength, 20),
QTextLength(QTextLength::PercentageLength, 1),
QTextLength(QTextLength::PercentageLength, 3)});
QTextTable *table = cursor.insertTable(1, 4, tableFormat);
auto insertText = [&table](int row, int column, const QString &text) {
auto cell = table->cellAt(row, column);
auto textCursor = cell.firstCursorPosition();
auto blockFormat = textCursor.blockFormat();
blockFormat.setAlignment(Qt::AlignCenter);
textCursor.setBlockFormat(blockFormat);
auto cellFormat = cell.format();
cellFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle);
cell.setFormat(cellFormat);
auto charFormat = textCursor.charFormat();
charFormat.setVerticalAlignment(QTextCharFormat::AlignMiddle);
charFormat.setFontPointSize(8);
textCursor.setCharFormat(charFormat);
textCursor.insertText(text);
};
insertText(0, 1, "Rohwertpunkte Total:");
insertText(0, 3, QString::number(getPoints()));
}
CheckableItems &CheckableTestModel::getItems(const QModelIndex &index)
{
if (index.row() < m_tests.size())
@ -127,8 +249,7 @@ CheckableItems &CheckableTestModel::getItems(const QModelIndex &index)
throw std::runtime_error("invalid index");
}
const CheckableItems &CheckableTestModel::getItems(
const QModelIndex &index) const
const CheckableItems &CheckableTestModel::getItems(const QModelIndex &index) const
{
if (index.row() < m_tests.size())
{
@ -162,7 +283,7 @@ const CheckableItem &CheckableTestModel::getItem(const QModelIndex &index) const
unsigned int CheckableTestModel::getPoints() const
{
unsigned int points = 0;
unsigned int points = 0;
for (const auto &test : m_tests)
{

View file

@ -2,6 +2,7 @@
#include "CheckableTest.h"
#include <QAbstractTableModel>
#include <QTextCursor>
class CheckableTestModel : public QAbstractTableModel
{
@ -27,9 +28,15 @@ public:
unsigned int getPoints() const;
virtual void printTo(QTextCursor &cursor) const;
protected:
virtual bool isValidIndex(const QModelIndex &index) const;
virtual std::string getName() const = 0;
void printTableTo(QTextCursor &cursor) const;
void printSummaryTo(QTextCursor &cursor) const;
private:
CheckableItems &getItems(const QModelIndex &index);
const CheckableItems &getItems(const QModelIndex &index) const;