File indexing completed on 2024-06-16 07:42:24

0001 /*
0002     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "k3bcutcombobox.h"
0008 
0009 #include <QEvent>
0010 #include <QStringList>
0011 #include <QRect>
0012 #include <QSize>
0013 #include <QFontMetrics>
0014 #include <QPixmap>
0015 #include <QSizePolicy>
0016 #include <QStyle>
0017 
0018 
0019 class K3b::CutComboBox::Private
0020 {
0021 public:
0022     Private() {
0023         method = CUT;
0024     }
0025 
0026     QStringList originalItems;
0027 
0028     int method;
0029     int width;
0030 };
0031 
0032 
0033 #ifdef __GNUC__
0034 #warning Use user data to store the full strings or a custom item delegate to paint the items
0035 #endif
0036 K3b::CutComboBox::CutComboBox( QWidget* parent )
0037     : KComboBox( parent )
0038 {
0039     d = new Private();
0040     //  setSizePolicy( QSizePolicy::Maximum, sizePolicy().horData(), sizePolicy().hasHeightForWidth() );
0041 }
0042 
0043 
0044 K3b::CutComboBox::CutComboBox( int method, QWidget* parent )
0045     : KComboBox( parent )
0046 {
0047     d = new Private();
0048     d->method = method;
0049 }
0050 
0051 
0052 K3b::CutComboBox::CutComboBox()
0053 {
0054     delete d;
0055 }
0056 
0057 
0058 void K3b::CutComboBox::setMethod( int m )
0059 {
0060     d->method = m;
0061     cutText();
0062 }
0063 
0064 
0065 QSize K3b::CutComboBox::sizeHint() const
0066 {
0067 //   QSize s(KComboBox::sizeHint());
0068 
0069 //   for( int i = 0; i < count(); i++ ) {
0070 //     int w = fontMetrics().width(d->originalItems[i]) +
0071 //       ( d->pixmaps[i].isNull() ? 0 : d->pixmaps[i].width() + 4);
0072 //     if( w > s.width() )
0073 //       s.setWidth( w );
0074 //   }
0075 
0076     return KComboBox::sizeHint();
0077 }
0078 
0079 QSize K3b::CutComboBox::minimumSizeHint() const
0080 {
0081     return KComboBox::minimumSizeHint();
0082 }
0083 
0084 
0085 void K3b::CutComboBox::setCurrentText( const QString& s )
0086 {
0087     int i;
0088     for( i = 0; i < count(); i++ )
0089         if ( d->originalItems[i] == s )
0090             break;
0091     if ( i < count() ) {
0092         setCurrentIndex(i);
0093     }
0094     else if( !d->originalItems.isEmpty() ) {
0095         d->originalItems[currentIndex()] = s;
0096         cutText();
0097     }
0098 }
0099 
0100 
0101 void K3b::CutComboBox::insertStringList( const QStringList&, int )
0102 {
0103     // FIXME
0104 }
0105 
0106 void K3b::CutComboBox::insertStrList( const char**, int, int)
0107 {
0108     // FIXME
0109 }
0110 
0111 void K3b::CutComboBox::insertItem( const QString& text, int index )
0112 {
0113     insertItem( index, QPixmap(), text );
0114 }
0115 
0116 void K3b::CutComboBox::insertItem( const QPixmap& pix, int i )
0117 {
0118     insertItem( i, pix, "" );
0119 }
0120 
0121 void K3b::CutComboBox::insertItem( const QPixmap& pixmap, const QString& text, int index )
0122 {
0123     if( index != -1 )
0124         d->originalItems.insert( d->originalItems.at(index), text );
0125     else
0126         d->originalItems.append( text );
0127 
0128     if( !pixmap.isNull() )
0129         KComboBox::insertItem( index, pixmap, "xx" );
0130     else
0131         KComboBox::insertItem( index, "xx" );
0132 
0133     cutText();
0134 }
0135 
0136 void K3b::CutComboBox::removeItem( int i )
0137 {
0138     d->originalItems.erase( d->originalItems.at(i) );
0139     KComboBox::removeItem( i );
0140 }
0141 
0142 void K3b::CutComboBox::changeItem( const QString& s, int i )
0143 {
0144     d->originalItems[i] = s;
0145     cutText();
0146 }
0147 
0148 void K3b::CutComboBox::changeItem( const QPixmap& pix, const QString& s, int i )
0149 {
0150     KComboBox::changeItem( pix, i );
0151     changeItem( s, i );
0152 }
0153 
0154 
0155 QString K3b::CutComboBox::text( int i ) const
0156 {
0157     if( i < (int)d->originalItems.count() )
0158         return d->originalItems[i];
0159     else
0160         return QString();
0161 }
0162 
0163 
0164 QString K3b::CutComboBox::currentText() const
0165 {
0166     if( currentItem() < (int)d->originalItems.count() )
0167         return d->originalItems[currentItem()];
0168     else
0169         return QString();
0170 }
0171 
0172 
0173 void K3b::CutComboBox::clear()
0174 {
0175     KComboBox::clear();
0176     d->originalItems.clear();
0177 }
0178 
0179 void K3b::CutComboBox::resizeEvent( QResizeEvent* e )
0180 {
0181     cutText();
0182 
0183     KComboBox::resizeEvent(e);
0184 }
0185 
0186 
0187 void K3b::CutComboBox::cutText()
0188 {
0189     d->width = QStyle::visualRect( style().querySubControlMetrics(QStyle::CC_ComboBox, this,
0190                                                                   QStyle::SC_ComboBoxEditField), this ).width();
0191 
0192     for( int i = 0; i < (int)d->originalItems.count(); ++i ) {
0193         int w = d->width;
0194         if ( pixmap(i) && !pixmap(i)->isNull() )
0195             w -= ( pixmap(i)->width() + 4 );
0196 
0197         QString text = fontMetrics().elidedText( d->originalItems[i],
0198                                                  d->method == SQUEEZE ? Qt::ElideMiddle : Qt::ElideRight,
0199                                                  w );
0200 
0201         // now insert the cut text
0202         if( pixmap(i) )
0203             KComboBox::changeItem( *pixmap(i), text, i );
0204         else
0205             KComboBox::changeItem( text, i );
0206     }
0207 }
0208 
0209 #include "moc_k3bcutcombobox.cpp"