2014-09-10 17:27:45 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file venumproperty.cpp
|
|
|
|
** @author hedgeware <internal(at)hedgeware.net>
|
|
|
|
** @date
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** All rights reserved. This program and the accompanying materials
|
|
|
|
** are made available under the terms of the GNU Lesser General Public License
|
|
|
|
** (LGPL) version 2.1 which accompanies this distribution, and is available at
|
|
|
|
** http://www.gnu.org/licenses/lgpl-2.1.html
|
|
|
|
**
|
|
|
|
** This library is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
** Lesser General Public License for more details.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "venumproperty.h"
|
|
|
|
|
|
|
|
#include <QComboBox>
|
|
|
|
#include <QCoreApplication>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QLocale>
|
|
|
|
#include <QWidget>
|
|
|
|
|
|
|
|
#include "../vproperty_p.h"
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VEnumProperty::VEnumProperty(const QString& name)
|
2014-09-20 18:05:21 +02:00
|
|
|
: VProperty(name, QVariant::Int), EnumerationLiterals()
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
VProperty::d_ptr->VariantValue = 0;
|
|
|
|
VProperty::d_ptr->VariantValue.convert(QVariant::Int);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Get the data how it should be displayed
|
2016-12-21 13:02:55 +01:00
|
|
|
QVariant VPE::VEnumProperty::data (int column, int role) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (EnumerationLiterals.empty())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return QVariant();
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
int tmpIndex = VProperty::d_ptr->VariantValue.toInt();
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (tmpIndex < 0 || tmpIndex >= EnumerationLiterals.count())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpIndex = 0;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (column == DPC_Data && Qt::DisplayRole == role)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return EnumerationLiterals.at(tmpIndex);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
|
|
|
else if (column == DPC_Data && Qt::EditRole == role)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return tmpIndex;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
else
|
|
|
|
return VProperty::data(column, role);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Returns an editor widget, or NULL if it doesn't supply one
|
2016-12-21 13:02:55 +01:00
|
|
|
QWidget* VPE::VEnumProperty::createEditor(QWidget * parent, const QStyleOptionViewItem& options,
|
|
|
|
const QAbstractItemDelegate* delegate)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2016-12-20 20:19:21 +01:00
|
|
|
Q_UNUSED(options)
|
|
|
|
Q_UNUSED(delegate)
|
2014-09-10 17:27:45 +02:00
|
|
|
QComboBox* tmpEditor = new QComboBox(parent);
|
|
|
|
tmpEditor->clear();
|
2015-02-11 11:33:13 +01:00
|
|
|
tmpEditor->setLocale(parent->locale());
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpEditor->addItems(EnumerationLiterals);
|
|
|
|
tmpEditor->setCurrentIndex(VProperty::d_ptr->VariantValue.toInt());
|
|
|
|
connect(tmpEditor, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
|
|
|
|
&VEnumProperty::currentIndexChanged);
|
|
|
|
|
|
|
|
VProperty::d_ptr->editor = tmpEditor;
|
|
|
|
return VProperty::d_ptr->editor;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Gets the data from the widget
|
2016-12-21 13:02:55 +01:00
|
|
|
QVariant VPE::VEnumProperty::getEditorData(const QWidget *editor) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-14 11:16:59 +02:00
|
|
|
const QComboBox* tmpEditor = qobject_cast<const QComboBox*>(editor);
|
2014-09-10 19:57:08 +02:00
|
|
|
if (tmpEditor)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return tmpEditor->currentIndex();
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
return QVariant(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Sets the enumeration literals
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VEnumProperty::setLiterals(const QStringList& literals)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
EnumerationLiterals = literals;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Get the settings. This function has to be implemented in a subclass in order to have an effect
|
2016-12-21 13:02:55 +01:00
|
|
|
QStringList VPE::VEnumProperty::getLiterals() const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return EnumerationLiterals;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Sets the value of the property
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VEnumProperty::setValue(const QVariant& value)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
int tmpIndex = value.toInt();
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (tmpIndex < 0 || tmpIndex >= EnumerationLiterals.count())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpIndex = 0;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
VProperty::d_ptr->VariantValue = tmpIndex;
|
|
|
|
VProperty::d_ptr->VariantValue.convert(QVariant::Int);
|
|
|
|
|
|
|
|
if (VProperty::d_ptr->editor != nullptr)
|
|
|
|
{
|
|
|
|
setEditorData(VProperty::d_ptr->editor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
QString VPE::VEnumProperty::type() const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return "enum";
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VProperty* VPE::VEnumProperty::clone(bool include_children, VProperty* container) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return VProperty::clone(include_children, container ? container : new VEnumProperty(getName()));
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VEnumProperty::setSetting(const QString& key, const QVariant& value)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (key == "literals")
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
setLiterals(value.toString().split(";;"));
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
QVariant VPE::VEnumProperty::getSetting(const QString& key) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (key == "literals")
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return getLiterals().join(";;");
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
else
|
|
|
|
return VProperty::getSetting(key);
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
QStringList VPE::VEnumProperty::getSettingKeys() const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return QStringList("literals");
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VEnumProperty::currentIndexChanged(int index)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(index)
|
|
|
|
UserChangeEvent *event = new UserChangeEvent();
|
|
|
|
QCoreApplication::postEvent ( VProperty::d_ptr->editor, event );
|
|
|
|
}
|