F:/KPlato/koffice/libs/flake/KoSelection.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002 
00003    Copyright (C) 2006 Boudewijn Rempt <boud@valdyas.org>
00004    Copyright (C) 2006 Thorsten Zachmann <zachmann@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 "KoSelection.h"
00023 #include "KoShapeContainer.h"
00024 #include "KoShapeGroup.h"
00025 #include "KoPointerEvent.h"
00026 
00027 #include <QPainter>
00028 #include <QTimer>
00029 
00030 KoSelection::KoSelection()
00031 {
00032     m_eventTriggered = false;
00033 }
00034 
00035 KoSelection::~KoSelection()
00036 {
00037 }
00038 
00039 void KoSelection::paint( QPainter &painter, const KoViewConverter &converter)
00040 {
00041 /*    if ( count() == 0 )
00042         return;
00043     painter.setRenderHint( QPainter::Antialiasing, false );
00044     QRectF bb = converter.documentToView( boundingRect() );
00045     QPen pen( Qt::blue ); //TODO make it configurable
00046     painter.setPen( pen );
00047     painter.drawRect( bb );
00048 */
00049 }
00050 
00051 void KoSelection::selectGroupChilds( KoShapeGroup *group )
00052 {
00053     if( ! group )
00054         return;
00055 
00056     foreach(KoShape *shape, group->iterator()) {
00057         if( m_selectedObjects.contains(shape))
00058             continue;
00059         m_selectedObjects << shape;
00060 
00061         KoShapeGroup* childGroup = dynamic_cast<KoShapeGroup*>( shape );
00062         if( childGroup )
00063             selectGroupChilds( childGroup );
00064     }
00065 }
00066 
00067 void KoSelection::select(KoShape * object)
00068 {
00069     Q_ASSERT(object != this);
00070     Q_ASSERT(object);
00071     if(! object->isSelectable())
00072         return;
00073     if(!m_selectedObjects.contains(object))
00074         m_selectedObjects << object;
00075 
00076     KoShapeGroup* group = dynamic_cast<KoShapeGroup*>(object);
00077     if( group )
00078         selectGroupChilds( group );
00079 
00080     KoShapeContainer *parent = object->parent();
00081     while( parent ) {
00082         KoShapeGroup *parentGroup = dynamic_cast<KoShapeGroup*>(parent);
00083         if( ! parentGroup ) break;
00084         if( ! m_selectedObjects.contains(parentGroup) ) {
00085             m_selectedObjects << parentGroup;
00086             selectGroupChilds( parentGroup );
00087         }
00088         parent = parentGroup->parent();
00089     }
00090 
00091     if(m_selectedObjects.count() == 1)
00092     {
00093         rotate(object->rotation());
00094         shear(object->shearX(), object->shearY());
00095     }
00096     else
00097     {
00098         rotate(0);
00099         shear(0, 0);
00100     }
00101     requestSelectionChangedEvent();
00102 }
00103 
00104 void KoSelection::deselect(KoShape * object)
00105 {
00106     if(! m_selectedObjects.contains(object))
00107         return;
00108     KoShapeGroup *group = dynamic_cast<KoShapeGroup*>(object->parent());
00109     if(group) {
00110         m_selectedObjects.remove(group);
00111         foreach(KoShape *shape, group->iterator())
00112             m_selectedObjects.remove(shape);
00113     }
00114     else
00115         m_selectedObjects.remove( object );
00116 
00117     if(m_selectedObjects.count() == 1)
00118     {
00119         rotate(firstSelectedShape()->rotation());
00120         shear(firstSelectedShape()->shearX(), firstSelectedShape()->shearY());
00121     }
00122     requestSelectionChangedEvent();
00123 }
00124 
00125 void KoSelection::deselectAll()
00126 {
00127     if(m_selectedObjects.count() == 0)
00128         return;
00129     m_selectedObjects.clear();
00130     requestSelectionChangedEvent();
00131 }
00132 
00133 void KoSelection::requestSelectionChangedEvent() {
00134     if(m_eventTriggered)
00135         return;
00136     m_eventTriggered = true;
00137     QTimer::singleShot(0, this, SLOT(selectionChangedEvent()));
00138 }
00139 
00140 void KoSelection::selectionChangedEvent() {
00141     m_eventTriggered = false;
00142     scale(1,1);
00143     boundingRect(); //has the side effect of updating the size and position
00144     emit selectionChanged();
00145 }
00146 
00147 int KoSelection::count() const
00148 {
00149     return m_selectedObjects.count();
00150 }
00151 
00152 bool KoSelection::hitTest( const QPointF &position ) const
00153 {
00154     if ( count() > 1 )
00155     {
00156         QRectF bb( boundingRect() );
00157         return bb.contains( position );
00158     }
00159     else if ( count() == 1 )
00160         return ( *m_selectedObjects.begin() )->hitTest( position );
00161     else // count == 0
00162         return false;
00163 }
00164 
00165 QRectF KoSelection::boundingRect() const
00166 {
00167     bool first=true;
00168     QRectF bb;
00169 
00170     QMatrix tmat = transformationMatrix(0);
00171     QMatrix itmat = tmat.inverted();
00172 
00173     if ( count() > 0 )
00174     {
00175         KoSelectionSet::const_iterator it = m_selectedObjects.begin();
00176         for ( ; it != m_selectedObjects.end(); ++it ) {
00177             if( dynamic_cast<KoShapeGroup*>( *it ))
00178                 continue;
00179             if(first) {
00180                 bb = ((*it)->transformationMatrix(0) * itmat).mapRect(QRectF(QPointF(),(*it)->size()));
00181                 first = false;
00182             }
00183             else
00184                 bb = bb.unite(
00185                  ((*it)->transformationMatrix(0) * itmat).mapRect(QRectF(QPointF(),(*it)->size())) );
00186         }
00187     }
00188 
00189     //just as well use the oppertunity to update the size and position
00190     (const_cast <KoSelection *>(this))->resize( bb.size() );
00191     QPointF p = tmat.map(bb.topLeft() + itmat.map(position()));
00192     (const_cast <KoSelection *>(this))->setPosition( p );
00193 
00194     return tmat.mapRect(bb);
00195 }
00196 
00197 const KoSelectionSet KoSelection::selectedShapes(KoFlake::SelectionType strip) const {
00198     KoSelectionSet answer;
00199     // strip the child objects when there is also a parent included.
00200     bool doStripping = strip == KoFlake::StrippedSelection;
00201     foreach (KoShape *shape, m_selectedObjects) {
00202         KoShapeContainer *container = shape->parent();
00203         if(strip != KoFlake::TopLevelSelection && dynamic_cast<KoShapeGroup*>(shape))
00204             // since a KoShapeGroup
00205             // guarentees all its children are selected at the same time as itself
00206             // is selected we will only return its children.
00207             continue;
00208         bool add = true;
00209         while(doStripping && add && container) {
00210             if(dynamic_cast<KoShapeGroup*>(container) == 0 && m_selectedObjects.contains(container))
00211                 add = false;
00212             container = container->parent();
00213         }
00214         if(strip == KoFlake::TopLevelSelection && container && m_selectedObjects.contains(container))
00215             add = false;
00216         if(add)
00217             answer << shape;
00218     }
00219     return answer;
00220 }
00221 
00222 bool KoSelection::isSelected(const KoShape *object) const {
00223     if(object == this)
00224         return true;
00225 
00226     QSetIterator<KoShape*> iter (m_selectedObjects);
00227     while(iter.hasNext()) {
00228         if(iter.next() == object)
00229             return true;
00230     }
00231 
00232     return false;
00233 }
00234 
00235 KoShape *KoSelection::firstSelectedShape(KoFlake::SelectionType strip) const {
00236     KoSelectionSet set = selectedShapes(strip);
00237     if(set.isEmpty())
00238         return 0;
00239     return *(set.begin());
00240 }
00241 
00242 #include "KoSelection.moc"

Généré le Wed Nov 22 23:40:59 2006 pour KPlato par  doxygen 1.5.1-p1