1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
#include "Util.h"
#include "UserSelectionFrame.h"
#include <QLayout>
#include <QLabel>
//____________________________________________________________
UserSelectionFrame::UserSelectionFrame( QWidget* parent ):
QWidget( parent ),
Counter( "UserSelectionFrame" ),
delay_( 500 )
{
Debug::Throw( "UserSelectionFrame::UserSelectionFrame\n" );
setLayout( new QHBoxLayout );
layout()->setMargin(0);
layout()->setSpacing(5);
layout()->addWidget( comboBox_ = new CustomComboBox( this ) );
comboBox_->setMinimumSize( QSize( 100, 0 ) );
comboBox_->setEditable( true );
comboBox_->setAutoCompletion( true );
comboBox_->setToolTip( tr( "User selection" ) );
// send appropriate signal directly
connect( comboBox_, SIGNAL(activated(QString)), SLOT(_userChanged()) );
// one must add a timer here so that the signal gets
// emitted only after some delay, to avoid to many signals are sent when typing
connect( comboBox_, SIGNAL(editTextChanged(QString)), SLOT(_delayedUserChanged()) );
}
//____________________________________________________________
QStringList UserSelectionFrame::users() const
{
QStringList out;
for( int i=0; i< comboBox_->QComboBox::count(); i++ )
{
const QString& user( comboBox_->itemText( i ) );
if( !user.isNull() ) out << user;
}
return out;
}
//____________________________________________________________
void UserSelectionFrame::setUser( const QString& user )
{
Debug::Throw() << "UserSelectionFrame::set - user: " << user << endl;
if( user.isNull() || user.isEmpty() ) return;
for( int i=0; i< comboBox_->QComboBox::count(); i++ )
{
if( user == comboBox_->itemText( i ) )
{
comboBox_->setCurrentIndex(i);
return;
}
}
comboBox_->addItem( user );
comboBox_->setEditText( user );
return;
}
//________________________________________________________________
void UserSelectionFrame::updateUsers( QStringList users )
{
Debug::Throw( "UserSelectionFrame::updateUsers.\n" );
// make sure default user and current user are in the list
const QString currentUser( user() );
if( !( currentUser.isEmpty() || users.contains( currentUser ) ) )
{ users.prepend( currentUser ); }
// take all items except current
for( int index = 0; index < comboBox_->QComboBox::count(); )
{
if( !currentUser.isEmpty() && comboBox_->itemText( index ) == currentUser ) index++;
else comboBox_->removeItem( index );
}
// insert items, respecting order
for( int index = 0; index < users.size(); ++index )
{ if( users[index] != currentUser ) comboBox_->insertItem( index, users[index] ); }
}
//____________________________________________________________
QString UserSelectionFrame::user() const
{
Debug::Throw( "UserSelectionFrame::user.\n" );
QString user( comboBox_->currentText() );
if( user.isNull() || user.isEmpty() ) return "";
return user.trimmed();
}
//____________________________________________________________
void UserSelectionFrame::_userChanged()
{
QString new_user( user() );
if( new_user == user_ ) return;
user_ = new_user;
emit userChanged( user_ );
}
//____________________________________________________________
void UserSelectionFrame::timerEvent( QTimerEvent* event )
{
if( event->timerId() == timer_.timerId() )
{
timer_.stop();
_userChanged();
} else return QWidget::timerEvent( event );
}
|