1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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
23 // File: QtxListBox.cxx
24 // Author: Sergey TELKOV
26 #include "QtxListBox.h"
29 #include <qlineedit.h>
34 QtxListBox::QtxListBox( QWidget* parent, const char* name, WFlags f )
35 : QListBox( parent, name, f ),
39 myEditDefault( true ),
40 myModifEnabled( true )
42 connect( this, SIGNAL( contentsMoving( int, int ) ),
43 this, SLOT( onContentsMoving( int, int ) ) );
49 QtxListBox::~QtxListBox()
54 \return true if editing is enabled
56 bool QtxListBox::isEditEnabled() const
62 Enables/disables editing
65 void QtxListBox::setEditEnabled( bool on )
67 if ( isEditEnabled() == on )
72 if ( !isEditEnabled() )
73 endEdition( defaultEditAction() );
77 \return default edit action
78 \sa setDefaultEditAction()
80 bool QtxListBox::defaultEditAction() const
86 Changes default edit action.
87 Pressing of ENTER button always accepts new value of edited item.
88 But other ways, such as focus out or edition of other item accepts
89 new value of edited item only if "default edit action" is true
90 \param def - new value
92 void QtxListBox::setDefaultEditAction( bool def )
98 \return modification enabled state
99 \sa setModificationEnabled()
101 bool QtxListBox::isModificationEnabled() const
103 return myModifEnabled;
107 Changes "modification enabled" state
108 If it is true, then pressing of CTRL + { Up, Down, Home, End } allows move items in list
109 \param on - new state
111 void QtxListBox::setModificationEnabled( bool on )
117 \return current edited item
119 QListBoxItem* QtxListBox::editedItem() const
121 return item( editedIndex() );
125 \return current edited index
127 int QtxListBox::editedIndex() const
133 Starts edition of item
134 \param idx - index of item
136 void QtxListBox::startEdition( const int idx )
138 if ( idx < 0 || editedIndex() == idx || !isEditEnabled() )
141 QLineEdit* ed = editor();
145 endEdition( defaultEditAction() );
149 ensureItemVisible( myEditIndex );
151 ed->setText( text( myEditIndex ) );
159 Starts edition of item
160 \param item - item to be edited
162 void QtxListBox::startEdition( const QListBoxItem* item )
164 startEdition( index( item ) );
168 Finishes edition of item
169 \param action - if it is true, then new values must be accepted
171 void QtxListBox::endEdition( const bool action )
173 int idx = editedIndex();
174 QLineEdit* ed = editor();
176 if ( idx < 0 || !ed )
185 int cur = currentItem();
188 changeItem( *pixmap( idx ), ed->text(), idx );
190 changeItem( ed->text(), idx );
192 setCurrentItem( cur );
194 emit itemEdited( idx );
195 emit itemEdited( item( idx ) );
200 Ensures that the item is visible.
201 \param idx - index of item
203 void QtxListBox::ensureItemVisible( const int idx )
208 if ( itemVisible( idx ) )
215 Ensures that the item is visible.
216 \param item - item to be made visible
218 void QtxListBox::ensureItemVisible( const QListBoxItem* item )
220 ensureItemVisible( index( item ) );
224 \return validator of item editor
226 const QValidator* QtxListBox::validator() const
228 const QValidator* v = 0;
230 v = editor()->validator();
235 Removes validator of item editor
237 void QtxListBox::clearValidator()
240 editor()->clearValidator();
244 Changes validator of item editor
245 \param v - new validator
247 void QtxListBox::setValidator( const QValidator* v )
250 editor()->setValidator( v );
255 \param idx - index of item
257 void QtxListBox::moveItemToTop( const int idx )
259 moveItem( idx, -idx );
264 \param idx - index of item
266 void QtxListBox::moveItemToBottom( const int idx )
268 moveItem( idx, count() - idx );
273 \param idx - index of item
274 \param step - changing of position
276 void QtxListBox::moveItem( const int idx, const int step )
278 QListBoxItem* i = item( idx );
279 if ( !i || step == 0 )
282 QListBoxItem* cur = item( currentItem() );
285 insertItem( i, QMAX( 0, idx + step ) );
287 setCurrentItem( index( cur ) );
289 int pos = index( i );
290 if ( myEditIndex == idx )
296 emit itemMoved( idx, pos );
301 \param i - position of item (if it is less than 0, then current position is used)
303 void QtxListBox::createItem( const int i )
305 if ( !isEditEnabled() )
308 int idx = i < 0 ? currentItem() : i;
309 idx = idx < 0 ? count() : idx;
310 idx = QMIN( (int)count(), idx );
312 insertItem( "", idx );
313 setCurrentItem( idx );
319 \param i - position of item (if it is less than 0, then current position is used)
321 void QtxListBox::deleteItem( const int i )
323 if ( !isEditEnabled() )
326 int idx = i < 0 ? currentItem() : i;
330 if ( editedIndex() == idx )
331 endEdition( defaultEditAction() );
338 Scrolls the content so that the point is in the top-left corner.
339 \param x, y - point co-ordinates
341 void QtxListBox::setContentsPos( int x, int y )
343 QListBox::setContentsPos( x, y );
349 Custom event filter, performs finish of edition on focus out, escape/return/enter pressing
351 bool QtxListBox::eventFilter( QObject* o, QEvent* e )
355 if ( e->type() == QEvent::FocusOut )
356 endEdition( defaultEditAction() );
358 if ( e->type() == QEvent::KeyPress )
360 QKeyEvent* ke = (QKeyEvent*)e;
361 if ( ke->key() == Key_Escape )
363 else if ( ke->key() == Key_Enter || ke->key() == Key_Return )
368 return QListBox::eventFilter( o, e );
372 Custom key press event handler
373 Allows to move items by CTRL + { Up, Down, Home, End }
375 void QtxListBox::keyPressEvent( QKeyEvent* e )
377 if ( e->key() == Key_Up && e->state() & ControlButton && isModificationEnabled() )
378 moveItem( currentItem(), -1 );
379 else if ( e->key() == Key_Down && e->state() & ControlButton && isModificationEnabled() )
380 moveItem( currentItem(), 1 );
381 else if ( e->key() == Key_Home && e->state() & ControlButton && isModificationEnabled() )
382 moveItemToTop( currentItem() );
383 else if ( e->key() == Key_End && e->state() & ControlButton && isModificationEnabled() )
384 moveItemToBottom( currentItem() );
385 else if ( e->key() == Key_Insert && e->state() & ControlButton )
386 createItem( currentItem() );
387 else if ( e->key() == Key_Delete && e->state() & ControlButton )
388 deleteItem( currentItem() );
390 QListBox::keyPressEvent( e );
394 Custom resize event handler
396 void QtxListBox::viewportResizeEvent( QResizeEvent* e )
398 QListBox::viewportResizeEvent( e );
404 Custom mouse double click event handler
406 void QtxListBox::mouseDoubleClickEvent( QMouseEvent* e )
408 if ( isEditEnabled() )
409 startEdition( itemAt( e->pos() ) );
411 QListBox::mouseDoubleClickEvent( e );
415 Updates editor on contents moving
417 void QtxListBox::onContentsMoving( int, int )
425 QLineEdit* QtxListBox::editor() const
429 QtxListBox* that = (QtxListBox*)this;
430 that->createEditor();
438 void QtxListBox::createEditor()
443 myEditor = new QLineEdit( viewport() );
445 myEditor->setLineWidth( 1 );
446 myEditor->setMidLineWidth( 0 );
447 myEditor->setFrameStyle( QFrame::Box | QFrame::Plain );
448 myEditor->installEventFilter( this );
452 addChild( myEditor );
458 void QtxListBox::updateEditor()
460 if ( !editedItem() || !editor() )
463 QRect r = itemRect( editedItem() );
467 int m = editor()->lineWidth();
468 r.addCoords( m, 0, 0, 0 );
470 const QPixmap* pix = pixmap( editedIndex() );
472 r.addCoords( pix->width() + 2, 0, 0, 0 );
474 editor()->setGeometry( r );