20#ifndef LIBREPCB_EDITOR_EDITABLELISTMODEL_H
21#define LIBREPCB_EDITOR_EDITABLELISTMODEL_H
34#include <optional.hpp>
82 : QAbstractTableModel(parent),
107 emit beginResetModel();
109 emit endResetModel();
113 emit beginResetModel();
116 emit endResetModel();
120 emit beginResetModel();
122 emit endResetModel();
126 emit beginResetModel();
128 emit endResetModel();
132 void add(
const QPersistentModelIndex& itemIndex)
noexcept {
136 QMessageBox::critical(
nullptr, tr(
"Error"), tr(
"Invalid value."));
141 QMessageBox::critical(
nullptr, tr(
"Error"),
142 tr(
"Value already contained in list."));
151 emit dataChanged(index(
mValues.count(), 0),
155 void remove(
const QPersistentModelIndex& itemIndex)
noexcept {
156 int row = itemIndex.data(Qt::EditRole).toInt();
157 if ((row >= 0) && (row <
mValues.count())) {
158 beginRemoveRows(QModelIndex(), row, row);
164 void moveUp(
const QPersistentModelIndex& itemIndex)
noexcept {
165 int row = itemIndex.data(Qt::EditRole).toInt();
168 emit dataChanged(index(row - 1, 0), index(row,
_COLUMN_COUNT - 1));
172 void moveDown(
const QPersistentModelIndex& itemIndex)
noexcept {
173 int row = itemIndex.data(Qt::EditRole).toInt();
174 if (row < (
mValues.count() - 1)) {
176 emit dataChanged(index(row, 0), index(row + 1,
_COLUMN_COUNT - 1));
181 int rowCount(
const QModelIndex& parent = QModelIndex())
const override {
182 if (!parent.isValid()) {
188 int columnCount(
const QModelIndex& parent = QModelIndex())
const override {
189 if (!parent.isValid()) {
195 QVariant
data(
const QModelIndex& index,
196 int role = Qt::DisplayRole)
const override {
197 if (!index.isValid()) {
201 tl::optional<ValueType> value =
mNewValue;
202 if ((index.row() >= 0) && (index.row() <
mValues.count())) {
203 value =
mValues.at(index.row());
205 bool showPlaceholder = (index.row() ==
mValues.count()) &&
207 switch (index.column()) {
210 case Qt::DisplayRole:
211 return showPlaceholder
214 case Qt::DecorationRole:
215 return value ?
mIcons.value(*value) : QIcon();
218 case Qt::ForegroundRole:
219 if (showPlaceholder) {
220 QColor color = qApp->palette().text().color();
222 return QBrush(color);
247 QVariant
headerData(
int section, Qt::Orientation orientation,
248 int role = Qt::DisplayRole)
const override {
249 if (orientation == Qt::Horizontal) {
250 if (role == Qt::DisplayRole) {
258 }
else if (orientation == Qt::Vertical) {
259 bool isLastRow = section >=
mValues.count();
260 if (role == Qt::DisplayRole) {
261 return isLastRow ? tr(
"New:") : QString::number(section + 1);
262 }
else if (role == Qt::TextAlignmentRole) {
263 return QVariant(Qt::AlignRight | Qt::AlignVCenter);
269 Qt::ItemFlags
flags(
const QModelIndex& index)
const override {
270 Qt::ItemFlags f = QAbstractTableModel::flags(index);
271 if (index.isValid() && (index.row() ==
mValues.count())) {
272 f |= Qt::ItemIsEditable;
277 bool setData(
const QModelIndex& index,
const QVariant& value,
278 int role = Qt::EditRole)
override {
279 bool isLastRow = index.row() >=
mValues.count();
281 if ((index.column() ==
COLUMN_TEXT) && role == Qt::EditRole) {
285 emit dataChanged(index, index);
301 return value.toDisplayString();
314 return value.toStr();
324 const QVariant& input,
const tl::optional<Uuid>& tag)
const noexcept {
330 const QVariant& input,
const tl::optional<QString>& tag)
const noexcept {
332 QString str = input.toString().trimmed();
333 return str.isEmpty() ? tl::nullopt : tl::make_optional(str);
337 const QVariant& input,
const tl::optional<QUrl>& tag)
const noexcept {
339 QUrl url = QUrl::fromUserInput(input.toString());
340 return url.isValid() ? tl::make_optional(url) : tl::nullopt;
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:58
static tl::optional< Uuid > tryFromString(const QString &str) noexcept
Try creating a Uuid from a string, returning empty optional if invalid.
Definition: uuid.cpp:119
A generic QAbstractTableModel subclass to view and edit list of various data types.
Definition: editablelistmodel.h:73
QHash< ValueType, QString > mDisplayTexts
Definition: editablelistmodel.h:359
QString getDisplayText(const QUrl &value) const noexcept
Definition: editablelistmodel.h:300
const T & getValues() const noexcept
Definition: editablelistmodel.h:92
tl::optional< ValueType > mDefaultValue
Definition: editablelistmodel.h:356
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition: editablelistmodel.h:247
void setDisplayText(const ValueType &value, const QString &text) noexcept
Definition: editablelistmodel.h:119
void setIcon(const ValueType &value, const QIcon &icon) noexcept
Definition: editablelistmodel.h:125
tl::optional< ValueType > mNewValue
Definition: editablelistmodel.h:357
QVariant getDataForValue(const QUrl &value) const noexcept
Definition: editablelistmodel.h:317
EditableListModel & operator=(const EditableListModel &rhs) noexcept
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: editablelistmodel.h:181
void moveDown(const QPersistentModelIndex &itemIndex) noexcept
Definition: editablelistmodel.h:172
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: editablelistmodel.h:269
EditableListModel(QObject *parent=nullptr) noexcept
Definition: editablelistmodel.h:81
void add(const QPersistentModelIndex &itemIndex) noexcept
Definition: editablelistmodel.h:132
void setPlaceholderText(const QString &text) noexcept
Definition: editablelistmodel.h:100
QVariant getDataForValue(const Uuid &value) const noexcept
Definition: editablelistmodel.h:313
void setValues(const T &values) noexcept
Definition: editablelistmodel.h:106
QString getDisplayText(const QString &value) const noexcept
Definition: editablelistmodel.h:304
QHash< ValueType, QIcon > mIcons
Definition: editablelistmodel.h:360
ComboBoxDelegate::Items mComboBoxItems
Definition: editablelistmodel.h:361
~EditableListModel() noexcept
Definition: editablelistmodel.h:89
tl::optional< Uuid > convertInputValue(const QVariant &input, const tl::optional< Uuid > &tag) const noexcept
Definition: editablelistmodel.h:323
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: editablelistmodel.h:195
void moveUp(const QPersistentModelIndex &itemIndex) noexcept
Definition: editablelistmodel.h:164
void remove(const QPersistentModelIndex &itemIndex) noexcept
Definition: editablelistmodel.h:155
void setDefaultValue(const ValueType &value) noexcept
Definition: editablelistmodel.h:95
QVariant getDataForValue(const QString &value) const noexcept
Definition: editablelistmodel.h:319
QString mPlaceholderText
Definition: editablelistmodel.h:358
T::value_type ValueType
Definition: editablelistmodel.h:74
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition: editablelistmodel.h:277
tl::optional< QString > convertInputValue(const QVariant &input, const tl::optional< QString > &tag) const noexcept
Definition: editablelistmodel.h:329
T mChoices
Definition: editablelistmodel.h:354
QString getDisplayText(const Uuid &value) const noexcept
Definition: editablelistmodel.h:296
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition: editablelistmodel.h:188
T mValues
Definition: editablelistmodel.h:355
EditableListModel(const EditableListModel &other) noexcept=delete
void setChoices(const T &choices) noexcept
Definition: editablelistmodel.h:112
Column
Definition: editablelistmodel.h:77
@ _COLUMN_COUNT
Definition: editablelistmodel.h:77
@ COLUMN_TEXT
Definition: editablelistmodel.h:77
@ COLUMN_ACTIONS
Definition: editablelistmodel.h:77
void updateComboBoxItems() noexcept
Definition: editablelistmodel.h:343
tl::optional< QUrl > convertInputValue(const QVariant &input, const tl::optional< QUrl > &tag) const noexcept
Definition: editablelistmodel.h:336
EditableListModelType
Definition: editablelistmodel.h:46
Definition: occmodel.cpp:77
Definition: comboboxdelegate.h:47
Definition: comboboxdelegate.h:52
void sort() noexcept
Definition: comboboxdelegate.cpp:39