2024-12-13 18:08:00 +08:00
|
|
|
|
#include "graphicsItem/handleText.h"
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
#include <QLineEdit>
|
|
|
|
|
|
#include <QGraphicsProxyWidget>
|
|
|
|
|
|
|
|
|
|
|
|
HandleText::HandleText(QGraphicsItem *parent)
|
|
|
|
|
|
: ItemControlHandle(parent)
|
|
|
|
|
|
,_font(QFont())
|
|
|
|
|
|
,_proxy(nullptr)
|
2025-11-26 20:33:13 +08:00
|
|
|
|
,_nIndex(-1)
|
|
|
|
|
|
,_type(0)
|
2024-12-13 18:08:00 +08:00
|
|
|
|
{
|
|
|
|
|
|
_editable = true;
|
|
|
|
|
|
_boundingRect = QRectF(-TEXT_WIDTH*0.5,-TEXT_WIDTH*0.5,TEXT_WIDTH,TEXT_HEIGHT);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HandleText::~HandleText()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HandleText::setText(QString str)
|
|
|
|
|
|
{
|
|
|
|
|
|
_text = str;
|
|
|
|
|
|
QFontMetrics fontMetrics(_font);
|
|
|
|
|
|
_boundingRect = fontMetrics.boundingRect(_text);
|
|
|
|
|
|
int w = _boundingRect.width();
|
|
|
|
|
|
int h = _boundingRect.height();
|
|
|
|
|
|
_boundingRect.moveTo(QPointF(-w*0.5,-h*0.5));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-06 16:36:50 +08:00
|
|
|
|
QString HandleText::getText() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return _text;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-13 18:08:00 +08:00
|
|
|
|
void HandleText::creatEditor()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!_editable)
|
|
|
|
|
|
return;
|
|
|
|
|
|
if(!_proxy)
|
|
|
|
|
|
_proxy = new QGraphicsProxyWidget(this);
|
|
|
|
|
|
_proxy->setVisible(true);
|
|
|
|
|
|
QLineEdit *editor = new QLineEdit();
|
|
|
|
|
|
editor->setText(_text);
|
|
|
|
|
|
editor->resize(boundingRect().size().toSize()*1.5);
|
|
|
|
|
|
_proxy->setWidget(editor);
|
|
|
|
|
|
|
|
|
|
|
|
// 将文本编辑的更改连接到文本项
|
|
|
|
|
|
connect(editor, &QLineEdit::textChanged, this, [this, editor]() {
|
|
|
|
|
|
//_text = editor->text();
|
|
|
|
|
|
setText(editor->text());
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 完成编辑后,删除QTextEdit
|
|
|
|
|
|
connect(editor, &QLineEdit::editingFinished, this, [this, editor]() {
|
|
|
|
|
|
//_text = editor->text();
|
|
|
|
|
|
setText(editor->text());
|
2025-02-06 16:36:50 +08:00
|
|
|
|
emit editFinish(editor->text());
|
2024-12-13 18:08:00 +08:00
|
|
|
|
_proxy->setVisible(false);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HandleText::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
|
|
|
|
|
{
|
|
|
|
|
|
painter->setPen(Qt::blue);
|
|
|
|
|
|
painter->setBrush(Qt::blue);
|
|
|
|
|
|
painter->setRenderHint(QPainter::TextAntialiasing, true);
|
|
|
|
|
|
int w = _boundingRect.width();
|
|
|
|
|
|
int h = _boundingRect.height();
|
2025-11-26 20:33:13 +08:00
|
|
|
|
if(_type == 0)
|
|
|
|
|
|
painter->drawText(QPointF(-w*0.5,h*0.5),_text);
|
|
|
|
|
|
else{
|
|
|
|
|
|
QString displayText = _sTagName + ":" + _text;
|
|
|
|
|
|
painter->drawText(boundingRect(), Qt::AlignCenter, displayText);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-13 18:08:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QRectF HandleText::boundingRect() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return _boundingRect;
|
|
|
|
|
|
}
|