27 #include <QXmlStreamWriter>
32 const int MaxCompression = 9;
35 QVariant toolTipRole(
const SessionItem& item,
int ncol)
37 QString result = item.
toolTip();
38 if (result.isEmpty()) {
40 if (ncol == 1 && item.
value().canConvert<QString>())
41 result = item.
value().toString();
44 return QVariant(result);
50 : QAbstractItemModel(parent)
51 , m_model_tag(std::move(model_tag))
73 Qt::ItemFlags result_flags = QAbstractItemModel::flags(
index);
74 if (
index.isValid()) {
75 result_flags |= Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled;
79 result_flags |= Qt::ItemIsEditable;
82 result_flags |= Qt::ItemIsDropEnabled;
84 result_flags |= Qt::ItemIsDropEnabled;
94 if (role == Qt::DisplayRole || role == Qt::EditRole) {
99 }
else if (role == Qt::ToolTipRole)
100 return toolTipRole(*item,
index.column());
101 else if (role == Qt::ForegroundRole)
102 return item->
isEnabled() ? QVariant() : QColor(Qt::gray);
104 if (item->
value().type() == QVariant::Bool)
105 return item->
value().toBool() ? Qt::Checked : Qt::Unchecked;
114 if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
144 if (row < 0 || column < 0 || column >=
columnCount(QModelIndex())
146 return QModelIndex();
151 return createIndex(row, column, item);
152 return QModelIndex();
157 if (!child.isValid())
158 return QModelIndex();
163 return QModelIndex();
164 return createIndex(parent_item->parentRow(), 0, parent_item);
167 return QModelIndex();
172 if (!
index.isValid())
175 QModelIndex dataIndex =
index;
187 for (
int i = 0; i < count; ++i)
199 if (indices.count() != 2)
203 auto* mime_data =
new QMimeData;
205 QXmlStreamWriter writer(&xml_data);
214 int column,
const QModelIndex& parent)
const
218 if (action == Qt::IgnoreAction)
220 if (action != Qt::MoveAction || column > 0 || !
data
227 QXmlStreamReader reader(xml_data);
228 while (!reader.atEnd()) {
230 if (reader.isStartElement()) {
232 const QString model_type =
234 return acceptable_child_items.contains(model_type);
242 const QModelIndex& parent)
244 if (action == Qt::IgnoreAction)
246 if (action != Qt::MoveAction || column > 0 || !
data
253 QXmlStreamReader reader(xml_data);
256 beginInsertRows(
parent, row, row);
268 return QModelIndex();
271 return createIndex(row, 0, item);
298 int row, QString tag)
312 QVector<QString> result;
314 result = parent_item->acceptableDefaultItemTypes();
341 throw Error(
"SessionModel::readFrom() -> Format error in p1");
347 if (reader->hasError())
348 throw Error(reader->errorString());
366 const QString tagName = tag.isEmpty() ? new_parent->
defaultTag() : tag;
374 if (row == previousIndex)
376 if (previousIndex >= 0 && row > previousIndex)
401 const QString tagName = tag.isEmpty() ? new_parent->
defaultTag() : tag;
404 QXmlStreamWriter writer(&xml_data);
407 QXmlStreamReader reader(xml_data);
410 return new_parent->
getItems(tagName).back();
415 QByteArray byte_array;
416 QXmlStreamWriter writer(&byte_array);
417 writer.setAutoFormatting(
true);
421 QXmlStreamReader reader(byte_array);
423 while (!reader.atEnd()) {
425 if (reader.isStartElement())
438 return QVector<SessionItem*>();
Defines namespace GUI::Model::ItemFactory.
Defines class MaterialItem.
Defines class Helpers functions.
Defines class SessionFlags.
Defines class SessionItem.
Defines class SessionModel.
Defines reader and writer classes for SessionModel.
The service to collect messages from different senders.
Base class for a GUI data item.
QString itemName() const
Get item name, return display name if no name is set.
bool registerTag(const QString &name, int min=0, int max=-1, QStringList modelTypes={})
Add new tag to this item with given name, min, max and types. max = -1 -> unlimited,...
QVector< SessionItem * > getItems(const QString &tag="") const
Returns vector of all items of given tag.
int numberOfChildren() const
Returns total number of children.
void insertChild(int row, SessionItem *item, const QString &tag="")
Insert item into given tag into given row.
QString displayName() const
Get display name of item, append index if ambigue.
QVariant value() const
Get value.
int rowOfChild(SessionItem *child) const
Returns row index of given child.
const SessionItemTags * sessionItemTags() const
QString defaultTag() const
Get default tag.
void setDefaultTag(const QString &tag)
Set default tag.
SessionItem * takeRow(int row)
Removes row from item and returns the item.
SessionItem * takeItem(int row, const QString &tag)
Remove item from given row from given tag.
SessionItem * parentItem() const
Returns parent of this item.
QVariant roleProperty(int role) const
Returns corresponding variant under given role, invalid variant when role is not present.
SessionItem * childAt(int row) const
Returns the child at the given row.
QString modelType() const
Get model type.
bool setRoleProperty(int role, const QVariant &value)
Set the contained role property to the given value. See also setTranslatorForRolePropertySetter.
void setModel(SessionModel *model)
Base class for a GUI data collection. A collection is e.g. all real data (RealDataModel)....
QVariant data(const QModelIndex &index, int role) const override
QModelIndex parent(const QModelIndex &child) const override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
SessionItem * moveItem(SessionItem *item, SessionItem *new_parent=nullptr, int row=-1, const QString &tag="")
Move given parameterized item to the new_parent at given row. If new_parent is not defined,...
SessionModel(QString model_tag, QObject *parent=nullptr)
QMimeData * mimeData(const QModelIndexList &indices) const override
SessionItem * itemForIndex(const QModelIndex &index) const
Qt::ItemFlags flags(const QModelIndex &index) const override
virtual QVector< SessionItem * > nonXMLItems() const
QString m_dragged_item_type
QModelIndex index(int row, int column, const QModelIndex &parent) const override
QStringList mimeTypes() const override
int columnCount(const QModelIndex &parent) const override
virtual void readFrom(QXmlStreamReader *reader, MessageService *messageService=nullptr)
void removeItem(SessionItem *item)
virtual void writeTo(QXmlStreamWriter *writer)
SessionItem * insertNewItem(QString model_type, SessionItem *parent_item=nullptr, int row=-1, QString tag="")
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override
virtual void initFrom(SessionModel *model, SessionItem *parent)
SessionItem * rootItem() const
QVector< QString > acceptableDefaultItemTypes(const QModelIndex &parent) const
bool removeRows(int row, int count, const QModelIndex &parent) override
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
int rowCount(const QModelIndex &parent) const override
SessionItem * m_root_item
QModelIndex indexOfItem(SessionItem *item) const
SessionItem * copy(const SessionItem *item_to_copy, SessionItem *new_parent=nullptr, const QString &tag="")
Copy given item to the new_parent at given row. Item intended for copying can belong to another model...
SessionItem * CreateItem(const QString &model_name, SessionItem *parent=nullptr)
create SessionItem of specific type and parent
void writeItemAndChildItems(QXmlStreamWriter *writer, const SessionItem *item)
void readItems(QXmlStreamReader *reader, SessionItem *parent, QString topTag="", MessageService *messageService=nullptr)
constexpr auto ItemMimeType
void writeModel(QXmlStreamWriter *writer, SessionItem *modelRootItem)
constexpr auto ModelTypeAttribute("ModelType")
constexpr auto ItemTag("Item")