qelectrotech-source-mirror/sources/ui/dynamicelementtextmodel.h

161 lines
4.7 KiB
C
Raw Normal View History

/*
2024-03-29 10:09:48 +01:00
Copyright 2006-2024 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DYNAMICELEMENTTEXTMODEL_H
#define DYNAMICELEMENTTEXTMODEL_H
2020-12-08 19:57:35 +01:00
#include "../qetgraphicsitem/dynamicelementtextitem.h"
2020-12-08 19:57:35 +01:00
#include <QHash>
#include <QStandardItemModel>
#include <qstyleditemdelegate.h>
class QUndoCommand;
class ElementTextItemGroup;
class Element;
/**
2020-08-16 11:19:36 +02:00
@brief The DynamicElementTextModel class
A model to use with QtView.
This model display and can edit the value of dynamic text of an element.
Set the delegate DynamicTextItemDelegate as delegate of this model.
*/
class DynamicElementTextModel : public QStandardItemModel
{
Q_OBJECT
public:
2020-07-15 20:20:07 +02:00
enum ValueType {
textFrom =1,
userText,
infoText,
compositeText,
txtAlignment,
size,
font,
color,
pos,
frame,
rotation,
keepVisualRotation,
2020-07-15 20:20:07 +02:00
textWidth,
grpAlignment,
grpPos,
grpRotation,
grpVAdjust,
grpName,
grpHoldBottom,
grpFrame
};
2020-07-15 20:20:07 +02:00
DynamicElementTextModel(Element *element, QObject *parent = nullptr);
~DynamicElementTextModel() override;
2020-07-15 20:20:07 +02:00
bool indexIsInGroup(const QModelIndex &index) const;
DynamicElementTextItem *textFromIndex(const QModelIndex &index) const;
DynamicElementTextItem *textFromItem(QStandardItem *item) const;
QModelIndex indexFromText(DynamicElementTextItem *text) const;
2020-08-20 21:58:23 +02:00
QUndoCommand *undoForEditedText(
DynamicElementTextItem *deti,
QUndoCommand *parent_undo = nullptr) const;
QUndoCommand *undoForEditedGroup(
ElementTextItemGroup *group,
QUndoCommand *parent_undo = nullptr) const;
2020-07-15 20:20:07 +02:00
ElementTextItemGroup *groupFromIndex(const QModelIndex &index) const;
ElementTextItemGroup *groupFromItem(QStandardItem *item) const;
QModelIndex indexFromGroup(ElementTextItemGroup *group) const;
bool indexIsText(const QModelIndex &index) const;
bool indexIsGroup(const QModelIndex &index) const;
2020-09-07 22:03:40 +02:00
bool canDropMimeData(
const QMimeData *data,
Qt::DropAction action,
int row,
int column,
const QModelIndex &parent) const override;
bool dropMimeData(
const QMimeData *data,
Qt::DropAction action,
int row,
int column,
const QModelIndex &parent) override;
2020-07-15 20:20:07 +02:00
QMimeData *mimeData(const QModelIndexList &indexes) const override;
QStringList mimeTypes() const override;
signals:
2020-07-15 20:20:07 +02:00
void dataChanged();
private:
2020-07-15 20:20:07 +02:00
QList<QStandardItem *> itemsForText(DynamicElementTextItem *deti);
void addText(DynamicElementTextItem *deti);
void removeText(DynamicElementTextItem *deti);
void addGroup(ElementTextItemGroup *group);
void removeGroup(ElementTextItemGroup *group);
2020-09-07 22:03:40 +02:00
void addTextToGroup(
DynamicElementTextItem *deti,
ElementTextItemGroup *group);
2020-08-20 21:58:23 +02:00
void removeTextFromGroup(DynamicElementTextItem *deti,
ElementTextItemGroup *group);
2020-09-07 22:03:40 +02:00
void enableSourceText(
DynamicElementTextItem *deti,
DynamicElementTextItem::TextFrom tf );
2020-07-15 20:20:07 +02:00
void enableGroupRotationAndPos(ElementTextItemGroup *group);
void itemDataChanged(QStandardItem *qsi);
void setConnection(DynamicElementTextItem *deti, bool set);
void setConnection(ElementTextItemGroup *group, bool set);
2020-08-20 21:58:23 +02:00
void updateDataFromText(DynamicElementTextItem *deti,
DynamicElementTextModel::ValueType type);
void updateDataFromGroup(ElementTextItemGroup *group,
DynamicElementTextModel::ValueType type);
2020-07-15 20:20:07 +02:00
private:
QPointer<Element> m_element;
QHash <DynamicElementTextItem *, QStandardItem *> m_texts_list;
QHash <ElementTextItemGroup *, QStandardItem *> m_groups_list;
2020-08-20 21:58:23 +02:00
QHash <DynamicElementTextItem *,
QList<QMetaObject::Connection>> m_hash_text_connect;
QHash <ElementTextItemGroup *,
QList<QMetaObject::Connection>> m_hash_group_connect;
2020-07-15 20:20:07 +02:00
bool m_block_dataChanged = false;
};
class DynamicTextItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
2020-07-15 20:20:07 +02:00
public:
DynamicTextItemDelegate(QObject *parent = Q_NULLPTR);
2020-09-07 22:03:40 +02:00
QWidget *createEditor(
QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void setModelData(
QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const override;
protected:
2020-07-15 20:20:07 +02:00
bool eventFilter(QObject *object, QEvent *event) override;
private:
2020-07-15 20:20:07 +02:00
QStringList availableInfo(DynamicElementTextItem *deti) const;
};
#endif // DYNAMICELEMENTTEXTMODEL_H