/
QGIFace.cpp
196 lines (167 loc) · 5.75 KB
/
QGIFace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/***************************************************************************
* Copyright (c) 2013 Luke Parry <l.parry@warwick.ac.uk> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <QAction>
#include <QApplication>
#include <QContextMenuEvent>
#include <QGraphicsScene>
#include <QMouseEvent>
#include <QGraphicsSceneHoverEvent>
#include <QPainterPathStroker>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#endif
#include <QFile>
#include <QTextStream>
#include <QRectF>
#include <QPointF>
#include <cmath>
#include <App/Application.h>
#include <App/Material.h>
#include <Base/Console.h>
#include <Base/Parameter.h>
#include "QGCustomSvg.h"
#include "QGCustomRect.h"
#include "QGIView.h"
#include "QGIFace.h"
using namespace TechDrawGui;
QGIFace::QGIFace(int index) :
projIndex(index),
m_colDefFill(Qt::white), //Qt::transparent? paper colour?
m_styleDef(Qt::SolidPattern),
m_styleSelect(Qt::SolidPattern)
{
setFlag(QGraphicsItem::ItemClipsChildrenToShape,true);
//setFiltersChildEvents(true);
m_styleCurrent = Qt::NoPen; //don't draw face lines, just fill
m_styleNormal = m_styleDef;
m_colNormalFill = m_colDefFill;
setPrettyNormal();
m_svg = new QGCustomSvg();
m_rect = new QGCustomRect();
m_rect->setParentItem(this);
m_svgCol = SVGCOLDEFAULT;
}
QGIFace::~QGIFace()
{
//nothing to do. every item is a child of QGIFace & will get removed/deleted when QGIF is deleted
}
void QGIFace::setPrettyNormal() {
m_fillStyle = m_styleNormal;
m_fillColor = m_colNormalFill;
QGIPrimPath::setPrettyNormal();
}
void QGIFace::setPrettyPre() {
m_fillStyle = m_styleSelect;
m_fillColor = getPreColor();
QGIPrimPath::setPrettyPre();
}
void QGIFace::setPrettySel() {
m_fillStyle = m_styleSelect;
m_fillColor = getSelectColor();
QGIPrimPath::setPrettySel();
}
void QGIFace::setFill(QColor c, Qt::BrushStyle s) {
m_colNormalFill = c;
m_styleNormal = s;
}
void QGIFace::setFill(QBrush b) {
m_colNormalFill = b.color();
m_styleNormal = b.style();
}
void QGIFace::resetFill() {
m_colNormalFill = m_colDefFill;
m_styleNormal = m_styleDef;
}
void QGIFace::setHatch(std::string fileSpec)
{
QString qfs(QString::fromStdString(fileSpec));
QFile f(qfs);
if (!f.open(QFile::ReadOnly | QFile::Text)) {
Base::Console().Error("QGIFace could not read %s\n",fileSpec.c_str());
return;
}
m_svgXML = f.readAll();
if (!m_svg->load(&m_svgXML)) {
Base::Console().Error("Error - Could not load hatch into SVG renderer for %s\n", fileSpec.c_str());
return;
}
buildHatch();
}
void QGIFace::setPath(const QPainterPath & path)
{
QGraphicsPathItem::setPath(path);
if (!m_svgXML.isEmpty()) {
buildHatch();
}
}
void QGIFace::buildHatch()
{
m_styleNormal = Qt::NoBrush;
double w = boundingRect().width();
double h = boundingRect().height();
QRectF r = boundingRect();
QPointF fCenter = r.center();
double nw = ceil(w / SVGSIZEW);
double nh = ceil(h / SVGSIZEH);
w = nw * SVGSIZEW;
h = nh * SVGSIZEW;
m_rect->setRect(0.,0.,w,-h);
m_rect->centerAt(fCenter);
r = m_rect->rect();
QByteArray before,after;
before.append(QString::fromStdString(SVGCOLPREFIX + SVGCOLDEFAULT));
after.append(QString::fromStdString(SVGCOLPREFIX + m_svgCol));
QByteArray colorXML = m_svgXML.replace(before,after);
for (int iw = 0; iw < int(nw); iw++) {
for (int ih = 0; ih < int(nh); ih++) {
QGCustomSvg* tile = new QGCustomSvg();
if (tile->load(&colorXML)) {
tile->setParentItem(m_rect);
tile->setPos(iw*SVGSIZEW,-h + ih*SVGSIZEH);
}
}
}
}
//c is a CSS color ie "#000000"
//set hatch color before building hatch
void QGIFace::setHatchColor(std::string c)
{
m_svgCol = c;
}
QRectF QGIFace::boundingRect() const
{
return shape().controlPointRect();
}
QPainterPath QGIFace::shape() const
{
return path();
}
void QGIFace::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
QStyleOptionGraphicsItem myOption(*option);
myOption.state &= ~QStyle::State_Selected;
m_brush.setStyle(m_fillStyle);
m_brush.setColor(m_fillColor);
setBrush(m_brush);
QGIPrimPath::paint (painter, &myOption, widget);
}