diff --git a/libk3b/tools/k3bintmapcombobox.cpp b/libk3b/tools/k3bintmapcombobox.cpp index d1deae36c..24e994ebc 100644 --- a/libk3b/tools/k3bintmapcombobox.cpp +++ b/libk3b/tools/k3bintmapcombobox.cpp @@ -1,160 +1,162 @@ /* * * Copyright (C) 2006-2008 Sebastian Trueg * * This file is part of the K3b project. * Copyright (C) 1998-2008 Sebastian Trueg * * This program 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. * See the file "COPYING" for the exact licensing terms. */ #include "k3bintmapcombobox.h" #include #include #include class K3b::IntMapComboBox::Private { public: QHash valueIndexMap; QList > values; QString topWhatsThis; QString bottomWhatsThis; void buildValueIndexMap() { valueIndexMap.clear(); for ( int i = 0; i < values.count(); ++i ) { valueIndexMap.insert( values[i].first, i ); } } bool haveCustomWhatsThis() const { for ( int i = 0; i < values.count(); ++i ) { if ( !values[i].second.isEmpty() ) { return true; } } return false; } void updateWhatsThis() { if ( haveCustomWhatsThis() ) { QString ws( topWhatsThis ); for( int i = 0; i < values.count(); ++i ) { ws += "

" + q->itemText( i ) + "
"; ws += values[i].second; } ws += "

" + bottomWhatsThis; q->setWhatsThis( ws ); } } K3b::IntMapComboBox* q; }; K3b::IntMapComboBox::IntMapComboBox( QWidget* parent ) : QComboBox( parent ), d( new Private() ) { d->q = this; connect( this, SIGNAL(highlighted(int)), this, SLOT(slotItemHighlighted(int)) ); connect( this, SIGNAL(activated(int)), this, SLOT(slotItemActivated(int)) ); + + setSizeAdjustPolicy(QComboBox::AdjustToContents); } K3b::IntMapComboBox::~IntMapComboBox() { delete d; } int K3b::IntMapComboBox::selectedValue() const { if( d->values.count() > QComboBox::currentIndex() && QComboBox::currentIndex() >= 0 ) return d->values[QComboBox::currentIndex()].first; else return 0; } void K3b::IntMapComboBox::setSelectedValue( int value ) { if( d->valueIndexMap.contains( value ) ) { QComboBox::setCurrentIndex( d->valueIndexMap[value] ); } } bool K3b::IntMapComboBox::hasValue( int value ) const { return d->valueIndexMap.contains( value ); } void K3b::IntMapComboBox::clear() { d->valueIndexMap.clear(); d->values.clear(); QComboBox::clear(); } bool K3b::IntMapComboBox::insertItem( int value, const QString& text, const QString& description, int index ) { if( d->valueIndexMap.contains( value ) ) return false; if ( index < 0 || index > QComboBox::count() ) { index = QComboBox::count(); } d->values.insert( index, qMakePair( value, description ) ); d->buildValueIndexMap(); QComboBox::insertItem( index, text ); d->updateWhatsThis(); // select a default value. This is always wanted in K3b if ( QComboBox::currentIndex() < 0 ) { setSelectedValue( d->values[0].first ); } return true; } void K3b::IntMapComboBox::slotItemHighlighted( int index ) { emit valueHighlighted( d->values[index].first ); } void K3b::IntMapComboBox::slotItemActivated( int index ) { emit valueChanged( d->values[index].first ); } void K3b::IntMapComboBox::addGlobalWhatsThisText( const QString& top, const QString& bottom ) { d->topWhatsThis = top; d->bottomWhatsThis = bottom; d->updateWhatsThis(); }