This commit is contained in:
Chenwenxuan
2024-03-06 14:54:30 +08:00
commit edac2715f0
1525 changed files with 809982 additions and 0 deletions

View File

@@ -0,0 +1,326 @@
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** 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 gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#include <QKeyEvent>
#include "rs_actioninterface.h"
#include "rs_graphicview.h"
#include "rs_commands.h"
#include "rs_dialogfactory.h"
#include "rs_coordinateevent.h"
#include "rs_debug.h"
/**
* Constructor.
*
* Sets the entity container on which the action class inherited
* from this interface operates.
*
* @param name Action name. This can be used internally for
* debugging mainly.
* @param container Entity container this action operates on.
* @param graphicView Graphic view instance this action operates on.
* Please note that an action belongs to this
* view.
* @param cursor Default mouse cursor for this action. If the action
* is suspended and resumed again the cursor will always
* be reset to the one given here.
*/
RS_ActionInterface::RS_ActionInterface(const char* name,
RS_EntityContainer& container,
RS_GraphicView& graphicView) :
RS_Snapper(container, graphicView) {
RS_DEBUG->print("RS_ActionInterface::RS_ActionInterface: Setting up action: \"%s\"", name);
this->name = name;
status = 0;
finished = false;
//triggerOnResume = false;
// graphic provides a pointer to the graphic if the
// entity container is a graphic (i.e. can also hold
// layers).
graphic = container.getGraphic();
// document pointer will be used for undo / redo
document = container.getDocument();
//this->cursor = cursor;
//setSnapMode(graphicView.getDefaultSnapMode());
actionType=RS2::ActionNone;
RS_DEBUG->print("RS_ActionInterface::RS_ActionInterface: Setting up action: \"%s\": OK", name);
}
/**
* Must be implemented to return the ID of this action.
*
* @todo no default implementation
*/
RS2::ActionType RS_ActionInterface::rtti() const{
return actionType;
}
/**
* @return name of this action
*/
QString RS_ActionInterface::getName() {
return name;
}
void RS_ActionInterface::setName(const char* _name) {
this->name=_name;
}
/**
* Called to initiate an action. This funtcion is often
* overwritten by the implementing action.
*
* @param status The status on which to initiate this action.
* default is 0 to begin the action.
*/
void RS_ActionInterface::init(int status)
{
setStatus(status);
if (status>=0) {
RS_Snapper::init();
updateMouseButtonHints();
updateMouseCursor();
}else{
//delete snapper when finished, bug#3416878
deleteSnapper();
}
}
/**
* Called when the mouse moves and this is the current action.
* This function can be overwritten by the implementing action.
* The default implementation keeps track of the mouse position.
*/
void RS_ActionInterface::mouseMoveEvent(QMouseEvent*) {}
/**
* Called when the left mouse button is pressed and this is the
* current action.
* This function can be overwritten by the implementing action.
* The default implementation does nothing.
*/
void RS_ActionInterface::mousePressEvent(QMouseEvent*) {}
/**
* Called when the left mouse button is released and this is
* the current action.
* This function can be overwritten by the implementing action.
* The default implementation does nothing.
*/
void RS_ActionInterface::mouseReleaseEvent(QMouseEvent*) {}
/**
* Called when a key is pressed and this is the current action.
* This function can be overwritten by the implementing action.
* The default implementation does nothing.
*/
void RS_ActionInterface::keyPressEvent(QKeyEvent* e) {
e->ignore();
}
/**
* Called when a key is released and this is the current action.
* This function can be overwritten by the implementing action.
* The default implementation does nothing.
*/
void RS_ActionInterface::keyReleaseEvent(QKeyEvent* e) {
e->ignore();
}
/**
* Coordinate event. Triggered usually from a command line.
* This function can be overwritten by the implementing action.
* The default implementation does nothing.
*/
void RS_ActionInterface::coordinateEvent(RS_CoordinateEvent*) {}
/**
* Called when a command from the command line is launched.
* and this is the current action.
* This function can be overwritten by the implementing action.
* The default implementation does nothing.
*/
void RS_ActionInterface::commandEvent(RS_CommandEvent*) {
}
/**
* Must be implemented to return the currently available commands
* for the command line.
*/
QStringList RS_ActionInterface::getAvailableCommands() {
return QStringList{};
}
/**
* Sets the current status (progress) of this action.
* The default implementation sets the class variable 'status' to the
* given value and finishes the action if 'status' is negative.
*
* @param status Status number. It's up to the action implementor
* what the action uses the status for. However, a
* negative status number finishes the action. Usually
* the status of an action increases for every step
* of progress and decreases when the user goes one
* step back (i.e. presses the right mouse button).
*/
void RS_ActionInterface::setStatus(int status) {
this->status = status;
updateMouseButtonHints();
updateMouseCursor();
if(status<0) finish();
}
/**
* @return Current status of this action.
*/
int RS_ActionInterface::getStatus() {
return status;
}
/**
* Triggers this action. This should be called after all
* data needed for this action was collected / set.
* The default implementation does nothing.
*/
void RS_ActionInterface::trigger() {}
/**
* Should be overwritten to update the mouse button hints
* wherever they might needed.
*/
void RS_ActionInterface::updateMouseButtonHints() {}
/**
* Should be overwritten to set the mouse cursor for this action.
*/
void RS_ActionInterface::updateMouseCursor() {}
/**
* @return true, if the action is finished and can be deleted.
*/
bool RS_ActionInterface::isFinished() {
return finished;
}
/**
* Forces a termination of the action without any cleanup.
*/
void RS_ActionInterface::setFinished() {
status = -1;
}
/**
* Finishes this action.
*/
void RS_ActionInterface::finish(bool /*updateTB*/)
{
RS_DEBUG->print("RS_ActionInterface::finish");
//refuse to quit the default action
if(rtti() != RS2::ActionDefault) {
status = -1;
finished = true;
hideOptions();
RS_Snapper::finish();
}
RS_DEBUG->print("RS_ActionInterface::finish: OK");
}
/**
* Called by the event handler to give this action a chance to
* communicate with its predecessor.
*/
void RS_ActionInterface::setPredecessor(RS_ActionInterface* pre) {
predecessor = pre;
}
/**
* Suspends this action while another action takes place.
*/
void RS_ActionInterface::suspend() {
RS_Snapper::suspend();
}
/**
* Resumes an action after it was suspended.
*/
void RS_ActionInterface::resume() {
updateMouseCursor();
updateMouseButtonHints();
RS_Snapper::resume();
}
/**
* Hides the tool options. Default implementation does nothing.
*/
void RS_ActionInterface::hideOptions() {
RS_Snapper::hideOptions();
}
/**
* Shows the tool options. Default implementation does nothing.
*/
void RS_ActionInterface::showOptions() {
RS_Snapper::showOptions();
}
void RS_ActionInterface::setActionType(RS2::ActionType actionType){
this->actionType=actionType;
}
/**
* Calls checkCommand() from the RS_COMMANDS module.
*/
bool RS_ActionInterface::checkCommand(const QString& cmd, const QString& str,
RS2::ActionType action) {
return RS_COMMANDS->checkCommand(cmd, str, action);
}
/**
* Calls command() from the RS_COMMANDS module.
*/
QString RS_ActionInterface::command(const QString& cmd) {
return RS_COMMANDS->command(cmd);
}
/**
* Calls msgAvailableCommands() from the RS_COMMANDS module.
*/
QString RS_ActionInterface::msgAvailableCommands() {
return RS_COMMANDS->msgAvailableCommands();
}

View File

@@ -0,0 +1,161 @@
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** 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 gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#ifndef RS_ACTIONINTERFACE_H
#define RS_ACTIONINTERFACE_H
#include <QObject>
#include "rs_snapper.h"
class QKeyEvent;
class RS_CommandEvent;
class RS_CoordinateEvent;
class RS_Graphic;
class RS_Document;
class QAction;
/**
* This is the interface that must be implemented for all
* action classes. Action classes handle actions such
* as drawing lines, moving entities or zooming in.
*
* Inherited from QObject for Qt translation features.
*
* @author Andrew Mustun
*/
class RS_ActionInterface : public QObject, public RS_Snapper {
Q_OBJECT
public:
RS_ActionInterface(const char* name,
RS_EntityContainer& container,
RS_GraphicView& graphicView);
virtual ~RS_ActionInterface() = default;
virtual RS2::ActionType rtti() const;
void setName(const char* _name);
QString getName();
virtual void init(int status=0);
virtual void mouseMoveEvent(QMouseEvent*);
virtual void mousePressEvent(QMouseEvent*);
virtual void mouseReleaseEvent(QMouseEvent*);
virtual void keyPressEvent(QKeyEvent* e);
virtual void keyReleaseEvent(QKeyEvent* e);
virtual void coordinateEvent(RS_CoordinateEvent*);
virtual void commandEvent(RS_CommandEvent*);
virtual QStringList getAvailableCommands();
virtual void setStatus(int status);
virtual int getStatus();
virtual void trigger();
virtual void updateMouseButtonHints();
virtual void updateMouseCursor();
virtual bool isFinished();
virtual void setFinished();
virtual void finish(bool updateTB = true );
virtual void setPredecessor(RS_ActionInterface* pre);
virtual void suspend();
virtual void resume();
virtual void hideOptions();
virtual void showOptions();
virtual void setActionType(RS2::ActionType actionType);
bool checkCommand(const QString& cmd, const QString& str,
RS2::ActionType action=RS2::ActionNone);
QString command(const QString& cmd);
QString msgAvailableCommands();
private:
/**
* Current status of the action. After an action has
* been created the action status is set to 0. Actions
* that are terminated have a stats of -1. Other status
* numbers can be used to describe the stage this action
* is in. E.g. a window zoom consists of selecting the
* first corner (status 0), and selecting the second
* corner (status 1).
*/
int status;
protected:
/** Action name. Used internally for debugging */
QString name;
/**
* This flag is set when the action has terminated and
* can be deleted.
*/
bool finished;
/**
* Pointer to the graphic is this container is a graphic.
* NULL otherwise
*/
RS_Graphic* graphic;
/**
* Pointer to the document (graphic or block) or NULL.
*/
RS_Document* document;
/**
* Pointer to the default mouse cursor for this action or NULL.
*/
//RS2::CursorType cursor;
/**
* Predecessor of this action or NULL.
*/
RS_ActionInterface* predecessor;
/**
* String prepended to the help text for currently available commands.
*/
//static QString msgAvailableCommands;
/**
* Command used for showing help for every action.
*/
//static QString cmdHelp;
/**
* Command for answering yes to a question.
*/
//static QString cmdYes;
//static QString cmdYes2;
/**
* Command for answering no to a question.
*/
//static QString cmdNo;
//static QString cmdNo2;
RS2::ActionType actionType;
};
#endif

