libs/flake

KoShapeRubberSelectStrategy.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002 
00003    Copyright (C) 2006 Thorsten Zachmann <zachmann@kde.org>
00004    Copyright (C) 2006 Thomas Zander <zander@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "KoShapeRubberSelectStrategy.h"
00023 
00024 #include <QPainter>
00025 #include <QMouseEvent>
00026 
00027 #include "KoPointerEvent.h"
00028 #include "KoShapeManager.h"
00029 #include "KoSelection.h"
00030 #include "KoCanvasBase.h"
00031 #include "KoTool.h"
00032 #include "KoSnapGuide.h"
00033 #include "KoSnapStrategy.h"
00034 
00035 KoShapeRubberSelectStrategy::KoShapeRubberSelectStrategy(KoTool *tool, KoCanvasBase *canvas, const QPointF &clicked, bool useSnapToGrid)
00036         : KoInteractionStrategy(tool, canvas)
00037         , m_snapGuide( new KoSnapGuide( canvas ) )
00038 {
00039     m_snapGuide->enableSnapStrategies( KoSnapStrategy::Grid );
00040     m_snapGuide->enableSnapping( useSnapToGrid );
00041 
00042     m_selectRect = QRectF( m_snapGuide->snap(clicked, 0 ), QSizeF(0, 0) );
00043 }
00044 
00045 KoShapeRubberSelectStrategy::~KoShapeRubberSelectStrategy()
00046 {
00047     delete m_snapGuide;
00048 }
00049 
00050 void KoShapeRubberSelectStrategy::paint(QPainter &painter, const KoViewConverter &converter)
00051 {
00052     painter.setRenderHint(QPainter::Antialiasing, false);
00053 
00054     QColor selectColor(Qt::blue);   // TODO make configurable
00055     selectColor.setAlphaF(0.5);
00056     QBrush sb(selectColor, Qt::SolidPattern);
00057     painter.setPen(QPen(sb, 0));
00058     painter.setBrush(sb);
00059     QRectF paintRect = converter.documentToView(m_selectRect);
00060     paintRect = paintRect.normalized();
00061     paintRect.adjust(0., -0.5, 0.5, 0.);
00062     if (painter.hasClipping())
00063         paintRect = paintRect.intersect(painter.clipRegion().boundingRect());
00064     painter.drawRect(paintRect);
00065 }
00066 
00067 void KoShapeRubberSelectStrategy::handleMouseMove(const QPointF &p, Qt::KeyboardModifiers modifiers)
00068 {
00069     QPointF point = m_snapGuide->snap( p, modifiers );
00070     if ((modifiers & Qt::AltModifier) != 0) {
00071         m_canvas->updateCanvas(m_selectRect.normalized());
00072         m_selectRect.moveTopLeft(m_selectRect.topLeft() - (m_lastPos - point));
00073         m_lastPos = point;
00074         m_canvas->updateCanvas(m_selectRect.normalized());
00075         return;
00076     }
00077     m_lastPos = point;
00078     QPointF old = m_selectRect.bottomRight();
00079     m_selectRect.setBottomRight(point);
00080     /*
00081         +---------------|--+
00082         |               |  |    We need to figure out rects A and B based on the two points. BUT
00083         |          old  | A|    we need to do that even if the points are switched places
00084         |             \ |  |    (i.e. the rect got smaller) and even if the rect is mirrored
00085         +---------------+  |    in either the horizontal or vertical axis.
00086         |       B          |
00087         +------------------+
00088                             `- point
00089     */
00090     QPointF x1 = old;
00091     x1.setY(m_selectRect.topLeft().y());
00092     qreal h1 = point.y() - x1.y();
00093     qreal h2 = old.y() - x1.y();
00094     QRectF A(x1, QSizeF(point.x() - x1.x(), point.y() < m_selectRect.top() ? qMin(h1, h2) : qMax(h1, h2)));
00095     A = A.normalized();
00096     m_canvas->updateCanvas(A);
00097 
00098     QPointF x2 = old;
00099     x2.setX(m_selectRect.topLeft().x());
00100     qreal w1 = point.x() - x2.x();
00101     qreal w2 = old.x() - x2.x();
00102     QRectF B(x2, QSizeF(point.x() < m_selectRect.left() ? qMin(w1, w2) : qMax(w1, w2), point.y() - x2.y()));
00103     B = B.normalized();
00104     m_canvas->updateCanvas(B);
00105 }
00106 
00107 void KoShapeRubberSelectStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
00108 {
00109     Q_UNUSED(modifiers);
00110     KoSelection * selection = m_canvas->shapeManager()->selection();
00111     QList<KoShape *> shapes(m_canvas->shapeManager()->shapesAt(m_selectRect));
00112     foreach(KoShape * shape, shapes) {
00113         if (!(shape->isSelectable() && shape->isVisible()))
00114             continue;
00115         selection->select(shape);
00116     }
00117     m_parent->repaintDecorations();
00118     m_canvas->updateCanvas(m_selectRect.normalized());
00119 }
00120 
00121 const QRectF KoShapeRubberSelectStrategy::selectRect() const
00122 {
00123     return m_selectRect.normalized();
00124 }