~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/actions/rs_actiondrawcirclecr.cpp

  • 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
#include "rs_actiondrawcirclecr.h"
 
28
 
 
29
#include <QAction>
 
30
#include "rs_dialogfactory.h"
 
31
#include "rs_graphicview.h"
 
32
#include "rs_commandevent.h"
 
33
 
 
34
/**
 
35
 * Constructor.
 
36
 */
 
37
RS_ActionDrawCircleCR::RS_ActionDrawCircleCR(RS_EntityContainer& container,
 
38
        RS_GraphicView& graphicView)
 
39
        :RS_PreviewActionInterface("Draw circles CR",
 
40
                           container, graphicView) {
 
41
 
 
42
    reset();
 
43
}
 
44
 
 
45
 
 
46
 
 
47
RS_ActionDrawCircleCR::~RS_ActionDrawCircleCR() {}
 
48
 
 
49
 
 
50
QAction* RS_ActionDrawCircleCR::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/) {
 
51
        // "Circle: Center, Radius"
 
52
    QAction* action = new QAction(tr("Center, &Radius"), NULL);
 
53
                action->setIcon(QIcon(":/extui/circlescr.png"));
 
54
    //action->zetStatusTip(tr("Draw circles with center and radius"));
 
55
    return action;
 
56
}
 
57
 
 
58
 
 
59
void RS_ActionDrawCircleCR::reset() {
 
60
    data = RS_CircleData(RS_Vector(false), 0.0);
 
61
}
 
62
 
 
63
 
 
64
 
 
65
void RS_ActionDrawCircleCR::init(int status) {
 
66
    RS_PreviewActionInterface::init(status);
 
67
}
 
68
 
 
69
 
 
70
 
 
71
void RS_ActionDrawCircleCR::trigger() {
 
72
    RS_PreviewActionInterface::trigger();
 
73
 
 
74
    RS_Circle* circle = new RS_Circle(container,
 
75
                                      data);
 
76
    circle->setLayerToActive();
 
77
    circle->setPenToActive();
 
78
    container->addEntity(circle);
 
79
 
 
80
    // upd. undo list:
 
81
    if (document!=NULL) {
 
82
        document->startUndoCycle();
 
83
        document->addUndoable(circle);
 
84
        document->endUndoCycle();
 
85
    }
 
86
        graphicView->redraw(RS2::RedrawDrawing);
 
87
    graphicView->moveRelativeZero(circle->getCenter());
 
88
 
 
89
    setStatus(SetCenter);
 
90
 
 
91
    RS_DEBUG->print("RS_ActionDrawCircleCR::trigger(): circle added: %d",
 
92
                    circle->getId());
 
93
}
 
94
 
 
95
 
 
96
 
 
97
void RS_ActionDrawCircleCR::mouseMoveEvent(QMouseEvent* e) {
 
98
    RS_DEBUG->print("RS_ActionDrawCircleCR::mouseMoveEvent begin");
 
99
 
 
100
    RS_Vector mouse = snapPoint(e);
 
101
    switch (getStatus()) {
 
102
    case SetCenter:
 
103
        data.center = mouse;
 
104
        deletePreview();
 
105
        preview->addEntity(new RS_Circle(preview,
 
106
                                         data));
 
107
        drawPreview();
 
108
        break;
 
109
    }
 
110
 
 
111
    RS_DEBUG->print("RS_ActionDrawCircleCR::mouseMoveEvent end");
 
112
}
 
113
 
 
114
 
 
115
 
 
116
void RS_ActionDrawCircleCR::mouseReleaseEvent(QMouseEvent* e) {
 
117
    if (e->button()==Qt::LeftButton) {
 
118
        RS_CoordinateEvent ce(snapPoint(e));
 
119
        coordinateEvent(&ce);
 
120
    } else if (e->button()==Qt::RightButton) {
 
121
        deletePreview();
 
122
        init(getStatus()-1);
 
123
    }
 
124
}
 
125
 
 
126
 
 
127
 
 
128
void RS_ActionDrawCircleCR::coordinateEvent(RS_CoordinateEvent* e) {
 
129
    if (e==NULL) {
 
130
        return;
 
131
    }
 
132
 
 
133
    RS_Vector mouse = e->getCoordinate();
 
134
 
 
135
    switch (getStatus()) {
 
136
    case SetCenter:
 
137
        data.center = mouse;
 
138
        trigger();
 
139
        break;
 
140
 
 
141
    default:
 
142
        break;
 
143
    }
 
144
}
 
145
 
 
146
 
 
147
 
 
148
void RS_ActionDrawCircleCR::commandEvent(RS_CommandEvent* e) {
 
149
    QString c = e->getCommand().toLower();
 
150
 
 
151
    if (checkCommand("help", c)) {
 
152
        RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
 
153
                                         + getAvailableCommands().join(", "));
 
154
        return;
 
155
    }
 
156
 
 
157
    switch (getStatus()) {
 
158
    case SetCenter:
 
159
        if (checkCommand("radius", c)) {
 
160
            deletePreview();
 
161
            setStatus(SetRadius);
 
162
        }
 
163
        break;
 
164
 
 
165
    case SetRadius: {
 
166
            bool ok;
 
167
            double r = RS_Math::eval(c, &ok);
 
168
            if (ok==true) {
 
169
                data.radius = r;
 
170
            } else {
 
171
                RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
 
172
            }
 
173
            RS_DIALOGFACTORY->requestOptions(this, true, true);
 
174
            setStatus(SetCenter);
 
175
        }
 
176
        break;
 
177
 
 
178
    default:
 
179
        break;
 
180
    }
 
181
}
 
182
 
 
183
 
 
184
 
 
185
QStringList RS_ActionDrawCircleCR::getAvailableCommands() {
 
186
    QStringList cmd;
 
187
 
 
188
    switch (getStatus()) {
 
189
    case SetCenter:
 
190
        cmd += command("radius");
 
191
        break;
 
192
    default:
 
193
        break;
 
194
    }
 
195
 
 
196
    return cmd;
 
197
}
 
198
 
 
199
void RS_ActionDrawCircleCR::updateMouseButtonHints() {
 
200
    switch (getStatus()) {
 
201
    case SetCenter:
 
202
        RS_DIALOGFACTORY->updateMouseWidget(tr("Specify circle center"),
 
203
                                            tr("Cancel"));
 
204
        break;
 
205
    case SetRadius:
 
206
        RS_DIALOGFACTORY->updateMouseWidget(tr("Specify circle radius"),
 
207
                                            tr("Back"));
 
208
        break;
 
209
    default:
 
210
        RS_DIALOGFACTORY->updateMouseWidget("", "");
 
211
        break;
 
212
    }
 
213
}
 
214
 
 
215
 
 
216
 
 
217
void RS_ActionDrawCircleCR::showOptions() {
 
218
    RS_ActionInterface::showOptions();
 
219
 
 
220
    RS_DIALOGFACTORY->requestOptions(this, true);
 
221
}
 
222
 
 
223
 
 
224
 
 
225
void RS_ActionDrawCircleCR::hideOptions() {
 
226
    RS_ActionInterface::hideOptions();
 
227
 
 
228
    RS_DIALOGFACTORY->requestOptions(this, false);
 
229
}
 
230
 
 
231
 
 
232
 
 
233
void RS_ActionDrawCircleCR::updateMouseCursor() {
 
234
    graphicView->setMouseCursor(RS2::CadCursor);
 
235
}
 
236
 
 
237
 
 
238
 
 
239
//void RS_ActionDrawCircleCR::updateToolBar() {
 
240
//    if (RS_DIALOGFACTORY!=NULL) {
 
241
//        if (isFinished()) {
 
242
//            RS_DIALOGFACTORY->resetToolBar();
 
243
//        }
 
244
//    }
 
245
//}
 
246
 
 
247
 
 
248
// EOF
 
249