KoSelection.cpp 4.87 KB
Newer Older
Thomas Zander's avatar
Thomas Zander committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/* This file is part of the KDE project

   Copyright (C) 2006 Boudewijn Rempt <boud@valdyas.org>
   Copyright (C) 2006 Thorsten Zachmann <zachmann@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KoSelection.h"
#include "KoShapeContainer.h"
#include "KoShapeGroup.h"
#include "KoGfxEvent.h"

#include <QPainter>
#include <QDebug>
#include <QTimer>

KoSelection::KoSelection()
{
    m_eventTriggered = false;
}

KoSelection::~KoSelection()
{
}

void KoSelection::paint( QPainter &painter, KoViewConverter &converter)
{
    if ( count() == 0 )
        return;
    painter.setRenderHint( QPainter::Antialiasing, false );
45
    QRectF bb = converter.normalToView( boundingRect() );
Thomas Zander's avatar
Thomas Zander committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    QPen pen( Qt::blue ); //TODO make it configurable
    painter.setPen( pen );
    painter.drawRect( bb );
}

void KoSelection::select(KoShape * object)
{
    Q_ASSERT(object != this);
    if(m_selectedObjects.contains(object))
        return;
    m_selectedObjects << object;
    KoShapeGroup *group = dynamic_cast<KoShapeGroup*>(object->parent());
    if(group && !m_selectedObjects.contains(group)) {
        m_selectedObjects << group;
        foreach(KoShape *shape, group->iterator()) {
            if(! m_selectedObjects.contains(shape))
                m_selectedObjects << shape;
        }
    }
    requestSelectionChangedEvent();
}

void KoSelection::deselect(KoShape * object)
{
    if(! m_selectedObjects.contains(object))
        return;
    KoShapeGroup *group = dynamic_cast<KoShapeGroup*>(object->parent());
    if(group) {
        m_selectedObjects.remove(group);
        foreach(KoShape *shape, group->iterator())
            m_selectedObjects.remove(shape);
    }
    else
        m_selectedObjects.remove( object );
    requestSelectionChangedEvent();
}

void KoSelection::deselectAll()
{
    if(m_selectedObjects.count() == 0)
        return;
    m_selectedObjects.clear();
    requestSelectionChangedEvent();
}

void KoSelection::requestSelectionChangedEvent() {
    if(m_eventTriggered)
        return;
    m_eventTriggered = true;
    QTimer::singleShot(0, this, SLOT(selectionChangedEvent()));
}

void KoSelection::selectionChangedEvent() {
    m_eventTriggered = false;
100
    QRectF bb( boundingRect() );
Thomas Zander's avatar
Thomas Zander committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114
    resize( bb.size() );
    setPosition( bb.topLeft() );
    emit selectionChanged();
}

int KoSelection::count() const
{
    return m_selectedObjects.count();
}

bool KoSelection::hitTest( const QPointF &position ) const
{
    if ( count() > 1 )
    {
115
        QRectF bb( boundingRect() );
Thomas Zander's avatar
Thomas Zander committed
116 117 118 119 120 121 122 123
        return bb.contains( position );
    }
    else if ( count() == 1 )
        return ( *m_selectedObjects.begin() )->hitTest( position );
    else // count == 0
        return false;
}

124
QRectF KoSelection::boundingRect() const
Thomas Zander's avatar
Thomas Zander committed
125 126 127 128 129 130 131 132 133 134
{
    bool first=true;
    QRectF bb;
    if ( count() > 0 )
    {
        KoSelectionSet::const_iterator it = m_selectedObjects.begin();
        for ( ; it != m_selectedObjects.end(); ++it ) {
            if( dynamic_cast<KoShapeGroup*>( *it ))
                continue;
            if(first) {
135
                bb = (*it)->boundingRect();
Thomas Zander's avatar
Thomas Zander committed
136 137 138
                first = false;
            }
            else
139
                bb = bb.unite( ( *it )->boundingRect() );
Thomas Zander's avatar
Thomas Zander committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        }
    }
    return bb;
}

const KoSelectionSet KoSelection::selectedObjects(KoFlake::SelectionType strip) const {
    KoSelectionSet answer;
    // strip the child objects when there is also a parent included.
    bool doStripping = strip == KoFlake::StrippedSelection;
    foreach (KoShape *shape, m_selectedObjects) {
        KoShapeContainer *container = shape->parent();
        if(dynamic_cast<KoShapeGroup*>(shape))
            continue;
        bool add = true;
        while(doStripping && add && container) {
            if(dynamic_cast<KoShapeGroup*>(container) == 0 && m_selectedObjects.contains(container))
                add = false;
            container = container->parent();
        }
        if(add)
            answer << shape;
    }
    return answer;
}

bool KoSelection::isSelected(const KoShape *object) const {
    if(object == this)
        return true;

    QSetIterator<KoShape*> iter (m_selectedObjects);
    while(iter.hasNext()) {
        if(iter.next() == object)
            return true;
    }

    return false;
}