Dynamic element text can have a frame

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5093 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2017-11-07 18:35:26 +00:00
parent d2d1b74fad
commit 6693bb6cad
9 changed files with 204 additions and 37 deletions

View File

@@ -114,6 +114,7 @@ QDomElement DynamicElementTextItem::toXml(QDomDocument &dom_doc) const
root_element.setAttribute("rotation", QString::number(QET::correctAngle(rotation())));
root_element.setAttribute("font_size", font().pointSize());
root_element.setAttribute("uuid", m_uuid.toString());
root_element.setAttribute("frame", m_frame? "true" : "false");
QMetaEnum me = textFromMetaEnum();
root_element.setAttribute("text_from", me.valueToKey(m_text_from));
@@ -174,6 +175,7 @@ void DynamicElementTextItem::fromXml(const QDomElement &dom_elmt)
QGraphicsTextItem::setRotation(dom_elmt.attribute("rotation", QString::number(0)).toDouble());
setFont(QETApp::diagramTextsFont(dom_elmt.attribute("font_size", QString::number(9)).toInt()));
m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString()));
m_frame = dom_elmt.attribute("frame", "false") == "true"? true : false;
QMetaEnum me = textFromMetaEnum();
m_text_from = DynamicElementTextItem::TextFrom(me.keyToValue(dom_elmt.attribute("text_from").toStdString().data()));
@@ -479,6 +481,18 @@ QString DynamicElementTextItem::compositeText() const
return m_composite_text;
}
void DynamicElementTextItem::setFrame(const bool frame)
{
m_frame = frame;
update();
emit frameChanged(m_frame);
}
bool DynamicElementTextItem::frame() const
{
return m_frame;
}
/**
* @brief DynamicElementTextItem::mousePressEvent
* @param event
@@ -653,6 +667,46 @@ void DynamicElementTextItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
}
void DynamicElementTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
DiagramTextItem::paint(painter, option, widget);
if (m_frame)
{
painter->save();
painter->setFont(QETApp::diagramTextsFont(fontSize()));
//Adjust the thickness according to the font size,
qreal w=0.3;
if(fontSize() >= 5)
{
w = (qreal)fontSize()*0.1;
if(w > 2.5)
w = 2.5;
}
QPen pen;
pen.setColor(color());
pen.setWidthF(w);
painter->setPen(pen);
painter->setRenderHint(QPainter::Antialiasing);
//Get the bounding rectangle of the text
QRectF text_bounding = painter->boundingRect(boundingRect(), toPlainText());
//Center text_bounding in the bounding rect of this
text_bounding.moveTop((boundingRect().height()-text_bounding.height())/2);
text_bounding.moveLeft((boundingRect().width() - text_bounding.width())/2);
//adjust only for better visual
text_bounding.adjust(-2,0,2,0);
//Adjust the rounding of the rectangle according to the size of the font
qreal ro = (qreal)fontSize()/3;
painter->drawRoundedRect(text_bounding, ro, ro);
painter->restore();
}
}
void DynamicElementTextItem::elementInfoChanged()
{
DiagramContext dc;

View File

@@ -44,6 +44,7 @@ class DynamicElementTextItem : public DiagramTextItem
Q_PROPERTY(TextFrom textFrom READ textFrom WRITE setTextFrom NOTIFY textFromChanged)
Q_PROPERTY(QString infoName READ infoName WRITE setInfoName NOTIFY infoNameChanged)
Q_PROPERTY(QString compositeText READ compositeText WRITE setCompositeText NOTIFY compositeTextChanged)
Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged)
public:
Q_ENUMS(TextFrom)
@@ -61,6 +62,7 @@ class DynamicElementTextItem : public DiagramTextItem
void textFromChanged(DynamicElementTextItem::TextFrom text_from);
void infoNameChanged(QString info);
void compositeTextChanged(QString text);
void frameChanged(bool frame);
public:
DynamicElementTextItem(Element *parent_element);
@@ -89,6 +91,8 @@ class DynamicElementTextItem : public DiagramTextItem
QString infoName() const;
void setCompositeText(const QString &text);
QString compositeText() const;
void setFrame(const bool frame);
bool frame() const;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
@@ -97,6 +101,7 @@ class DynamicElementTextItem : public DiagramTextItem
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private:
void elementInfoChanged();
@@ -132,6 +137,7 @@ class DynamicElementTextItem : public DiagramTextItem
QMetaObject::Connection m_report_formula_con;
QList<QMetaObject::Connection> m_formula_connection;
QColor m_user_color;
bool m_frame = false;
};
#endif // DYNAMICELEMENTTEXTITEM_H