FreeCAD

Форк
0
/
QGIPrimPath.cpp 
327 строк · 8.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2016 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 <QGraphicsScene>
28
# include <QGraphicsSceneHoverEvent>
29
# include <QPainter>
30
# include <QStyleOptionGraphicsItem>
31
#endif
32

33
#include <App/Application.h>
34

35
#include <Gui/Selection.h>
36

37
#include <Mod/TechDraw/App/DrawView.h>
38

39
#include "QGIPrimPath.h"
40
#include "PreferencesGui.h"
41
#include "QGIView.h"
42

43

44
using namespace TechDrawGui;
45
using namespace TechDraw;
46

47
QGIPrimPath::QGIPrimPath():
48
    m_width(0),
49
    m_capStyle(Qt::RoundCap),
50
    m_fillStyleCurrent (Qt::NoBrush),
51
    m_fillOverride(false)
52
{
53
    setCacheMode(QGraphicsItem::NoCache);
54
    setFlag(QGraphicsItem::ItemIsSelectable, true);
55
    setFlag(QGraphicsItem::ItemIsMovable, false);
56
    setFlag(QGraphicsItem::ItemIsFocusable, true);      // to get key press events
57

58
    setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
59
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
60
    setAcceptHoverEvents(true);
61

62
    isHighlighted = false;
63
    multiselectActivated = false;
64

65
    m_colOverride = false;
66
    m_colNormal = getNormalColor();
67
    m_colCurrent = m_colNormal;
68
    m_styleNormal = Qt::SolidLine;
69
    m_styleCurrent = m_styleNormal;
70
    m_pen.setStyle(m_styleCurrent);
71
    m_capStyle = prefCapStyle();
72
    m_pen.setCapStyle(m_capStyle);
73
    m_pen.setWidthF(m_width);
74

75
    m_fillDef = Qt::NoBrush;
76
    m_fillSelect = Qt::SolidPattern;
77
    m_fillNormal = m_fillDef;
78
    m_fillStyleCurrent = m_fillNormal;
79

80
    m_colDefFill = Qt::white;
81
    setFillColor(m_colDefFill);
82

83
    setPrettyNormal();
84
}
85

86
QVariant QGIPrimPath::itemChange(GraphicsItemChange change, const QVariant &value)
87
{
88
    if (change == ItemSelectedHasChanged && scene()) {
89
        if(isSelected()) {
90
            setPrettySel();
91
            setFocus();
92
        } else {
93
            setPrettyNormal();
94
        }
95
    }
96
    return QGraphicsPathItem::itemChange(change, value);
97
}
98

99
void QGIPrimPath::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
100
{
101
    if (!isSelected()) {
102
        setPrettyPre();
103
    }
104
    setFocus();
105
    QGraphicsPathItem::hoverEnterEvent(event);
106
}
107

108
void QGIPrimPath::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
109
{
110
    if(!isSelected()) {
111
        setPrettyNormal();
112
    }
113

114
    QGraphicsPathItem::hoverLeaveEvent(event);
115
}
116

117

118
void QGIPrimPath::setPrettyNormal() {
119

120
    m_colCurrent = m_colNormal;
121
    m_fillColorCurrent = m_colNormalFill;
122
}
123

124
void QGIPrimPath::setPrettyPre() {
125
    m_colCurrent = getPreColor();
126
    if (!m_fillOverride) {
127
        m_fillColorCurrent = getPreColor();
128
    }
129
}
130

131
void QGIPrimPath::setPrettySel() {
132
    m_colCurrent = getSelectColor();
133
    if (!m_fillOverride) {
134
        m_fillColorCurrent = getSelectColor();
135
    }
136
}
137

138
//wf: why would a face use its parent's normal colour?
139
//this always goes to parameter
140
QColor QGIPrimPath::getNormalColor()
141
{
142
    QGIView *parent;
143

144
    if (m_colOverride) {
145
        return m_colNormal;
146
    }
147

148
    QGraphicsItem* qparent = parentItem();
149
    if (!qparent) {
150
        parent = nullptr;
151
    } else {
152
        parent = dynamic_cast<QGIView *> (qparent);
153
    }
154

155
    if (parent) {
156
        return parent->getNormalColor();
157
    }
158
    return PreferencesGui::normalQColor();
159
}
160

161
QColor QGIPrimPath::getPreColor()
162
{
163
    QGIView *parent;
164
    QGraphicsItem* qparent = parentItem();
165
    if (!qparent) {
166
        parent = nullptr;
167
    } else {
168
        parent = dynamic_cast<QGIView *> (qparent);
169
    }
170

171
    if (parent) {
172
        return parent->getPreColor();
173
    }
174
    return PreferencesGui::preselectQColor();
175
}
176

177
QColor QGIPrimPath::getSelectColor()
178
{
179
    QGIView *parent;
180
    QGraphicsItem* qparent = parentItem();
181
    if (!qparent) {
182
        parent = nullptr;
183
    } else {
184
        parent = dynamic_cast<QGIView *> (qparent);
185
    }
186

187
    if (parent) {
188
        return parent->getSelectColor();
189
    }
190
    return PreferencesGui::selectQColor();
191
}
192

