1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: QtxListBox.cxx
23 // Author: Sergey TELKOV
25 #include "QtxListBox.h"
28 #include <qlineedit.h>
33 QtxListBox::QtxListBox( QWidget* parent, const char* name, WFlags f )
34 : QListBox( parent, name, f ),
38 myEditDefault( true ),
39 myModifEnabled( true )
41 connect( this, SIGNAL( contentsMoving( int, int ) ),
42 this, SLOT( onContentsMoving( int, int ) ) );
48 QtxListBox::~QtxListBox()
53 \return true if editing is enabled
55 bool QtxListBox::isEditEnabled() const
61 Enables/disables editing
64 void QtxListBox::setEditEnabled( bool on )
66 if ( isEditEnabled() == on )
71 if ( !isEditEnabled() )
72 endEdition( defaultEditAction() );
76 \return default edit action
77 \sa setDefaultEditAction()
79 bool QtxListBox::defaultEditAction() const
85 Changes default edit action.
86 Pressing of ENTER button always accepts new value of edited item.
87 But other ways, such as focus out or edition of other item accepts
88 new value of edited item only if "default edit action" is true
89 \param def - new value
91 void QtxListBox::setDefaultEditAction( bool def )
97 \return modification enabled state
98 \sa setModificationEnabled()
100 bool QtxListBox::isModificationEnabled() const
102 return myModifEnabled;
106 Changes "modification enabled" state
107 If it is true, then pressing of CTRL + { Up, Down, Home, End } allows move items in list
108 \param on - new state
110 void QtxListBox::setModificationEnabled( bool on )
116 \return current edited item
118 QListBoxItem* QtxListBox::editedItem() const
120 return item( editedIndex() );
124 \return current edited index
126 int QtxListBox::editedIndex() const
132 Starts edition of item
133 \param idx - index of item
135 void QtxListBox::startEdition( const int idx )
137 if ( idx < 0 || editedIndex() == idx || !isEditEnabled() )
140 QLineEdit* ed = editor();
144 endEdition( defaultEditAction() );
148 ensureItemVisible( myEditIndex );
150 ed->setText( text( myEditIndex ) );
158 Starts edition of item
159 \param item - item to be edited
161 void QtxListBox::startEdition( const QListBoxItem* item )
163 startEdition( index( item ) );
167 Finishes edition of item
168 \param action - if it is true, then new values must be accepted
170 void QtxListBox::endEdition( const bool action )
172 int idx = editedIndex();
173 QLineEdit* ed = editor();
175 if ( idx < 0 || !ed )
184 int cur = currentItem();
187 changeItem( *pixmap( idx ), ed->text(), idx );
189 changeItem( ed->text(), idx );
191 setCurrentItem( cur );
193 emit itemEdited( idx );
194 emit itemEdited( item( idx ) );
199 Ensures that the item is visible.
200 \param idx - index of item
202 void QtxListBox::ensureItemVisible( const int idx )
207 if ( itemVisible( idx ) )
214 Ensures that the item is visible.
215 \param item - item to be made visible
217 void QtxListBox::ensureItemVisible( const QListBoxItem* item )
219 ensureItemVisible( index( item ) );
223 \return validator of item editor
225 const QValidator* QtxListBox::validator() const
227 const QValidator* v = 0;
229 v = editor()->validator();
234 Removes validator of item editor
236 void QtxListBox::clearValidator()
239 editor()->clearValidator();
243 Changes validator of item editor
244 \param v - new validator
246 void QtxListBox::setValidator( const QValidator* v )
249 editor()->setValidator( v );
254 \param idx - index of item
256 void QtxListBox::moveItemToTop( const int idx )
258 moveItem( idx, -idx );
263 \param idx - index of item
265 void QtxListBox::moveItemToBottom( const int idx )
267 moveItem( idx, count() - idx );
272 \param idx - index of item
273 \param step - changing of position
275 void QtxListBox::moveItem( const int idx, const int step )
277 QListBoxItem* i = item( idx );
278 if ( !i || step == 0 )
281 QListBoxItem* cur = item( currentItem() );
284 insertItem( i, QMAX( 0, idx + step ) );
286 setCurrentItem( index( cur ) );
288 int pos = index( i );
289 if ( myEditIndex == idx )
295 emit itemMoved( idx, pos );
300 \param i - position of item (if it is less than 0, then current position is used)
302 void QtxListBox::createItem( const int i )
304 if ( !isEditEnabled() )
307 int idx = i < 0 ? currentItem() : i;
308 idx = idx < 0 ? count() : idx;
309 idx = QMIN( (int)count(), idx );
311 insertItem( "", idx );
312 setCurrentItem( idx );
318 \param i - position of item (if it is less than 0, then current position is used)
320 void QtxListBox::deleteItem( const int i )
322 if ( !isEditEnabled() )
325 int idx = i < 0 ? currentItem() : i;
329 if ( editedIndex() == idx )
330 endEdition( defaultEditAction() );
337 Scrolls the content so that the point is in the top-left corner.
338 \param x, y - point co-ordinates
340 void QtxListBox::setContentsPos( int x, int y )
342 QListBox::setContentsPos( x, y );
348 Custom event filter, performs finish of edition on focus out, escape/return/enter pressing
350 bool QtxListBox::eventFilter( QObject* o, QEvent* e )
354 if ( e->type() == QEvent::FocusOut )
355 endEdition( defaultEditAction() );
357 if ( e->type() == QEvent::KeyPress )
359 QKeyEvent* ke = (QKeyEvent*)e;
360 if ( ke->key() == Key_Escape )
362 else if ( ke->key() == Key_Enter || ke->key() == Key_Return )
367 return QListBox::eventFilter( o, e );
371 Custom key press event handler
372 Allows to move items by CTRL + { Up, Down, Home, End }
374 void QtxListBox::keyPressEvent( QKeyEvent* e )
376 if ( e->key() == Key_Up && e->state() & ControlButton && isModificationEnabled() )
377 moveItem( currentItem(), -1 );
378 else if ( e->key() == Key_Down && e->state() & ControlButton && isModificationEnabled() )
379 moveItem( currentItem(), 1 );
380 else if ( e->key() == Key_Home && e->state() & ControlButton && isModificationEnabled() )
381 moveItemToTop( currentItem() );
382 else if ( e->key() == Key_End && e->state() & ControlButton && isModificationEnabled() )
383 moveItemToBottom( currentItem() );
384 else if ( e->key() == Key_Insert && e->state() & ControlButton )
385 createItem( currentItem() );
386 else if ( e->key() == Key_Delete && e->state() & ControlButton )
387 deleteItem( currentItem() );
389 QListBox::keyPressEvent( e );
393 Custom resize event handler
395 void QtxListBox::viewportResizeEvent( QResizeEvent* e )
397 QListBox::viewportResizeEvent( e );
403 Custom mouse double click event handler
405 void QtxListBox::mouseDoubleClickEvent( QMouseEvent* e )
407 if ( isEditEnabled() )
408 startEdition( itemAt( e->pos() ) );
410 QListBox::mouseDoubleClickEvent( e );
414 Updates editor on contents moving
416 void QtxListBox::onContentsMoving( int, int )
424 QLineEdit* QtxListBox::editor() const
428 QtxListBox* that = (QtxListBox*)this;
429 that->createEditor();
437 void QtxListBox::createEditor()
442 myEditor = new QLineEdit( viewport() );
444 myEditor->setLineWidth( 1 );
445 myEditor->setMidLineWidth( 0 );
446 myEditor->setFrameStyle( QFrame::Box | QFrame::Plain );
447 myEditor->installEventFilter( this );
451 addChild( myEditor );
457 void QtxListBox::updateEditor()
459 if ( !editedItem() || !editor() )
462 QRect r = itemRect( editedItem() );
466 int m = editor()->lineWidth();
467 r.addCoords( m, 0, 0, 0 );
469 const QPixmap* pix = pixmap( editedIndex() );
471 r.addCoords( pix->width() + 2, 0, 0, 0 );
473 editor()->setGeometry( r );