190
lib/actions/rs_preview.cpp Normal file
View File

@@ -0,0 +1,190 @@
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** 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 gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#include "rs_preview.h"
#include "rs_entitycontainer.h"
#include "rs_line.h"
#include "rs_graphicview.h"
#include "rs_information.h"
#include "rs_settings.h"
/**
* Constructor.
*/
RS_Preview::RS_Preview(RS_EntityContainer* parent)
: RS_EntityContainer(parent, true) {
RS_SETTINGS->beginGroup("/Appearance");
maxEntities = RS_SETTINGS->readNumEntry("/MaxPreview", 100);
RS_SETTINGS->endGroup();
}
/**
* Adds an entity to this preview and removes any attributes / layer
* connections before that.
*/
void RS_Preview::addEntity(RS_Entity* entity) {
if (!entity || entity->isUndone()) {
return;
}
// only border preview for complex entities:
//if ((entity->count() > maxEntities-count()) &&
bool addBorder = false;
if (entity->rtti()==RS2::EntityImage || entity->rtti()==RS2::EntityHatch ||
entity->rtti()==RS2::EntityInsert) {
addBorder = true;
} else {
if (entity->isContainer() && entity->rtti()!=RS2::EntitySpline) {
if (entity->countDeep() > maxEntities-countDeep()) {
addBorder = true;
}
}
}
if (addBorder) {
RS_Vector min = entity->getMin();
RS_Vector max = entity->getMax();
RS_Line* l1 =
new RS_Line(this, {min.x, min.y}, {max.x, min.y});
RS_Line* l2 =
new RS_Line(this, {max.x, min.y}, {max.x, max.y});
RS_Line* l3 =
new RS_Line(this, {max.x, max.y}, {min.x, max.y});
RS_Line* l4 =
new RS_Line(this, {min.x, max.y}, {min.x, min.y});
RS_EntityContainer::addEntity(l1);
RS_EntityContainer::addEntity(l2);
RS_EntityContainer::addEntity(l3);
RS_EntityContainer::addEntity(l4);
delete entity;
entity = nullptr;
} else {
entity->setLayer(nullptr);
entity->setSelected(false);
entity->reparent(this);
// Don't set this pen, let drawing routines decide entity->setPenToActive();
RS_EntityContainer::addEntity(entity);
}
}
/**
* Clones the given entity and adds the clone to the preview.
*/
void RS_Preview::addCloneOf(RS_Entity* entity) {
if (!entity) {
return;
}
RS_Entity* clone = entity->clone();
clone->reparent(this);
addEntity(clone);
}
/**
* Adds all entities from 'container' to the preview (unselected).
*/
void RS_Preview::addAllFrom(RS_EntityContainer& container) {
int c=0;
for(auto e: container){
if (c<maxEntities) {
RS_Entity* clone = e->clone();
clone->setSelected(false);
clone->reparent(this);
c+=clone->countDeep();
addEntity(clone);
// clone might be nullptr after this point
}
}
}
/**
* Adds all selected entities from 'container' to the preview (unselected).
*/
void RS_Preview::addSelectionFrom(RS_EntityContainer& container) {
int c=0;
for(auto e: container){
if (e->isSelected() && c<maxEntities) {
RS_Entity* clone = e->clone();
clone->setSelected(false);
clone->reparent(this);
c+=clone->countDeep();
addEntity(clone);
// clone might be nullptr after this point
}
}
}
/**
* Adds all entities in the given range and those which have endpoints
* in the given range to the preview.
*/
void RS_Preview::addStretchablesFrom(RS_EntityContainer& container,
const RS_Vector& v1, const RS_Vector& v2) {
int c=0;
for(auto e: container){
if (e->isVisible() &&
e->rtti()!=RS2::EntityHatch &&
((e->isInWindow(v1, v2)) ||
e->hasEndpointsWithinWindow(v1, v2)) &&
c<maxEntities) {
RS_Entity* clone = e->clone();
//clone->setSelected(false);
clone->reparent(this);
c+=clone->countDeep();
addEntity(clone);
// clone might be nullptr after this point
}
}
}
void RS_Preview::draw(RS_Painter* painter, RS_GraphicView* view,
double& patternOffset) {
if (!(painter && view)) {
return;
}
foreach (auto e, entities)
{
e->draw(painter, view, patternOffset);
}
}

