Seamly2D
Code documentation
scene_rect.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * @file scene_rect.cpp *
4  * @author DSCaskey *
5  * @date 3.30.2021 *
6  * Copyright (C) 2017 Seamly, LLC *
7  * *
8  * https://github.com/fashionfreedom/seamly2d *
9  * *
10  ***************************************************************************
11  **
12  ** Seamly2D is free software: you can redistribute it and/or modify
13  ** it under the terms of the GNU General Public License as published by
14  ** the Free Software Foundation, either version 3 of the License, or
15  ** (at your option) any later version.
16  **
17  ** Seamly2D is distributed in the hope that it will be useful,
18  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  ** GNU General Public License for more details.
21  **
22  ** You should have received a copy of the GNU General Public License
23  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
24  **
25  **************************************************************************/
26 
27 #include "scene_rect.h"
28 #include "../vmisc/def.h"
29 #include "../vmisc/vabstractapplication.h"
30 #include "../vgeometry/vpointf.h"
31 #include "global.h"
32 #include "scalesceneitems.h"
33 
34 #include <QBrush>
35 #include <QFont>
36 #include <QPen>
37 #include <QColor>
38 #include <QtDebug>
39 
40 //---------------------------------------------------------------------------------------------------------------------
41 SceneRect::SceneRect(const QColor &lineColor, QGraphicsItem *parent)
42  : QGraphicsRectItem(parent)
43  , m_rectColor(QColor(correctColor(this,lineColor)))
44  , m_onlyPoint(false)
45  , m_isHovered(false)
46  , m_showPointName(true)
47 {
48  setZValue(100);
49  setAcceptHoverEvents(true);
50  setFlag(QGraphicsItem::ItemIsFocusable, true); // For keyboard input focus
51 }
52 
53 //---------------------------------------------------------------------------------------------------------------------
54 void SceneRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
55 {
56  const qreal scale = sceneScale(scene());
57 
58  setRectPen(scale);
59  scaleRectSize(this, scale * .75);
60 
61  QGraphicsRectItem::paint(painter, option, widget);
62 }
63 
64 //---------------------------------------------------------------------------------------------------------------------
66 {
67  setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
68  setPos(static_cast<QPointF>(point));
69  setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
70 }
71 
72 //---------------------------------------------------------------------------------------------------------------------
73 void SceneRect::setOnlyPoint(bool value)
74 {
75  m_onlyPoint = value;
76 }
77 
78 //---------------------------------------------------------------------------------------------------------------------
80 {
81  return m_onlyPoint;
82 }
83 
84 void SceneRect::setPointColor(const QString &value)
85 {
86  m_rectColor = QColor(value);
87 }
88 
89 //---------------------------------------------------------------------------------------------------------------------
90 void SceneRect::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
91 {
92  m_isHovered = true;
93  QGraphicsRectItem::hoverEnterEvent(event);
94 }
95 
96 //---------------------------------------------------------------------------------------------------------------------
97 void SceneRect::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
98 {
99  m_isHovered = false;
100  QGraphicsRectItem::hoverLeaveEvent(event);
101 }
102 
103 //---------------------------------------------------------------------------------------------------------------------
104 void SceneRect::setRectPen(qreal scale)
105 {
106  const qreal width = scaleWidth(m_isHovered ? widthMainLine : widthHairLine, scale);
107 
108  QColor brushColor = m_rectColor;
109  brushColor.setAlpha(100);
110 
111  setPen(QPen(m_rectColor, width));
112  if (!qApp->Settings()->isWireframe())
113  {
114  setBrush(QBrush(brushColor, Qt::SolidPattern));
115  }
116  else
117  {
118  setBrush(QBrush(brushColor, Qt::NoBrush));
119  }
120 }
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) Q_DECL_OVERRIDE
Definition: scene_rect.cpp:54
void setOnlyPoint(bool value)
Definition: scene_rect.cpp:73
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
Definition: scene_rect.cpp:90
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
Definition: scene_rect.cpp:97
QColor m_rectColor
Definition: scene_rect.h:51
void setPointColor(const QString &value)
Definition: scene_rect.cpp:84
bool m_isHovered
Definition: scene_rect.h:53
bool m_onlyPoint
m_rectColor color of point.
Definition: scene_rect.h:52
void setRectPen(qreal scale)
Definition: scene_rect.cpp:104
virtual void refreshPointGeometry(const VPointF &point)
Definition: scene_rect.cpp:65
SceneRect(const QColor &lineColor, QGraphicsItem *parent=nullptr)
Definition: scene_rect.cpp:41
bool isOnlyPoint() const
Definition: scene_rect.cpp:79
The VPointF class keep data of point.
Definition: vpointf.h:75
const qreal widthMainLine
Definition: global.cpp:60
qreal sceneScale(QGraphicsScene *scene)
Definition: global.cpp:63
qreal scaleWidth(qreal width, qreal scale)
Definition: global.cpp:130
QColor correctColor(const QGraphicsItem *item, const QColor &color)
Definition: global.cpp:80
void scaleRectSize(QGraphicsRectItem *item, qreal scale)
Definition: global.cpp:122
const qreal widthHairLine
Definition: global.cpp:61
#define qApp
Definition: vapplication.h:67