📄 rs_graphicview.cpp
字号:
/****************************************************************************** $Id: rs_graphicview.cpp 1938 2004-12-09 23:09:53Z andrew $**** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.**** This file is part of the qcadlib Library project.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid qcadlib Professional Edition licenses may use ** this file in accordance with the qcadlib Commercial License** Agreement provided with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.ribbonsoft.com for further details.**** Contact info@ribbonsoft.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "rs_graphicview.h"#include <stdio.h>#include "rs_application.h"#include "rs_actioninterface.h"#include "rs_block.h"#include "rs_eventhandler.h"#include "rs_graphic.h"#include "rs_grid.h"#include "rs_insert.h"#include "rs_keyevent.h"#include "rs_layer.h"#include "rs_line.h"#include "rs_mouseevent.h"#include "rs_painter.h"#include "rs_text.h"#include "rs_settings.h"#include "rs_solid.h"/** * Constructor. */RS_GraphicView::RS_GraphicView() : background(), foreground() { drawingMode = RS2::ModeFull; printing = false; deleteMode = false; factor = RS_Vector(1.0,1.0); offsetX = 0; offsetY = 0; previousFactor = RS_Vector(1.0,1.0); previousOffsetX = 0; previousOffsetY = 0; container = NULL; eventHandler = new RS_EventHandler(this); gridColor = Qt::gray; metaGridColor = RS_Color(64,64,64); grid = new RS_Grid(this); updateEnabled = 0; zoomFrozen = false; //gridVisible = true; draftMode = false; painter = NULL; //drawRecursion = 0; borderLeft = 0; borderTop = 0; borderRight = 0; borderBottom = 0; relativeZero = RS_Vector(false); relativeZeroLocked=false; mx = my = 0; defaultSnapMode = RS2::SnapFree; defaultSnapRes = RS2::RestrictNothing; /* background = RS_Color(0,0,0); foreground = RS_Color(255,255,255); gridColor = RS_Color("gray"); metaGridColor = RS_Color("#404040"); selectedColor = RS_Color("#a54747"); highlightedColor = RS_Color("#739373"); */ RS_SETTINGS->beginGroup("/Appearance"); setBackground(QColor(RS_SETTINGS->readEntry("/BackgroundColor", "#000000"))); setGridColor(QColor(RS_SETTINGS->readEntry("/GridColor", "#7F7F7F"))); setMetaGridColor(QColor(RS_SETTINGS->readEntry("/MetaGridColor", "#3F3F3F"))); setSelectedColor(QColor(RS_SETTINGS->readEntry("/SelectedColor", "#A54747"))); setHighlightedColor(QColor(RS_SETTINGS->readEntry("/HighlightedColor", "#739373"))); RS_SETTINGS->endGroup(); printPreview = false; simulationSpeed = 100; simulationSmooth = false; simulationRapid = false; simulationRunning = false; //currentInsert = NULL;}/** * Destructor. */RS_GraphicView::~RS_GraphicView() { //delete eventHandler; if (painter!=NULL) { delete painter; } delete grid;}/** * Must be called by any derrived class in the destructor. */void RS_GraphicView::cleanUp() { delete eventHandler;}/** * Sets the pointer to the graphic which contains the entities * which are visualized by this widget. */void RS_GraphicView::setContainer(RS_EntityContainer* container) { this->container = container; //adjustOffsetControls();}/** * Sets the zoom factor in X for this visualization of the graphic. */void RS_GraphicView::setFactorX(double f) { if (!zoomFrozen) { factor.x = fabs(f); }}/** * Sets the zoom factor in Y for this visualization of the graphic. */void RS_GraphicView::setFactorY(double f) { if (!zoomFrozen) { factor.y = fabs(f); }}/** * @return true if the grid is switched on. */bool RS_GraphicView::isGridOn() { if (container!=NULL) { RS_Graphic* g = container->getGraphic(); if (g!=NULL) { return g->isGridOn(); } } return true;}/** * Centers the drawing in x-direction. */void RS_GraphicView::centerOffsetX() { if (container!=NULL && !zoomFrozen) { offsetX = (int)(((getWidth()-borderLeft-borderRight) - (container->getSize().x*factor.x))/2.0 - (container->getMin().x*factor.x)) + borderLeft; }}/** * Centers the drawing in y-direction. */void RS_GraphicView::centerOffsetY() { if (container!=NULL && !zoomFrozen) { offsetY = (int)((getHeight()-borderTop-borderBottom - (container->getSize().y*factor.y))/2.0 - (container->getMin().y*factor.y)) + borderBottom; }}/** * Centers the given coordinate in the view in x-direction. */void RS_GraphicView::centerX(double v) { if (!zoomFrozen) { offsetX = (int)((v*factor.x) - (double)(getWidth()-borderLeft-borderRight)/2.0); }}/** * Centers the given coordinate in the view in y-direction. */void RS_GraphicView::centerY(double v) { if (!zoomFrozen) { offsetY = (int)((v*factor.y) - (double)(getHeight()-borderTop-borderBottom)/2.0); }}void RS_GraphicView::updateView() { static int running = 0; running++; if (running<100) { adjustZoomControls(); adjustOffsetControls(); } running--; if(running==0) { redraw(); }}/** * @return Current action or NULL. */RS_ActionInterface* RS_GraphicView::getDefaultAction() { if (eventHandler!=NULL) { return eventHandler->getDefaultAction(); } else { return NULL; }}/** * Sets the default action of the event handler. */void RS_GraphicView::setDefaultAction(RS_ActionInterface* action) { if (eventHandler!=NULL) { eventHandler->setDefaultAction(action); }}/** * @return Current action or NULL. */RS_ActionInterface* RS_GraphicView::getCurrentAction() { if (eventHandler!=NULL) { return eventHandler->getCurrentAction(); } else { return NULL; }}/** * Sets the current action of the event handler. */void RS_GraphicView::setCurrentAction(RS_ActionInterface* action) { RS_DEBUG->print("RS_GraphicView::setCurrentAction"); if (eventHandler!=NULL) { eventHandler->setCurrentAction(action); } RS_DEBUG->print("RS_GraphicView::setCurrentAction: OK");}/** * Kills all running selection actions. Called when a selection action * is launched to reduce confusion. */void RS_GraphicView::killSelectActions() { if (eventHandler!=NULL) { eventHandler->killSelectActions(); }}/** * Kills all running actions. */void RS_GraphicView::killAllActions() { if (eventHandler!=NULL) { eventHandler->killAllActions(); }}/** * Go back in menu or current action. */void RS_GraphicView::back() { if (eventHandler!=NULL && eventHandler->hasAction()) { eventHandler->back(); } else { if (RS_DIALOGFACTORY!=NULL) { RS_DIALOGFACTORY->requestPreviousMenu(); } }}/** * Go forward with the current action. */void RS_GraphicView::enter() { if (eventHandler!=NULL && eventHandler->hasAction()) { eventHandler->enter(); }}/** * Called by the actual GUI class which implements the RS_GraphicView * interface to notify qcadlib about mouse events. */void RS_GraphicView::mousePressEvent(RS_MouseEvent* e) { if (eventHandler!=NULL) { eventHandler->mousePressEvent(e); }}/** * Called by the actual GUI class which implements the RS_GraphicView * interface to notify qcadlib about mouse events. */void RS_GraphicView::mouseReleaseEvent(RS_MouseEvent* e) { RS_DEBUG->print("RS_GraphicView::mouseReleaseEvent"); if (eventHandler!=NULL) { if (RS2::qtToRsButtonState(e->button())!=RS2::RightButton || eventHandler->hasAction()) { eventHandler->mouseReleaseEvent(e); //e->accept(); } else { back();#if QT_VERSION>=0x030000 e->accept();#endif } } RS_DEBUG->print("RS_GraphicView::mouseReleaseEvent: OK");}/** * Called by the actual GUI class which implements the RS_GraphicView * interface to notify qcadlib about mouse events. */void RS_GraphicView::mouseMoveEvent(RS_MouseEvent* e) { RS_DEBUG->print("RS_GraphicView::mouseMoveEvent begin"); RS_Graphic* graphic = NULL; if (container->rtti()==RS2::EntityGraphic) { graphic = (RS_Graphic*)container; } RS_DEBUG->print("RS_GraphicView::mouseMoveEvent 001"); if (e!=NULL) { mx = e->x(); my = e->y(); } RS_DEBUG->print("RS_GraphicView::mouseMoveEvent 002"); if (eventHandler!=NULL) { eventHandler->mouseMoveEvent(e); } RS_DEBUG->print("RS_GraphicView::mouseMoveEvent 003"); if (eventHandler==NULL || !eventHandler->hasAction() && graphic!=NULL) { RS_Vector mouse = toGraph(RS_Vector(mx, my)); RS_Vector relMouse = mouse - getRelativeZero(); if (RS_DIALOGFACTORY!=NULL) { RS_DIALOGFACTORY->updateCoordinateWidget(mouse, relMouse); } } RS_DEBUG->print("RS_GraphicView::mouseMoveEvent end");}/** * Called by the actual GUI class which implements the RS_GraphicView * interface to notify qcadlib about mouse events. */void RS_GraphicView::mouseLeaveEvent() { if (eventHandler!=NULL) { eventHandler->mouseLeaveEvent(); }}/** * Called by the actual GUI class which implements the RS_GraphicView * interface to notify qcadlib about mouse events. */void RS_GraphicView::mouseEnterEvent() { if (eventHandler!=NULL) { eventHandler->mouseEnterEvent(); }}/** * Called by the actual GUI class which implements the RS_GraphicView * interface to notify qcadlib about key events. */void RS_GraphicView::keyPressEvent(RS_KeyEvent* e) { if (eventHandler!=NULL) { eventHandler->keyPressEvent(e); }}/** * Called by the actual GUI class which implements the RS_GraphicView * interface to notify qcadlib about key events. */void RS_GraphicView::keyReleaseEvent(RS_KeyEvent* e) { if (eventHandler!=NULL) { eventHandler->keyReleaseEvent(e); }}/** * Called by the actual GUI class which implements a command line. */void RS_GraphicView::commandEvent(RS_CommandEvent* e) { if (eventHandler!=NULL) { eventHandler->commandEvent(e); }}/** * Enables coordinate input in the command line. */void RS_GraphicView::enableCoordinateInput() { if (eventHandler!=NULL) { eventHandler->enableCoordinateInput(); }}/** * Disables coordinate input in the command line. */void RS_GraphicView::disableCoordinateInput() { if (eventHandler!=NULL) { eventHandler->disableCoordinateInput(); }}/** * zooms in by factor f */void RS_GraphicView::zoomIn(double f, const RS_Vector& center) { if (f<1.0e-6) { RS_DEBUG->print(RS_Debug::D_WARNING, "RS_GraphicView::zoomIn: invalid factor"); return; } if (simulationRunning) { return; } RS_Vector c = center; if (c.valid==false) { c = toGraph(RS_Vector(getWidth()/2, getHeight()/2)); } zoomWindow( toGraph(RS_Vector(0,0)) .scale(c, RS_Vector(1.0/f,1.0/f)), toGraph(RS_Vector(getWidth(),getHeight())) .scale(c, RS_Vector(1.0/f,1.0/f))); //adjustOffsetControls(); //adjustZoomControls(); //updateGrid(); //redraw();}/** * zooms in by factor f in x */void RS_GraphicView::zoomInX(double f) { if (simulationRunning) { return; } factor.x*=f; offsetX=(int)((offsetX-getWidth()/2)*f)+getWidth()/2; adjustOffsetControls(); adjustZoomControls(); updateGrid(); redraw();}/** * zooms in by factor f in y */void RS_GraphicView::zoomInY(double f) { if (simulationRunning) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -