Commit a92ee1f2 authored by Ralf Habacker's avatar Ralf Habacker
Browse files

Derive class ResolutionWidget from ComboBoxWidgetBase

It was required to add support for a "post label" to class
ComboBoxWidgetBase.
parent 5db769b4
......@@ -15,8 +15,9 @@
#include <QHBoxLayout>
#include <QLabel>
ComboBoxWidgetBase::ComboBoxWidgetBase(const QString &title, QWidget *parent)
ComboBoxWidgetBase::ComboBoxWidgetBase(const QString &title, const QString &postLabel, QWidget *parent)
: QWidget(parent)
, m_postLabel(nullptr)
{
QHBoxLayout *layout = new QHBoxLayout;
layout->setContentsMargins(0,0,0,0);
......@@ -25,11 +26,19 @@ ComboBoxWidgetBase::ComboBoxWidgetBase(const QString &title, QWidget *parent)
m_editField = new KComboBox(this);
m_editField->setEditable(true);
m_editField->setDuplicatesEnabled(false); // only allow one of each type in box
#if QT_VERSION < 0x050000
m_editField->setCompletionMode(KGlobalSettings::CompletionPopup);
#endif
layout->addWidget(m_editField, 2);
m_label->setBuddy(m_editField);
if (!postLabel.isEmpty()) {
m_postLabel = new QLabel(postLabel, this);
layout->addWidget(m_postLabel);
}
setLayout(layout);
setFocusProxy(m_editField);
}
/**
......@@ -42,4 +51,6 @@ void ComboBoxWidgetBase::addToLayout(QGridLayout *layout, int row)
{
layout->addWidget(m_label, row, 0);
layout->addWidget(m_editField, row, 1);
if (m_postLabel)
layout->addWidget(m_postLabel, row, 2);
}
......@@ -21,11 +21,12 @@ class ComboBoxWidgetBase : public QWidget
{
Q_OBJECT
public:
explicit ComboBoxWidgetBase(const QString &title, QWidget *parent = nullptr);
explicit ComboBoxWidgetBase(const QString &title, const QString &postLabel = QString(), QWidget *parent = nullptr);
void addToLayout(QGridLayout *layout, int row);
protected:
QLabel *m_label;
QLabel *m_postLabel;
KComboBox *m_editField;
};
......
/*
Copyright 2015 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
Copyright 2015,2019 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
......@@ -35,40 +35,14 @@
* @param parent QWidget parent
*/
ResolutionWidget::ResolutionWidget(QWidget *parent) :
QWidget(parent)
ComboBoxWidgetBase(i18n("&Resolution:"), i18n("DPI"), parent)
{
QHBoxLayout *layout = new QHBoxLayout;
layout->setContentsMargins(0,0,0,0);
m_label = new QLabel(i18n("&Resolution:"), this);
layout->addWidget(m_label);
m_comboBox = new KComboBox(true, this);
layout->addWidget(m_comboBox, 2);
m_label->setBuddy(m_comboBox);
QLabel *postLabel = new QLabel(i18n("DPI"), this);
layout->addWidget(postLabel);
m_comboBox->setDuplicatesEnabled(false); // only allow one of each type in box
#if QT_VERSION < 0x050000
m_comboBox->setCompletionMode(KGlobalSettings::CompletionPopup);
#endif
m_comboBox->clear();
m_comboBox->addItem(QLatin1String("default"), QVariant(0.0));
m_editField->clear();
m_editField->addItem(QLatin1String("default"), QVariant(0.0));
foreach(const QString &key, resolutions()) {
m_comboBox->addItem(key, QVariant(key.toFloat()));
m_editField->addItem(key, QVariant(key.toFloat()));
}
setLayout(layout);
connect(m_comboBox, SIGNAL(editTextChanged(QString)), this, SLOT(slotTextChanged(QString)));
}
/**
* Add widget to a layout.
* @param layout The layout to add the widgets to.
*/
void ResolutionWidget::addToLayout(QHBoxLayout *layout)
{
layout->addWidget(m_label);
layout->addWidget(m_comboBox);
connect(m_editField, SIGNAL(editTextChanged(QString)), this, SLOT(slotTextChanged(QString)));
}
/**
......@@ -77,12 +51,12 @@ void ResolutionWidget::addToLayout(QHBoxLayout *layout)
*/
float ResolutionWidget::currentResolution()
{
QVariant v = m_comboBox->itemData(m_comboBox->currentIndex());
QVariant v = m_editField->itemData(m_editField->currentIndex());
if (v.canConvert<float>()) {
return v.value<float>();
} else {
bool ok;
float value = m_comboBox->currentText().toFloat(&ok);
float value = m_editField->currentText().toFloat(&ok);
return ok ? value : 0.0;
}
}
......@@ -119,10 +93,10 @@ QStringList ResolutionWidget::resolutions()
*/
void ResolutionWidget::slotTextChanged(const QString &text)
{
if (m_comboBox->currentText() == QLatin1String("default"))
if (m_editField->currentText() == QLatin1String("default"))
return;
bool ok;
text.toFloat(&ok);
if (!ok)
m_comboBox->setEditText(QLatin1String(""));
m_editField->setEditText(QLatin1String(""));
}
/*
Copyright 2015 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
Copyright 2015,2019 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
......@@ -21,18 +21,17 @@
#ifndef RESOLUTIONWIDGET_H
#define RESOLUTIONWIDGET_H
#include <QWidget>
#include "comboboxwidgetbase.h"
class KComboBox;
class QHBoxLayout;
class QLabel;
class ResolutionWidget : public QWidget
class ResolutionWidget : public ComboBoxWidgetBase
{
Q_OBJECT
public:
explicit ResolutionWidget(QWidget *parent = 0);
void addToLayout(QHBoxLayout *layout);
float currentResolution();
private slots:
......@@ -40,8 +39,6 @@ private slots:
private:
QStringList resolutions();
QLabel *m_label;
KComboBox *m_comboBox;
};
#endif // RESOLUTIONWIDGET_H
......@@ -13,7 +13,7 @@
#include <KComboBox>
SelectLayoutTypeWidget::SelectLayoutTypeWidget(const QString &title, Uml::LayoutType::Enum selected, QWidget *parent)
: ComboBoxWidgetBase(title, parent)
: ComboBoxWidgetBase(title, QString(), parent)
{
for (int layoutTypeNo = Uml::LayoutType::Undefined + 1; layoutTypeNo < Uml::LayoutType::N_LAYOUTTYPES; ++layoutTypeNo) {
Uml::LayoutType::Enum lt = Uml::LayoutType::fromInt(layoutTypeNo);
......
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