1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "OB_ListView.h"
21 #include "OB_Filter.h"
22 #include "OB_ListItem.h"
24 #include <SUIT_DataObject.h>
26 #include <qdragobject.h>
31 OB_ListView::OB_ListView( QWidget* parent, const char* name, WFlags f )
32 : QtxListView( parent, name, f ),
40 OB_ListView::OB_ListView( const int state, QWidget* parent, const char* name, WFlags f )
41 : QtxListView( state, parent, name, f ),
49 OB_ListView::~OB_ListView()
57 OB_Filter* OB_ListView::filter() const
66 void OB_ListView::setFilter( OB_Filter* f )
76 \return true if item passes filter
78 bool OB_ListView::isOk( QListViewItem* item ) const
81 SUIT_DataObject* obj = dataObject( item );
82 if ( obj && filter() )
83 ok = filter()->isOk( obj );
88 Creates new drag object
90 QDragObject* OB_ListView::dragObject()
94 for ( QListViewItemIterator it( this ); it.current(); ++it )
95 if ( it.current()->isSelected() )
96 myItems.append( it.current() );
98 return new QTextDrag( "", this );
102 Custom drag enter event filter
104 void OB_ListView::dragEnterEvent( QDragEnterEvent* e )
110 Custom drag move event filter
112 void OB_ListView::dragMoveEvent( QDragMoveEvent* e )
114 QListViewItem* item = dropItem( e );
116 if ( isDropAccepted( item ) )
118 setCurrentItem( item );
126 Custom drop event filter
128 void OB_ListView::dropEvent( QDropEvent* e )
130 QListViewItem* item = dropItem( e );
131 if ( isDropAccepted( item ) )
134 emit dropped( myItems, item, e->action() );
140 Custom key press event filter
142 void OB_ListView::keyPressEvent( QKeyEvent* ke )
144 if ( ( ke->key() == Qt::Key_Plus || ke->key() == Qt::Key_Minus ) && ke->state() & ControlButton )
146 bool isOpen = ke->key() == Qt::Key_Plus;
147 for ( QListViewItemIterator it( this ); it.current(); ++it )
148 if ( it.current()->childCount() )
149 it.current()->setOpen( isOpen );
152 QtxListView::keyPressEvent( ke );
156 Finds item, in that dragged objects are dropped by QDropEvent
159 QListViewItem* OB_ListView::dropItem( QDropEvent* e ) const
161 QListViewItem* item = 0;
163 item = itemAt( QPoint( e->pos().x() - viewport()->x(), e->pos().y() - viewport()->y() ) );
169 \return SUIT object by tree item
170 \param item - tree item
172 SUIT_DataObject* OB_ListView::dataObject( QListViewItem* item ) const
177 SUIT_DataObject* obj = 0;
179 if ( dynamic_cast<OB_ListItem*>( item ) )
180 obj = dynamic_cast<OB_ListItem*>( item )->dataObject();
181 else if ( dynamic_cast<OB_CheckListItem*>( item ) )
182 obj = dynamic_cast<OB_CheckListItem*>( item )->dataObject();
188 \return true if it is possible to drop into item
189 \param item - tree item to be checked
191 bool OB_ListView::isDropAccepted( QListViewItem* item ) const
195 for ( QPtrListIterator<QListViewItem> it( myItems ); it.current() && res; ++it )
196 res = res && isDropAccepted( it.current(), item );
202 \return true if it is possible to drop one item into other
203 \param drag - dragged item
204 \param drop - destination item
206 bool OB_ListView::isDropAccepted( QListViewItem* drag, QListViewItem* drop ) const
208 SUIT_DataObject* dragObj = dataObject( drag );
209 SUIT_DataObject* dropObj = dataObject( drop );
211 if ( !dragObj || !dropObj )
214 return dropObj->isDropAccepted( dragObj );
219 \param col - column index
220 \param width - column width
222 void OB_ListView::setColumnWidth( int col, int width )
224 int max = columnMaxWidth( col );
225 if ( max>0 && width>max )
228 QtxListView::setColumnWidth( col, width );
232 \return column max width
233 \param col - column index
235 int OB_ListView::columnMaxWidth( const int col ) const
238 if( myMaxColWidth.contains( col ) )
239 res = myMaxColWidth[col];
240 else if( myMaxColRatio.contains( col ) )
241 res = int( myMaxColRatio[col]*height() );
246 Changes column max width
247 \param col - column index
248 \param w - column max width
250 void OB_ListView::setColumnMaxWidth( const int col, const int w )
252 myMaxColWidth.insert( col, w );
256 \return column max ratio
257 \param col - column index
259 double OB_ListView::columnMaxRatio( const int col ) const
262 if( myMaxColRatio.contains( col ) )
263 res = myMaxColRatio[col];
268 Changes column max ratio
269 \param col - column index
270 \param w - column max ratio
272 void OB_ListView::setColumnMaxRatio( const int col, const double r )
274 myMaxColRatio.insert( col, r );