222 lines
7.8 KiB
C++
222 lines
7.8 KiB
C++
// This file is part of cevo-evo.
|
|
//
|
|
// This software is governed by the CeCILL license under French law and
|
|
// abiding by the rules of distribution of free software. You can use,
|
|
// modify and/or redistribute the software under the terms of the CeCILL
|
|
// license as circulated by CEA, CNRS and INRIA at the following URL
|
|
// "http://www.cecill.info".
|
|
//
|
|
// As a counterpart to the access to the source code and rights to copy,
|
|
// modify and redistribute granted by the license, users are provided only
|
|
// with a limited warranty and the software's author, the holder of the
|
|
// economic rights, and the successive licensors have only limited
|
|
// liability.
|
|
//
|
|
// In this respect, the user's attention is drawn to the risks associated
|
|
// with loading, using, modifying and/or developing or reproducing the
|
|
// software by the user in light of its specific status of free software,
|
|
// that may mean that it is complicated to manipulate, and that also
|
|
// therefore means that it is reserved for developers and experienced
|
|
// professionals having in-depth computer knowledge. Users are therefore
|
|
// encouraged to load and test the software's suitability as regards their
|
|
// requirements in conditions enabling the security of their systems and/or
|
|
// data to be ensured and, more generally, to use and operate it in the
|
|
// same conditions as regards security.
|
|
//
|
|
// The fact that you are presently reading this means that you have had
|
|
// knowledge of the CeCILL license and that you accept its terms.
|
|
|
|
#pragma once
|
|
|
|
#include <QColor>
|
|
#include <QMetaType>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
#include <QVariant>
|
|
#include <QVector>
|
|
|
|
#include <algorithm>
|
|
#include <type_traits>
|
|
|
|
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<T> 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<ItemField> {
|
|
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<QColor>(); }
|
|
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<Item *>(); }
|
|
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<T> 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<T> 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<T>.
|
|
template <typename T>
|
|
class ItemList : public BaseItemList {
|
|
static_assert(std::is_base_of<Item, T>::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<T *>(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<T *>(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<T *> m_items;
|
|
};
|
|
|
|
} // namespace Cevo
|
|
|
|
Q_DECLARE_METATYPE(Cevo::Item *)
|