#pragma once #include #include #include #include #include #include #include #include namespace Cevo { // Equivalent of ItemList.pas's TDataType. enum class DataType { None, String, Boolean, Integer, Float, Color, Time, Date, DateTime, Enumeration, Reference }; QString dataTypeStr(DataType dataType); // Equivalent of ItemList.pas's TItemField (a value type here - the original // needed a class only because of Object Pascal's TObjectList plumbing). struct ItemField { QString sysName; QString name; int index = 0; DataType dataType = DataType::None; QStringList enumStates; int visibleIfIndex = 0; // >0: only visible if field[visibleIfIndex]'s enum selection is > 0 }; // Equivalent of ItemList.pas's TItemFields. class ItemFields : public QVector { public: ItemField &addField(int index, const QString &sysName, const QString &name, DataType dataType); const ItemField *searchByIndex(int index) const; }; class BaseItemList; // Equivalent of ItemList.pas's TItem. XML load/save (LoadFromNode/ // SaveToNode in the original) is NOT ported: not used by any of the ported // Common/Forms dialogs. class Item { public: virtual ~Item() = default; int id = 0; QString name; virtual ItemFields getFields() const; // base: just the "Name" field const ItemField *getField(int index) const; virtual QVariant getValue(int index) const; virtual void setValue(int index, const QVariant &value); int getValueInt(int index) const { return getValue(index).toInt(); } QString getValueString(int index) const { return getValue(index).toString(); } QColor getValueColor(int index) const { return getValue(index).value(); } bool getValueBool(int index) const { return getValue(index).toBool(); } int getValueEnum(int index) const { return getValue(index).toInt(); } Item *getValueReference(int index) const { return getValue(index).value(); } QString getValueAsText(int index) const; void setValueInt(int index, int value) { setValue(index, value); } void setValueString(int index, const QString &value) { setValue(index, value); } void setValueColor(int index, const QColor &value) { setValue(index, QVariant::fromValue(value)); } void setValueBool(int index, bool value) { setValue(index, value); } void setValueEnum(int index, int value) { setValue(index, value); } void setValueReference(int index, Item *value) { setValue(index, QVariant::fromValue(value)); } virtual void assign(const Item &source); virtual bool compare(const Item &other) const; virtual QString className() const; // TItem.GetClassName virtual BaseItemList *getReferenceList(int index) const { Q_UNUSED(index); return nullptr; } }; // Equivalent of ItemList.pas's TBaseItemList: a type-erased view onto an // ItemList so UI code (FormItem/FormList) doesn't need to know T. Unlike // the original (which needed a closure-based adapter because Object Pascal // generics have no common runtime base), this is just a plain abstract // interface - ItemList below implements it directly. class BaseItemList { public: virtual ~BaseItemList() = default; virtual QString name() const = 0; virtual int count() const = 0; virtual Item *at(int index) const = 0; virtual int add(Item *value) = 0; // takes ownership, returns the new index virtual int remove(Item *value) = 0; // deletes value, returns its former index (-1 if not found) virtual Item *createItem(const QString &name = QString()) const = 0; // does NOT add to the list virtual QString getNextAvailableName(const QString &name) const = 0; virtual ItemFields getItemFields() const = 0; virtual Item *findById(int id) const = 0; }; // Equivalent of ItemList.pas's TItemList. template class ItemList : public BaseItemList { static_assert(std::is_base_of::value, "T must derive from Cevo::Item"); public: ~ItemList() override { qDeleteAll(m_items); } QString name() const override { return T().className(); } int count() const override { return m_items.count(); } Item *at(int index) const override { return m_items.at(index); } T *itemAt(int index) const { return m_items.at(index); } int add(Item *value) override { auto *item = static_cast(value); item->id = nextId(); m_items.append(item); return m_items.size() - 1; } T *addItem(const QString &itemName = QString()) { T *item = createTypedItem(itemName); item->id = nextId(); m_items.append(item); return item; } int remove(Item *value) override { const int index = m_items.indexOf(static_cast(value)); if (index >= 0) delete m_items.takeAt(index); return index; } T *createTypedItem(const QString &itemName) const { T *item = new T(); item->name = itemName; return item; } Item *createItem(const QString &itemName) const override { return createTypedItem(itemName); } QString getNextAvailableName(const QString &baseName) const override { QString candidate = baseName + QStringLiteral(" 1"); while (findByName(candidate)) candidate = incrementName(candidate); return candidate; } ItemFields getItemFields() const override { return T().getFields(); } Item *findById(int itemId) const override { for (T *item : m_items) if (item->id == itemId) return item; return nullptr; } T *findByName(const QString &itemName) const { for (T *item : m_items) if (item->name == itemName) return item; return nullptr; } private: static QString incrementName(const QString &itemName) { const int i = itemName.lastIndexOf(' '); if (i >= 0) { bool ok = false; const int num = itemName.mid(i + 1).toInt(&ok); if (ok) return itemName.left(i).trimmed() + ' ' + QString::number(num + 1); } return itemName + QStringLiteral(" 2"); } int nextId() { int maxId = 0; for (T *item : m_items) maxId = std::max(maxId, item->id); return maxId + 1; } QVector m_items; }; } // namespace Cevo Q_DECLARE_METATYPE(Cevo::Item *)