+ // For this tool, we check first to see if anything is selected. If so, we
+ // delete those and *don't* select the delete tool.
+ if (drawing->numSelected > 0)
+ {
+// drawing->DeleteSelectedItems();
+ DeleteSelectedObjects(drawing->document.objects);
+ drawing->update();
+ deleteAct->setChecked(false);
+ return;
+ }
+
+ // Otherwise, toggle the state of the tool
+ ClearUIToolStatesExcept(deleteAct);
+ SetInternalToolStates();
+}
+
+
+void ApplicationWindow::DimensionTool(void)
+{
+ ClearUIToolStatesExcept(addDimensionAct);
+ SetInternalToolStates();
+}
+
+
+void ApplicationWindow::RotateTool(void)
+{
+ ClearUIToolStatesExcept(rotateAct);
+
+ // Do tear-down if Rotate tool has been turned off
+ if (!rotateAct->isChecked())
+ drawing->RotateHandler(ToolCleanup, Point(0, 0));
+
+ SetInternalToolStates();
+}
+
+
+void ApplicationWindow::MirrorTool(void)
+{
+ ClearUIToolStatesExcept(mirrorAct);
+ SetInternalToolStates();
+}
+
+
+void ApplicationWindow::TrimTool(void)
+{
+ ClearUIToolStatesExcept(trimAct);
+ SetInternalToolStates();
+}
+
+
+void ApplicationWindow::TriangulateTool(void)
+{
+ ClearUIToolStatesExcept(triangulateAct);
+ SetInternalToolStates();
+}
+
+
+void ApplicationWindow::AddLineTool(void)
+{
+ ClearUIToolStatesExcept(addLineAct);
+ SetInternalToolStates();
+}
+
+
+void ApplicationWindow::AddCircleTool(void)
+{
+ ClearUIToolStatesExcept(addCircleAct);
+ SetInternalToolStates();
+}
+
+
+void ApplicationWindow::AddArcTool(void)
+{
+ ClearUIToolStatesExcept(addArcAct);
+ SetInternalToolStates();
+}
+
+
+void ApplicationWindow::AddPolygonTool(void)
+{
+ ClearUIToolStatesExcept(addPolygonAct);
+ SetInternalToolStates();
+}
+
+
+void ApplicationWindow::AddSplineTool(void)
+{
+ ClearUIToolStatesExcept(addSplineAct);
+ SetInternalToolStates();
+}
+
+
+void ApplicationWindow::ZoomInTool(void)
+{
+ double zoomFactor = 2.0;
+/*
+We need to find the center of the screen, then figure out where the new corner
+will be in the zoomed in window.
+
+So we know in Qt coords, the center is found via:
+size.width() / 2 --> xCenter
+size.height() / 2 --> yCenter
+
+transform x/yCenter to Cartesian coordinates. So far, so good.
+
+when zooming in, new origin will be (xCenter - origin.x) / 2, (yCenter - origin.y) / 2
+(after subtracting from center, that is...)
+*/
+ QSize size = drawing->size();
+ Vector center(size.width() / 2.0, size.height() / 2.0);
+//printf("Zoom in... Center=%.2f,%.2f; ", center.x, center.y);
+ center = Painter::QtToCartesianCoords(center);
+//printf("(%.2f,%.2f); origin=%.2f,%.2f; ", center.x, center.y, Painter::origin.x, Painter::origin.y);
+ Vector newOrigin = center - ((center - Global::origin) / zoomFactor);
+//printf("newOrigin=%.2f,%.2f;\n", newOrigin.x, newOrigin.y);
+ Global::origin = newOrigin;
+
+//printf("Zoom in... level going from %02f to ", Painter::zoom);
+ // This just zooms leaving origin intact... should zoom in at the current
+ // center! [DONE]
+ Global::zoom *= zoomFactor;
+ Global::gridSpacing = drawing->gridPixels / Global::zoom;
+ drawing->UpdateGridBackground();
+ drawing->update();
+
+ zoomIndicator->setText(QString("Grid: %1\", BU: Inch").arg(Global::gridSpacing));
+ baseUnitInput->setText(QString("%1").arg(Global::gridSpacing));
+}
+
+
+void ApplicationWindow::ZoomOutTool(void)
+{
+/*
+Ok, real example.
+center = (436, 311)
+origin = (223, 160.5)
+newOrigin should be (-10, -10)
+Why isn't it?
+
+center - origin = (213, 150.5)
+origin - center = (-213, -150.5)
+x 2 = (-426, -301)
++ center = (-10, -10)
+
+*/
+ double zoomFactor = 2.0;
+ QSize size = drawing->size();
+ Vector center(size.width() / 2.0, size.height() / 2.0);
+//printf("Zoom out... Center=%.2f,%.2f; ", center.x, center.y);
+ center = Painter::QtToCartesianCoords(center);
+//printf("(%.2f,%.2f); origin=%.2f,%.2f; ", center.x, center.y, Painter::origin.x, Painter::origin.y);
+// Vector newOrigin = (center - Painter::origin) * zoomFactor;
+// Vector newOrigin = center - (Painter::origin * zoomFactor);
+ Vector newOrigin = center + ((Global::origin - center) * zoomFactor);
+//printf("newOrigin=%.2f,%.2f;\n", newOrigin.x, newOrigin.y);
+ Global::origin = newOrigin;
+//printf("Zoom out...\n");
+ // This just zooms leaving origin intact... should zoom out at the current
+ // center! [DONE]
+ Global::zoom /= zoomFactor;
+ Global::gridSpacing = drawing->gridPixels / Global::zoom;
+ drawing->UpdateGridBackground();
+ drawing->update();
+
+ zoomIndicator->setText(QString("Grid: %1\", BU: Inch").arg(Global::gridSpacing));
+ baseUnitInput->setText(QString("%1").arg(Global::gridSpacing));
+}
+
+
+void ApplicationWindow::ClearUIToolStatesExcept(QAction * exception)
+{
+ QAction * actionList[] = {
+ addArcAct, addLineAct, addCircleAct, addDimensionAct, addPolygonAct,
+ addSplineAct, deleteAct, rotateAct, mirrorAct, trimAct, triangulateAct, 0
+ };
+
+ for(int i=0; actionList[i]!=0; i++)
+ {
+ if (actionList[i] != exception)
+ actionList[i]->setChecked(false);
+ }
+}
+
+
+void ApplicationWindow::SetInternalToolStates(void)
+{
+ // We can be sure that if we've come here, then either an active tool is
+ // being deactivated, or a new tool is being created. In either case, the
+ // old tool needs to be deleted.
+ Global::toolState = TSNone;
+
+ if (addLineAct->isChecked())
+ Global::tool = TTLine;
+ else if (addCircleAct->isChecked())
+ Global::tool = TTCircle;
+ else if (addArcAct->isChecked())
+ Global::tool = TTArc;
+ else if (addDimensionAct->isChecked())
+ Global::tool = TTDimension;
+ else if (addSplineAct->isChecked())
+ Global::tool = TTSpline;
+ else if (addPolygonAct->isChecked())
+ Global::tool = TTPolygon;
+ else if (deleteAct->isChecked())
+ Global::tool = TTDelete;
+ else if (mirrorAct->isChecked())
+ Global::tool = TTMirror;
+ else if (rotateAct->isChecked())
+ Global::tool = TTRotate;
+ else if (trimAct->isChecked())
+ Global::tool = TTTrim;
+ else if (triangulateAct->isChecked())
+ Global::tool = TTTriangulate;
+ else
+ Global::tool = TTNone;
+
+ drawing->update();