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 // File: QtxListBox.cxx
20 // Author: Sergey TELKOV
22 #include "QtxListBox.h"
25 #include <qlineedit.h>
30 QtxListBox::QtxListBox( QWidget* parent, const char* name, WFlags f )
31 : QListBox( parent, name, f ),
35 myEditDefault( true ),
36 myModifEnabled( true )
38 connect( this, SIGNAL( contentsMoving( int, int ) ),
39 this, SLOT( onContentsMoving( int, int ) ) );
45 QtxListBox::~QtxListBox()
50 \return true if editing is enabled
52 bool QtxListBox::isEditEnabled() const
58 Enables/disables editing
61 void QtxListBox::setEditEnabled( bool on )
63 if ( isEditEnabled() == on )
68 if ( !isEditEnabled() )
69 endEdition( defaultEditAction() );
73 \return default edit action
74 \sa setDefaultEditAction()
76 bool QtxListBox::defaultEditAction() const
82 Changes default edit action.
83 Pressing of ENTER button always accepts new value of edited item.
84 But other ways, such as focus out or edition of other item accepts
85 new value of edited item only if "default edit action" is true
86 \param def - new value
88 void QtxListBox::setDefaultEditAction( bool def )
94 \return modification enabled state
95 \sa setModificationEnabled()
97 bool QtxListBox::isModificationEnabled() const
99 return myModifEnabled;
103 Changes "modification enabled" state
104 If it is true, then pressing of CTRL + { Up, Down, Home, End } allows move items in list
105 \param on - new state
107 void QtxListBox::setModificationEnabled( bool on )
113 \return current edited item
115 QListBoxItem* QtxListBox::editedItem() const
117 return item( editedIndex() );
121 \return current edited index
123 int QtxListBox::editedIndex() const
129 Starts edition of item
130 \param idx - index of item
132 void QtxListBox::startEdition( const int idx )
134 if ( idx < 0 || editedIndex() == idx || !isEditEnabled() )
137 QLineEdit* ed = editor();
141 endEdition( defaultEditAction() );
145 ensureItemVisible( myEditIndex );
147 ed->setText( text( myEditIndex ) );
155 Starts edition of item
156 \param item - item to be edited
158 void QtxListBox::startEdition( const QListBoxItem* item )
160 startEdition( index( item ) );
164 Finishes edition of item
165 \param action - if it is true, then new values must be accepted
167 void QtxListBox::endEdition( const bool action )
169 int idx = editedIndex();
170 QLineEdit* ed = editor();
172 if ( idx < 0 || !ed )
181 int cur = currentItem();
184 changeItem( *pixmap( idx ), ed->text(), idx );
186 changeItem( ed->text(), idx );
188 setCurrentItem( cur );
190 emit itemEdited( idx );
191 emit itemEdited( item( idx ) );
196 Ensures that the item is visible.
197 \param idx - index of item
199 void QtxListBox::ensureItemVisible( const int idx )
204 if ( itemVisible( idx ) )
211 Ensures that the item is visible.
212 \param item - item to be made visible
214 void QtxListBox::ensureItemVisible( const QListBoxItem* item )
216 ensureItemVisible( index( item ) );
220 \return validator of item editor
222 const QValidator* QtxListBox::validator() const
224 const QValidator* v = 0;
226 v = editor()->validator();
231 Removes validator of item editor
233 void QtxListBox::clearValidator()
236 editor()->clearValidator();
240 Changes validator of item editor
241 \param v - new validator
243 void QtxListBox::setValidator( const QValidator* v )
246 editor()->setValidator( v );
251 \param idx - index of item
253 void QtxListBox::moveItemToTop( const int idx )
255 moveItem( idx, -idx );
260 \param idx - index of item
262 void QtxListBox::moveItemToBottom( const int idx )
264 moveItem( idx, count() - idx );
269 \param idx - index of item
270 \param step - changing of position
272 void QtxListBox::moveItem( const int idx, const int step )
274 QListBoxItem* i = item( idx );
275 if ( !i || step == 0 )
278 QListBoxItem* cur = item( currentItem() );
281 insertItem( i, QMAX( 0, idx + step ) );
283 setCurrentItem( index( cur ) );
285 int pos = index( i );
286 if ( myEditIndex == idx )
292 emit itemMoved( idx, pos );
297 \param i - position of item (if it is less than 0, then current position is used)
299 void QtxListBox::createItem( const int i )
301 if ( !isEditEnabled() )
304 int idx = i < 0 ? currentItem() : i;
305 idx = idx < 0 ? count() : idx;
306 idx = QMIN( (int)count(), idx );
308 insertItem( "", idx );
309 setCurrentItem( idx );
315 \param i - position of item (if it is less than 0, then current position is used)
317 void QtxListBox::deleteItem( const int i )
319 if ( !isEditEnabled() )
322 int idx = i < 0 ? currentItem() : i;
326 if ( editedIndex() == idx )
327 endEdition( defaultEditAction() );
334 Scrolls the content so that the point is in the top-left corner.
335 \param x, y - point co-ordinates
337 void QtxListBox::setContentsPos( int x, int y )
339 QListBox::setContentsPos( x, y );
345 Custom event filter, performs finish of edition on focus out, escape/return/enter pressing
347 bool QtxListBox::eventFilter( QObject* o, QEvent* e )
351 if ( e->type() == QEvent::FocusOut )
352 endEdition( defaultEditAction() );
354 if ( e->type() == QEvent::KeyPress )
356 QKeyEvent* ke = (QKeyEvent*)e;
357 if ( ke->key() == Key_Escape )
359 else if ( ke->key() == Key_Enter || ke->key() == Key_Return )
364 return QListBox::eventFilter( o, e );
368 Custom key press event handler
369 Allows to move items by CTRL + { Up, Down, Home, End }
371 void QtxListBox::keyPressEvent( QKeyEvent* e )
373 if ( e->key() == Key_Up && e->state() & ControlButton && isModificationEnabled() )
374 moveItem( currentItem(), -1 );
375 else if ( e->key() == Key_Down && e->state() & ControlButton && isModificationEnabled() )
376 moveItem( currentItem(), 1 );
377 else if ( e->key() == Key_Home && e->state() & ControlButton && isModificationEnabled() )
378 moveItemToTop( currentItem() );
379 else if ( e->key() == Key_End && e->state() & ControlButton && isModificationEnabled() )
380 moveItemToBottom( currentItem() );
381 else if ( e->key() == Key_Insert && e->state() & ControlButton )
382 createItem( currentItem() );
383 else if ( e->key() == Key_Delete && e->state() & ControlButton )
384 deleteItem( currentItem() );
386 QListBox::keyPressEvent( e );
390 Custom resize event handler
392 void QtxListBox::viewportResizeEvent( QResizeEvent* e )
394 QListBox::viewportResizeEvent( e );
400 Custom mouse double click event handler
402 void QtxListBox::mouseDoubleClickEvent( QMouseEvent* e )
404 if ( isEditEnabled() )
405 startEdition( itemAt( e->pos() ) );
407 QListBox::mouseDoubleClickEvent( e );
411 Updates editor on contents moving
413 void QtxListBox::onContentsMoving( int, int )
421 QLineEdit* QtxListBox::editor() const
425 QtxListBox* that = (QtxListBox*)this;
426 that->createEditor();
434 void QtxListBox::createEditor()
439 myEditor = new QLineEdit( viewport() );
441 myEditor->setLineWidth( 1 );
442 myEditor->setMidLineWidth( 0 );
443 myEditor->setFrameStyle( QFrame::Box | QFrame::Plain );
444 myEditor->installEventFilter( this );
448 addChild( myEditor );
454 void QtxListBox::updateEditor()
456 if ( !editedItem() || !editor() )
459 QRect r = itemRect( editedItem() );
463 int m = editor()->lineWidth();
464 r.addCoords( m, 0, 0, 0 );
466 const QPixmap* pix = pixmap( editedIndex() );
468 r.addCoords( pix->width() + 2, 0, 0, 0 );
470 editor()->setGeometry( r );