k3bintmapcombobox.cpp 3.61 KB
Newer Older
1
/*
2
 *
Sebastian Trueg's avatar
Sebastian Trueg committed
3
 * Copyright (C) 2006-2008 Sebastian Trueg <trueg@k3b.org>
4 5
 *
 * This file is part of the K3b project.
Sebastian Trueg's avatar
Sebastian Trueg committed
6
 * Copyright (C) 1998-2008 Sebastian Trueg <trueg@k3b.org>
7 8 9 10 11 12 13 14 15 16
 *
 * 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"

Yuri Chornoivan's avatar
Yuri Chornoivan committed
17 18 19
#include <QDebug>
#include <QHash>
#include <QPair>
20 21


22
class K3b::IntMapComboBox::Private
23 24
{
public:
Sebastian Trueg's avatar
Sebastian Trueg committed
25 26 27 28 29 30 31 32 33 34 35 36
    QHash<int, int> valueIndexMap;
    QList<QPair<int, QString> > values;

    QString topWhatsThis;
    QString bottomWhatsThis;

    void buildValueIndexMap() {
        valueIndexMap.clear();
        for ( int i = 0; i < values.count(); ++i ) {
            valueIndexMap.insert( values[i].first, i );
        }
    }
Sebastian Trueg's avatar
Sebastian Trueg committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50

    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 ) {
Urs Wolfer's avatar
Urs Wolfer committed
51
                ws += "<p><b>" + q->itemText( i ) + "</b><br>";
Sebastian Trueg's avatar
Sebastian Trueg committed
52 53 54 55 56 57 58 59
                ws += values[i].second;
            }
            ws += "<p>" + bottomWhatsThis;

            q->setWhatsThis( ws );
        }
    }

60
    K3b::IntMapComboBox* q;
61 62 63
};


64
K3b::IntMapComboBox::IntMapComboBox( QWidget* parent )
Sebastian Trueg's avatar
Sebastian Trueg committed
65 66
    : QComboBox( parent ),
      d( new Private() )
67
{
Sebastian Trueg's avatar
Sebastian Trueg committed
68 69
    d->q = this;

Sebastian Trueg's avatar
Sebastian Trueg committed
70 71 72 73
    connect( this, SIGNAL(highlighted(int)),
             this, SLOT(slotItemHighlighted(int)) );
    connect( this, SIGNAL(activated(int)),
             this, SLOT(slotItemActivated(int)) );
74 75

    setSizeAdjustPolicy(QComboBox::AdjustToContents);
76 77 78
}


79
K3b::IntMapComboBox::~IntMapComboBox()
80
{
Sebastian Trueg's avatar
Sebastian Trueg committed
81
    delete d;
82 83 84
}


85
int K3b::IntMapComboBox::selectedValue() const
86
{
Sebastian Trueg's avatar
Sebastian Trueg committed
87 88 89
    if( d->values.count() > QComboBox::currentIndex() &&
        QComboBox::currentIndex() >= 0 )
        return d->values[QComboBox::currentIndex()].first;
Sebastian Trueg's avatar
Sebastian Trueg committed
90 91
    else
        return 0;
92 93 94
}


95
void K3b::IntMapComboBox::setSelectedValue( int value )
96
{
Sebastian Trueg's avatar
Sebastian Trueg committed
97
    if( d->valueIndexMap.contains( value ) ) {
Sebastian Trueg's avatar
Sebastian Trueg committed
98
        QComboBox::setCurrentIndex( d->valueIndexMap[value] );
Sebastian Trueg's avatar
Sebastian Trueg committed
99 100 101 102
    }
}


103
bool K3b::IntMapComboBox::hasValue( int value ) const
Sebastian Trueg's avatar
Sebastian Trueg committed
104 105
{
    return d->valueIndexMap.contains( value );
106 107 108
}


109
void K3b::IntMapComboBox::clear()
110
{
Sebastian Trueg's avatar
Sebastian Trueg committed
111 112
    d->valueIndexMap.clear();
    d->values.clear();
113

Sebastian Trueg's avatar
Sebastian Trueg committed
114
    QComboBox::clear();
115 116 117
}


118
bool K3b::IntMapComboBox::insertItem( int value, const QString& text, const QString& description, int index )
119
{
Sebastian Trueg's avatar
Sebastian Trueg committed
120 121 122
    if( d->valueIndexMap.contains( value ) )
        return false;

Sebastian Trueg's avatar
Sebastian Trueg committed
123 124
    if ( index < 0 || index > QComboBox::count() ) {
        index = QComboBox::count();
Sebastian Trueg's avatar
Sebastian Trueg committed
125
    }
126

Sebastian Trueg's avatar
Sebastian Trueg committed
127 128
    d->values.insert( index, qMakePair<int, QString>( value, description ) );
    d->buildValueIndexMap();
129

Sebastian Trueg's avatar
Sebastian Trueg committed
130
    QComboBox::insertItem( index, text );
131

Sebastian Trueg's avatar
Sebastian Trueg committed
132
    d->updateWhatsThis();
133

Sebastian Trueg's avatar
Sebastian Trueg committed
134
    // select a default value. This is always wanted in K3b
Sebastian Trueg's avatar
Sebastian Trueg committed
135
    if ( QComboBox::currentIndex() < 0 ) {
Sebastian Trueg's avatar
Sebastian Trueg committed
136 137
        setSelectedValue( d->values[0].first );
    }
138

Sebastian Trueg's avatar
Sebastian Trueg committed
139
    return true;
140 141 142
}


143
void K3b::IntMapComboBox::slotItemHighlighted( int index )
144
{
Sebastian Trueg's avatar
Sebastian Trueg committed
145
    emit valueHighlighted( d->values[index].first );
146 147 148
}


149
void K3b::IntMapComboBox::slotItemActivated( int index )
150
{
Sebastian Trueg's avatar
Sebastian Trueg committed
151
    emit valueChanged( d->values[index].first );
152 153
}

154

155
void K3b::IntMapComboBox::addGlobalWhatsThisText( const QString& top, const QString& bottom )
156
{
Sebastian Trueg's avatar
Sebastian Trueg committed
157 158
    d->topWhatsThis = top;
    d->bottomWhatsThis = bottom;
Sebastian Trueg's avatar
Sebastian Trueg committed
159
    d->updateWhatsThis();
160 161
}

162