~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/lib/actions/rs_snapper.h

  • Committer: Scott Howard
  • Date: 2014-02-21 19:07:55 UTC
  • Revision ID: showard@debian.org-20140221190755-csjax9wb146hgdq4
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** This file is part of the LibreCAD project, a 2D CAD program
 
4
**
 
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
 
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
 
7
**
 
8
**
 
9
** This file may be distributed and/or modified under the terms of the
 
10
** GNU General Public License version 2 as published by the Free Software
 
11
** Foundation and appearing in the file gpl-2.0.txt included in the
 
12
** packaging of this file.
 
13
**
 
14
** This program is distributed in the hope that it will be useful,
 
15
** but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
** GNU General Public License for more details.
 
18
**
 
19
** You should have received a copy of the GNU General Public License
 
20
** along with this program; if not, write to the Free Software
 
21
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
22
**
 
23
** This copyright notice MUST APPEAR in all copies of the script!
 
24
**
 
25
**********************************************************************/
 
26
 
 
27
#pragma once
 
28
//#ifndef RS_SNAPPER_H
 
29
//#define RS_SNAPPER_H
 
30
 
 
31
#include "rs_entitycontainer.h"
 
32
 
 
33
#include "rs.h"
 
34
#include "rs_coordinateevent.h"
 
35
 
 
36
class RS_Entity;
 
37
class RS_GraphicView;
 
38
class RS_Vector;
 
39
class RS_Preview;
 
40
class QMouseEvent;
 
41
 
 
42
/**
 
43
  * This class holds information on how to snap the mouse.
 
44
  *
 
45
  * @author Kevin Cox
 
46
  */
 
47
struct RS_SnapMode {
 
48
public:
 
49
    bool snapFree;     /// Whether to snap freely
 
50
    bool snapGrid;     /// Whether to snap to grid or not.
 
51
    bool snapEndpoint;     /// Whether to snap to endpoints or not.
 
52
    bool snapMiddle;       /// Whether to snap to midpoints or not.
 
53
    bool snapDistance;       /// Whether to snap to distance from endpoints or not.
 
54
    bool snapCenter;       /// Whether to snap to centers or not.
 
55
    bool snapIntersection; /// Whether to snap to intersections or not.
 
56
 
 
57
    bool snapOnEntity;     /// Whether to snap to entities or not.
 
58
 
 
59
    RS2::SnapRestriction restriction; /// The restriction on the free snap.
 
60
 
 
61
    double distance; /// The distance to snap before defaulting to free snaping.
 
62
 
 
63
    /**
 
64
      * Default Constructor
 
65
      *
 
66
      * Creates a RS_SnapMode that specifies only free snapping.
 
67
      *
 
68
      */
 
69
    RS_SnapMode() { hardReset(); }
 
70
 
 
71
    /**
 
72
      * Disable all snapping.
 
73
      *
 
74
      * This effectivly puts the object into free snap mode.
 
75
      *
 
76
      * @returns A refrence to itself.
 
77
      */
 
78
    RS_SnapMode &clear(void) {
 
79
        snapFree     = false;
 
80
        snapGrid     = false;
 
81
        snapEndpoint     = false;
 
82
        snapMiddle       = false;
 
83
        snapDistance       = false;
 
84
        snapCenter       = false;
 
85
        snapOnEntity     = false;
 
86
        snapIntersection = false;
 
87
 
 
88
        restriction = RS2::RestrictNothing;
 
89
 
 
90
        return *this;
 
91
    }
 
92
 
 
93
    /**
 
94
     * Reset to default settings
 
95
     *
 
96
     * @returns A refrence to itself.
 
97
     */
 
98
    RS_SnapMode &hardReset(void) {
 
99
        snapFree     = false;
 
100
        snapGrid     = false;
 
101
        snapEndpoint     = false;
 
102
        snapMiddle       = false;
 
103
        snapDistance       = false;
 
104
        snapCenter       = false;
 
105
        snapOnEntity     = false;
 
106
        snapIntersection = false;
 
107
 
 
108
        restriction = RS2::RestrictNothing;
 
109
 
 
110
        distance = 5;
 
111
 
 
112
        return *this;
 
113
    }
 
114
};
 
115
 
 
116
/**
 
117
 * This class is used for snapping functions in a graphic view.
 
118
 * Actions are usually derrived from this base class if they need
 
119
 * to catch entities or snap to coordinates. Use the methods to
 
120
 * retrieve a graphic coordinate from a mouse coordinate.
 
121
 *
 
122
 * Possible snapping functions are described in RS_SnapMode.
 
123
 *
 
124
 * @author Andrew Mustun
 
125
 */
 
