Add PeriphParamDelegate to PeriphParamModel
The delegate will translate all of the parameter constraints (such as ranges) to a suitable editor for the type.
Currently, only integer data types are supported for parameters, will add more when (if) peripheral parameters get more complex.
MODIFY
src/io/iobase.h
src/io/iobase.h
@@ -93,3 +93,5 @@ protected:
}
};
} // namespace Ripes
+
+Q_DECLARE_METATYPE(Ripes::IOParam);

MODIFY
src/io/ioledmatrix.cpp
src/io/ioledmatrix.cpp
@@ -9,8 +9,8 @@ IOLedMatrix::IOLedMatrix(QWidget* parent) : IOBase(parent) {
constexpr unsigned defaultWidth = 25;
// Parameters
- m_parameters[HEIGHT] = IOParam(WIDTH, "Height", defaultWidth, true, 0, m_maxSideWidth);
- m_parameters[WIDTH] = IOParam(WIDTH, "Width", defaultWidth + 10, true, 0, m_maxSideWidth);
+ m_parameters[HEIGHT] = IOParam(WIDTH, "Height", defaultWidth, true, 1, m_maxSideWidth);
+ m_parameters[WIDTH] = IOParam(WIDTH, "Width", defaultWidth + 10, true, 1, m_maxSideWidth);
m_parameters[SIZE] = IOParam(SIZE, "LED size", 8, true, 1, 100);
m_pen.setWidth(1);

MODIFY
src/io/periphparammodel.cpp
src/io/periphparammodel.cpp
#include "periphparammodel.h"
#include <QHeaderView>
+#include <QSpinBox>
#include "iobase.h"
#include "processorhandler.h"
namespace Ripes {
+PeriphParamDelegate::PeriphParamDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
+
+QWidget* PeriphParamDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&,
+ const QModelIndex& index) const {
+ const IOParam param = index.data(Qt::UserRole).value<IOParam>();
+ QSpinBox* editor = new QSpinBox(parent);
+ if (param.hasRange) {
+ editor->setRange(param.min.toUInt(), param.max.toUInt());
+ }
+ return editor;
+}
+
+void PeriphParamDelegate::setEditorData(QWidget* e, const QModelIndex& index) const {
+ QSpinBox* editor = dynamic_cast<QSpinBox*>(e);
+ if (editor) {
+ const IOParam param = index.data(Qt::UserRole).value<IOParam>();
+ editor->setValue(param.value.toUInt());
+ }
+}
+
+void PeriphParamDelegate::setModelData(QWidget* e, QAbstractItemModel* model, const QModelIndex& index) const {
+ QSpinBox* editor = dynamic_cast<QSpinBox*>(e);
+ if (editor) {
+ model->setData(index, editor->text(), Qt::EditRole);
+ }
+}
+
PeriphParamModel::PeriphParamModel(IOBase* peripheral, QObject* parent)
: QAbstractTableModel(parent), m_peripheral(peripheral) {}
@@ -53,10 +81,13 @@ QVariant PeriphParamModel::data(const QModelIndex& index, int role) const {
}
case Column::Value: {
switch (role) {
+ case Qt::EditRole:
case Qt::DisplayRole:
return m_peripheral->parameters().at(idx).value;
case Qt::TextAlignmentRole:
return Qt::AlignCenter;
+ case Qt::UserRole:
+ return QVariant::fromValue(m_peripheral->parameters().at(idx));
}
break;
}

MODIFY
src/io/periphparammodel.h
src/io/periphparammodel.h
#include "radix.h"
#include <QAbstractTableModel>
+#include <QStyledItemDelegate>
namespace Ripes {
class IOBase;
+class PeriphParamDelegate : public QStyledItemDelegate {
+public:
+ PeriphParamDelegate(QObject* parent);
+ QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
+ void setEditorData(QWidget* editor, const QModelIndex& index) const override;
+ void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
+};
+
/**
* @brief The PeriphParamModel class
* A model for Qt's MVC framework, responsible for representing and editing the parameters which are exposed by a memory

MODIFY
src/ioperipheraltab.cpp
src/ioperipheraltab.cpp
@@ -19,6 +19,7 @@ IOPeripheralTab::IOPeripheralTab(QWidget* parent, IOBase* peripheral)
m_ui->parameterView->setModel(new PeriphParamModel(peripheral, this));
m_ui->parameterView->horizontalHeader()->setStretchLastSection(true);
m_ui->parameterView->resizeColumnsToContents();
+ m_ui->parameterView->setItemDelegateForColumn(PeriphParamModel::Value, new PeriphParamDelegate(this));
}
IOPeripheralTab::~IOPeripheralTab() {

Add PeriphParamDelegate to PeriphParamModel

The delegate will translate all of the parameter constraints (such as ranges) to a suitable editor for the type.
Currently, only integer data types are supported for parameters, will add more when (if) peripheral parameters get more complex.