Skip to content

Commit

Permalink
infoArea: algorithm cleanup, bug#3484727
Browse files Browse the repository at this point in the history
  • Loading branch information
dxli committed Feb 8, 2012
1 parent 77e52ae commit 3724dfd
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 226 deletions.
162 changes: 46 additions & 116 deletions src/actions/rs_actioninfoarea.cpp
Expand Up @@ -35,12 +35,12 @@

RS_ActionInfoArea::RS_ActionInfoArea(RS_EntityContainer& container,
RS_GraphicView& graphicView)
:RS_PreviewActionInterface("Info Area",
container, graphicView) {}
:RS_PreviewActionInterface("Info Area",
container, graphicView) {}


QAction* RS_ActionInfoArea::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/) {
/* QAction* action = new QAction(tr("Polygonal Area"),
/* QAction* action = new QAction(tr("Polygonal Area"),
tr("&Polygonal Area"),
QKeySequence(), NULL); */
QAction* action = new QAction(tr("Polygonal &Area"), NULL);
Expand All @@ -53,91 +53,59 @@ QAction* RS_ActionInfoArea::createGUIAction(RS2::ActionType /*type*/, QObject* /
void RS_ActionInfoArea::init(int status) {
RS_ActionInterface::init(status);

currentLine = NULL;
closingLine = NULL;
deletePreview();
point1.valid=false;
ia.reset();
if(status==SetFirstPoint){
deletePreview();
ia.reset();
}

//std::cout << "RS_ActionInfoArea::init: " << status << "\n";
//RS_DEBUG->print( "RS_ActionInfoArea::init: %d" ,status );
}



void RS_ActionInfoArea::trigger() {

RS_DEBUG->print("RS_ActionInfoArea::trigger()");
if (ia.isValid()) {
display();
}
display();

init(SetFirstPoint);
/*
if (point1.valid && point2.valid) {
double dist = point1.distanceTo(point2);
QString str;
str.sprintf("%.6f", dist);
RS_DIALOGFACTORY->commandMessage(tr("Distance: %1").arg(str));
}
*/
}

//todo: we regenerate the whole preview, it's possible to generate needed lines only
/** display area circumference and preview of polygon **/
void RS_ActionInfoArea::display() {
// ia.close(true);
ia.calculate();
double area = ia.getArea();
double circ = ia.getCircumference();
// ia.close(false); //undo close polygon action

// RS_DEBUG->print("RS_ActionInfoArea::trigger: area: %f", area);
RS_DIALOGFACTORY->commandMessage(tr("Circumference: %1").arg(circ));
RS_DIALOGFACTORY->commandMessage(tr("Area: %1").arg(area));
deletePreview();
if(ia.size() < 1) {
return;
}
switch(ia.size()){
case 2:
preview->addEntity(new RS_Line(preview,ia.at(0),ia.at(1)));
break;
default:
for(int i=0;i<ia.size();i++){
preview->addEntity(new RS_Line(preview,ia.at(i),ia.at((i+1) % ia.size())));
}
double area = ia.getArea();
double circ = ia.getCircumference();

RS_DIALOGFACTORY->commandMessage(tr("Circumference: %1").arg(circ));
RS_DIALOGFACTORY->commandMessage(tr("Area: %1").arg(area));
break;
}
drawPreview();

}


void RS_ActionInfoArea::mouseMoveEvent(QMouseEvent* e) {
//RS_DEBUG->print("RS_ActionInfoArea::mouseMoveEvent begin");

RS_Vector mouse = snapPoint(e);
RS_Vector mouse = snapPoint(e);
if ( getStatus()==SetNextPoint) {


if (prev.valid && point1.valid) {
// deletePreview();
if (currentLine!=NULL) {
preview->removeEntity(currentLine);
currentLine = NULL;
}
if (closingLine!=NULL) {
preview->removeEntity(closingLine);
closingLine = NULL;
}

currentLine = new RS_Line(preview,
RS_LineData(prev,
mouse));
preview->addEntity(currentLine);

if (preview->count()>1) {
closingLine = new RS_Line(preview,
RS_LineData(mouse,
point1));

preview->addEntity(closingLine);
bool added=false;
if(ia.duplicated(mouse) == false) {
ia.addPoint(mouse);
added=true;
}
display();
if(added){
ia.pop_back();
}
}

drawPreview();
}

ia.push_back(mouse);
display();
ia.pop_back();
}

//RS_DEBUG->print("RS_ActionInfoArea::mouseMoveEvent end");
Expand All @@ -164,50 +132,23 @@ void RS_ActionInfoArea::coordinateEvent(RS_CoordinateEvent* e) {

RS_Vector mouse = e->getCoordinate();
if(ia.duplicated(mouse)) {
RS_DEBUG->print(RS_Debug::D_WARNING,"RS_ActionInfoArea::coordinateEvent(): duplicated point");
ia.push_back(mouse);
RS_DIALOGFACTORY->commandMessage(tr("Closing Point: %1/%2")
.arg(mouse.x).arg(mouse.y));
trigger();
return;
}
graphicView->moveRelativeZero(mouse);

ia.addPoint(mouse);
prev = mouse;
RS_DIALOGFACTORY->commandMessage(tr("Point: %1/%2")
.arg(mouse.x).arg(mouse.y));
ia.push_back(mouse);
RS_DIALOGFACTORY->commandMessage(tr("Point: %1/%2")
.arg(mouse.x).arg(mouse.y));
switch (getStatus()) {
case SetFirstPoint:
point1=mouse;
deletePreview();



setStatus(SetNextPoint);
break;

case SetNextPoint:
if( point1.valid ){
//point2 = mouse;
/*deletePreview();
*/

currentLine = NULL;

// graphicView->moveRelativeZero(mouse);

// automatically detect that the polyline is now closed
if (ia.isClosed()) {
// deletePreview();
trigger();
}else{
if(ia.count()>=3) {
display();
}
}
}else{
point1=mouse;
}

//trigger();
//setStatus(SetFirstPoint);
display();
break;

default:
Expand All @@ -220,8 +161,8 @@ void RS_ActionInfoArea::updateMouseButtonHints() {
switch (getStatus()) {
case SetFirstPoint:
RS_DIALOGFACTORY->updateMouseWidget(
tr("Specify first point of polygon"),
tr("Cancel"));
tr("Specify first point of polygon"),
tr("Cancel"));
break;
case SetNextPoint:
RS_DIALOGFACTORY->updateMouseWidget(
Expand All @@ -240,15 +181,4 @@ void RS_ActionInfoArea::updateMouseCursor() {
graphicView->setMouseCursor(RS2::CadCursor);
}



//void RS_ActionInfoArea::updateToolBar() {
// if (RS_DIALOGFACTORY!=NULL) {
// if (isFinished()) {
// RS_DIALOGFACTORY->resetToolBar();
// }
// }
//}


// EOF
14 changes: 4 additions & 10 deletions src/actions/rs_actioninfoarea.h
Expand Up @@ -38,7 +38,7 @@
* @author Andrew Mustun
*/
class RS_ActionInfoArea : public RS_PreviewActionInterface {
Q_OBJECT
Q_OBJECT
public:
/**
* Action States.
Expand All @@ -50,7 +50,7 @@ class RS_ActionInfoArea : public RS_PreviewActionInterface {

public:
RS_ActionInfoArea(RS_EntityContainer& container,
RS_GraphicView& graphicView);
RS_GraphicView& graphicView);
~RS_ActionInfoArea() {}

static QAction* createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/);
Expand All @@ -65,19 +65,13 @@ class RS_ActionInfoArea : public RS_PreviewActionInterface {
virtual void mouseMoveEvent(QMouseEvent* e);
virtual void mouseReleaseEvent(QMouseEvent* e);

virtual void coordinateEvent(RS_CoordinateEvent* e);
virtual void coordinateEvent(RS_CoordinateEvent* e);

virtual void updateMouseButtonHints();
virtual void updateMouseCursor();
// virtual void updateToolBar();

private:
RS_Vector point1;
RS_Vector prev;
RS_Entity* currentLine;
RS_Entity* closingLine;
//RS_Vector point2;
RS_InfoArea ia;
RS_InfoArea ia;
};

#endif

0 comments on commit 3724dfd

Please sign in to comment.