Salome HOME
Copyrights update
[modules/gui.git] / src / ObjBrowser / OB_ListView.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "OB_ListView.h"
20
21 #include "OB_Filter.h"
22 #include "OB_ListItem.h"
23
24 #include <SUIT_DataObject.h>
25
26 #include <qdragobject.h>
27
28 OB_ListView::OB_ListView( QWidget* parent, const char* name, WFlags f )
29 : QtxListView( parent, name, f ),
30 myFilter( 0 )
31 {
32 }
33
34 OB_ListView::OB_ListView( const int state, QWidget* parent, const char* name, WFlags f )
35 : QtxListView( state, parent, name, f ),
36 myFilter( 0 )
37 {
38 }
39
40 OB_ListView::~OB_ListView()
41 {
42   delete myFilter;
43 }
44
45 OB_Filter* OB_ListView::filter() const
46 {
47   return myFilter;
48 }
49
50 void OB_ListView::setFilter( OB_Filter* f )
51 {
52   if ( myFilter == f )
53     return;
54
55   delete myFilter;
56   myFilter = f;
57 }
58
59 bool OB_ListView::isOk( QListViewItem* item ) const
60 {
61   bool ok = true;
62   SUIT_DataObject* obj = dataObject( item );
63   if ( obj && filter() )
64     ok = filter()->isOk( obj );
65   return ok;
66 }
67
68 QDragObject* OB_ListView::dragObject()
69 {
70   myItems.clear();
71
72   for ( QListViewItemIterator it( this ); it.current(); ++it )
73     if ( it.current()->isSelected() )
74       myItems.append( it.current() );
75
76   return new QTextDrag( "", this );
77 }
78
79 void OB_ListView::dragEnterEvent( QDragEnterEvent* e )
80 {
81   e->accept();
82 }
83
84 void OB_ListView::dragMoveEvent( QDragMoveEvent* e )
85 {
86   QListViewItem* item = dropItem( e );
87
88   if ( isDropAccepted( item ) )
89   {
90     setCurrentItem( item );
91     e->accept( true );
92   }
93   else
94     e->accept( false );
95 }
96
97 void OB_ListView::dropEvent( QDropEvent* e )
98 {
99   QListViewItem* item = dropItem( e );
100   if ( isDropAccepted( item ) )
101   {
102     e->accept();
103     emit dropped( myItems, item, e->action() );
104   }
105   myItems.clear();
106 }
107
108 void OB_ListView::keyPressEvent( QKeyEvent* ke )
109 {
110   if ( ( ke->key() == Qt::Key_Plus || ke->key() == Qt::Key_Minus ) && ke->state() & ControlButton )
111   {
112     bool isOpen = ke->key() == Qt::Key_Plus;
113     for ( QListViewItemIterator it( this ); it.current(); ++it )
114       if ( it.current()->childCount() )
115         it.current()->setOpen( isOpen );
116   }
117   else
118     QtxListView::keyPressEvent( ke );
119 }
120
121 QListViewItem* OB_ListView::dropItem( QDropEvent* e ) const
122 {
123   QListViewItem* item = 0;
124   if ( e )
125     item = itemAt( QPoint( e->pos().x() - viewport()->x(), e->pos().y() - viewport()->y() ) );
126
127   return item;
128 }
129
130 SUIT_DataObject* OB_ListView::dataObject( QListViewItem* item ) const
131 {
132   if ( !item )
133     return 0;
134
135   SUIT_DataObject* obj = 0;
136
137   if ( dynamic_cast<OB_ListItem*>( item ) )
138     obj = dynamic_cast<OB_ListItem*>( item )->dataObject();
139   else if ( dynamic_cast<OB_CheckListItem*>( item ) )
140     obj = dynamic_cast<OB_CheckListItem*>( item )->dataObject();
141
142   return obj;
143 }
144
145 bool OB_ListView::isDropAccepted( QListViewItem* item ) const
146 {
147   bool res = true;
148
149   for ( QPtrListIterator<QListViewItem> it( myItems ); it.current() && res; ++it )
150     res = res && isDropAccepted( it.current(), item );
151
152   return res;
153 }
154
155 bool OB_ListView::isDropAccepted( QListViewItem* drag, QListViewItem* drop ) const
156 {
157   SUIT_DataObject* dragObj = dataObject( drag );
158   SUIT_DataObject* dropObj = dataObject( drop );
159
160   if ( !dragObj || !dropObj )
161     return false;
162
163   return dropObj->isDropAccepted( dragObj );
164 }