Commit 7cf3550f authored by Ralf Habacker's avatar Ralf Habacker
Browse files

Let class UMLStereotypeWidget use ComboBoxWidgetBase as base class

Followup for commit f0b38d10.
parent b795aa90
......@@ -4,7 +4,7 @@
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* copyright (C) 2002-2014 *
* copyright (C) 2002-2014,2019 *
* Umbrello UML Modeller Authors <umbrello-devel@kde.org> *
***************************************************************************/
......@@ -26,17 +26,11 @@
Q_DECLARE_METATYPE(UMLStereotype*);
UMLStereotypeWidget::UMLStereotypeWidget(UMLObject *object, QWidget *parent)
: QWidget(parent),
m_object(object)
: ComboBoxWidgetBase(i18n("Stereotype &name:"), QString(), parent)
, m_object(object)
{
Q_ASSERT(m_object);
init();
}
UMLStereotypeWidget::~UMLStereotypeWidget()
{
delete m_comboBox;
delete m_label;
insertItems(m_object->umlStereotype());
}
/**
......@@ -45,19 +39,7 @@ UMLStereotypeWidget::~UMLStereotypeWidget()
*/
void UMLStereotypeWidget::setEditable(bool state)
{
m_comboBox->setEditable(state);
}
/**
* Add this widget to a given grid layout. Umbrello dialogs places labels in column 0
* and the editable field in column 1.
* @param layout The layout to which the widget should be added
* @param row The row in the grid layout where the widget should be placed
*/
void UMLStereotypeWidget::addToLayout(QGridLayout *layout, int row)
{
layout->addWidget(m_label, row, 0);
layout->addWidget(m_comboBox, row, 1);
m_editField->setEditable(state);
}
/**
......@@ -65,50 +47,27 @@ void UMLStereotypeWidget::addToLayout(QGridLayout *layout, int row)
*/
void UMLStereotypeWidget::apply()
{
if (m_comboBox->currentText().isEmpty()) {
if (m_editField->currentText().isEmpty()) {
m_object->setUMLStereotype(0);
return;
}
QVariant v = m_comboBox->itemData(m_comboBox->currentIndex());
QVariant v = m_editField->itemData(m_editField->currentIndex());
if (v.canConvert<UMLStereotype*>()) {
UMLStereotype *selected = v.value<UMLStereotype*>();
if (m_object->umlStereotype()) {
if (m_object->umlStereotype()->name() != m_comboBox->currentText())
if (m_object->umlStereotype()->name() != m_editField->currentText())
m_object->setUMLStereotype(selected);
}
else
m_object->setUMLStereotype(selected);
} else {
UMLStereotype *stereotype = new UMLStereotype(m_comboBox->currentText());
UMLStereotype *stereotype = new UMLStereotype(m_editField->currentText());
UMLApp::app()->document()->addStereotype(stereotype);
m_object->setUMLStereotype(stereotype);
}
}
/**
* setup widgets
*/
void UMLStereotypeWidget::init()
{
QHBoxLayout *layout = new QHBoxLayout;
layout->setContentsMargins(0,0,0,0);
m_label = new QLabel(i18n("Stereotype &name:"), this);
layout->addWidget(m_label);
m_comboBox = new KComboBox(true, this);
layout->addWidget(m_comboBox, 2);
m_label->setBuddy(m_comboBox);
m_comboBox->setDuplicatesEnabled(false); // only allow one of each type in box
#if QT_VERSION < 0x050000
m_comboBox->setCompletionMode(KGlobalSettings::CompletionPopup);
#endif
insertItems(m_object->umlStereotype());
setLayout(layout);
}
/**
* Insert stereotypes into combo box and select the currently used stereotype.
* @param type currently used stereotype
......@@ -126,18 +85,18 @@ void UMLStereotypeWidget::insertItems(UMLStereotype *type)
types[type->name()] = type;
}
m_comboBox->clear();
m_comboBox->addItem(QLatin1String(""), QVariant(0));
m_editField->clear();
m_editField->addItem(QLatin1String(""), QVariant(0));
foreach(const QString &key, types.keys()) { // krazy:exclude=foreach
m_comboBox->addItem(key, QVariant::fromValue((types[key])));
m_editField->addItem(key, QVariant::fromValue((types[key])));
}
// select the given parameter
if (type) {
int currentIndex = m_comboBox->findText(type->name());
int currentIndex = m_editField->findText(type->name());
if (currentIndex > -1) {
m_comboBox->setCurrentIndex(currentIndex);
m_editField->setCurrentIndex(currentIndex);
}
m_comboBox->completionObject()->addItem(type->name());
m_editField->completionObject()->addItem(type->name());
}
}
......@@ -4,39 +4,29 @@
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* copyright (C) 2002-2014 *
* copyright (C) 2002-2014,2019 *
* Umbrello UML Modeller Authors <umbrello-devel@kde.org> *
***************************************************************************/
#ifndef UMLSTEREOTYPEWIDGET_H
#define UMLSTEREOTYPEWIDGET_H
#include <QWidget>
#include "comboboxwidgetbase.h"
class UMLObject;
class UMLStereotype;
class KComboBox;
class QGridLayout;
class QLabel;
class UMLStereotypeWidget : public QWidget
class UMLStereotypeWidget : public ComboBoxWidgetBase
{
public:
explicit UMLStereotypeWidget(UMLObject *object, QWidget *parent=0);
~UMLStereotypeWidget();
void setEditable(bool state);
void addToLayout(QGridLayout *layout, int row);
void apply();
protected:
QLabel *m_label;
KComboBox *m_comboBox;
UMLObject *m_object;
void init();
void insertItems(UMLStereotype *type);
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment