FreeCAD

Форк
0
/
QGCustomText.cpp 
254 строки · 7.1 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2015 WandererFan <wandererfan@gmail.com>                *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <cassert>
26

27
# include <QGraphicsSceneHoverEvent>
28
# include <QPainter>
29
# include <QRectF>
30
# include <QStyleOptionGraphicsItem>
31
#endif
32

33
#include <Base/Console.h>
34
#include <Base/Parameter.h>
35
#include <Mod/TechDraw/App/Preferences.h>
36

37
#include "QGCustomText.h"
38
#include "PreferencesGui.h"
39
#include "QGICMark.h"
40
#include "ZVALUE.h"
41

42
using namespace TechDraw;
43
using namespace TechDrawGui;
44

45
QGCustomText::QGCustomText(QGraphicsItem* parent) :
46
    QGraphicsTextItem(parent), isHighlighted(false)
47
{
48
    setCacheMode(QGraphicsItem::NoCache);
49
    setAcceptHoverEvents(false);
50
    setFlag(QGraphicsItem::ItemIsSelectable, false);
51
    setFlag(QGraphicsItem::ItemIsMovable, false);
52

53
    m_colCurrent = getNormalColor();
54
    m_colNormal  = m_colCurrent;
55
    tightBounding = false;
56
}
57

58
void QGCustomText::centerAt(QPointF centerPos)
59
{
60
      centerAt(centerPos.x(), centerPos.y());
61
}
62

63
void QGCustomText::centerAt(double cX, double cY)
64
{
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.;
70
    setPos(newX, newY);
71
}
72

73
void QGCustomText::justifyLeftAt(QPointF centerPos, bool vCenter)
74
{
75
    justifyLeftAt(centerPos.x(), centerPos.y(), vCenter);
76
}
77

78
void QGCustomText::justifyLeftAt(double cX, double cY, bool vCenter)
79
{
80
    QRectF box = boundingRect();
81
    double height = box.height();
82
    double newY = cY - height;
83
    if (vCenter) {
84
        newY = cY - height/2.;
85
    }
86
    setPos(cX, newY);
87
}
88

89
void QGCustomText::justifyRightAt(QPointF centerPos, bool vCenter)
90
{
91
    justifyRightAt(centerPos.x(), centerPos.y(), vCenter);
92
}
93

94
void QGCustomText::justifyRightAt(double cX, double cY, bool vCenter)
95
{
96
    QRectF box = boundingRect();
97
    double width = box.width();
98
    double height = box.height();
99
    double newX = cX - width;
100
    double newY = cY - height;
101
    if (vCenter) {
102
        newY = cY - height/2.;
103
    }
104
    setPos(newX, newY);
105
}
106

107
double QGCustomText::getHeight()
108
{
109
    return boundingRect().height();
110
}
111

112
double QGCustomText::getWidth()
113
{
114
    return boundingRect().width();
115
}
116
QVariant QGCustomText::itemChange(GraphicsItemChange change, const QVariant &value)
117
{
118
//    Base::Console().Message("QGCT::itemChange - this: %X change: %d\n", this, change);
119
    if (change == ItemSelectedHasChanged && scene()) {
120
        if(isSelected()) {
121
            setPrettySel();
122
        } else {
123
            setPrettyNormal();
124
        }
125
    }
126
    return QGraphicsTextItem::itemChange(change, value);
127
}
128

129

130
void QGCustomText::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
131
{
132
    if (!isSelected()) {
133
        setPrettyPre();
134
    }
135
    QGraphicsTextItem::hoverEnterEvent(event);
136
}
137

138
void QGCustomText::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
139
{
140
    if(!isSelected()) {
141
        setPrettyNormal();
142
    }
143
    QGraphicsTextItem::hoverLeaveEvent(event);
144
}
145

146
void QGCustomText::setPrettyNormal() {
147
    m_colCurrent = m_colNormal;
148
    setDefaultTextColor(m_colCurrent);
149
    update();
150
}
151

152
void QGCustomText::setPrettyPre() {
153
    m_colCurrent = getPreColor();
154
    setDefaultTextColor(m_colCurrent);
155
    update();
156
}
157

158
void QGCustomText::setPrettySel() {
159
    m_colCurrent = getSelectColor();
160
    setDefaultTextColor(m_colCurrent);
161
    update();
162
}
163

164
void QGCustomText::setColor(QColor c)
165
{
166
    m_colNormal = c;
167
    m_colCurrent = c;
168
    QGraphicsTextItem::setDefaultTextColor(c);
169
}
170

171
void QGCustomText::setTightBounding(bool tight)
172
{
173
    tightBounding = tight;
174
}
175

176
void QGCustomText::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
177
    QStyleOptionGraphicsItem myOption(*option);
178
    myOption.state &= ~QStyle::State_Selected;
179

180
//    painter->setPen(Qt::green);
181
//    painter->drawRect(boundingRect());          //good for debugging
182

183
    QGraphicsTextItem::paint (painter, &myOption, widget);
184
}
185

186
QRectF QGCustomText::boundingRect() const
187
{
188
    if (toPlainText().isEmpty()) {
189
        return QRectF();
190
    } else if (tightBounding) {
191
        return tightBoundingRect();
192
    } else {
193
        return QGraphicsTextItem::boundingRect();
194
    }
195
}
196

197
QRectF QGCustomText::tightBoundingRect() const
198
{
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;
204

205
    // Adjust the bounding box 50% towards the Qt tightBoundingRect(),
206
    // except chomp some extra empty space above the font (1.75*y_adj)
207
    result.adjust(x_adj, 1.75*y_adj, -x_adj, -y_adj);
208

209
    return result;
210
}
211

212
// Calculate the amount of difference between tight and relaxed bounding boxes
213
QPointF QGCustomText::tightBoundingAdjust() const
214
{
215
    QRectF original = QGraphicsTextItem::boundingRect();
216
    QRectF tight = tightBoundingRect();
217

218
    return QPointF(tight.x()-original.x(), tight.y()-original.y());
219
}
220

221
QColor QGCustomText::getNormalColor()    //preference!
222
{
223
    return PreferencesGui::normalQColor();
224
}
225

226
QColor QGCustomText::getPreColor()
227
{
228
    return PreferencesGui::preselectQColor();
229
}
230

231
QColor QGCustomText::getSelectColor()
232
{
233
    return PreferencesGui::selectQColor();
234
}
235

236
Base::Reference<ParameterGrp> QGCustomText::getParmGroup()
237
{
238
    return Preferences::getPreferenceGroup("Colors");
239
}
240

241
void QGCustomText::makeMark(double x, double y)
242
{
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);
249
}
250

251
void QGCustomText::makeMark(Base::Vector3d v)
252
{
253
    makeMark(v.x, v.y);
254
}
255

256

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.