1 /*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*******************************************************************************
21 * Includes
22 ******************************************************************************/
23 #include "bi_polygon.h"
24
25 #include "../../project.h"
26 #include "../board.h"
27 #include "../boardlayerstack.h"
28
29 #include <librepcb/common/geometry/polygon.h>
30 #include <librepcb/common/graphics/graphicsscene.h>
31 #include <librepcb/common/graphics/polygongraphicsitem.h>
32
33 #include <QtCore>
34
35 /*******************************************************************************
36 * Namespace
37 ******************************************************************************/
38 namespace librepcb {
39 namespace project {
40
41 /*******************************************************************************
42 * Constructors / Destructor
43 ******************************************************************************/
44
BI_Polygon(Board & board,const BI_Polygon & other)45 BI_Polygon::BI_Polygon(Board& board, const BI_Polygon& other) : BI_Base(board) {
46 mPolygon.reset(new Polygon(Uuid::createRandom(), *other.mPolygon));
47 init();
48 }
49
BI_Polygon(Board & board,const SExpression & node,const Version & fileFormat)50 BI_Polygon::BI_Polygon(Board& board, const SExpression& node,
51 const Version& fileFormat)
52 : BI_Base(board) {
53 mPolygon.reset(new Polygon(node, fileFormat));
54 init();
55 }
56
BI_Polygon(Board & board,const Polygon & polygon)57 BI_Polygon::BI_Polygon(Board& board, const Polygon& polygon) : BI_Base(board) {
58 mPolygon.reset(new Polygon(polygon));
59 init();
60 }
61
BI_Polygon(Board & board,const Uuid & uuid,const GraphicsLayerName & layerName,const UnsignedLength & lineWidth,bool fill,bool isGrabArea,const Path & path)62 BI_Polygon::BI_Polygon(Board& board, const Uuid& uuid,
63 const GraphicsLayerName& layerName,
64 const UnsignedLength& lineWidth, bool fill,
65 bool isGrabArea, const Path& path)
66 : BI_Base(board) {
67 mPolygon.reset(
68 new Polygon(uuid, layerName, lineWidth, fill, isGrabArea, path));
69 init();
70 }
71
init()72 void BI_Polygon::init() {
73 mGraphicsItem.reset(
74 new PolygonGraphicsItem(*mPolygon, mBoard.getLayerStack()));
75 mGraphicsItem->setZValue(Board::ZValue_Default);
76
77 // connect to the "attributes changed" signal of the board
78 connect(&mBoard, &Board::attributesChanged, this,
79 &BI_Polygon::boardAttributesChanged);
80 }
81
~BI_Polygon()82 BI_Polygon::~BI_Polygon() noexcept {
83 mGraphicsItem.reset();
84 mPolygon.reset();
85 }
86
87 /*******************************************************************************
88 * General Methods
89 ******************************************************************************/
90
addToBoard()91 void BI_Polygon::addToBoard() {
92 if (isAddedToBoard()) {
93 throw LogicError(__FILE__, __LINE__);
94 }
95 BI_Base::addToBoard(mGraphicsItem.data());
96 }
97
removeFromBoard()98 void BI_Polygon::removeFromBoard() {
99 if (!isAddedToBoard()) {
100 throw LogicError(__FILE__, __LINE__);
101 }
102 BI_Base::removeFromBoard(mGraphicsItem.data());
103 }
104
serialize(SExpression & root) const105 void BI_Polygon::serialize(SExpression& root) const {
106 mPolygon->serialize(root);
107 }
108
109 /*******************************************************************************
110 * Inherited from BI_Base
111 ******************************************************************************/
112
getGrabAreaScenePx() const113 QPainterPath BI_Polygon::getGrabAreaScenePx() const noexcept {
114 return mGraphicsItem->sceneTransform().map(mGraphicsItem->shape());
115 }
116
getUuid() const117 const Uuid& BI_Polygon::getUuid() const noexcept {
118 return mPolygon->getUuid();
119 }
120
isSelectable() const121 bool BI_Polygon::isSelectable() const noexcept {
122 const GraphicsLayer* layer =
123 mBoard.getLayerStack().getLayer(*mPolygon->getLayerName());
124 return layer && layer->isVisible();
125 }
126
setSelected(bool selected)127 void BI_Polygon::setSelected(bool selected) noexcept {
128 BI_Base::setSelected(selected);
129 mGraphicsItem->setSelected(selected);
130 }
131
132 /*******************************************************************************
133 * Private Slots
134 ******************************************************************************/
135
boardAttributesChanged()136 void BI_Polygon::boardAttributesChanged() {
137 mGraphicsItem->update();
138 }
139
140 /*******************************************************************************
141 * End of File
142 ******************************************************************************/
143
144 } // namespace project
145 } // namespace librepcb
146