1 
2 #include "combobutton.h"
3 
4 #include <QApplication>
5 #include <QLayout>
6 #include <QString>
7 #include <QAbstractItemView>
8 #include <KIcon>
9 #include <KPushButton>
10 #include <KComboBox>
11 
12 
ComboButton(QWidget * parent)13 ComboButton::ComboButton( QWidget *parent )
14     : QWidget( parent )
15 {
16     m_increaseHeight = 0;
17 
18     QGridLayout *grid = new QGridLayout( this );
19     grid->setContentsMargins( 0, 0, 0, 0 );
20 
21     m_box = new KComboBox( this );
22     grid->addWidget( m_box, 0, 0 );
23     connect( m_box, SIGNAL(activated(int)), this, SLOT(boxActivated(int)) );
24     setFocusProxy( m_box );
25 
26     m_button = new KPushButton( QString(), this );
27     grid->addWidget( m_button, 0, 0 );
28     connect( m_button, SIGNAL(clicked()), this, SLOT(buttonClicked()) );
29     m_iconHight = m_button->iconSize().height();
30 
31 //     m_sizeMode = Max;
32 
33     balanceSize();
34 }
35 
~ComboButton()36 ComboButton::~ComboButton()
37 {}
38 
balanceSize()39 void ComboButton::balanceSize()
40 {
41     const int fontHeight = QFontMetrics(QApplication::font()).boundingRect("M").size().height();
42 
43     const int width = m_button->sizeHint().width();
44 
45     const int height = ( m_box->sizeHint().height() > m_button->sizeHint().height() ) ? m_box->sizeHint().height() : m_button->sizeHint().height();
46 
47     m_box->setFixedSize( width+1.2*fontHeight, height+m_increaseHeight );
48     m_box->view()->setMinimumWidth( m_box->view()->sizeHintForColumn(0) );
49     m_button->setFixedSize( width, height+m_increaseHeight );
50     m_button->setIconSize( QSize(m_iconHight+m_increaseHeight,m_iconHight+m_increaseHeight) );
51 }
52 
repaintButton()53 void ComboButton::repaintButton()
54 {
55     m_button->setText( m_box->currentText() );
56     m_button->setIcon( KIcon(m_box->itemIcon(m_box->currentIndex())) );
57     balanceSize();
58 }
59 
insertItem(const QString & text,int index)60 void ComboButton::insertItem( const QString &text, int index )
61 {
62     if( index == -1 ) index = m_box->count();
63     m_box->insertItem( index, text );
64     if( text.count() > m_box->minimumContentsLength() ) m_box->setMinimumContentsLength( text.count() );
65     repaintButton();
66 }
67 
insertItem(const KIcon & icon,const QString & text,int index)68 void ComboButton::insertItem( const KIcon &icon, const QString &text, int index )
69 {
70     if( index == -1 ) index = m_box->count();
71     m_box->insertItem( index, icon, text );
72     if( text.count() > m_box->minimumContentsLength() ) m_box->setMinimumContentsLength( text.count() );
73     repaintButton();
74 }
75 
increaseHeight(int height)76 void ComboButton::increaseHeight( int height )
77 {
78     m_increaseHeight = height;
79     balanceSize();
80 }
81 
boxActivated(int index)82 void ComboButton::boxActivated( int index )
83 {
84     repaintButton();
85     emit clicked( index );
86 }
87 
buttonClicked()88 void ComboButton::buttonClicked()
89 {
90     emit clicked( m_box->currentIndex() );
91 }
92 
93 // void ComboButton::setSizeMode( int mode )
94 // {
95 //     m_sizeMode = mode;
96 //     balanceSize();
97 // }
98 
99 // int ComboButton::sizeMode()
100 // {
101 //     return m_sizeMode;
102 // }
103 
setFont(const QFont & font)104 void ComboButton::setFont( const QFont& font )
105 {
106     m_button->setFont( font );
107     m_box->setFont( font );
108 }
109 
font()110 QFont ComboButton::font()
111 {
112     return m_button->font();
113 }
114 
115