1
/****************************************************************************
3
** This file is part of the LibreCAD project, a 2D CAD program
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
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.
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.
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
23
** This copyright notice MUST APPEAR in all copies of the script!
25
**********************************************************************/
27
#include "rs_actiondrawarc.h"
30
#include "rs_dialogfactory.h"
31
#include "rs_graphicview.h"
32
#include "rs_commands.h"
33
#include "rs_commandevent.h"
36
RS_ActionDrawArc::RS_ActionDrawArc(RS_EntityContainer& container,
37
RS_GraphicView& graphicView)
38
:RS_PreviewActionInterface("Draw arcs",
39
container, graphicView) {
46
RS_ActionDrawArc::~RS_ActionDrawArc() {}
48
QAction* RS_ActionDrawArc::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/) {
49
// (tr("Arc: Center, Point, Angles")
50
QAction* action = new QAction(tr("&Center, Point, Angles"),NULL);
51
action->setIcon(QIcon(":/extui/arcscraa.png"));
52
//action->zetStatusTip(tr("Draw arcs"));
57
void RS_ActionDrawArc::reset() {
58
//bool rev = data.reversed;
61
data = RS_ArcData(RS_Vector(false),
66
data = RS_ArcData(RS_Vector(false),
75
void RS_ActionDrawArc::init(int status) {
76
RS_PreviewActionInterface::init(status);
83
void RS_ActionDrawArc::trigger() {
84
RS_PreviewActionInterface::trigger();
86
RS_Arc* arc = new RS_Arc(container,
88
arc->setLayerToActive();
89
arc->setPenToActive();
90
container->addEntity(arc);
94
document->startUndoCycle();
95
document->addUndoable(arc);
96
document->endUndoCycle();
99
graphicView->redraw(RS2::RedrawDrawing);
100
graphicView->moveRelativeZero(arc->getCenter());
102
setStatus(SetCenter);
105
RS_DEBUG->print("RS_ActionDrawArc::trigger(): arc added: %d",
111
void RS_ActionDrawArc::mouseMoveEvent(QMouseEvent* e) {
112
RS_DEBUG->print("RS_ActionDrawArc::mouseMoveEvent begin");
114
RS_Vector mouse = snapPoint(e);
115
switch (getStatus()) {
121
if (data.center.valid) {
122
data.radius = data.center.distanceTo(mouse);
124
preview->addEntity(new RS_Circle(preview,
125
RS_CircleData(data.center,
132
data.angle1 = data.center.angleTo(mouse);
134
data.angle2 = RS_Math::correctAngle(data.angle1-M_PI/3);
136
data.angle2 = RS_Math::correctAngle(data.angle1+M_PI/3);
139
preview->addEntity(new RS_Arc(preview,
145
data.angle2 = data.center.angleTo(mouse);
147
preview->addEntity(new RS_Arc(preview,
153
data.angle2 = data.angle1 + data.center.angleTo(mouse);
155
preview->addEntity(new RS_Arc(preview,
160
case SetChordLength: {
161
double x = data.center.distanceTo(mouse);
162
if (fabs(x/(2*data.radius))<=1.0) {
163
data.angle2 = data.angle1 + asin(x/(2*data.radius)) * 2;
165
preview->addEntity(new RS_Arc(preview,
177
RS_DEBUG->print("RS_ActionDrawArc::mouseMoveEvent end");
182
void RS_ActionDrawArc::mouseReleaseEvent(QMouseEvent* e) {
183
if (e->button()==Qt::LeftButton) {
184
RS_CoordinateEvent ce(snapPoint(e));
185
coordinateEvent(&ce);
186
} else if (e->button()==Qt::RightButton) {
194
void RS_ActionDrawArc::coordinateEvent(RS_CoordinateEvent* e) {
198
RS_Vector mouse = e->getCoordinate();
200
switch (getStatus()) {
203
graphicView->moveRelativeZero(mouse);
204
setStatus(SetRadius);
208
if (data.center.valid) {
209
data.radius = data.center.distanceTo(mouse);
211
setStatus(SetAngle1);
215
data.angle1 = data.center.angleTo(mouse);
216
setStatus(SetAngle2);
220
data.angle2 = data.center.angleTo(mouse);
225
data.angle2 = data.angle1 + data.center.angleTo(mouse);
229
case SetChordLength: {
230
double x = data.center.distanceTo(mouse);
231
if (fabs(x/(2*data.radius))<=1.0) {
232
data.angle2 = data.angle1 + asin(x/(2*data.radius)) * 2;
245
void RS_ActionDrawArc::commandEvent(RS_CommandEvent* e) {
246
QString c = e->getCommand().toLower();
248
if (RS_COMMANDS->checkCommand("help", c)) {
249
if (RS_DIALOGFACTORY!=NULL) {
250
RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
251
+ getAvailableCommands().join(", "));
256
if (RS_COMMANDS->checkCommand("reversed", c)) {
258
setReversed(!isReversed());
260
if (RS_DIALOGFACTORY!=NULL) {
261
RS_DIALOGFACTORY->requestOptions(this, true, true);
266
switch (getStatus()) {
270
double r = RS_Math::eval(c, &ok);
273
setStatus(SetAngle1);
275
if (RS_DIALOGFACTORY!=NULL) {
276
RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
284
double a = RS_Math::eval(c, &ok);
286
data.angle1 = RS_Math::deg2rad(a);
287
setStatus(SetAngle2);
289
if (RS_DIALOGFACTORY!=NULL) {
290
RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
297
if (RS_COMMANDS->checkCommand("angle", c)) {
298
setStatus(SetIncAngle);
299
} else if (RS_COMMANDS->checkCommand("chord length", c)) {
300
setStatus(SetChordLength);
303
double a = RS_Math::eval(c, &ok);
305
data.angle2 = RS_Math::deg2rad(a);
308
if (RS_DIALOGFACTORY!=NULL) {
309
RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
318
double a = RS_Math::eval(c, &ok);
320
data.angle2 = data.angle1 + RS_Math::deg2rad(a);
323
if (RS_DIALOGFACTORY!=NULL) {
324
RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
330
case SetChordLength: {
332
double l = RS_Math::eval(c, &ok);
334
if (fabs(l/(2*data.radius))<=1.0) {
335
data.angle2 = data.angle1 + asin(l/(2*data.radius)) * 2;
338
if (RS_DIALOGFACTORY!=NULL) {
339
RS_DIALOGFACTORY->commandMessage(
340
tr("Not a valid chord length"));
344
if (RS_DIALOGFACTORY!=NULL) {
345
RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
358
QStringList RS_ActionDrawArc::getAvailableCommands() {
360
cmd += RS_COMMANDS->command("reversed");
365
void RS_ActionDrawArc::updateMouseButtonHints() {
366
if (RS_DIALOGFACTORY!=NULL) {
367
switch (getStatus()) {
369
RS_DIALOGFACTORY->updateMouseWidget(tr("Specify center"), tr("Cancel"));
372
RS_DIALOGFACTORY->updateMouseWidget(tr("Specify radius"), tr("Back"));
375
RS_DIALOGFACTORY->updateMouseWidget(
376
tr("Specify start angle:"), tr("Back"));
379
RS_DIALOGFACTORY->updateMouseWidget(
380
tr("Specify end angle or [angle/chord length]"),
384
RS_DIALOGFACTORY->updateMouseWidget(tr("Specify included angle:"),
388
RS_DIALOGFACTORY->updateMouseWidget(tr("Specify chord length:"),
392
RS_DIALOGFACTORY->updateMouseWidget("", "");
400
void RS_ActionDrawArc::showOptions() {
401
RS_ActionInterface::showOptions();
403
if (RS_DIALOGFACTORY!=NULL) {
404
RS_DIALOGFACTORY->requestOptions(this, true);
410
void RS_ActionDrawArc::hideOptions() {
411
RS_ActionInterface::hideOptions();
413
if (RS_DIALOGFACTORY!=NULL) {
414
RS_DIALOGFACTORY->requestOptions(this, false);
420
void RS_ActionDrawArc::updateMouseCursor() {
421
graphicView->setMouseCursor(RS2::CadCursor);
426
//void RS_ActionDrawArc::updateToolBar() {
427
// if (RS_DIALOGFACTORY!=NULL) {
428
// if (isFinished()) {
429
// RS_DIALOGFACTORY->resetToolBar();
431
// RS_DIALOGFACTORY->showCadToolBar(rtti());