193
void QGIPrimPath::setWidth(double w)
194
{
195
//    Base::Console().Message("QGIPP::setWidth(%.3f)\n", w);
196
    m_width = w;
197
    m_pen.setWidthF(m_width);
198
}
199

200
void QGIPrimPath::setStyle(Qt::PenStyle s)
201
{
202
// TODO: edge lines for faces are drawn with setStyle(Qt::NoPen) and trigger this message.
203
//    Base::Console().Warning("QGIPP::setStyle(Qt: %d) is deprecated. Use setLinePen instead\n", s);
204
    m_styleNormal = s;
205
    m_styleCurrent = s;
206
}
207

208
void QGIPrimPath::setStyle(int s)
209
{
210
// TODO: edge lines for faces are drawn with setStyle(Qt::NoPen) and trigger this message.
211
//    Base::Console().Warning("QGIPP::setStyle(int: %d) is deprecated. Use setLinePen instead\n", s);
212
    m_styleCurrent = static_cast<Qt::PenStyle>(s);
213
    m_styleNormal = static_cast<Qt::PenStyle>(s);
214
}
215

216
void QGIPrimPath::setNormalColor(QColor c)
217
{
218
    m_colNormal = c;
219
    m_colOverride = true;
220
    m_colCurrent = m_colNormal;
221
}
222

223
void QGIPrimPath::setCapStyle(Qt::PenCapStyle c)
224
{
225
    m_capStyle = c;
226
    m_pen.setCapStyle(c);
227
}
228

229
Base::Reference<ParameterGrp> QGIPrimPath::getParmGroup()
230
{
231
    return Preferences::getPreferenceGroup("Colors");
232
}
233

234
//EdgeCapStyle param changed from UInt (Qt::PenCapStyle) to Int (QComboBox index)
235
Qt::PenCapStyle QGIPrimPath::prefCapStyle()
236
{
237
    return (Qt::PenCapStyle)Preferences::LineCapStyle();
238
}
239

240
void QGIPrimPath::mousePressEvent(QGraphicsSceneMouseEvent *event)
241
{
242
    Qt::KeyboardModifiers originalModifiers = event->modifiers();
243
    if (event->button()&Qt::LeftButton) {
244
        multiselectActivated = false;
245
    }
246

247
    if (event->button() == Qt::LeftButton
248
        && multiselectEligible()
249
        && PreferencesGui::multiSelection()) {
250

251
        auto parent = dynamic_cast<QGIView *>(parentItem());
252
        if (parent) {
253
            std::vector<Gui::SelectionObject> selection = Gui::Selection().getSelectionEx();
254
            if (selection.size() == 1
255
                && selection.front().getObject() == parent->getViewObject()) {
256

257
                multiselectActivated = true;
258
                event->setModifiers(originalModifiers | Qt::ControlModifier);
259
            }
260
        }
261
    }
262

263
    QGraphicsPathItem::mousePressEvent(event);
264

265
    event->setModifiers(originalModifiers);
266
}
267

268
void QGIPrimPath::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
269
{
270
    Qt::KeyboardModifiers originalModifiers = event->modifiers();
271
    if ((event->button()&Qt::LeftButton) && multiselectActivated) {
272
        if (PreferencesGui::multiSelection()) {
273
            event->setModifiers(originalModifiers | Qt::ControlModifier);
274
        }
275

276
        multiselectActivated = false;
277
    }
278

279
    QGraphicsPathItem::mouseReleaseEvent(event);
280

281
    event->setModifiers(originalModifiers);
282
}
283

284
void QGIPrimPath::setFill(QColor c, Qt::BrushStyle s) {
285
    setFillColor(c);
286
    m_fillNormal = s;
287
    m_fillStyleCurrent = s;
288
}
289

290
void QGIPrimPath::setFill(QBrush b) {
291
    setFillColor(b.color());
292
    m_fillNormal = b.style();
293
    m_fillStyleCurrent = b.style();
294
}
295

296
void QGIPrimPath::resetFill() {
297
    m_colNormalFill = m_colDefFill;
298
    m_fillNormal = m_fillDef;
299
    m_fillStyleCurrent = m_fillDef;
300
}
301

302
//set PlainFill
303
void QGIPrimPath::setFillColor(QColor c)
304
{
305
    m_colNormalFill = c;
306
    m_fillColorCurrent = m_colNormalFill;
307
}
308

309
void QGIPrimPath::setCurrentPen()
310
{
311
    m_pen.setWidthF(m_width);
312
    m_pen.setColor(m_colCurrent);
313
}
314

315
void QGIPrimPath::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
316
    QStyleOptionGraphicsItem myOption(*option);
317
    myOption.state &= ~QStyle::State_Selected;
318

319
    setCurrentPen();
320
    setPen(m_pen);
321

322
    m_brush.setColor(m_fillColorCurrent);
323
    m_brush.setStyle(m_fillStyleCurrent);
324
    setBrush(m_brush);
325

326
    QGraphicsPathItem::paint (painter, &myOption, widget);
327
}
328

329

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

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

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

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