60
lib/actions/rs_preview.h Normal file
View File

@@ -0,0 +1,60 @@
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** 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 gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#ifndef RS_PREVIEW_H
#define RS_PREVIEW_H
#include "rs_entitycontainer.h"
/**
* This class supports previewing. The RS_Snapper class uses
* an instance of RS_Preview to preview entities, ranges,
* lines, arcs, ... on the fly.
*
* @author Andrew Mustun
*/
class RS_Preview : public RS_EntityContainer {
public:
RS_Preview(RS_EntityContainer* parent=nullptr);
~RS_Preview() = default;
virtual RS2::EntityType rtti() const override{
return RS2::EntityPreview;
}
virtual void addEntity(RS_Entity* entity) override;
void addCloneOf(RS_Entity* entity);
virtual void addSelectionFrom(RS_EntityContainer& container);
virtual void addAllFrom(RS_EntityContainer& container);
virtual void addStretchablesFrom(RS_EntityContainer& container,
const RS_Vector& v1, const RS_Vector& v2);
void draw(RS_Painter* painter, RS_GraphicView* view, double& patternOffset) override;
private:
int maxEntities;
};
#endif

View File

@@ -0,0 +1,130 @@
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** 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 gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#include "rs_previewactioninterface.h"
#include "rs_graphicview.h"
#include "rs_preview.h"
#include "rs_debug.h"
/**
* Constructor.
*
* Sets the entity container on which the action class inherited
* from this interface operates.
*/
RS_PreviewActionInterface::RS_PreviewActionInterface(const char* name,
RS_EntityContainer& container,
RS_GraphicView& graphicView) :
RS_ActionInterface(name, container, graphicView)
,preview(new RS_Preview(&container))
// ,offset(new RS_Vector{})
{
RS_DEBUG->print("RS_PreviewActionInterface::RS_PreviewActionInterface: Setting up action with preview: \"%s\"", name);
// preview is linked to the container for getting access to
// document settings / dictionary variables
preview->setLayer(NULL);
hasPreview = true;
RS_DEBUG->print("RS_PreviewActionInterface::RS_PreviewActionInterface: Setting up action with preview: \"%s\": OK", name);
}
/** Destructor */
RS_PreviewActionInterface::~RS_PreviewActionInterface() {
deletePreview();
}
void RS_PreviewActionInterface::init(int status) {
deletePreview();
RS_ActionInterface::init(status);
}
void RS_PreviewActionInterface::finish(bool updateTB) {
deletePreview();
RS_ActionInterface::finish(updateTB);
}
void RS_PreviewActionInterface::suspend() {
RS_ActionInterface::suspend();
deletePreview();
}
void RS_PreviewActionInterface::resume() {
RS_ActionInterface::resume();
drawPreview();
}
void RS_PreviewActionInterface::trigger() {
RS_ActionInterface::trigger();
deletePreview();
}
/**
* Deletes the preview from the screen.
*/
void RS_PreviewActionInterface::deletePreview() {
if (hasPreview){
//avoid deleting NULL or empty preview
preview->clear();
hasPreview=false;
}
if(!graphicView->isCleanUp()){
graphicView->getOverlayContainer(RS2::ActionPreviewEntity)->clear();
}
}
/**
* Draws / deletes the current preview.
*/
void RS_PreviewActionInterface::drawPreview() {
// RVT_PORT How does offset work?? painter->setOffset(offset);
RS_EntityContainer *container=graphicView->getOverlayContainer(RS2::ActionPreviewEntity);
container->clear();
container->setOwner(false); // Little hack for now so we don't delete the preview twice
container->addEntity(preview.get());
graphicView->redraw(RS2::RedrawOverlay);
hasPreview=true;
}

