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_1p.h"
26
#include "rs_dialogfactory.h"
27
#include "rs_graphicview.h"
28
#include "rs_commandevent.h"
30
#include "rs_circle.h"
31
#include "lc_quadratic.h"
37
RS_ActionDrawCircleTan2_1P::RS_ActionDrawCircleTan2_1P(
38
RS_EntityContainer& container,
39
RS_GraphicView& graphicView)
40
:RS_PreviewActionInterface("Draw tangent circle 2P",
41
container, graphicView),
42
cData(RS_Vector(0.,0.),1.),
46
enTypeList<<RS2::EntityLine<<RS2::EntityArc<<RS2::EntityCircle;
51
RS_ActionDrawCircleTan2_1P::~RS_ActionDrawCircleTan2_1P() {
56
QAction* RS_ActionDrawCircleTan2_1P::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/) {
59
action = new QAction(tr("Common Tangential Circle 1 Point"), NULL);
60
action->setIcon(QIcon(":/extui/circletan2_1p.png"));
64
void RS_ActionDrawCircleTan2_1P::init(int status) {
65
RS_PreviewActionInterface::init(status);
67
RS_Snapper::suspend();
70
if (status<=SetCircle2) {
71
bool updateNeeded(false);
72
if(circles.size()>=2 && circles[1]!=NULL) {
73
if(circles[1]->isHighlighted()){
74
circles[1]->setHighlighted(false);
78
if(status<= SetCircle1 && circles.size()>=1&&circles[0]!=NULL) {
79
if(circles[0]->isHighlighted()){
80
circles[0]->setHighlighted(false);
84
if(updateNeeded) graphicView->redraw(RS2::RedrawDrawing);
90
void RS_ActionDrawCircleTan2_1P::finish(bool updateTB){
91
if( circles.size() >0) {
92
foreach(RS_AtomicEntity* circle, circles)
93
circle->setHighlighted(false);
94
graphicView->redraw(RS2::RedrawDrawing);
96
RS_PreviewActionInterface::finish(updateTB);
99
//void RS_ActionDrawCircleTan2_1P::finish(bool updateTB){
100
//// for(int i=0;i<circles.size();i++) circles[i]->setHighlighted(false);
101
//// graphicView->redraw(RS2::RedrawDrawing);
102
//// circles.clear();
103
// RS_PreviewActionInterface::finish(updateTB);
107
void RS_ActionDrawCircleTan2_1P::trigger() {
108
// std::cout<<__FILE__<<" : "<<__FUNCTION__<<" : line "<<__LINE__<<std::endl;
109
// std::cout<<"begin"<<std::endl;
111
RS_PreviewActionInterface::trigger();
114
RS_Circle* c=new RS_Circle(container, cData);
116
container->addEntity(c);
119
if (document!=NULL) {
120
document->startUndoCycle();
121
document->addUndoable(c);
122
document->endUndoCycle();
126
foreach(RS_AtomicEntity* circle, circles)
127
circle->setHighlighted(false);
128
graphicView->redraw(RS2::RedrawDrawing);
132
RS_DEBUG->print("RS_ActionDrawCircleTan2_1P::trigger():"
133
" entity added: %d", c->getId());
138
bool RS_ActionDrawCircleTan2_1P::getCenters()
140
if(circles.size()<2) return false;
141
LC_Quadratic lc0(circles[0], point);
142
LC_Quadratic lc1(circles[1], point);
144
auto&& list=LC_Quadratic::getIntersection(lc0,lc1);
146
for(unsigned int i=0;i<list.size();i++){
147
auto&& vp=list.get(i);
148
auto&& ds=vp.distanceTo(point)-RS_TOLERANCE;
149
bool validBranch(true);
150
for(int j=0;j<2;j++){
151
if(circles[j]->rtti()==RS2::EntityCircle||circles[j]->rtti()==RS2::EntityArc){
152
if( vp.distanceTo(circles[j]->getCenter()) <= ds) {
158
if(validBranch) centers.push_back(vp);
160
return centers.size()>0;
163
void RS_ActionDrawCircleTan2_1P::mouseMoveEvent(QMouseEvent* e) {
164
RS_DEBUG->print("RS_ActionDrawCircleTan2_1P::mouseMoveEvent begin");
166
switch( getStatus()){
172
coord=graphicView->toGraph(e->x(),e->y());
178
if(preparePreview()){
179
RS_Circle* e=new RS_Circle(preview, cData);
180
preview->addEntity(e);
183
RS_DEBUG->print("RS_ActionDrawCircleTan2_1P::mouseMoveEvent end");
186
//void RS_ActionDrawCircleTan2_1P::setRadius(const double& r)
189
// if(getStatus() == SetPoint2){
190
// RS_Circle c(NULL,cData);
191
// centers=c.createTan2_1P(circle,cData.radius);
195
bool RS_ActionDrawCircleTan2_1P::preparePreview(){
196
if( getCenters() ==false) return false;
197
// for(int i=0;i<centers.size();i++){
198
// double ds2=(centers[i]-point).squared();
199
// if( (centers[i]-circles[0]).squared()<ds2
201
cData.center=centers.getClosest(coord);
202
cData.radius=point.distanceTo(cData.center);
206
RS_Entity* RS_ActionDrawCircleTan2_1P::catchCircle(QMouseEvent* e) {
208
RS_Entity* en = catchEntity(e,enTypeList, RS2::ResolveAll);
209
if(en == NULL) return ret;
210
if(en->isVisible()==false) return ret;
211
for(int i=0;i<getStatus();i++) {
212
if(en->getId() == circles[i]->getId()) return ret; //do not pull in the same line again
214
if(en->getParent() != NULL) {
215
if ( en->getParent()->rtti() == RS2::EntityInsert /**Insert*/
216
|| en->getParent()->rtti() == RS2::EntitySpline
217
|| en->getParent()->rtti() == RS2::EntityMText /**< Text 15*/
218
|| en->getParent()->rtti() == RS2::EntityText /**< Text 15*/
219
|| en->getParent()->rtti() == RS2::EntityDimAligned /**< Aligned Dimension */
220
|| en->getParent()->rtti() == RS2::EntityDimLinear /**< Linear Dimension */
221
|| en->getParent()->rtti() == RS2::EntityDimRadial /**< Radial Dimension */
222
|| en->getParent()->rtti() == RS2::EntityDimDiametric /**< Diametric Dimension */
223
|| en->getParent()->rtti() == RS2::EntityDimAngular /**< Angular Dimension */
224
|| en->getParent()->rtti() == RS2::EntityDimLeader /**< Leader Dimension */
232
void RS_ActionDrawCircleTan2_1P::mouseReleaseEvent(QMouseEvent* e) {
233
// Proceed to next status
234
if (e->button()==Qt::LeftButton) {
236
switch (getStatus()) {
240
circles.resize(getStatus());
241
RS_AtomicEntity* en = static_cast<RS_AtomicEntity*>(catchCircle(e));
242
if (en==NULL) return;
243
// circle = static_cast<RS_AtomicEntity*>(en);
244
en->setHighlighted(true);
246
graphicView->redraw(RS2::RedrawDrawing);
247
setStatus(getStatus()+1);
252
RS_Vector snapped = snapPoint(e);
253
RS_CoordinateEvent ce(snapped);
254
coordinateEvent(&ce);
258
coord=graphicView->toGraph(e->x(),e->y());
259
if(preparePreview()) trigger();
265
} else if (e->button()==Qt::RightButton) {
266
// Return to last status:
275
void RS_ActionDrawCircleTan2_1P::coordinateEvent(RS_CoordinateEvent* e) {
277
RS_Vector mouse = e->getCoordinate();
284
if(centers.size()==1) trigger();
285
else setStatus(getStatus()+1);
297
//fixme, support command line
300
void RS_ActionDrawCircleTan2_1P::commandEvent(RS_CommandEvent* e) {
301
QString c = e->getCommand().toLower();
303
if (checkCommand("help", c)) {
304
if (RS_DIALOGFACTORY!=NULL) {
305
RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
306
+ getAvailableCommands().join(", "));
311
switch (getStatus()) {
314
double m = RS_Math::eval(c, &ok);
316
ratio = m / major.magnitude();
320
setStatus(SetAngle1);
323
if (RS_DIALOGFACTORY!=NULL) {
324
RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
332
double a = RS_Math::eval(c, &ok);
334
angle1 = RS_Math::deg2rad(a);
335
setStatus(SetAngle2);
337
if (RS_DIALOGFACTORY!=NULL) {
338
RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
346
double a = RS_Math::eval(c, &ok);
348
angle2 = RS_Math::deg2rad(a);
351
if (RS_DIALOGFACTORY!=NULL) {
352
RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
365
//void RS_ActionDrawCircleTan2_1P::showOptions() {
366
// RS_DEBUG->print("RS_ActionDrawCircleTan2_1P::showOptions");
367
// if(RS_DIALOGFACTORY != NULL){
368
// RS_ActionInterface::showOptions();
370
// RS_DIALOGFACTORY->requestOptions(this, true);
372
// RS_DEBUG->print("RS_ActionDrawCircleTan2_1P::showOptions: OK");
377
//void RS_ActionDrawCircleTan2_1P::hideOptions() {
378
// if(RS_DIALOGFACTORY != NULL){
379
// RS_ActionInterface::hideOptions();
381
// RS_DIALOGFACTORY->requestOptions(this, false);
386
QStringList RS_ActionDrawCircleTan2_1P::getAvailableCommands() {
393
void RS_ActionDrawCircleTan2_1P::updateMouseButtonHints() {
394
if (RS_DIALOGFACTORY!=NULL) {
395
switch (getStatus()) {
397
RS_DIALOGFACTORY->updateMouseWidget(tr("Specify a line/arc/circle"),
402
RS_DIALOGFACTORY->updateMouseWidget(tr("Specify the another arc/circle"),
407
RS_DIALOGFACTORY->updateMouseWidget(tr("Specify a point on the tangent circle"),
411
RS_DIALOGFACTORY->updateMouseWidget(tr("Select the center of the tangent circle"),
415
RS_DIALOGFACTORY->updateMouseWidget("", "");
423
void RS_ActionDrawCircleTan2_1P::updateMouseCursor() {
424
graphicView->setMouseCursor(RS2::CadCursor);
429
//void RS_ActionDrawCircleTan2_1P::updateToolBar() {
430
// if (RS_DIALOGFACTORY!=NULL) {
431
// if (isFinished()) {
432
// RS_DIALOGFACTORY->resetToolBar();