1
/****************************************************************************
3
* Draw circle by foci and a point on circle
5
Copyright (C) 2012 Dongxu Li (dongxuli2011@gmail.com)
6
Copyright (C) 2011 R. van Twisk (librecad@rvt.dds.nl)
8
This program is free software; you can redistribute it and/or
9
modify it under the terms of the GNU General Public License
10
as published by the Free Software Foundation; either version 2
11
of the License, or (at your option) any later version.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
**********************************************************************/
23
#include "rs_actiondrawcircletan2.h"
26
#include "rs_dialogfactory.h"
27
#include "rs_graphicview.h"
28
#include "rs_commandevent.h"
34
RS_ActionDrawCircleTan2::RS_ActionDrawCircleTan2(
35
RS_EntityContainer& container,
36
RS_GraphicView& graphicView)
37
:RS_PreviewActionInterface("Draw circle inscribed",
38
container, graphicView),
39
cData(RS_Vector(0.,0.),1.),
43
enTypeList<<RS2::EntityLine<<RS2::EntityArc<<RS2::EntityCircle;
48
RS_ActionDrawCircleTan2::~RS_ActionDrawCircleTan2() {
53
QAction* RS_ActionDrawCircleTan2::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/) {
56
action = new QAction(tr("Circle Tangential &2"), NULL);
57
action->setIcon(QIcon(":/extui/circletan2.png"));
61
void RS_ActionDrawCircleTan2::init(int status) {
62
RS_PreviewActionInterface::init(status);
64
RS_Snapper::suspend();
67
if (status==SetCircle1) {
73
void RS_ActionDrawCircleTan2::finish(bool updateTB){
75
for(int i=0;i<circles.size();i++) {
76
if(circles.at(i) != NULL) circles.at(i)->setHighlighted(false);
78
graphicView->redraw(RS2::RedrawDrawing);
81
RS_PreviewActionInterface::finish(updateTB);
84
//void RS_ActionDrawCircleTan2::finish(bool updateTB){
85
//// for(int i=0;i<circles.size();i++) circles[i]->setHighlighted(false);
86
//// graphicView->redraw(RS2::RedrawDrawing);
88
// RS_PreviewActionInterface::finish(updateTB);
92
void RS_ActionDrawCircleTan2::trigger() {
93
// std::cout<<__FILE__<<" : "<<__FUNCTION__<<" : line "<<__LINE__<<std::endl;
94
// std::cout<<"begin"<<std::endl;
96
RS_PreviewActionInterface::trigger();
99
RS_Circle* circle=new RS_Circle(container, cData);
101
container->addEntity(circle);
104
if (document!=NULL) {
105
document->startUndoCycle();
106
document->addUndoable(circle);
107
document->endUndoCycle();
110
for(int i=0;i<circles.size();i++) circles[i]->setHighlighted(false);
111
graphicView->redraw(RS2::RedrawDrawing);
115
setStatus(SetCircle1);
117
RS_DEBUG->print("RS_ActionDrawCircleTan2::trigger():"
118
" entity added: %d", circle->getId());
123
void RS_ActionDrawCircleTan2::mouseMoveEvent(QMouseEvent* e) {
124
RS_DEBUG->print("RS_ActionDrawCircleTan2::mouseMoveEvent begin");
126
switch(getStatus() ){
128
// RS_Entity* en = catchEntity(e, enTypeList, RS2::ResolveAll);
129
coord= graphicView->toGraph(e->x(), e->y());
130
// circles[getStatus()]=static_cast<RS_Line*>(en);
131
if(preparePreview()) {
133
RS_Circle* e=new RS_Circle(preview, cData);
134
preview->addEntity(e);
142
RS_DEBUG->print("RS_ActionDrawCircleTan2::mouseMoveEvent end");
145
void RS_ActionDrawCircleTan2::setRadius(const double& r)
148
if(getStatus() == SetCenter){
149
RS_Circle c(NULL,cData);
150
centers=c.createTan2(circles,cData.radius);
154
bool RS_ActionDrawCircleTan2::getCenters(){
155
if(getStatus() != SetCircle2) return false;
156
RS_Circle c(NULL,cData);
157
centers=c.createTan2(circles,cData.radius);
158
valid= (centers.size()>0);
162
bool RS_ActionDrawCircleTan2::preparePreview(){
164
cData.center=centers.getClosest(coord);
169
RS_Entity* RS_ActionDrawCircleTan2::catchCircle(QMouseEvent* e) {
171
RS_Entity* en = catchEntity(e,enTypeList, RS2::ResolveAll);
172
if(en == NULL) return ret;
173
if(en->isVisible()==false) return ret;
174
for(int i=0;i<getStatus();i++) {
175
if(en->getId() == circles[i]->getId()) return ret; //do not pull in the same line again
177
if(en->getParent() != NULL) {
178
if ( en->getParent()->rtti() == RS2::EntityInsert /**Insert*/
179
|| en->getParent()->rtti() == RS2::EntitySpline
180
|| en->getParent()->rtti() == RS2::EntityMText /**< Text 15*/
181
|| en->getParent()->rtti() == RS2::EntityText /**< Text 15*/
182
|| en->getParent()->rtti() == RS2::EntityDimAligned /**< Aligned Dimension */
183
|| en->getParent()->rtti() == RS2::EntityDimLinear /**< Linear Dimension */
184
|| en->getParent()->rtti() == RS2::EntityDimRadial /**< Radial Dimension */
185
|| en->getParent()->rtti() == RS2::EntityDimDiametric /**< Diametric Dimension */
186
|| en->getParent()->rtti() == RS2::EntityDimAngular /**< Angular Dimension */
187
|| en->getParent()->rtti() == RS2::EntityDimLeader /**< Leader Dimension */
195
void RS_ActionDrawCircleTan2::mouseReleaseEvent(QMouseEvent* e) {
196
// Proceed to next status
197
if (e->button()==Qt::LeftButton) {
199
switch (getStatus()) {
202
RS_Entity* en = catchCircle(e);
203
if (en==NULL) return;
204
circles.resize(getStatus());
205
circles.push_back(static_cast<RS_AtomicEntity*>(en));
206
if(getStatus()==SetCircle1 || getCenters()){
207
circles.at(circles.size()-1)->setHighlighted(true);
208
graphicView->redraw(RS2::RedrawDrawing);
209
setStatus(getStatus()+1);
214
coord= graphicView->toGraph(e->x(), e->y());
215
if( preparePreview()) trigger();
221
} else if (e->button()==Qt::RightButton) {
222
// Return to last status:
224
circles[getStatus()-1]->setHighlighted(false);
226
graphicView->redraw(RS2::RedrawDrawing);
234
//void RS_ActionDrawCircleTan2::coordinateEvent(RS_CoordinateEvent* e) {
238
//fixme, support command line
241
void RS_ActionDrawCircleTan2::commandEvent(RS_CommandEvent* e) {
242
QString c = e->getCommand().toLower();
244
if (checkCommand("help", c)) {
245
if (RS_DIALOGFACTORY!=NULL) {
246
RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
247
+ getAvailableCommands().join(", "));
252
switch (getStatus()) {
255
double m = RS_Math::eval(c, &ok);
257
ratio = m / major.magnitude();
261
setStatus(SetAngle1);
264
if (RS_DIALOGFACTORY!=NULL) {
265
RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
273
double a = RS_Math::eval(c, &ok);
275
angle1 = RS_Math::deg2rad(a);
276
setStatus(SetAngle2);
278
if (RS_DIALOGFACTORY!=NULL) {
279
RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
287
double a = RS_Math::eval(c, &ok);
289
angle2 = RS_Math::deg2rad(a);
292
if (RS_DIALOGFACTORY!=NULL) {
293
RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
306
void RS_ActionDrawCircleTan2::showOptions() {
307
RS_DEBUG->print("RS_ActionDrawCircleTan2::showOptions");
308
if(RS_DIALOGFACTORY != NULL){
309
RS_ActionInterface::showOptions();
311
RS_DIALOGFACTORY->requestOptions(this, true);
313
RS_DEBUG->print("RS_ActionDrawCircleTan2::showOptions: OK");
318
void RS_ActionDrawCircleTan2::hideOptions() {
319
if(RS_DIALOGFACTORY != NULL){
320
RS_ActionInterface::hideOptions();
322
RS_DIALOGFACTORY->requestOptions(this, false);
327
QStringList RS_ActionDrawCircleTan2::getAvailableCommands() {
334
void RS_ActionDrawCircleTan2::updateMouseButtonHints() {
335
if (RS_DIALOGFACTORY!=NULL) {
336
switch (getStatus()) {
338
RS_DIALOGFACTORY->updateMouseWidget(tr("Specify the first line/arc/circle"),
343
RS_DIALOGFACTORY->updateMouseWidget(tr("Specify the second line/arc/circle"),
348
RS_DIALOGFACTORY->updateMouseWidget(tr("Select the center of the tangent circle"),
352
RS_DIALOGFACTORY->updateMouseWidget("", "");
360
void RS_ActionDrawCircleTan2::updateMouseCursor() {
361
graphicView->setMouseCursor(RS2::CadCursor);
366
//void RS_ActionDrawCircleTan2::updateToolBar() {
367
// if (RS_DIALOGFACTORY!=NULL) {
368
// if (isFinished()) {
369
// RS_DIALOGFACTORY->resetToolBar();