126
class RS_Snapper {
 
127
public:
 
128
    RS_Snapper(RS_EntityContainer& container, RS_GraphicView& graphicView);
 
129
    virtual ~RS_Snapper();
 
130
 
 
131
    void init();
 
132
        void finish();
 
133
 
 
134
    /**
 
135
     * @return Pointer to the entity which was the key entity for the
 
136
     * last successful snapping action. If the snap mode is "end point"
 
137
     * the key entity is the entity whos end point was caught.
 
138
     * If the snap mode didn't require an entity (e.g. free, grid) this
 
139
     * method will return NULL.
 
140
     */
 
141
    RS_Entity* getKeyEntity() {
 
142
        return keyEntity;
 
143
    }
 
144
 
 
145
    /** Sets a new snap mode. */
 
146
    void setSnapMode(const RS_SnapMode& snapMode);
 
147
 
 
148
    RS_SnapMode *getSnapMode(void) {
 
149
        return &this->snapMode;
 
150
    }
 
151
    /** Sets a new snap restriction. */
 
152
    void setSnapRestriction(RS2::SnapRestriction /*snapRes*/) {
 
153
        //this->snapRes = snapRes;
 
154
    }
 
155
 
 
156
        /**
 
157
         * Sets the snap range in pixels for catchEntity().
 
158
         *
 
159
         * @see catchEntity()
 
160
         */
 
161
        void setSnapRange(int r) {
 
162
                snapRange = r;
 
163
        }
 
164
 
 
165
        /**manually set snapPoint*/
 
166
    RS_Vector snapPoint(const RS_Vector& coord, bool setSpot = false);
 
167
    RS_Vector snapPoint(QMouseEvent* e);
 
168
    RS_Vector snapFree(QMouseEvent* e);
 
169
 
 
170
    RS_Vector snapFree(const RS_Vector& coord);
 
171
    RS_Vector snapGrid(const RS_Vector& coord);
 
172
    RS_Vector snapEndpoint(const RS_Vector& coord);
 
173
    RS_Vector snapOnEntity(const RS_Vector& coord);
 
174
    RS_Vector snapCenter(const RS_Vector& coord);
 
175
    RS_Vector snapMiddle(const RS_Vector& coord);
 
176
    RS_Vector snapDist(const RS_Vector& coord);
 
177
    RS_Vector snapIntersection(const RS_Vector& coord);
 
178
    //RS_Vector snapDirect(RS_Vector coord, bool abs);
 
179
 
 
180
    RS_Vector restrictOrthogonal(const RS_Vector& coord);
 
181
    RS_Vector restrictHorizontal(const RS_Vector& coord);
 
182
    RS_Vector restrictVertical(const RS_Vector& coord);
 
183
 
 
184
 
 
185
    //RS_Entity* catchLeafEntity(const RS_Vector& pos);
 
186
    //RS_Entity* catchLeafEntity(QMouseEvent* e);
 
187
    RS_Entity* catchEntity(const RS_Vector& pos,
 
188
                           RS2::ResolveLevel level=RS2::ResolveNone);
 
189
    RS_Entity* catchEntity(QMouseEvent* e,
 
190
                           RS2::ResolveLevel level=RS2::ResolveNone);
 
191
    // catch Entity closest to pos and of the given entity type of enType, only search for a particular entity type
 
192
    RS_Entity* catchEntity(const RS_Vector& pos, RS2::EntityType enType,
 
193
                           RS2::ResolveLevel level=RS2::ResolveNone);
 
194
    RS_Entity* catchEntity(QMouseEvent* e, RS2::EntityType enType,
 
195
                           RS2::ResolveLevel level=RS2::ResolveNone);
 
196
    RS_Entity* catchEntity(QMouseEvent* e, const QVector<RS2::EntityType>& enTypeList,
 
197
                           RS2::ResolveLevel level=RS2::ResolveNone);
 
198
 
 
199
    /**
 
200
     * Suspends this snapper while another action takes place.
 
201
     */
 
202
    virtual void suspend() {
 
203
                // RVT Don't delete the snapper here!
 
204
        // RVT_PORT (can be deleted)();
 
205
        snapSpot = snapCoord = RS_Vector(false);
 
206
    }
 
207
 
 
208
    /**
 
209
     * Resumes this snapper after it has been suspended.
 
210
     */
 
211
    virtual void resume() {
 
212
        drawSnapper();
 
213
    }
 
214
 
 
215
    virtual void hideOptions();
 
216
    virtual void showOptions();
 
217
 
 
218
    void drawSnapper();
 
219
    static unsigned int snapModeToInt(const RS_SnapMode& s);
 
220
    static RS_SnapMode intToSnapMode(unsigned int);
 
221
 
 
222
protected:
 
223
    void deleteSnapper();
 
224
    double getSnapRange() const;
 
225
    RS_EntityContainer* container;
 
226
    RS_GraphicView* graphicView;
 
227
    RS_Entity* keyEntity;
 
228
    RS_Vector snapCoord;
 
229
    RS_Vector snapSpot;
 
230
    RS_SnapMode snapMode;
 
231
    //RS2::SnapRestriction snapRes;
 
232
    /**
 
233
     * Snap distance for snaping to points with a
 
234
     * given distance from endpoints.
 
235
     */
 
236
    double distance;
 
237
    /**
 
238
     * Snap to equidistant middle points
 
239
     * default to 1, i.e., equidistant to start/end points
 
240
     */
 
241
    int middlePoints;
 
242
        /**
 
243
         * Snap range for catching entities.
 
244
         */
 
245
        int snapRange;
 
246
        /**
 
247
         * Show large cross hairs.
 
248
         */
 
249
        bool showCrosshairs;
 
250
        bool finished;
 
251
};
 
252
 
 
253
//#endif
 
254
//EOF