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/
19 // File: QtxListBox.cxx
20 // Author: Sergey TELKOV
22 #include "QtxListBox.h"
25 #include <qlineedit.h>
27 QtxListBox::QtxListBox( QWidget* parent, const char* name, WFlags f )
28 : QListBox( parent, name, f ),
32 myEditDefault( true ),
33 myModifEnabled( true )
35 connect( this, SIGNAL( contentsMoving( int, int ) ),
36 this, SLOT( onContentsMoving( int, int ) ) );
39 QtxListBox::~QtxListBox()
43 bool QtxListBox::isEditEnabled() const
48 void QtxListBox::setEditEnabled( bool on )
50 if ( isEditEnabled() == on )
55 if ( !isEditEnabled() )
56 endEdition( defaultEditAction() );
59 bool QtxListBox::defaultEditAction() const
64 void QtxListBox::setDefaultEditAction( bool def )
69 bool QtxListBox::isModificationEnabled() const
71 return myModifEnabled;
74 void QtxListBox::setModificationEnabled( bool on )
79 QListBoxItem* QtxListBox::editedItem() const
81 return item( editedIndex() );
84 int QtxListBox::editedIndex() const
89 void QtxListBox::startEdition( const int idx )
91 if ( idx < 0 || editedIndex() == idx || !isEditEnabled() )
94 QLineEdit* ed = editor();
98 endEdition( defaultEditAction() );
102 ensureItemVisible( myEditIndex );
104 ed->setText( text( myEditIndex ) );
111 void QtxListBox::startEdition( const QListBoxItem* item )
113 startEdition( index( item ) );
116 void QtxListBox::endEdition( const bool action )
118 int idx = editedIndex();
119 QLineEdit* ed = editor();
121 if ( idx < 0 || !ed )
130 int cur = currentItem();
133 changeItem( *pixmap( idx ), ed->text(), idx );
135 changeItem( ed->text(), idx );
137 setCurrentItem( cur );
139 emit itemEdited( idx );
140 emit itemEdited( item( idx ) );
144 void QtxListBox::ensureItemVisible( const int idx )
149 if ( itemVisible( idx ) )
155 void QtxListBox::ensureItemVisible( const QListBoxItem* item )
157 ensureItemVisible( index( item ) );
160 const QValidator* QtxListBox::validator() const
162 const QValidator* v = 0;
164 v = editor()->validator();
168 void QtxListBox::clearValidator()
171 editor()->clearValidator();
174 void QtxListBox::setValidator( const QValidator* v )
177 editor()->setValidator( v );
180 void QtxListBox::moveItemToTop( const int idx )
182 moveItem( idx, -idx );
185 void QtxListBox::moveItemToBottom( const int idx )
187 moveItem( idx, count() - idx );
190 void QtxListBox::moveItem( const int idx, const int step )
192 QListBoxItem* i = item( idx );
193 if ( !i || step == 0 )
196 QListBoxItem* cur = item( currentItem() );
199 insertItem( i, QMAX( 0, idx + step ) );
201 setCurrentItem( index( cur ) );
203 int pos = index( i );
204 if ( myEditIndex == idx )
210 emit itemMoved( idx, pos );
213 void QtxListBox::createItem( const int i )
215 if ( !isEditEnabled() )
218 int idx = i < 0 ? currentItem() : i;
219 idx = idx < 0 ? count() : idx;
220 idx = QMIN( (int)count(), idx );
222 insertItem( "", idx );
223 setCurrentItem( idx );
227 void QtxListBox::deleteItem( const int i )
229 if ( !isEditEnabled() )
232 int idx = i < 0 ? currentItem() : i;
236 if ( editedIndex() == idx )
237 endEdition( defaultEditAction() );
243 void QtxListBox::setContentsPos( int x, int y )
245 QListBox::setContentsPos( x, y );
250 bool QtxListBox::eventFilter( QObject* o, QEvent* e )
254 if ( e->type() == QEvent::FocusOut )
255 endEdition( defaultEditAction() );
257 if ( e->type() == QEvent::KeyPress )
259 QKeyEvent* ke = (QKeyEvent*)e;
260 if ( ke->key() == Key_Escape )
262 else if ( ke->key() == Key_Enter || ke->key() == Key_Return )
267 return QListBox::eventFilter( o, e );
270 void QtxListBox::keyPressEvent( QKeyEvent* e )
272 if ( e->key() == Key_Up && e->state() & ControlButton && isModificationEnabled() )
273 moveItem( currentItem(), -1 );
274 else if ( e->key() == Key_Down && e->state() & ControlButton && isModificationEnabled() )
275 moveItem( currentItem(), 1 );
276 else if ( e->key() == Key_Home && e->state() & ControlButton && isModificationEnabled() )
277 moveItemToTop( currentItem() );
278 else if ( e->key() == Key_End && e->state() & ControlButton && isModificationEnabled() )
279 moveItemToBottom( currentItem() );
280 else if ( e->key() == Key_Insert && e->state() & ControlButton )
281 createItem( currentItem() );
282 else if ( e->key() == Key_Delete && e->state() & ControlButton )
283 deleteItem( currentItem() );
285 QListBox::keyPressEvent( e );
288 void QtxListBox::viewportResizeEvent( QResizeEvent* e )
290 QListBox::viewportResizeEvent( e );
295 void QtxListBox::mouseDoubleClickEvent( QMouseEvent* e )
297 if ( isEditEnabled() )
298 startEdition( itemAt( e->pos() ) );
300 QListBox::mouseDoubleClickEvent( e );
303 void QtxListBox::onContentsMoving( int, int )
308 QLineEdit* QtxListBox::editor() const
312 QtxListBox* that = (QtxListBox*)this;
313 that->createEditor();
318 void QtxListBox::createEditor()
323 myEditor = new QLineEdit( viewport() );
325 myEditor->setLineWidth( 1 );
326 myEditor->setMidLineWidth( 0 );
327 myEditor->setFrameStyle( QFrame::Box | QFrame::Plain );
328 myEditor->installEventFilter( this );
332 addChild( myEditor );
335 void QtxListBox::updateEditor()
337 if ( !editedItem() || !editor() )
340 QRect r = itemRect( editedItem() );
344 int m = editor()->lineWidth();
345 r.addCoords( m, 0, 0, 0 );
347 const QPixmap* pix = pixmap( editedIndex() );
349 r.addCoords( pix->width() + 2, 0, 0, 0 );
351 editor()->setGeometry( r );