00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "KoInteractionStrategy.h"
00021 #include "KoSelection.h"
00022 #include "KoShapeManager.h"
00023 #include "KoPointerEvent.h"
00024 #include "KoShapeRubberSelectStrategy.h"
00025 #include "KoShapeMoveStrategy.h"
00026 #include "KoShapeRotateStrategy.h"
00027 #include "KoShapeShearStrategy.h"
00028 #include "KoShapeResizeStrategy.h"
00029 #include "KoCreateShapeStrategy.h"
00030 #include "KoCreateShapesTool.h"
00031 #include "KoInteractionTool.h"
00032 #include "KoCanvasBase.h"
00033 #include "KoTool.h"
00034
00035 #include <kcommand.h>
00036
00037 #include <QMouseEvent>
00038
00039 void KoInteractionStrategy::cancelInteraction() {
00040 KCommand *cmd = createCommand();
00041 if(cmd) {
00042 cmd->unexecute();
00043 delete cmd;
00044 }
00045 }
00046
00047 KoInteractionStrategy::KoInteractionStrategy(KoTool *parent, KoCanvasBase *canvas)
00048 : m_parent(parent)
00049 , m_canvas(canvas)
00050 {
00051 }
00052
00053
00054 KoInteractionStrategy* KoInteractionStrategy::createStrategy(KoPointerEvent *event, KoInteractionTool *parent, KoCanvasBase *canvas) {
00055 if((event->buttons() & Qt::LeftButton) == 0)
00056 return 0;
00057
00058 KoCreateShapesTool *crs = dynamic_cast<KoCreateShapesTool*>(parent);
00059 if(crs)
00060 return new KoCreateShapeStrategy(crs, canvas, event->point);
00061
00062 KoShapeManager *shapeManager = canvas->shapeManager();
00063 KoSelection *select = shapeManager->selection();
00064 bool insideSelection, editableShape=false;
00065 KoFlake::SelectionHandle handle = parent->handleAt(event->point, &insideSelection);
00066
00067 foreach (KoShape* shape, select->selectedShapes()) {
00068 if (shape->isVisible() && !shape->isLocked()) {
00069 editableShape = true;
00070 break;
00071 }
00072 }
00073
00074 if(editableShape && (event->modifiers() == Qt::NoModifier )) {
00075
00076 if(handle != KoFlake::NoHandle) {
00077 if(insideSelection)
00078 return new KoShapeResizeStrategy(parent, canvas, event->point, handle);
00079 if(handle == KoFlake::TopMiddleHandle || handle == KoFlake::RightMiddleHandle ||
00080 handle == KoFlake::BottomMiddleHandle || handle == KoFlake::LeftMiddleHandle)
00081 return new KoShapeShearStrategy(parent, canvas, event->point, handle);
00082 return new KoShapeRotateStrategy(parent, canvas, event->point);
00083 }
00084
00085 if(select->boundingRect().contains(event->point))
00086 return new KoShapeMoveStrategy(parent, canvas, event->point);
00087 }
00088
00089 KoShape * object( shapeManager->shapeAt( event->point, (event->modifiers() & Qt::ShiftButton) ? KoFlake::NextUnselected : KoFlake::ShapeOnTop, true ) );
00090 if( !object && handle == KoFlake::NoHandle) {
00091 if ( ( event->modifiers() & Qt::ControlModifier ) == 0 )
00092 {
00093 parent->repaintDecorations();
00094 select->deselectAll();
00095 }
00096 return new KoShapeRubberSelectStrategy(parent, canvas, event->point);
00097 }
00098
00099 if(select->isSelected(object)) {
00100 if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier ) {
00101 parent->repaintDecorations();
00102 select->deselect(object);
00103 }
00104 }
00105 else if(handle == KoFlake::NoHandle) {
00106 parent->repaintDecorations();
00107 if ( ( event->modifiers() & Qt::ControlModifier ) == 0 )
00108 shapeManager->selection()->deselectAll();
00109 select->select(object);
00110 parent->repaintDecorations();
00111 return new KoShapeMoveStrategy(parent, canvas, event->point);
00112 }
00113 return 0;
00114 }
00115
00116 void KoInteractionStrategy::applyGrid(QPointF &point) {
00117
00118
00119
00120 double gridX, gridY;
00121 m_canvas->gridSize(&gridX, &gridY);
00122
00123
00124 point.setX( static_cast<int>( point.x() / gridX + 1e-10 ) * gridX );
00125 point.setY( static_cast<int>( point.y() / gridY + 1e-10 ) * gridY );
00126 }