3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 06/16/2010 Created this file. :-)
15 #include "graphicview.h"
17 #include "rs_dialogfactory.h"
19 #include "rs_eventhandler.h"
21 #include "rs_linetypepattern.h"
22 #include "paintinterface.h"
30 GraphicView::GraphicView(): background(), foreground()
32 drawingMode = RS2::ModeFull;
35 factor = Vector(1.0, 1.0);
38 previousFactor = Vector(1.0, 1.0);
42 eventHandler = new RS_EventHandler(this);
44 metaGridColor = RS_Color(64, 64, 64);
45 grid = new RS_Grid(this);
56 relativeZero = Vector(false);
57 relativeZeroLocked = false;
61 defaultSnapMode = RS2::SnapFree;
62 defaultSnapRes = RS2::RestrictNothing;
64 settings.beginGroup("Appearance");
65 setBackground(QColor(settings.value("BackgroundColor", "#000000").toString()));
66 setGridColor(QColor(settings.value("GridColor", "#7F7F7F").toString()));
67 setMetaGridColor(QColor(settings.value("MetaGridColor", "#3F3F3F").toString()));
68 setSelectedColor(QColor(settings.value("SelectedColor", "#A54747").toString()));
69 setHighlightedColor(QColor(settings.value("HighlightedColor", "#739373").toString()));
74 simulationSpeed = 100;
75 simulationSmooth = false;
76 simulationRapid = false;
77 simulationRunning = false;
83 GraphicView::~GraphicView()
89 * Must be called by any derrived class in the destructor.
91 void GraphicView::cleanUp()
97 * @return Pointer to the graphic entity if the entity container
98 * connected to this view is a graphic and valid.
101 Drawing * GraphicView::getGraphic()
103 if (container && container->rtti() == RS2::EntityGraphic)
104 return (Drawing *)container;
110 * Sets the drawing mode.
112 void GraphicView::setDrawingMode(RS2::DrawingMode m)
118 * @return Current drawing mode.
120 RS2::DrawingMode GraphicView::getDrawingMode()
126 * Activates or deactivates the delete mode.
128 void GraphicView::setDeleteMode(bool m)
134 * @reval true Deleting instead of drawing.
135 * false Normal drawing mode.
137 bool GraphicView::getDeleteMode()
142 /** This virtual method must be overwritten and is then
143 called whenever the view changed */
144 void GraphicView::adjustOffsetControls()
148 /** This virtual method must be overwritten and is then
149 called whenever the view changed */
150 void GraphicView::adjustZoomControls()
155 * Sets the background color. Note that applying the background
156 * color for the widget is up to the implementing class.
158 void GraphicView::setBackground(const RS_Color & bg)
162 // bright background:
163 if (bg.red() + bg.green() + bg.blue() > 380)
164 // foreground = Qt::black;
165 foreground = RS_Color(0, 0, 0);
167 // foreground = Qt::white;
168 foreground = RS_Color(255, 255, 255);
172 * @return Current background color.
174 RS_Color GraphicView::getBackground()
180 * @return Current foreground color.
182 RS_Color GraphicView::getForeground()
188 * Sets the grid color.
190 void GraphicView::setGridColor(const RS_Color & c)
196 * Sets the meta grid color.
198 void GraphicView::setMetaGridColor(const RS_Color & c)
204 * Sets the selection color.
206 void GraphicView::setSelectedColor(const RS_Color & c)
212 * Sets the highlight color.
214 void GraphicView::setHighlightedColor(const RS_Color & c)
216 highlightedColor = c;
220 * This virtual method can be overwritten to set the mouse
221 * cursor to the given type.
223 void GraphicView::setMouseCursor(RS2::CursorType /*c*/)
227 RS_EntityContainer * GraphicView::getContainer()
232 void GraphicView::setFactor(double f)
238 Vector GraphicView::getFactor()
244 * Sets the offset of the graphic.
246 void GraphicView::setOffset(int ox, int oy)
252 void GraphicView::setOffsetX(int ox)
257 void GraphicView::setOffsetY(int oy)
262 int GraphicView::getOffsetX()
267 int GraphicView::getOffsetY()
273 * Sets a fixed border in pixel around the graphic. This border
274 * specifies how far the user can scroll outside the graphic
277 void GraphicView::setBorders(int left, int top, int right, int bottom)
282 borderBottom = bottom;
285 int GraphicView::getBorderLeft()
290 int GraphicView::getBorderTop()
295 int GraphicView::getBorderRight()
300 int GraphicView::getBorderBottom()
305 void GraphicView::disableUpdate()
310 void GraphicView::enableUpdate()
315 bool GraphicView::isUpdateEnabled()
317 return (updateEnabled == 0);
320 void GraphicView::freezeZoom(bool freeze)
325 bool GraphicView::isZoomFrozen()
331 * Sets the pointer to the graphic which contains the entities
332 * which are visualized by this widget.
334 void GraphicView::setContainer(RS_EntityContainer * container)
336 this->container = container;
340 * Sets the zoom factor in X for this visualization of the graphic.
342 void GraphicView::setFactorX(double f)
349 * Sets the zoom factor in Y for this visualization of the graphic.
351 void GraphicView::setFactorY(double f)
358 * @return true if the grid is switched on.
360 bool GraphicView::isGridOn()
364 Drawing * g = container->getGraphic();
367 return g->isGridOn();
374 * Centers the drawing in x-direction.
376 void GraphicView::centerOffsetX()
378 if (container != NULL && !zoomFrozen)
379 offsetX = (int)(((getWidth() - borderLeft - borderRight)
380 - (container->getSize().x * factor.x)) / 2.0
381 - (container->getMin().x * factor.x)) + borderLeft;
385 * Centers the drawing in y-direction.
387 void GraphicView::centerOffsetY()
389 if (container != NULL && !zoomFrozen)
390 offsetY = (int)((getHeight() - borderTop - borderBottom
391 - (container->getSize().y * factor.y)) / 2.0
392 - (container->getMin().y * factor.y)) + borderBottom;
396 * Centers the given coordinate in the view in x-direction.
398 void GraphicView::centerX(double v)
401 offsetX = (int)((v * factor.x) - (double)(getWidth() - borderLeft - borderRight) / 2.0);
405 * Centers the given coordinate in the view in y-direction.
407 void GraphicView::centerY(double v)
410 offsetY = (int)((v * factor.y) - (double)(getHeight() - borderTop - borderBottom) / 2.0);
413 void GraphicView::updateView()
415 static int running = 0;
420 adjustZoomControls();
421 adjustOffsetControls();
431 * @return Current action or NULL.
433 ActionInterface * GraphicView::getDefaultAction()
435 if (eventHandler != NULL)
436 return eventHandler->getDefaultAction();
442 * Sets the default action of the event handler.
444 void GraphicView::setDefaultAction(ActionInterface * action)
447 eventHandler->setDefaultAction(action);
451 * @return Current action or NULL.
453 ActionInterface * GraphicView::getCurrentAction()
456 return eventHandler->getCurrentAction();
462 * Sets the current action of the event handler.
464 void GraphicView::setCurrentAction(ActionInterface * action)
466 RS_DEBUG->print("GraphicView::setCurrentAction");
469 eventHandler->setCurrentAction(action);
471 RS_DEBUG->print("GraphicView::setCurrentAction: OK");
475 * Kills all running selection actions. Called when a selection action
476 * is launched to reduce confusion.
478 void GraphicView::killSelectActions()
481 eventHandler->killSelectActions();
485 * Kills all running actions.
487 void GraphicView::killAllActions()
490 eventHandler->killAllActions();
494 * Go back in menu or current action.
496 void GraphicView::back()
498 if (eventHandler && eventHandler->hasAction())
499 eventHandler->back();
500 else if (RS_DIALOGFACTORY)
501 RS_DIALOGFACTORY->requestPreviousMenu();
505 * Go forward with the current action.
507 void GraphicView::enter()
509 if (eventHandler && eventHandler->hasAction())
510 eventHandler->enter();
514 * Called by the actual GUI class which implements the GraphicView
515 * interface to notify qcadlib about mouse events.
517 void GraphicView::mousePressEvent(QMouseEvent * e)
520 eventHandler->mousePressEvent(e);
524 * Called by the actual GUI class which implements the GraphicView
525 * interface to notify qcadlib about mouse events.
527 void GraphicView::mouseReleaseEvent(QMouseEvent * e)
529 RS_DEBUG->print("GraphicView::mouseReleaseEvent");
534 if (e->button() != Qt::RightButton || eventHandler->hasAction())
536 eventHandler->mouseReleaseEvent(e);
544 RS_DEBUG->print("GraphicView::mouseReleaseEvent: OK");
548 * Called by the actual GUI class which implements the GraphicView
549 * interface to notify qcadlib about mouse events.
551 void GraphicView::mouseMoveEvent(QMouseEvent * e)
553 RS_DEBUG->print("GraphicView::mouseMoveEvent begin");
555 Drawing * graphic = NULL;
557 if (container->rtti() == RS2::EntityGraphic)
558 graphic = (Drawing *)container;
560 RS_DEBUG->print("GraphicView::mouseMoveEvent 001");
568 RS_DEBUG->print("GraphicView::mouseMoveEvent 002");
571 eventHandler->mouseMoveEvent(e);
573 RS_DEBUG->print("GraphicView::mouseMoveEvent 003");
575 if (!eventHandler || !eventHandler->hasAction() && graphic)
577 Vector mouse = toGraph(Vector(mx, my));
578 Vector relMouse = mouse - getRelativeZero();
580 if (RS_DIALOGFACTORY)
581 RS_DIALOGFACTORY->updateCoordinateWidget(mouse, relMouse);
584 RS_DEBUG->print("GraphicView::mouseMoveEvent end");
588 * Called by the actual GUI class which implements the GraphicView
589 * interface to notify qcadlib about mouse events.
591 void GraphicView::mouseLeaveEvent()
594 eventHandler->mouseLeaveEvent();
598 * Called by the actual GUI class which implements the GraphicView
599 * interface to notify qcadlib about mouse events.
601 void GraphicView::mouseEnterEvent()
604 eventHandler->mouseEnterEvent();
608 * Called by the actual GUI class which implements the GraphicView
609 * interface to notify qcadlib about key events.
611 void GraphicView::keyPressEvent(QKeyEvent * e)
614 eventHandler->keyPressEvent(e);
618 * Called by the actual GUI class which implements the GraphicView
619 * interface to notify qcadlib about key events.
621 void GraphicView::keyReleaseEvent(QKeyEvent * e)
624 eventHandler->keyReleaseEvent(e);
628 * Called by the actual GUI class which implements a command line.
630 void GraphicView::commandEvent(RS_CommandEvent * e)
633 eventHandler->commandEvent(e);
637 * Enables coordinate input in the command line.
639 void GraphicView::enableCoordinateInput()
642 eventHandler->enableCoordinateInput();
646 * Disables coordinate input in the command line.
648 void GraphicView::disableCoordinateInput()
651 eventHandler->disableCoordinateInput();
655 * zooms in by factor f
657 void GraphicView::zoomIn(double f, const Vector & center)
661 RS_DEBUG->print(RS_Debug::D_WARNING, "GraphicView::zoomIn: invalid factor");
665 if (simulationRunning)
669 if (c.valid == false)
670 c = toGraph(Vector(getWidth() / 2, getHeight() / 2));
672 zoomWindow(toGraph(Vector(0, 0)).scale(c, Vector(1.0 / f, 1.0 / f)),
673 toGraph(Vector(getWidth(), getHeight())).scale(c, Vector(1.0 / f, 1.0 / f)));
677 * zooms in by factor f in x
679 void GraphicView::zoomInX(double f)
681 if (simulationRunning)
685 offsetX = (int)((offsetX - getWidth() / 2) * f) + getWidth() / 2;
686 adjustOffsetControls();
687 adjustZoomControls();
693 * zooms in by factor f in y
695 void GraphicView::zoomInY(double f)
697 if (simulationRunning)
701 offsetY = (int)((offsetY - getHeight() / 2) * f)+getHeight() / 2;
702 adjustOffsetControls();
703 adjustZoomControls();
709 * zooms out by factor f
711 void GraphicView::zoomOut(double f, const Vector & center)
715 RS_DEBUG->print(RS_Debug::D_WARNING,
716 "GraphicView::zoomOut: invalid factor");
720 if (simulationRunning)
727 * zooms out by factor f in x
729 void GraphicView::zoomOutX(double f)
733 RS_DEBUG->print(RS_Debug::D_WARNING,
734 "GraphicView::zoomOutX: invalid factor");
738 if (simulationRunning)
742 offsetX = (int)(offsetX / f);
743 adjustOffsetControls();
744 adjustZoomControls();
750 * zooms out by factor of y
752 void GraphicView::zoomOutY(double f)
756 RS_DEBUG->print(RS_Debug::D_WARNING, "GraphicView::zoomOutY: invalid factor");
760 if (simulationRunning)
764 offsetY = (int)(offsetY / f);
765 adjustOffsetControls();
766 adjustZoomControls();
774 * @param axis include axis in zoom
775 * @param keepAspectRatio true: keep aspect ratio 1:1
776 * false: factors in x and y are stretched to the max
778 void GraphicView::zoomAuto(bool axis, bool keepAspectRatio)
780 RS_DEBUG->print("GraphicView::zoomAuto");
782 if (simulationRunning)
787 if (container != NULL)
789 container->calculateBorders();
794 sx = std::max(container->getMax().x, 0.0) - std::min(container->getMin().x, 0.0);
795 sy = std::max(container->getMax().y, 0.0) - std::min(container->getMin().y, 0.0);
799 sx = container->getSize().x;
800 sy = container->getSize().y;
805 if (sx > RS_TOLERANCE)
806 fx = (getWidth() - borderLeft - borderRight) / sx;
810 if (sy > RS_TOLERANCE)
811 fy = (getHeight() - borderTop - borderBottom) / sy;
815 RS_DEBUG->print("f: %f/%f", fx, fy);
818 fx = fy = std::min(fx, fy);
820 RS_DEBUG->print("f: %f/%f", fx, fy);
822 if (fx < RS_TOLERANCE)
828 RS_DEBUG->print("f: %f/%f", fx, fy);
830 RS_DEBUG->print("adjustOffsetControls");
831 adjustOffsetControls();
832 RS_DEBUG->print("adjustZoomControls");
833 adjustZoomControls();
834 RS_DEBUG->print("centerOffsetX");
836 RS_DEBUG->print("centerOffsetY");
838 RS_DEBUG->print("updateGrid");
843 RS_DEBUG->print("GraphicView::zoomAuto OK");
847 * Shows previous view.
849 void GraphicView::zoomPrevious()
851 RS_DEBUG->print("GraphicView::zoomPrevious");
853 if (simulationRunning)
861 * Saves the current view as previous view to which we can
862 * switch back later with @see restoreView().
864 void GraphicView::saveView()
866 previousOffsetX = offsetX;
867 previousOffsetY = offsetY;
868 previousFactor = factor;
872 * Restores the view previously saved with
875 void GraphicView::restoreView()
877 int pox = previousOffsetX;
878 int poy = previousOffsetY;
879 Vector pf = previousFactor;
887 adjustOffsetControls();
888 adjustZoomControls();
894 * performs autozoom in y only
896 * @param axis include axis in zoom
898 void GraphicView::zoomAutoY(bool axis)
900 if (simulationRunning)
903 if (container != NULL)
905 double visibleHeight = 0.0;
906 double minY = RS_MAXDOUBLE;
907 double maxY = RS_MINDOUBLE;
908 bool noChange = false;
910 for(RS_Entity * e=container->firstEntity(RS2::ResolveNone);
911 e!=NULL; e = container->nextEntity(RS2::ResolveNone))
913 if (e->rtti() == RS2::EntityLine)
915 RS_Line * l = (RS_Line *)e;
917 x1 = toGuiX(l->getStartpoint().x);
918 x2 = toGuiX(l->getEndpoint().x);
920 if (x1 > 0.0 && x1 < (double)getWidth() || x2 > 0.0 && x2 < (double)getWidth())
922 minY = std::min(minY, l->getStartpoint().y);
923 minY = std::min(minY, l->getEndpoint().y);
924 maxY = std::max(maxY, l->getStartpoint().y);
925 maxY = std::max(maxY, l->getEndpoint().y);
931 visibleHeight = std::max(maxY, 0.0) - std::min(minY, 0.0);
933 visibleHeight = maxY - minY;
935 if (visibleHeight < 1.0)
940 if (visibleHeight > 1.0e-6)
942 fy = (getHeight() - borderTop - borderBottom) / visibleHeight;
944 if (factor.y < 0.000001)
948 if (noChange == false)
952 offsetY = (int)((getHeight() - borderTop - borderBottom - (visibleHeight * factor.y)) / 2.0
953 - (minY * factor.y)) + borderBottom;
954 adjustOffsetControls();
955 adjustZoomControls();
959 RS_DEBUG->print("Auto zoom y ok");
964 * Zooms the area given by v1 and v2.
966 * @param keepAspectRatio true: keeps the aspect ratio 1:1
967 * false: zooms exactly the selected range to the
968 * current graphic view
970 void GraphicView::zoomWindow(Vector v1, Vector v2, bool keepAspectRatio)
972 if (simulationRunning)
977 double zoomX = 480.0; // Zoom for X-Axis
978 double zoomY = 640.0; // Zoom for Y-Axis (Set smaller one)
979 double dum; // Dummy for switching values
982 // Switch left/right and top/bottom is necessary:
997 // Get zoom in X and zoom in Y:
998 if (v2.x - v1.x > 1.0e-6)
999 zoomX = getWidth() / (v2.x - v1.x);
1001 if (v2.y - v1.y > 1.0e-6)
1002 zoomY = getHeight() / (v2.y - v1.y);
1004 // Take smaller zoom:
1005 if (keepAspectRatio)
1009 if (getWidth() != 0)
1011 zoomX = zoomY = ((double)(getWidth() - 2 * zoomBorder)) /
1012 (double)getWidth() * zoomX;
1017 if (getHeight() != 0)
1019 zoomX = zoomY = ((double)(getHeight() - 2 * zoomBorder)) /
1020 (double)getHeight() * zoomY;
1031 // Borders in pixel after zoom
1032 int pixLeft = (int)(v1.x * zoomX);
1033 int pixTop = (int)(v2.y * zoomY);
1034 int pixRight = (int)(v2.x * zoomX);
1035 int pixBottom = (int)(v1.y * zoomY);
1037 // Set new offset for zero point:
1038 offsetX = - pixLeft + (getWidth() - pixRight + pixLeft) / 2;
1039 offsetY = - pixTop + (getHeight() - pixBottom + pixTop) / 2;
1043 adjustOffsetControls();
1044 adjustZoomControls();
1050 * Centers the point v1.
1052 void GraphicView::zoomPan(int dx, int dy)
1054 if (simulationRunning)
1061 adjustOffsetControls();
1068 * Scrolls in the given direction.
1070 void GraphicView::zoomScroll(RS2::Direction direction)
1072 if (simulationRunning)
1091 adjustOffsetControls();
1092 adjustZoomControls();
1098 * Zooms to page extends.
1100 void GraphicView::zoomPage()
1102 RS_DEBUG->print("GraphicView::zoomPage");
1104 if (container == NULL)
1107 if (simulationRunning)
1110 Drawing * graphic = container->getGraphic();
1112 if (graphic == NULL)
1115 Vector s = graphic->getPaperSize();
1116 Vector pinsbase = graphic->getPaperInsertionBase();
1120 if (s.x > RS_TOLERANCE)
1121 fx = (getWidth() - borderLeft - borderRight) / s.x;
1125 if (s.y > RS_TOLERANCE)
1126 fy = (getHeight() - borderTop - borderBottom) / s.y;
1130 RS_DEBUG->print("f: %f/%f", fx, fy);
1132 fx = fy = std::min(fx, fy);
1134 RS_DEBUG->print("f: %f/%f", fx, fy);
1136 if (fx < RS_TOLERANCE)
1142 RS_DEBUG->print("f: %f/%f", fx, fy);
1146 adjustOffsetControls();
1147 adjustZoomControls();
1153 * Draws the entities within the given range.
1155 void GraphicView::drawWindow(Vector v1, Vector v2)
1157 RS_DEBUG->print("GraphicView::drawWindow() begin");
1159 if (simulationRunning)
1164 for(RS_Entity * se=container->firstEntity(RS2::ResolveNone); se!=NULL;
1165 se=container->nextEntity(RS2::ResolveNone))
1167 if (se->isInWindow(v1, v2))
1172 RS_DEBUG->print("GraphicView::drawWindow() end");
1176 * Draws the entities. If painter is NULL a new painter will
1177 * be created and destroyed.
1179 void GraphicView::drawIt()
1181 if (!isUpdateEnabled())
1183 //printf("GraphicView::drawIt(): isUpdateEnabled() == false!\n");
1187 if (simulationRunning)
1189 //printf("GraphicView::drawIt(): simulationRunning == true!\n");
1193 settings.beginGroup("Appearance");
1194 draftMode = settings.value("DraftMode", false).toBool();
1195 settings.endGroup();
1199 //printf("GraphicView::drawIt(): painter == NULL!\n");
1205 // drawing paper border:
1206 if (isPrintPreview())
1208 // drawing meta grid:
1212 // drawing entities:
1213 //#warning "!!! This looks like a bug, no match for 'drawEntity(RS_Entity *, bool) !!!"
1214 // and indeed it *is* a bug... true is converted to 1.0 here. Dumb, dumb, dumb.
1215 drawEntity(container);//, true);
1217 // drawing zero points:
1218 if (!isPrintPreview())
1225 if (!isPrintPreview())
1230 * Sets the pen of the painter object to the suitable pen for the given
1233 void GraphicView::setPenForEntity(RS_Entity * e)
1235 if (drawingMode == RS2::ModePreview /*|| draftMode==true*/)
1238 if (!painter || painter->isPreviewMode())
1241 // set color of entity
1242 // Getting pen from entity (or layer)
1243 RS_Pen pen = e->getPen(true);
1245 int w = pen.getWidth();
1253 double uf = 1.0; // unit factor
1254 double wf = 1.0; // width factor
1255 Drawing * graphic = container->getGraphic();
1259 uf = RS_Units::convert(1.0, RS2::Millimeter, graphic->getUnit());
1261 if ((isPrinting() || isPrintPreview()) && graphic->getPaperScale() > 1.0e-6)
1262 wf = 1.0 / graphic->getPaperScale();
1265 pen.setScreenWidth(toGuiDX(w / 100.0 * uf * wf));
1269 //pen.setWidth(RS2::Width00);
1270 pen.setScreenWidth(0);
1273 // prevent drawing with 1-width which is slow:
1274 if (RS_Math::round(pen.getScreenWidth()) == 1)
1275 pen.setScreenWidth(0.0);
1277 // prevent background color on background drawing:
1278 if (pen.getColor().stripFlags() == background.stripFlags())
1279 pen.setColor(foreground);
1281 // this entity is selected:
1282 if (e->isSelected())
1284 pen.setLineType(RS2::DotLine);
1285 //pen.setColor(RS_Color(0xa5,0x47,0x47));
1286 pen.setColor(selectedColor);
1289 // this entity is highlighted:
1290 if (e->isHighlighted())
1292 //pen.setColor(RS_Color(0x73, 0x93, 0x73));
1293 pen.setColor(highlightedColor);
1296 // deleting not drawing:
1297 if (getDeleteMode())
1298 pen.setColor(background);
1300 painter->setPen(pen);
1304 * Draws an entity. Might be recusively called e.g. for polylines.
1305 * If the class wide painter is NULL a new painter will be created
1306 * and destroyed afterwards.
1308 * @param patternOffset Offset of line pattern (used for connected
1309 * lines e.g. in splines).
1310 * @param db Double buffering on (recommended) / off
1312 void GraphicView::drawEntity(RS_Entity * e, double patternOffset, bool db)
1314 //RS_DEBUG->print("GraphicView::drawEntity() begin");
1316 // update is diabled:
1317 if (!isUpdateEnabled())
1320 // given entity is NULL:
1324 // entity is not visible:
1325 if (!e->isVisible())
1328 // test if the entity is in the viewport
1329 if (!e->isContainer() && !isPrinting()
1330 && (!painter || !painter->isPreviewMode())
1331 && (toGuiX(e->getMax().x) < 0 || toGuiX(e->getMin().x) > getWidth()
1332 || toGuiY(e->getMin().y) < 0 || toGuiY(e->getMax().y) > getHeight()))
1334 //printf("GraphicView::drawEntity(): Bailing out of big test!!!\n");
1339 //RS_DEBUG->print("recursion 1: %d", drawRecursion);
1344 //RS_DEBUG->print("draw plain");
1347 // large texts as rectangles:
1348 if (e->rtti() == RS2::EntityText)
1350 if (toGuiDX(((RS_Text *)e)->getHeight()) < 4 || e->countDeep() > 100)
1351 painter->drawRect(toGui(e->getMin()), toGui(e->getMax()));
1353 drawEntityPlain(e, patternOffset);
1355 // all images as rectangles:
1356 else if (e->rtti() == RS2::EntityImage)
1357 painter->drawRect(toGui(e->getMin()), toGui(e->getMax()));
1359 else if (e->rtti() == RS2::EntityHatch)
1364 drawEntityPlain(e, patternOffset);
1367 drawEntityPlain(e, patternOffset);
1369 // draw reference points:
1370 if (e->isSelected())
1372 if (!e->isParentSelected())
1374 VectorSolutions s = e->getRefPoints();
1376 for(int i=0; i<s.getNumber(); ++i)
1379 RS_Color col = RS_Color(0, 0, 255);
1381 if (e->rtti() == RS2::EntityPolyline)
1383 if (i == 0 || i == s.getNumber() - 1)
1388 // col = QColor(0, 64, 255);
1389 col = RS_Color(0, 64, 255);
1394 // col = QColor(0, 0, 128);
1395 col = RS_Color(0, 0, 128);
1400 if (getDeleteMode())
1401 painter->drawHandle(toGui(s.get(i)), background, sz);
1403 painter->drawHandle(toGui(s.get(i)), col, sz);
1408 //RS_DEBUG->print("draw plain OK");
1409 //RS_DEBUG->print("GraphicView::drawEntity() end");
1413 * Deletes an entity with the background color.
1414 * Might be recusively called e.g. for polylines.
1416 void GraphicView::deleteEntity(RS_Entity * e)
1418 #warning "!!! This is part of obsolete rendering !!!"
1419 setDeleteMode(true);
1421 setDeleteMode(false);
1426 * The painter must be initialized and all the attributes (pen) must be set.
1428 void GraphicView::drawEntityPlain(RS_Entity * e, double patternOffset/*= 0.0*/)
1430 //Problems can still occur here when passing in a deleted object... It won't be
1431 //NULL, but it will cause a segfault here...
1434 //printf("GraphicView::drawEntityPlain(): Entity passed in is NULL!\n");
1438 //printf("GraphicView::drawEntityPlain(): Passing in painter=%08X, view=%08X\n", painter, this);
1439 e->draw(painter, this, patternOffset);
1443 * Simulates this drawing in slow motion.
1445 void GraphicView::simulateIt()
1447 if (simulationRunning)
1450 simulationRunning = true;
1451 simulationLast = Vector(0.0, 0.0);
1455 // drawing paper border:
1456 if (isPrintPreview())
1459 // drawing meta grid:
1460 if (!isPrintPreview())
1464 if (!isPrintPreview())
1467 // drawing entities:
1468 RS_Pen pen(foreground, RS2::Width00, RS2::SolidLine);
1469 simulateEntity(container, pen);
1471 // drawing zero points:
1472 if (!isPrintPreview())
1478 simulationRunning = false;
1482 * Simulates the given entity.
1484 * @param smooth If true, the entity will be drawn slowly (pixel by pixel).
1486 void GraphicView::simulateEntity(RS_Entity * e, const RS_Pen & pen)
1488 if (painter == NULL || e == NULL)
1491 if (e->isContainer())
1493 RS_EntityContainer * ec = (RS_EntityContainer *)e;
1495 for(RS_Entity* en=ec->firstEntity(RS2::ResolveNone);
1496 en!=NULL; en = ec->nextEntity(RS2::ResolveNone))
1498 if (en->isVisible() && en->isUndone() == false)
1501 if (en->isAtomic() && simulationRapid)
1503 Vector sp = ((RS_AtomicEntity *)en)->getStartpoint();
1505 if (sp.distanceTo(simulationLast) > 1.0e-4)
1507 RS_Pen rpen(RS_Color(0, 0, 255), RS2::Width00, RS2::SolidLine);
1508 RS_Line rapidLine(NULL, RS_LineData(simulationLast, sp));
1509 simulateEntity(&rapidLine, rpen);
1513 if (en->isHighlighted())
1515 RS_Pen hpen(highlightedColor, RS2::Width00, RS2::SolidLine);
1516 simulateEntity(en, hpen);
1519 simulateEntity(en, pen);
1522 simulationLast = ((RS_AtomicEntity *)en)->getEndpoint();
1524 if (!simulationSmooth)
1525 simulationDelay(true);
1531 if (simulationSmooth)
1535 case RS2::EntityLine:
1537 RS_Line * line = (RS_Line *)e;
1538 drawLineSmooth(toGui(line->getStartpoint()), toGui(line->getEndpoint()), pen);
1543 case RS2::EntityArc:
1545 RS_Arc * arc = (RS_Arc *)e;
1546 drawArcSmooth(toGui(arc->getCenter()), toGuiDX(arc->getRadius()),
1547 arc->getAngle1(), arc->getAngle2(), arc->isReversed(), pen);
1551 case RS2::EntityCircle:
1553 RS_Circle * circle = (RS_Circle *)e;
1554 drawArcSmooth(toGui(circle->getCenter()), toGuiDX(circle->getRadius()),
1555 0.0, 2.0 * M_PI, false, pen);
1565 painter->setPen(pen);
1572 * Delay for slow motion simulation.
1574 * @param step true: stepping mode (entity by entity simulation). adds a delay.
1576 void GraphicView::simulationDelay(bool step)
1579 settings.beginGroup("CAM");
1580 double fact = settings.value("SimulationFactor", 12000.0).toDouble();
1581 settings.endGroup();
1583 // simulationSpeed: 0..100
1586 delay = (int)(((1.0 / (simulationSpeed + 1.0)) * fact) - (fact / 100.0));
1591 static int call = 0;
1593 if (call >= (fact - delay) / 1000)
1596 for(int i=0; i<delay; ++i)
1597 RS_APP->processEvents(10);
1602 delay = (int)(((1.0 / (simulationSpeed + 1.0)) * fact) - (fact / 100.0));
1609 for(int i=0; i<delay; ++i)
1611 #warning "Qt3->4 conversion: commented out problem line... !!! FIX !!!"
1612 // RS_APP->processEvents(10);
1618 * Draws a line slowly from (x1, y1) to (x2, y2). This is used for simulation only.
1620 void GraphicView::drawLineSmooth(const Vector & p1, const Vector & p2, const RS_Pen & pen)
1622 double alpha = p1.angleTo(p2);
1623 double xStep, yStep;
1626 if (RS_Math::cmpDouble(alpha, 0.0) || RS_Math::cmpDouble(alpha, 2 * M_PI))
1632 else if (RS_Math::cmpDouble(alpha, M_PI / 2.0))
1638 else if (RS_Math::cmpDouble(alpha, M_PI))
1644 else if (RS_Math::cmpDouble(alpha, M_PI / 2.0 * 3.0))
1650 else if (fabs(p2.x - p1.x) > fabs(p2.y - p1.y))
1657 yStep = tan(alpha) * xStep;
1667 xStep = yStep / tan(alpha);
1676 if (lx >= 0.0 && lx <= (double)getWidth() && ly >= 0.0 && ly <= (double)getHeight())
1678 painter->setPen(pen);
1679 painter->drawGridPoint(Vector(lx, ly));
1688 while ((xIsOne && ((lx >= p1.x && lx <= p2.x) || (lx >= p2.x && lx <= p1.x)))
1689 || (!xIsOne && ((ly >= p1.y && ly <= p2.y) || (ly >= p2.y && ly <= p1.y))));
1692 void GraphicView::drawArcSmooth(const Vector & center, double radius, double a1, double a2, bool rev,
1697 painter->setPen(pen);
1698 painter->drawGridPoint(center);
1702 int ix1 = RS_Math::round(center.x + cos(a1) * radius);
1703 int iy1 = RS_Math::round(center.y - sin(a1) * radius);
1704 int ix2 = RS_Math::round(center.x + cos(a2) * radius);
1705 int iy2 = RS_Math::round(center.y - sin(a2) * radius);
1706 int k2x = 0; // Next point on circle
1708 int k1x = ix1; // Prev point on circle
1710 double aStep; // Angle Step (rad)
1711 double a; // Actual Angle (rad)
1712 double a2cp = a2; // Copy of a2
1714 if (1.0 / (radius * factor.x) <= 1.0)
1715 aStep = asin(1.0 / (radius * factor.x));
1724 // Arc Counterclockwise:
1726 if (a1 > a2cp - 0.01)
1729 for(a=a1+aStep; a<=a2cp; a+=aStep)
1731 k2x = RS_Math::round(center.x+cos(a)*radius);
1732 k2y = RS_Math::round(center.y-sin(a)*radius);
1733 painter->setPen(pen);
1735 if ((k2x >= 0 && k2x <= painter->getWidth()
1736 && k2y >= 0 && k2y <= painter->getHeight())
1737 || (k1x >= 0 && k1x <= painter->getWidth()
1738 && k1y >= 0 && k1y <= painter->getHeight()))
1740 painter->drawLine(Vector(k1x, k1y), Vector(k2x, k2y));
1748 painter->setPen(pen);
1749 painter->drawLine(Vector(k2x, k2y), Vector(ix2, iy2));
1755 if (a1 < a2cp + 0.01)
1758 for(a=a1-aStep; a>=a2cp; a-=aStep)
1760 k2x = RS_Math::round(center.x + cos(a) * radius);
1761 k2y = RS_Math::round(center.y - sin(a) * radius);
1762 painter->setPen(pen);
1764 if ((k2x >=0 && k2x <= painter->getWidth()
1765 && k2y >= 0 && k2y <= painter->getHeight())
1766 || (k1x >= 0 && k1x <= painter->getWidth()
1767 && k1y >= 0 && k1y <= painter->getHeight()))
1769 painter->drawLine(Vector(k1x, k1y), Vector(k2x, k2y));
1777 painter->setPen(pen);
1778 painter->drawLine(Vector(k2x, k2y), Vector(ix2, iy2));
1784 * @return Pointer to the static pattern struct that belongs to the
1785 * given pattern type or NULL.
1787 RS_LineTypePattern * GraphicView::getPattern(RS2::LineType t)
1791 case RS2::SolidLine:
1792 return &patternSolidLine;
1796 return &patternDotLine;
1799 return &patternDotLine2;
1801 case RS2::DotLineX2:
1802 return &patternDotLineX2;
1806 return &patternDashLine;
1808 case RS2::DashLine2:
1809 return &patternDashLine2;
1811 case RS2::DashLineX2:
1812 return &patternDashLineX2;
1815 case RS2::DashDotLine:
1816 return &patternDashDotLine;
1818 case RS2::DashDotLine2:
1819 return &patternDashDotLine2;
1821 case RS2::DashDotLineX2:
1822 return &patternDashDotLineX2;
1825 case RS2::DivideLine:
1826 return &patternDivideLine;
1828 case RS2::DivideLine2:
1829 return &patternDivideLine2;
1831 case RS2::DivideLineX2:
1832 return &patternDivideLineX2;
1835 case RS2::CenterLine:
1836 return &patternCenterLine;
1838 case RS2::CenterLine2:
1839 return &patternCenterLine2;
1841 case RS2::CenterLineX2:
1842 return &patternCenterLineX2;
1845 case RS2::BorderLine:
1846 return &patternBorderLine;
1848 case RS2::BorderLine2:
1849 return &patternBorderLine2;
1851 case RS2::BorderLineX2:
1852 return &patternBorderLineX2;
1855 case RS2::LineByLayer:
1856 return &patternBlockLine;
1858 case RS2::LineByBlock:
1859 return &patternBlockLine;
1869 * This virtual method can be overwritten to draw the absolute
1870 * zero. It's called from within drawIt(). The default implemetation
1871 * draws a simple red round zero point.
1873 * Actually, we have to rework the rendering code because the way that QCad did
1874 * it was wrong on so many levels... Part of that is making sure the rendering
1875 * path is 100% clear!
1879 void GraphicView::drawAbsoluteZero()
1886 // RS_Pen p(Qt::red, RS2::Width00, RS2::SolidLine);
1887 //Using Qt::red doesn't seem to work here...
1888 //It's because Qt colors and RS_Color are not 100% compatible...
1889 RS_Pen p(RS_Color(255, 0, 0), RS2::Width00, RS2::SolidLine);
1892 painter->drawLine(Vector(toGuiX(0.0) - zr, toGuiY(0.0)),
1893 Vector(toGuiX(0.0) + zr, toGuiY(0.0)));
1895 painter->drawLine(Vector(toGuiX(0.0), toGuiY(0.0) - zr),
1896 Vector(toGuiX(0.0), toGuiY(0.0) + zr));
1900 * This virtual method can be overwritten to draw the relative
1901 * zero point. It's called from within drawIt(). The default implemetation
1902 * draws a simple red round zero point.
1906 void GraphicView::drawRelativeZero()
1908 if (!relativeZero.valid || !painter)
1913 if (!relativeZero.valid)
1914 printf("GraphicView::drawRelativeZero(): relativeZero is NOT valid!!!\n");
1917 printf("GraphicView::drawRelativeZero(): painter is NOT valid!!!\n");
1923 //Using Qt::red doesn't seem to work here...
1924 RS_Pen p(RS_Color(255, 0, 0), RS2::Width00, RS2::SolidLine);
1926 painter->setXORMode();
1930 painter->drawLine(Vector(toGuiX(relativeZero.x) - zr, toGuiY(relativeZero.y)),
1931 Vector(toGuiX(relativeZero.x) + zr, toGuiY(relativeZero.y)));
1933 painter->drawLine(Vector(toGuiX(relativeZero.x), toGuiY(relativeZero.y) - zr),
1934 Vector(toGuiX(relativeZero.x), toGuiY(relativeZero.y) + zr));
1936 painter->drawCircle(toGui(relativeZero), zr);
1937 painter->setNormalMode();
1941 * Draws the paper border (for print previews).
1945 void GraphicView::drawPaper()
1950 Drawing * graphic = container->getGraphic();
1955 if (graphic->getPaperScale() < 1.0e-6)
1962 painter->setPen(RS_Pen(Qt::gray));
1964 Vector pinsbase = graphic->getPaperInsertionBase();
1965 Vector size = graphic->getPaperSize();
1966 double scale = graphic->getPaperScale();
1968 Vector v1 = toGui((Vector(0, 0) - pinsbase) / scale);
1969 Vector v2 = toGui((size - pinsbase) / scale);
1972 painter->fillRect(0,0, getWidth(), getHeight(), RS_Color(200, 200, 200));
1975 painter->fillRect((int)(v1.x) + 6, (int)(v1.y) + 6,
1976 (int)((v2.x - v1.x)), (int)((v2.y - v1.y)), RS_Color(64, 64, 64));
1979 painter->fillRect((int)(v1.x), (int)(v1.y),
1980 (int)((v2.x - v1.x)), (int)((v2.y - v1.y)), RS_Color(64, 64, 64));
1983 painter->fillRect((int)(v1.x) + 1, (int)(v1.y) - 1,
1984 (int)((v2.x - v1.x)) - 2, (int)((v2.y - v1.y)) + 2, RS_Color(255, 255, 255));
1992 void GraphicView::drawGrid()
1994 if (!grid || isGridOn() == false)
1996 // printf("GraphicView::drawGrid(): Aborting: grid=%08X, isGridOn=%s\n", grid, (isGridOn() ? "true" : "false"));
2001 painter->setPen(gridColor);
2003 //Problem here: pts is NULL!
2004 Vector * pts = grid->getPoints();
2008 for(int i=0; i<grid->count(); ++i)
2009 painter->drawGridPoint(toGui(pts[i]));
2012 // printf("GraphicView::drawGrid(): pts == NULL!\n");
2015 QString info = grid->getInfo();
2016 updateGridStatusWidget(info);
2020 * Draws the meta grid.
2024 void GraphicView::drawMetaGrid()
2026 if (!grid || isGridOn() == false)
2032 RS_Pen pen(metaGridColor, RS2::Width00, RS2::DotLine);
2033 painter->setPen(pen);
2036 double * mx = grid->getMetaX();
2037 double * my = grid->getMetaY();
2041 for(int i=0; i<grid->countMetaX(); ++i)
2042 painter->drawLine(Vector(toGuiX(mx[i]), 0), Vector(toGuiX(mx[i]), getHeight()));
2047 for(int i=0; i<grid->countMetaY(); ++i)
2048 painter->drawLine(Vector(0, toGuiY(my[i])), Vector(getWidth(), toGuiY(my[i])));
2053 * Updates the grid if there is one.
2055 void GraphicView::updateGrid()
2061 RS_Grid * GraphicView::getGrid()
2066 void GraphicView::updateGridStatusWidget(const QString & /*text*/)
2070 RS2::SnapMode GraphicView::getDefaultSnapMode()
2072 return defaultSnapMode;
2075 RS2::SnapRestriction GraphicView::getSnapRestriction()
2077 return defaultSnapRes;
2081 * Sets the default snap mode used by newly created actions.
2083 void GraphicView::setDefaultSnapMode(RS2::SnapMode sm)
2085 defaultSnapMode = sm;
2088 eventHandler->setSnapMode(sm);
2090 //OK, the above sets the snap mode in the snapper that's derived from
2091 //the RS_ActionInterface and RS_Snapper. So the following should fix
2092 //us up, hm notwithstanding. [and it does. :-)]
2094 snapper.setSnapMode(sm);
2098 * Sets a snap restriction (e.g. orthogonal).
2100 void GraphicView::setSnapRestriction(RS2::SnapRestriction sr)
2102 defaultSnapRes = sr;
2104 if (eventHandler != NULL)
2105 eventHandler->setSnapRestriction(sr);
2109 * Translates a vector in real coordinates to a vector in screen coordinates.
2111 Vector GraphicView::toGui(Vector v)
2113 return Vector(toGuiX(v.x), toGuiY(v.y), 0.0);
2117 * Translates a real coordinate in X to a screen coordinate X.
2118 * @param visible Pointer to a boolean which will contain true
2119 * after the call if the coordinate is within the visible range.
2121 double GraphicView::toGuiX(double x, bool * visible)
2123 if (visible != NULL)
2125 double res = x * factor.x + offsetX;
2127 if (res > 0.0 && res < getWidth())
2133 return x * factor.x + offsetX;
2137 * Translates a real coordinate in Y to a screen coordinate Y.
2139 double GraphicView::toGuiY(double y)
2141 return -y * factor.y + getHeight() - offsetY;
2145 * Translates a real coordinate distance to a screen coordinate distance.
2147 double GraphicView::toGuiDX(double d)
2149 return d * factor.x;
2153 * Translates a real coordinate distance to a screen coordinate distance.
2155 double GraphicView::toGuiDY(double d)
2157 return d * factor.y;
2161 * Translates a vector in screen coordinates to a vector in real coordinates.
2163 Vector GraphicView::toGraph(Vector v)
2165 return Vector(toGraphX(RS_Math::round(v.x)), toGraphY(RS_Math::round(v.y)), 0.0);
2169 * Translates two screen coordinates to a vector in real coordinates.
2171 Vector GraphicView::toGraph(int x, int y)
2173 return Vector(toGraphX(x), toGraphY(y), 0.0);
2177 * Translates a screen coordinate in X to a real coordinate X.
2179 double GraphicView::toGraphX(int x)
2181 return (x - offsetX) / factor.x;
2185 * Translates a screen coordinate in Y to a real coordinate Y.
2187 double GraphicView::toGraphY(int y)
2189 return -(y - getHeight() + offsetY) / factor.y;
2193 * Translates a screen coordinate distance to a real coordinate distance.
2195 double GraphicView::toGraphDX(int d)
2197 return d / factor.x;
2201 * Translates a screen coordinate distance to a real coordinate distance.
2203 double GraphicView::toGraphDY(int d)
2205 return d / factor.y;
2209 * (Un-)Locks the position of the relative zero.
2211 * @param lock true: lock, false: unlock
2213 void GraphicView::lockRelativeZero(bool lock)
2215 relativeZeroLocked = lock;
2219 * @return true if the position of the realtive zero point is
2222 bool GraphicView::isRelativeZeroLocked()
2224 return relativeZeroLocked;
2228 * @return Relative zero coordinate.
2230 Vector GraphicView::getRelativeZero()
2232 return relativeZero;
2236 * Sets the relative zero coordinate (if not locked)
2237 * without deleting / drawing the point.
2239 void GraphicView::setRelativeZero(const Vector & pos)
2241 if (!relativeZeroLocked)
2246 * Sets the relative zero coordinate, deletes the old position
2247 * on the screen and draws the new one.
2249 void GraphicView::moveRelativeZero(const Vector & pos)
2251 setRelativeZero(pos);
2254 RS_EventHandler * GraphicView::getEventHandler()
2256 return eventHandler;
2260 * Enables or disables print preview.
2262 void GraphicView::setPrintPreview(bool pv)
2268 * @retval true This is a print preview graphic view.
2269 * @retval false Otherwise.
2271 bool GraphicView::isPrintPreview()
2273 return printPreview;
2277 * Enables or disables printing.
2279 void GraphicView::setPrinting(bool p)
2285 * @retval true This is a a graphic view for printing.
2286 * @retval false Otherwise.
2288 bool GraphicView::isPrinting()
2294 * @retval true Draft mode is on for this view (all lines with 1 pixel / no style scaling).
2295 * @retval false Otherwise.
2297 bool GraphicView::isDraftMode()
2303 * Sets the simulation speed in percentage.
2305 void GraphicView::setSimulationSpeed(int s)
2307 simulationSpeed = s;
2311 * @return the simulation speed in percentage.
2313 int GraphicView::getSimulationSpeed()
2315 return simulationSpeed;
2319 * Sets the simulation smooth mode.
2321 void GraphicView::setSimulationSmooth(bool s)
2323 simulationSmooth = s;
2326 * Sets the simulation rapid mode.
2328 void GraphicView::setSimulationRapid(bool r)
2330 simulationRapid = r;
2334 * @return the simulation rapid mode.
2336 bool GraphicView::getSimulationRapid()
2338 return simulationRapid;