Back to index

salome-gui  6.5.0
QtxComboBox.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:      QtxComboBox.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxComboBox.h"
00027 
00028 #include <QStandardItemModel>
00029 #include <QLineEdit>
00030 #include <QEvent>
00031 #include <QApplication>
00032 
00038 class QtxComboBox::Model : public QStandardItemModel
00039 {
00040 public:
00041   Model( QObject* parent = 0 );
00042   ~Model();
00043   void setCleared( const bool );
00044 
00045   QVariant data( const QModelIndex&, int = Qt::DisplayRole ) const;
00046 
00047 private:
00048   bool   myCleared;
00049 };
00050 
00056 QtxComboBox::Model::Model( QObject* parent )
00057   : QStandardItemModel( 0, 1, parent ),
00058     myCleared( false )
00059 {
00060 }
00061 
00066 QtxComboBox::Model::~Model()
00067 {
00068 }
00069 
00075 void QtxComboBox::Model::setCleared( const bool isClear )
00076 {
00077   if ( myCleared == isClear )
00078     return;
00079   
00080   myCleared = isClear;
00081 }
00082 
00090 QVariant QtxComboBox::Model::data( const QModelIndex& index, int role ) const
00091 {
00092   return ( myCleared && ( role == Qt::DisplayRole || role == Qt::DecorationRole ) ) ?
00093     QVariant() : QStandardItemModel::data( index, role );
00094 }
00095 
00103 #define CLEAR_EVENT QEvent::Type( QEvent::User + 123 )
00104 
00105 class QtxComboBox::ClearEvent : public QEvent
00106 {
00107 public:
00108   ClearEvent();
00109 };
00110 
00115 QtxComboBox::ClearEvent::ClearEvent() : QEvent( CLEAR_EVENT )
00116 {
00117 }
00118 
00133 QtxComboBox::QtxComboBox( QWidget* parent )
00134 : QComboBox( parent ),
00135   myCleared( false )
00136 {
00137   connect( this, SIGNAL( currentIndexChanged( int ) ),  this, SLOT( onCurrentChanged( int ) ) );
00138   setModel( new Model( this ) );
00139 }
00140 
00146 QtxComboBox::~QtxComboBox()
00147 {
00148 }
00149 
00154 bool QtxComboBox::isCleared() const
00155 {
00156   return myCleared;
00157 }
00158 
00163 void QtxComboBox::setCleared( const bool isClear )
00164 {
00165   if ( myCleared == isClear )
00166     return;
00167     
00168   myCleared = isClear;
00169 
00170   if ( lineEdit() )
00171     lineEdit()->setText( myCleared ? QString( "" ) : itemText( currentIndex() ) );
00172 
00173   update();
00174 }
00175 
00180 int QtxComboBox::currentId() const
00181 {
00182   return id( currentIndex() );
00183 }
00184 
00189 void QtxComboBox::setCurrentId( int num )
00190 {
00191   setCurrentIndex( index( num ) );
00192 }
00193 
00199 void QtxComboBox::setId( const int index, const int id )
00200 {
00201   setItemData( index, QVariant( id ), (Qt::ItemDataRole)IdRole );
00202 }
00203 
00208 void QtxComboBox::paintEvent( QPaintEvent* e )
00209 {
00210   Model* m = dynamic_cast<Model*>( model() );
00211   if ( m )
00212     m->setCleared( myCleared );
00213   QComboBox::paintEvent( e );
00214   if ( m )
00215     m->setCleared( false );
00216 }
00217 
00222 void QtxComboBox::childEvent( QChildEvent* e )
00223 {
00224   if ( ( e->added() || e->polished() ) && qobject_cast<QLineEdit*>( e->child() ) )
00225     QApplication::postEvent( this, new ClearEvent() );
00226 }
00227 
00232 void QtxComboBox::customEvent( QEvent* e )
00233 {
00234   if ( e->type() == CLEAR_EVENT && lineEdit() && myCleared )
00235     lineEdit()->setText( "" );
00236 }
00237 
00242 void QtxComboBox::onCurrentChanged( int idx )
00243 {
00244   if ( idx != -1 )
00245   {
00246     resetClear();
00247     emit activatedId( id( idx ) );
00248   }
00249 }
00250 
00254 void QtxComboBox::resetClear()
00255 {
00256   if ( !myCleared )
00257     return;
00258   
00259   myCleared = false;
00260   update();
00261 }
00262 
00268 int QtxComboBox::id( const int idx ) const
00269 {
00270   int id = -1;
00271   QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole );
00272   if ( v.canConvert( QVariant::Int ) )
00273     id = v.toInt();
00274   return id;
00275 }
00276 
00282 int QtxComboBox::index( const int ident ) const
00283 {
00284   int idx = -1;
00285   for ( int i = 0; i < (int)count() && idx == -1; i++ )
00286   {
00287     if ( id( i ) == ident )
00288       idx = i;
00289   }
00290   return idx;
00291 }
00292 
00297 bool QtxComboBox::hasId( const int idx ) const
00298 {
00299   QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole );
00300   return v.canConvert( QVariant::Int );
00301 }
00302