1 | /* This file is part of the KDE project |
2 | Copyright (C) 2009 Dag Andersen <danders@get2net.dk> |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to |
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "kpttreecombobox.h" |
21 | |
22 | #include <klocale.h> |
23 | #include <kdebug.h> |
24 | |
25 | #include <QModelIndex> |
26 | #include <QTreeView> |
27 | #include <QHBoxLayout> |
28 | #include <QHeaderView> |
29 | #include <QStylePainter> |
30 | |
31 | namespace KPlato |
32 | { |
33 | |
34 | |
35 | //---------------------- |
36 | TreeComboBox::TreeComboBox( QWidget *parent ) |
37 | : KComboBox( parent ), |
38 | m_selectionmode( QAbstractItemView::ExtendedSelection ) |
39 | { |
40 | m_showcolumns << 0; |
41 | m_showheader = false; |
42 | |
43 | updateView(); |
44 | |
45 | connect( this, SIGNAL(activated(int)), SLOT(slotSelectionChanged()) ); |
46 | } |
47 | |
48 | void TreeComboBox::updateView() |
49 | { |
50 | QTreeView *v = new QTreeView(); |
51 | setView( v ); |
52 | v->setSelectionMode( m_selectionmode ); |
53 | // don't want to have mouseover select an item |
54 | v->disconnect(SIGNAL(entered(QModelIndex))); |
55 | |
56 | QHeaderView *h = v->header(); |
57 | for ( int i = 0; i < h->count(); ++i ) { |
58 | h->setSectionHidden( i, ! m_showcolumns.contains( i ) ); |
59 | } |
60 | h->setVisible( m_showheader ); |
61 | v->setRootIsDecorated( false ); |
62 | } |
63 | |
64 | QTreeView *TreeComboBox::view() const |
65 | { |
66 | return static_cast<QTreeView*>( KComboBox::view() ); |
67 | } |
68 | |
69 | void TreeComboBox::setModel( QAbstractItemModel *model ) |
70 | { |
71 | KComboBox::setModel( model ); |
72 | updateView(); |
73 | } |
74 | |
75 | QAbstractItemModel *TreeComboBox::model() const |
76 | { |
77 | return KComboBox::model(); |
78 | } |
79 | |
80 | void TreeComboBox::setSelectionMode( QAbstractItemView::SelectionMode mode ) |
81 | { |
82 | m_selectionmode = mode; |
83 | view()->setSelectionMode( mode ); |
84 | } |
85 | |
86 | void TreeComboBox::slotSelectionChanged() |
87 | { |
88 | updateCurrentIndexes( view()->selectionModel()->selectedRows() ); |
89 | } |
90 | |
91 | void TreeComboBox::() |
92 | { |
93 | QComboBox::showPopup(); |
94 | // now clean up things we want different |
95 | QItemSelectionModel *sm = view()->selectionModel(); |
96 | sm->clearSelection(); |
97 | view()->setSelectionMode( m_selectionmode ); |
98 | view()->setSelectionBehavior( QAbstractItemView::SelectRows ); |
99 | foreach ( const QModelIndex &i, m_currentIndexes ) { |
100 | if ( i.isValid() ) { |
101 | sm->select( i, QItemSelectionModel::Select | QItemSelectionModel::Rows ); |
102 | } |
103 | } |
104 | if ( ! sm->selectedRows().contains( sm->currentIndex() ) ) { |
105 | sm->setCurrentIndex( sm->selectedRows().value( 0 ), QItemSelectionModel::NoUpdate ); |
106 | } |
107 | } |
108 | |
109 | void TreeComboBox::paintEvent( QPaintEvent *event ) |
110 | { |
111 | Q_UNUSED(event); |
112 | QStylePainter painter(this); |
113 | painter.setPen(palette().color(QPalette::Text)); |
114 | |
115 | // draw the combobox frame, focusrect and selected etc. |
116 | QStyleOptionComboBox opt; |
117 | initStyleOption(&opt); |
118 | QStringList lst; |
119 | foreach ( const QPersistentModelIndex &idx, m_currentIndexes ) { |
120 | if ( idx.isValid() ) { |
121 | lst << idx.data().toString(); |
122 | } |
123 | } |
124 | opt.currentText = lst.isEmpty() ? i18n( "None" ) : lst.join( "," ); |
125 | painter.drawComplexControl(QStyle::CC_ComboBox, opt); |
126 | |
127 | // draw the icon and text |
128 | painter.drawControl(QStyle::CE_ComboBoxLabel, opt); |
129 | } |
130 | |
131 | void TreeComboBox::setCurrentIndexes( const QModelIndexList &lst ) |
132 | { |
133 | m_currentIndexes.clear(); |
134 | foreach ( const QModelIndex &idx, lst ) { |
135 | m_currentIndexes << QPersistentModelIndex( idx ); |
136 | } |
137 | } |
138 | |
139 | void TreeComboBox::setCurrentIndexes( const QList<QPersistentModelIndex> &lst ) |
140 | { |
141 | m_currentIndexes = lst; |
142 | } |
143 | |
144 | void TreeComboBox::updateCurrentIndexes( const QModelIndexList &lst ) |
145 | { |
146 | QList<QPersistentModelIndex> x; |
147 | foreach ( const QModelIndex &idx, lst ) { |
148 | x << QPersistentModelIndex( idx ); |
149 | } |
150 | if ( x == m_currentIndexes ) { |
151 | return; |
152 | } |
153 | m_currentIndexes = x; |
154 | emit changed(); |
155 | } |
156 | |
157 | } //namespace KPlato |
158 | |
159 | #include "kpttreecombobox.moc" |
160 | |
161 | |