File indexing completed on 2024-04-28 04:49:51

0001 /*
0002     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "k3bintmapcombobox.h"
0007 
0008 #include <QDebug>
0009 #include <QHash>
0010 #include <QPair>
0011 
0012 
0013 class K3b::IntMapComboBox::Private
0014 {
0015 public:
0016     QHash<int, int> valueIndexMap;
0017     QList<QPair<int, QString> > values;
0018 
0019     QString topWhatsThis;
0020     QString bottomWhatsThis;
0021 
0022     void buildValueIndexMap() {
0023         valueIndexMap.clear();
0024         for ( int i = 0; i < values.count(); ++i ) {
0025             valueIndexMap.insert( values[i].first, i );
0026         }
0027     }
0028 
0029     bool haveCustomWhatsThis() const {
0030         for ( int i = 0; i < values.count(); ++i ) {
0031             if ( !values[i].second.isEmpty() ) {
0032                 return true;
0033             }
0034         }
0035         return false;
0036     }
0037 
0038     void updateWhatsThis() {
0039         if ( haveCustomWhatsThis() ) {
0040             QString ws( topWhatsThis );
0041             for( int i = 0; i < values.count(); ++i ) {
0042                 ws += "<p><b>" + q->itemText( i ) + "</b><br>";
0043                 ws += values[i].second;
0044             }
0045             ws += "<p>" + bottomWhatsThis;
0046 
0047             q->setWhatsThis( ws );
0048         }
0049     }
0050 
0051     K3b::IntMapComboBox* q;
0052 };
0053 
0054 
0055 K3b::IntMapComboBox::IntMapComboBox( QWidget* parent )
0056     : QComboBox( parent ),
0057       d( new Private() )
0058 {
0059     d->q = this;
0060 
0061     connect( this, SIGNAL(highlighted(int)),
0062              this, SLOT(slotItemHighlighted(int)) );
0063     connect( this, SIGNAL(activated(int)),
0064              this, SLOT(slotItemActivated(int)) );
0065 
0066     setSizeAdjustPolicy(QComboBox::AdjustToContents);
0067 }
0068 
0069 
0070 K3b::IntMapComboBox::~IntMapComboBox()
0071 {
0072     delete d;
0073 }
0074 
0075 
0076 int K3b::IntMapComboBox::selectedValue() const
0077 {
0078     if( d->values.count() > QComboBox::currentIndex() &&
0079         QComboBox::currentIndex() >= 0 )
0080         return d->values[QComboBox::currentIndex()].first;
0081     else
0082         return 0;
0083 }
0084 
0085 
0086 void K3b::IntMapComboBox::setSelectedValue( int value )
0087 {
0088     if( d->valueIndexMap.contains( value ) ) {
0089         QComboBox::setCurrentIndex( d->valueIndexMap[value] );
0090     }
0091 }
0092 
0093 
0094 bool K3b::IntMapComboBox::hasValue( int value ) const
0095 {
0096     return d->valueIndexMap.contains( value );
0097 }
0098 
0099 
0100 void K3b::IntMapComboBox::clear()
0101 {
0102     d->valueIndexMap.clear();
0103     d->values.clear();
0104 
0105     QComboBox::clear();
0106 }
0107 
0108 
0109 bool K3b::IntMapComboBox::insertItem( int value, const QString& text, const QString& description, int index )
0110 {
0111     if( d->valueIndexMap.contains( value ) )
0112         return false;
0113 
0114     if ( index < 0 || index > QComboBox::count() ) {
0115         index = QComboBox::count();
0116     }
0117 
0118     d->values.insert( index, qMakePair( value, description ) );
0119     d->buildValueIndexMap();
0120 
0121     QComboBox::insertItem( index, text );
0122 
0123     d->updateWhatsThis();
0124 
0125     // select a default value. This is always wanted in K3b
0126     if ( QComboBox::currentIndex() < 0 ) {
0127         setSelectedValue( d->values[0].first );
0128     }
0129 
0130     return true;
0131 }
0132 
0133 
0134 void K3b::IntMapComboBox::slotItemHighlighted( int index )
0135 {
0136     emit valueHighlighted( d->values[index].first );
0137 }
0138 
0139 
0140 void K3b::IntMapComboBox::slotItemActivated( int index )
0141 {
0142     emit valueChanged( d->values[index].first );
0143 }
0144 
0145 
0146 void K3b::IntMapComboBox::addGlobalWhatsThisText( const QString& top, const QString& bottom )
0147 {
0148     d->topWhatsThis = top;
0149     d->bottomWhatsThis = bottom;
0150     d->updateWhatsThis();
0151 }
0152 
0153 #include "moc_k3bintmapcombobox.cpp"