23
#include "PreCompiled.h"
27
# include <QGraphicsSceneHoverEvent>
30
# include <QStyleOptionGraphicsItem>
33
#include <Base/Console.h>
34
#include <Base/Parameter.h>
35
#include <Mod/TechDraw/App/Preferences.h>
37
#include "QGCustomText.h"
38
#include "PreferencesGui.h"
42
using namespace TechDraw;
43
using namespace TechDrawGui;
45
QGCustomText::QGCustomText(QGraphicsItem* parent) :
46
QGraphicsTextItem(parent), isHighlighted(false)
48
setCacheMode(QGraphicsItem::NoCache);
49
setAcceptHoverEvents(false);
50
setFlag(QGraphicsItem::ItemIsSelectable, false);
51
setFlag(QGraphicsItem::ItemIsMovable, false);
53
m_colCurrent = getNormalColor();
54
m_colNormal = m_colCurrent;
55
tightBounding = false;
58
void QGCustomText::centerAt(QPointF centerPos)
60
centerAt(centerPos.x(), centerPos.y());
63
void QGCustomText::centerAt(double cX, double cY)
65
QRectF box = boundingRect();
66
double width = box.width();
67
double height = box.height();
68
double newX = cX - width/2.;
69
double newY = cY - height/2.;
73
void QGCustomText::justifyLeftAt(QPointF centerPos, bool vCenter)
75
justifyLeftAt(centerPos.x(), centerPos.y(), vCenter);
78
void QGCustomText::justifyLeftAt(double cX, double cY, bool vCenter)
80
QRectF box = boundingRect();
81
double height = box.height();
82
double newY = cY - height;
84
newY = cY - height/2.;
89
void QGCustomText::justifyRightAt(QPointF centerPos, bool vCenter)
91
justifyRightAt(centerPos.x(), centerPos.y(), vCenter);
94
void QGCustomText::justifyRightAt(double cX, double cY, bool vCenter)
96
QRectF box = boundingRect();
97
double width = box.width();
98
double height = box.height();
99
double newX = cX - width;
100
double newY = cY - height;
102
newY = cY - height/2.;
107
double QGCustomText::getHeight()
109
return boundingRect().height();
112
double QGCustomText::getWidth()
114
return boundingRect().width();
116
QVariant QGCustomText::itemChange(GraphicsItemChange change, const QVariant &value)
119
if (change == ItemSelectedHasChanged && scene()) {
126
return QGraphicsTextItem::itemChange(change, value);
130
void QGCustomText::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
135
QGraphicsTextItem::hoverEnterEvent(event);
138
void QGCustomText::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
143
QGraphicsTextItem::hoverLeaveEvent(event);
146
void QGCustomText::setPrettyNormal() {
147
m_colCurrent = m_colNormal;
148
setDefaultTextColor(m_colCurrent);
152
void QGCustomText::setPrettyPre() {
153
m_colCurrent = getPreColor();
154
setDefaultTextColor(m_colCurrent);
158
void QGCustomText::setPrettySel() {
159
m_colCurrent = getSelectColor();
160
setDefaultTextColor(m_colCurrent);
164
void QGCustomText::setColor(QColor c)
168
QGraphicsTextItem::setDefaultTextColor(c);
171
void QGCustomText::setTightBounding(bool tight)
173
tightBounding = tight;
176
void QGCustomText::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
177
QStyleOptionGraphicsItem myOption(*option);
178
myOption.state &= ~QStyle::State_Selected;
183
QGraphicsTextItem::paint (painter, &myOption, widget);
186
QRectF QGCustomText::boundingRect() const
188
if (toPlainText().isEmpty()) {
190
} else if (tightBounding) {
191
return tightBoundingRect();
193
return QGraphicsTextItem::boundingRect();
197
QRectF QGCustomText::tightBoundingRect() const
199
QFontMetrics qfm(font());
200
QRectF result = QGraphicsTextItem::boundingRect();
201
QRectF tight = qfm.tightBoundingRect(toPlainText());
202
qreal x_adj = (result.width() - tight.width())/4.0;
203
qreal y_adj = (result.height() - tight.height())/4.0;
207
result.adjust(x_adj, 1.75*y_adj, -x_adj, -y_adj);
213
QPointF QGCustomText::tightBoundingAdjust() const
215
QRectF original = QGraphicsTextItem::boundingRect();
216
QRectF tight = tightBoundingRect();
218
return QPointF(tight.x()-original.x(), tight.y()-original.y());
221
QColor QGCustomText::getNormalColor()
223
return PreferencesGui::normalQColor();
226
QColor QGCustomText::getPreColor()
228
return PreferencesGui::preselectQColor();
231
QColor QGCustomText::getSelectColor()
233
return PreferencesGui::selectQColor();
236
Base::Reference<ParameterGrp> QGCustomText::getParmGroup()
238
return Preferences::getPreferenceGroup("Colors");
241
void QGCustomText::makeMark(double x, double y)
243
QGICMark* cmItem = new QGICMark(-1);
244
cmItem->setParentItem(this);
245
cmItem->setPos(x, y);
246
cmItem->setThick(1.0);
247
cmItem->setSize(40.0);
248
cmItem->setZValue(ZVALUE::VERTEX);
251
void QGCustomText::makeMark(Base::Vector3d v)