-
Notifications
You must be signed in to change notification settings - Fork 1
/
PCBObject.h
143 lines (117 loc) · 4.25 KB
/
PCBObject.h
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
/*
Copyright (C) 2010-2011 Igor Izyumin
This file is part of xpcb.
xpcb is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
xpcb 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with xpcb. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PCBOBJECT_H
#define PCBOBJECT_H
#include <QObject>
#include <QPainter>
#include <QUndoCommand>
#include <QChildEvent>
#include "global.h"
#include "Log.h"
// Forward declarations for visitor abstract class
//class Area;
//class Line;
//class PartPin;
//class Pin;
//class Part;
//class Text;
//class Vertex;
//class Segment;
//class Padstack;
//class PCBObjectVisitor;
class PCBDoc;
class PCBObjState;
/// Abstract base class for PCB graphical objects
/// PCBObject is an abstract base class for all graphical PCB objects. It provides a common
/// interface for working with PCB objects, such as selection, collision detection, visibility,
/// and drawing. It also provides a facility for identifying particular objects
class PCBObject : public QObject
{
Q_OBJECT
public:
explicit PCBObject(QObject *parent);
/// Draws the object using the provided QPainter. This function is
/// called multiple times during a single redraw operation, once for each layer.
/// \param painter the painter to use
/// \param layer the PCB layer to draw
virtual void draw(QPainter *painter, const Layer& layer) const = 0;
/// Returns the object's bounding box
virtual QRect bbox() const = 0;
/// Returns the unique object identifier
/// \return object ID
int getid() const {return objID;}
/// Returns true if the object was hit (pt is less than the specified
/// distance away from the object).
virtual bool testHit(QPoint /* pt */, int /* distance */,
const Layer& /*l*/) const { return false; }
/// Returns the object's transform (from the object's coordinate system to
/// PCB coordinates).
/// This interface is used to map child widgets into the parent's coordinate system.
/// This is used by part and footprint description text, pins, padstacks,
/// and other PCBObjects that are contained in another PCBObject.
/// The default implementation returns a null transform.
virtual QTransform transform() const;
/// Returns a snapshot of this object's state (the memento pattern).
virtual PCBObjState getState() const = 0;
/// Restores this object's state from the supplied object. Returns true
/// if successful, false otherwise.
virtual bool loadState(PCBObjState &state) = 0;
signals:
/// Emitted when the object is modified
void changed();
/// Emitted when the object's transform changes
void transformChanged(QTransform t);
protected slots:
virtual void onParentTransformChanged(QTransform) {}
protected:
virtual void childEvent(QChildEvent *);
private:
static int getNextID();
int objID;
static int nextObjID;
};
/// Abstract base class for PCBObject mementos
class PCBObjStateInternal
{
public:
// needed to make object polymorphic for RTTI
virtual ~PCBObjStateInternal() { }
};
/// Wrapper class that takes care of dealing with smartpointers.
class PCBObjState
{
public:
PCBObjState() {}
PCBObjState(PCBObjStateInternal* ptr) : mPtr(ptr) { }
const QSharedPointer<PCBObjStateInternal> ptr() const { return mPtr; }
protected:
QSharedPointer<PCBObjStateInternal> mPtr;
};
/// Universal undo command
class PCBObjEditCmd : public QUndoCommand
{
public:
PCBObjEditCmd(QUndoCommand* parent, QSharedPointer<PCBObject> obj,
PCBObjState prevState)
: QUndoCommand(parent), mObj(obj), mPrevState(prevState),
mNewState(obj->getState()) {}
virtual void undo() { mObj->loadState(mPrevState); }
virtual void redo() { mObj->loadState(mNewState); }
private:
QSharedPointer<PCBObject> mObj;
PCBObjState mPrevState;
PCBObjState mNewState;
};
#endif // PCBOBJECT_H