krita

kis_strategy_move.cc

00001 /*
00002  *  Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include <qpoint.h>
00020 #include <qcolor.h>
00021 
00022 #include <kaction.h>
00023 #include <kcommand.h>
00024 #include <klocale.h>
00025 #include <kdebug.h>
00026 
00027 #include "kis_canvas_controller.h"
00028 #include "kis_canvas_subject.h"
00029 #include "kis_image.h"
00030 #include "kis_layer.h"
00031 #include "kis_strategy_move.h"
00032 #include "kis_undo_adapter.h"
00033 
00034 KisStrategyMove::KisStrategyMove()
00035 {
00036     reset(0);
00037 }
00038 
00039 KisStrategyMove::KisStrategyMove(KisCanvasSubject *subject)
00040 {
00041     reset(subject);
00042 }
00043 
00044 KisStrategyMove::~KisStrategyMove()
00045 {
00046 }
00047 
00048 void KisStrategyMove::reset(KisCanvasSubject *subject)
00049 {
00050     m_subject = subject;
00051     m_dragging = false;
00052 
00053     if (m_subject) {
00054         m_controller = subject->canvasController();
00055     } else {
00056         m_controller = 0;
00057     }
00058 }
00059 
00060 void KisStrategyMove::startDrag(const QPoint& pos)
00061 {
00062     // pos is the user chosen handle point
00063 
00064     if (m_subject) {
00065         KisImageSP img;
00066         KisLayerSP dev;
00067 
00068         if (!(img = m_subject->currentImg()))
00069             return;
00070 
00071         dev = img->activeLayer();
00072 
00073         if (!dev || !dev->visible())
00074             return;
00075 
00076         m_dragging = true;
00077         m_dragStart.setX(pos.x());
00078         m_dragStart.setY(pos.y());
00079         m_layerStart.setX(dev->x());
00080         m_layerStart.setY(dev->y());
00081         m_layerPosition = m_layerStart;
00082     }
00083 }
00084 
00085 void KisStrategyMove::drag(const QPoint& original)
00086 {
00087     // original is the position of the user chosen handle point
00088 
00089     if (m_subject && m_dragging) {
00090         KisImageSP img = m_subject->currentImg();
00091         KisLayerSP dev;
00092 
00093         if (img && (dev = img->activeLayer())) {
00094             QPoint pos = original;
00095             QRect rc;
00096 
00097             pos -= m_dragStart; // convert to delta
00098             rc = dev->extent();
00099             dev->setX(dev->x() + pos.x());
00100             dev->setY(dev->y() + pos.y());
00101             rc = rc.unite(dev->extent());
00102 
00103             m_layerPosition = QPoint(dev->x(), dev->y());
00104             m_dragStart = original;
00105 
00106             dev->setDirty(rc);
00107         }
00108     }
00109 }
00110 
00111 void KisStrategyMove::endDrag(const QPoint& pos, bool undo)
00112 {
00113     if (m_subject && m_dragging) {
00114         KisImageSP img = m_subject->currentImg();
00115         KisLayerSP dev;
00116 
00117         if (img && (dev = img->activeLayer())) {
00118             drag(pos);
00119             m_dragging = false;
00120 
00121             if (undo && img->undo()) {
00122                 KCommand *cmd = dev->moveCommand(m_layerStart, m_layerPosition);
00123                 Q_CHECK_PTR(cmd);
00124                 
00125                 KisUndoAdapter *adapter = img->undoAdapter();
00126                 if (adapter) {
00127                     adapter->addCommand(cmd);
00128                 } else {
00129                     delete cmd;
00130                 }
00131             }
00132             img->setModified();
00133         }
00134     }
00135 }
00136 
00137 void KisStrategyMove::simpleMove(const QPoint& pt1, const QPoint& pt2)
00138 {
00139     startDrag(pt1);
00140     endDrag(pt2);
00141 }
00142 
00143 void KisStrategyMove::simpleMove(Q_INT32 x1, Q_INT32 y1, Q_INT32 x2, Q_INT32 y2)
00144 {
00145     startDrag(QPoint(x1, y1));
00146     endDrag(QPoint(x2, y2));
00147 }
00148 
KDE Home | KDE Accessibility Home | Description of Access Keys