View File

@@ -0,0 +1,69 @@
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** 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 gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#ifndef RS_PREVIEWACTIONINTERFACE_H
#define RS_PREVIEWACTIONINTERFACE_H
#include <memory>
#include "rs_actioninterface.h"
/**
* This is the interface that must be implemented for all
* action classes which need a preview.
*
* @author Andrew Mustun
*/
class RS_PreviewActionInterface : public RS_ActionInterface {
public:
RS_PreviewActionInterface(const char* name,
RS_EntityContainer& container,
RS_GraphicView& graphicView);
~RS_PreviewActionInterface() override;
void init(int status=0) override;
void finish(bool updateTB=true) override;
void suspend() override;
void resume() override;
void trigger() override;
void drawPreview();
void deletePreview();
private:
protected:
/**
* Preview that holds the entities to be previewed.
*/
std::unique_ptr<RS_Preview> preview;
bool hasPreview;//whether preview is in use
// /**
// * Current offset of the preview.
// */
// std::unique_ptr<RS_Vector> offset;
};
#endif

1016
lib/actions/rs_snapper.cpp Normal file

File diff suppressed because it is too large Load Diff

237
lib/actions/rs_snapper.h Normal file
View File

@@ -0,0 +1,237 @@
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2015 Dongxu Li (dongxuli2011@gmail.com)
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** 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 gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#ifndef RS_SNAPPER_H
#define RS_SNAPPER_H
#include<memory>
#include "rs.h"
class RS_Entity;
class RS_GraphicView;
class RS_Vector;
class RS_Preview;
class QMouseEvent;
class RS_EntityContainer;
/**
* This class holds information on how to snap the mouse.
*
* @author Kevin Cox
*/
struct RS_SnapMode {
/* SnapModes for RS_SnapMode to Int conversion and vice versa
*
* The conversion is only used for save/restore of active snap modes in application settings.
* Don't change existing mode order, because this will mess up settings on upgrades
*
* When adding new values, take care for correct implementation in \p toInt() and \p fromInt()
*/
enum SnapModes {
SnapIntersection = 1 << 0,
SnapOnEntity = 1 << 1,
SnapCenter = 1 << 2,
SnapDistance = 1 << 3,
SnapMiddle = 1 << 4,
SnapEndpoint = 1 << 5,
SnapGrid = 1 << 6,
SnapFree = 1 << 7,
RestrictHorizontal = 1 << 8,
RestrictVertical = 1 << 9,
RestrictOrthogonal = RestrictHorizontal | RestrictVertical,
SnapAngle = 1 << 10
};
bool snapIntersection {false}; //< Whether to snap to intersections or not.
bool snapOnEntity {false}; //< Whether to snap to entities or not.
bool snapCenter {false}; //< Whether to snap to centers or not.
bool snapDistance {false}; //< Whether to snap to distance from endpoints or not.
bool snapMiddle {false}; //< Whether to snap to midpoints or not.
bool snapEndpoint {false}; //< Whether to snap to endpoints or not.
bool snapGrid {false}; //< Whether to snap to grid or not.
bool snapFree {false}; //< Whether to snap freely
bool snapAngle {false}; //< Whether to snap along line under certain angle
RS2::SnapRestriction restriction {RS2::RestrictNothing}; /// The restriction on the free snap.
double distance {5.0}; //< The distance to snap before defaulting to free snapping.
/**
* Disable all snapping.
*
* This effectively puts the object into free snap mode.
*
* @returns A reference to itself.
*/
RS_SnapMode const & clear(void);
bool operator == (RS_SnapMode const& rhs) const;
static uint toInt(const RS_SnapMode& s); //< convert to int, to save settings
static RS_SnapMode fromInt(unsigned int); //< convert from int, to restore settings
};
typedef std::initializer_list<RS2::EntityType> EntityTypeList;
/**
* This class is used for snapping functions in a graphic view.
* Actions are usually derived from this base class if they need
* to catch entities or snap to coordinates. Use the methods to
* retrieve a graphic coordinate from a mouse coordinate.
*
* Possible snapping functions are described in RS_SnapMode.
*
* @author Andrew Mustun
*/
class RS_Snapper {
public:
RS_Snapper() = delete;
RS_Snapper(RS_EntityContainer& container, RS_GraphicView& graphicView);
virtual ~RS_Snapper();
void init();
//!
//! \brief finish stop using snapper
//!
void finish();
/**
* @return Pointer to the entity which was the key entity for the
* last successful snapping action. If the snap mode is "end point"
* the key entity is the entity whose end point was caught.
* If the snap mode didn't require an entity (e.g. free, grid) this
* method will return NULL.
*/
RS_Entity* getKeyEntity() const {
return keyEntity;
}
/** Sets a new snap mode. */
void setSnapMode(const RS_SnapMode& snapMode);
RS_SnapMode const* getSnapMode() const;
RS_SnapMode* getSnapMode();
/** Sets a new snap restriction. */
void setSnapRestriction(RS2::SnapRestriction /*snapRes*/) {
//this->snapRes = snapRes;
}
/**
* Sets the snap range in pixels for catchEntity().
*
* @see catchEntity()
*/
void setSnapRange(int r) {
snapRange = r;
}
/**manually set snapPoint*/
RS_Vector snapPoint(const RS_Vector& coord, bool setSpot = false);
RS_Vector snapPoint(QMouseEvent* e);
RS_Vector snapFree(QMouseEvent* e);
RS_Vector snapFree(const RS_Vector& coord);
RS_Vector snapGrid(const RS_Vector& coord);
RS_Vector snapEndpoint(const RS_Vector& coord);
RS_Vector snapOnEntity(const RS_Vector& coord);
RS_Vector snapCenter(const RS_Vector& coord);
RS_Vector snapMiddle(const RS_Vector& coord);
RS_Vector snapDist(const RS_Vector& coord);
RS_Vector snapIntersection(const RS_Vector& coord);
//RS_Vector snapDirect(RS_Vector coord, bool abs);
RS_Vector snapToAngle(const RS_Vector &coord, const RS_Vector &ref_coord, const double ang_res);
RS_Vector restrictOrthogonal(const RS_Vector& coord);
RS_Vector restrictHorizontal(const RS_Vector& coord);
RS_Vector restrictVertical(const RS_Vector& coord);
//RS_Entity* catchLeafEntity(const RS_Vector& pos);
//RS_Entity* catchLeafEntity(QMouseEvent* e);
RS_Entity* catchEntity(const RS_Vector& pos,
RS2::ResolveLevel level=RS2::ResolveNone);
RS_Entity* catchEntity(QMouseEvent* e,
RS2::ResolveLevel level=RS2::ResolveNone);
// catch Entity closest to pos and of the given entity type of enType, only search for a particular entity type
RS_Entity* catchEntity(const RS_Vector& pos, RS2::EntityType enType,
RS2::ResolveLevel level=RS2::ResolveNone);
RS_Entity* catchEntity(QMouseEvent* e, RS2::EntityType enType,
RS2::ResolveLevel level=RS2::ResolveNone);
RS_Entity* catchEntity(QMouseEvent* e, const EntityTypeList& enTypeList,
RS2::ResolveLevel level=RS2::ResolveNone);
/**
* Suspends this snapper while another action takes place.
*/
virtual void suspend();
/**
* Resumes this snapper after it has been suspended.
*/
virtual void resume() {
drawSnapper();
}
virtual void hideOptions();
virtual void showOptions();
void drawSnapper();
protected:
void deleteSnapper();
double getSnapRange() const;
RS_EntityContainer* container;
RS_GraphicView* graphicView;
RS_Entity* keyEntity;
RS_SnapMode snapMode;
//RS2::SnapRestriction snapRes;
/**
* Snap distance for snapping to points with a
* given distance from endpoints.
*/
double m_SnapDistance;
/**
* Snap to equidistant middle points
* default to 1, i.e., equidistant to start/end points
*/
int middlePoints;
/**
* Snap range for catching entities.
*/
int snapRange;
bool finished{false};
private:
struct ImpData;
std::unique_ptr<ImpData> pImpData;
struct Indicator;
Indicator* snap_indicator{nullptr};
};
#endif
//EOF