Changeset View
Changeset View
Standalone View
Standalone View
src/commonfrontend/widgets/TreeViewComboBox.cpp
Show All 32 Lines | |||||
33 | #include "backend/lib/macros.h" | 33 | #include "backend/lib/macros.h" | ||
34 | 34 | | |||
35 | #include <QEvent> | 35 | #include <QEvent> | ||
36 | #include <QGroupBox> | 36 | #include <QGroupBox> | ||
37 | #include <QHeaderView> | 37 | #include <QHeaderView> | ||
38 | #include <QLineEdit> | 38 | #include <QLineEdit> | ||
39 | #include <QTreeView> | 39 | #include <QTreeView> | ||
40 | #include <QVBoxLayout> | 40 | #include <QVBoxLayout> | ||
41 | #include <QStylePainter> | ||||
41 | 42 | | |||
42 | #include <KLocalizedString> | 43 | #include <KLocalizedString> | ||
43 | 44 | | |||
44 | #include <cstring> // strcmp() | 45 | #include <cstring> // strcmp() | ||
45 | 46 | | |||
46 | /*! | 47 | /*! | ||
47 | \class TreeViewComboBox | 48 | \class TreeViewComboBox | ||
48 | \brief Provides a QTreeView in a QComboBox. | 49 | \brief Provides a QTreeView in a QComboBox. | ||
Show All 22 Lines | 53 | TreeViewComboBox::TreeViewComboBox(QWidget* parent) : QComboBox(parent), | |||
71 | m_treeView->setUniformRowHeights(true); | 72 | m_treeView->setUniformRowHeights(true); | ||
72 | 73 | | |||
73 | m_lineEdit->setPlaceholderText(i18n("Search/Filter text")); | 74 | m_lineEdit->setPlaceholderText(i18n("Search/Filter text")); | ||
74 | m_lineEdit->setClearButtonEnabled(true); | 75 | m_lineEdit->setClearButtonEnabled(true); | ||
75 | m_lineEdit->setFocus(); | 76 | m_lineEdit->setFocus(); | ||
76 | 77 | | |||
77 | addItem(QString()); | 78 | addItem(QString()); | ||
78 | setCurrentIndex(0); | 79 | setCurrentIndex(0); | ||
80 | setEditText(m_lineEditText); | ||||
79 | 81 | | |||
80 | // signal activated() is platform dependent | 82 | // signal activated() is platform dependent | ||
81 | connect(m_treeView, &QTreeView::pressed, this, &TreeViewComboBox::treeViewIndexActivated); | 83 | connect(m_treeView, &QTreeView::pressed, this, &TreeViewComboBox::treeViewIndexActivated); | ||
82 | connect(m_lineEdit, &QLineEdit::textChanged, this, &TreeViewComboBox::filterChanged); | 84 | connect(m_lineEdit, &QLineEdit::textChanged, this, &TreeViewComboBox::filterChanged); | ||
83 | } | 85 | } | ||
84 | 86 | | |||
85 | void TreeViewComboBox::setTopLevelClasses(const QList<AspectType>& list) | 87 | void TreeViewComboBox::setTopLevelClasses(const QList<AspectType>& list) | ||
86 | { | 88 | { | ||
Show All 11 Lines | 99 | void TreeViewComboBox::setModel(QAbstractItemModel* model) { | |||
98 | m_treeView->setModel(model); | 100 | m_treeView->setModel(model); | ||
99 | 101 | | |||
100 | //show only the first column in the combo box | 102 | //show only the first column in the combo box | ||
101 | for (int i = 1; i < model->columnCount(); i++) | 103 | for (int i = 1; i < model->columnCount(); i++) | ||
102 | m_treeView->hideColumn(i); | 104 | m_treeView->hideColumn(i); | ||
103 | 105 | | |||
104 | //Expand the complete tree in order to see everything in the first popup. | 106 | //Expand the complete tree in order to see everything in the first popup. | ||
105 | m_treeView->expandAll(); | 107 | m_treeView->expandAll(); | ||
108 | | ||||
109 | setEditText(m_lineEditText); | ||||
106 | } | 110 | } | ||
107 | 111 | | |||
108 | /*! | 112 | /*! | ||
109 | Sets the current item to be the item at \a index and selects it. | 113 | Sets the current item to be the item at \a index and selects it. | ||
110 | \sa currentIndex() | 114 | \sa currentIndex() | ||
111 | */ | 115 | */ | ||
112 | void TreeViewComboBox::setCurrentModelIndex(const QModelIndex& index) { | 116 | void TreeViewComboBox::setCurrentModelIndex(const QModelIndex& index) { | ||
113 | m_treeView->setCurrentIndex(index); | 117 | m_treeView->setCurrentIndex(index); | ||
Show All 13 Lines | 129 | /*! | |||
127 | Triggers showTopLevelOnly() to show toplevel items only. | 131 | Triggers showTopLevelOnly() to show toplevel items only. | ||
128 | */ | 132 | */ | ||
129 | void TreeViewComboBox::showPopup() { | 133 | void TreeViewComboBox::showPopup() { | ||
130 | if (!m_treeView->model() || !m_treeView->model()->hasChildren()) | 134 | if (!m_treeView->model() || !m_treeView->model()->hasChildren()) | ||
131 | return; | 135 | return; | ||
132 | 136 | | |||
133 | QModelIndex root = m_treeView->model()->index(0,0); | 137 | QModelIndex root = m_treeView->model()->index(0,0); | ||
134 | showTopLevelOnly(root); | 138 | showTopLevelOnly(root); | ||
135 | | ||||
136 | m_lineEdit->setText(QString()); | | |||
137 | m_groupBox->show(); | 139 | m_groupBox->show(); | ||
138 | m_groupBox->resize(this->width(), 250); | 140 | m_groupBox->resize(this->width(), 250); | ||
139 | m_groupBox->move(mapToGlobal( this->rect().topLeft() )); | 141 | m_groupBox->move(mapToGlobal( this->rect().topLeft() )); | ||
142 | | ||||
143 | setEditText(m_lineEditText); | ||||
144 | } | ||||
145 | | ||||
146 | /*! | ||||
147 | \reimp | ||||
148 | TODO: why do I have to reimplement paintEvent. It should work | ||||
149 | also without | ||||
150 | */ | ||||
151 | void TreeViewComboBox::paintEvent(QPaintEvent *) | ||||
152 | { | ||||
153 | QStylePainter painter(this); | ||||
154 | painter.setPen(palette().color(QPalette::Text)); | ||||
155 | // draw the combobox frame, focusrect and selected etc. | ||||
156 | QStyleOptionComboBox opt; | ||||
157 | initStyleOption(&opt); | ||||
158 | opt.currentText = currentText(); // TODO: why it's not working when letting this away? | ||||
159 | painter.drawComplexControl(QStyle::CC_ComboBox, opt); | ||||
160 | // draw the icon and text | ||||
161 | painter.drawControl(QStyle::CE_ComboBoxLabel, opt); | ||||
140 | } | 162 | } | ||
141 | 163 | | |||
142 | void TreeViewComboBox::hidePopup() { | 164 | void TreeViewComboBox::hidePopup() { | ||
143 | m_groupBox->hide(); | 165 | m_groupBox->hide(); | ||
144 | } | 166 | } | ||
145 | 167 | | |||
168 | | ||||
169 | void TreeViewComboBox::useCurrentIndexText(const bool set) { | ||||
170 | m_useCurrentIndexText = set; | ||||
171 | } | ||||
172 | | ||||
173 | /*! | ||||
174 | \property QComboBox::currentText | ||||
175 | \brief the current text | ||||
176 | If the combo box is editable, the current text is the value displayed | ||||
177 | by the line edit. Otherwise, it is the value of the current item or | ||||
178 | an empty string if the combo box is empty or no current item is set. | ||||
179 | The setter setCurrentText() simply calls setEditText() if the combo box is editable. | ||||
180 | Otherwise, if there is a matching text in the list, currentIndex is set to the | ||||
181 | corresponding index. | ||||
182 | If m_useCurrentIndexText is false, the Text set with setText is used. The intention of displaying | ||||
183 | this text is to show a text in the case of removed element. | ||||
184 | \sa editable, setEditText() | ||||
185 | */ | ||||
186 | QString TreeViewComboBox::currentText() const { | ||||
187 | if (lineEdit()) | ||||
188 | return lineEdit()->text(); | ||||
189 | else if (currentModelIndex().isValid() && m_useCurrentIndexText) | ||||
190 | return itemText(currentIndex()); | ||||
191 | else if (!m_useCurrentIndexText) | ||||
192 | return m_lineEditText; | ||||
193 | else | ||||
194 | return QString(); | ||||
195 | } | ||||
196 | | ||||
197 | void TreeViewComboBox::setText(QString text) { | ||||
198 | m_lineEditText = text; | ||||
199 | } | ||||
200 | | ||||
201 | void TreeViewComboBox::setInvalid(bool invalid, QString tooltip) { | ||||
202 | if (invalid) { | ||||
203 | setStyleSheet("background: red;"); | ||||
204 | setToolTip(tooltip); | ||||
205 | return; | ||||
206 | } | ||||
207 | | ||||
208 | setToolTip(""); | ||||
209 | setStyleSheet(""); | ||||
210 | } | ||||
211 | | ||||
146 | /*! | 212 | /*! | ||
147 | Hides the non-toplevel items of the model used in the tree view. | 213 | Hides the non-toplevel items of the model used in the tree view. | ||
148 | */ | 214 | */ | ||
149 | void TreeViewComboBox::showTopLevelOnly(const QModelIndex & index) { | 215 | void TreeViewComboBox::showTopLevelOnly(const QModelIndex & index) { | ||
150 | int rows = index.model()->rowCount(index); | 216 | int rows = index.model()->rowCount(index); | ||
151 | for (int i = 0; i < rows; i++) { | 217 | for (int i = 0; i < rows; i++) { | ||
152 | QModelIndex child = index.model()->index(i, 0, index); | 218 | QModelIndex child = index.model()->index(i, 0, index); | ||
153 | showTopLevelOnly(child); | 219 | showTopLevelOnly(child); | ||
▲ Show 20 Lines • Show All 87 Lines • Show Last 20 Lines |