1 /*
2 SPDX-License-Identifier: GPL-2.0-or-later
3 SPDX-FileCopyrightText: 2019-2020 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
4 */
5
6 #include "comboboxwidgetbase.h"
7
8 #include <KComboBox>
9
10 #include <QHBoxLayout>
11 #include <QLabel>
12
ComboBoxWidgetBase(const QString & title,const QString & postLabel,QWidget * parent)13 ComboBoxWidgetBase::ComboBoxWidgetBase(const QString &title, const QString &postLabel, QWidget *parent)
14 : QWidget(parent)
15 , m_postLabel(nullptr)
16 {
17 QHBoxLayout *layout = new QHBoxLayout;
18 layout->setContentsMargins(0,0,0,0);
19 m_label = new QLabel(title, this);
20 layout->addWidget(m_label);
21
22 m_editField = new KComboBox(this);
23 m_editField->setEditable(true);
24 m_editField->setDuplicatesEnabled(false); // only allow one of each type in box
25 #if QT_VERSION < 0x050000
26 m_editField->setCompletionMode(KGlobalSettings::CompletionPopup);
27 #endif
28 layout->addWidget(m_editField, 2);
29 m_label->setBuddy(m_editField);
30
31 if (!postLabel.isEmpty()) {
32 m_postLabel = new QLabel(postLabel, this);
33 layout->addWidget(m_postLabel);
34 }
35 setLayout(layout);
36 setFocusProxy(m_editField);
37 }
38
39 /**
40 * Return pointer to the KComboBox edit field.
41 */
editField()42 KComboBox * ComboBoxWidgetBase::editField()
43 {
44 return m_editField;
45 }
46
47 /**
48 * Add this widget to a given grid layout. Umbrello dialogs place labels in column 0
49 * and the editable field in column 1.
50 * @param layout The layout to which the widget should be added
51 * @param row The row in the grid layout where the widget should be placed
52 * @param startColumn The first column in the grid layout where the widget should be placed
53 */
addToLayout(QGridLayout * layout,int row,int startColumn)54 void ComboBoxWidgetBase::addToLayout(QGridLayout *layout, int row, int startColumn)
55 {
56 layout->addWidget(m_label, row, startColumn);
57 layout->addWidget(m_editField, row, startColumn + 1);
58 if (m_postLabel)
59 layout->addWidget(m_postLabel, row, startColumn + 2);
60 }
61