00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "Select.h"
00023 #include "GraphScene.h"
00024 #include "graph.h"
00025 #include "node.h"
00026 #include "edge.h"
00027 #include "NodeItem.h"
00028 #include "OrientedEdgeItem.h"
00029
00030 #include <KLocale>
00031 #include <KDebug>
00032 #include <QDebug>
00033
00034 SelectAction::SelectAction( GraphScene* scene, QObject* parent )
00035 : AbstractAction( scene, parent )
00036 {
00037 setText( i18n( "Select" ) );
00038 setToolTip( i18n( "Select Items by clicking on them." ) );
00039 setIcon( KIcon( "rocsselect" ) );
00040 _selectionRect = 0;
00041 _name = "select";
00042 }
00043
00044 SelectAction::~SelectAction() {}
00045
00046 void SelectAction::executePress( QPointF pos )
00047 {
00048 if( ! _graph ) return;
00049
00050 _p1 = pos;
00051 _selectionRect = new QGraphicsRectItem();
00052 _selectionRect->setFlag( QGraphicsItem::ItemIsSelectable, false );
00053 _graphScene->addItem( _selectionRect );
00054 kDebug() << "Press Executed.";
00055 }
00056
00057 void SelectAction::executeMove( QPointF pos )
00058 {
00059 if( ! _graph ) return;
00060 if( _selectionRect == 0 ) return;
00061 QPointF p1 = _p1;
00062
00063 if( p1.x() > pos.x() )
00064 {
00065 int x = pos.x();
00066 pos.setX( p1.x() );
00067 p1.setX( x );
00068 }
00069 if( p1.y() > pos.y() )
00070 {
00071 int y = pos.y();
00072 pos.setY( p1.y() );
00073 p1.setY( y );
00074 }
00075
00076
00077 _selectionRect->setRect( QRectF( p1, pos ) );
00078 }
00079
00080 void SelectAction::executeRelease( QPointF pos )
00081 {
00082 if( ! _graph ) return;
00083 if( _selectionRect == 0 ) return;
00084
00085 _graphScene->removeItem( _selectionRect );
00086
00087 delete _selectionRect;
00088 _selectionRect = 0;
00089
00090 QList<QGraphicsItem*> currentSelection = _graphScene->selectedItems();
00091 foreach( QGraphicsItem * i, currentSelection )
00092 {
00093 i->setSelected( false );
00094 i->update();
00095 }
00096
00097 if( pos == _p1 )
00098 {
00099 singleSelect( pos );
00100 }
00101 else
00102 {
00103 multiSelect( pos );
00104 }
00105 }
00106
00107 void SelectAction::multiSelect( QPointF pos )
00108 {
00109 QList<QGraphicsItem*> items = _graphScene->items( QRectF( _p1, pos ) );
00110 foreach( QGraphicsItem * i, items )
00111 {
00112 if( !( qgraphicsitem_cast<NodeItem*>( i )
00113 || qgraphicsitem_cast<OrientedEdgeItem*>( i ) ) )
00114 {
00115 items.removeAll( i );
00116 }
00117 }
00118 if( items.size() == 1 )
00119 {
00120 singleSelect( items[0]->pos() );
00121 return;
00122 }
00123
00124 foreach( QGraphicsItem * i, items )
00125 {
00126 i->setSelected( true );
00127 i->update();
00128 }
00129 }
00130
00131 #include <QtAlgorithms>
00132 bool zValue( QGraphicsItem* i1, QGraphicsItem* i2 )
00133 {
00134 return i1->zValue() < i2->zValue();
00135 }
00136
00137 void SelectAction::singleSelect( QPointF pos )
00138 {
00139 QGraphicsItem* item = 0;
00140 QGraphicsEllipseItem* tmpItem = new QGraphicsEllipseItem( pos.x() - 5, pos.y() - 5, 10, 10 );
00141 _graphScene->addItem( tmpItem );
00142
00143 if( tmpItem->collidingItems().empty() )
00144 {
00145 kDebug() << "No Collisions";
00146 _graphScene->removeItem( tmpItem );
00147 delete tmpItem;
00148 return;
00149 }
00150 else
00151 {
00152 QList<QGraphicsItem*> items = tmpItem->collidingItems();
00153
00154 qSort( items.begin(), items.end(), zValue );
00155 item = items.at( items.size() - 1 );
00156 if( item->zValue() == -1000 )
00157 {
00158 emit ItemSelectedChanged( 0 );
00159 delete tmpItem;
00160 return;
00161 }
00162 }
00163
00164 item->setSelected( true );
00165 emit ItemSelectedChanged( item );
00166 item->update();
00167 qDebug() << "Item Selected!";
00168 delete tmpItem;
00169 }
00170 #include "Select.moc"