diff --git a/src/libs/vpropertyexplorer/plugins/vlinecolorproperty.cpp b/src/libs/vpropertyexplorer/plugins/vlinecolorproperty.cpp new file mode 100644 index 000000000..f4fce1de4 --- /dev/null +++ b/src/libs/vpropertyexplorer/plugins/vlinecolorproperty.cpp @@ -0,0 +1,170 @@ +/************************************************************************ + ** + ** @file vlinecolorproperty.cpp + ** @author Roman Telezhynskyi + ** @date 7 2, 2015 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentine project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2015 Valentina project + ** All Rights Reserved. + ** + ** Valentina is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina 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 General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with Valentina. If not, see . + ** + *************************************************************************/ + +#include "vlinecolorproperty.h" +#include "../vproperty_p.h" + +#include +#include + +using namespace VPE; + +VLineColorProperty::VLineColorProperty(const QString &name) + : VProperty(name, QVariant::Int), colors(), indexList() +{ + VProperty::d_ptr->VariantValue = 0; + VProperty::d_ptr->VariantValue.convert(QVariant::Int); +} + +QVariant VLineColorProperty::data(int column, int role) const +{ + if (colors.empty()) + { + return QVariant(); + } + + int tmpIndex = VProperty::d_ptr->VariantValue.toInt(); + + if (tmpIndex < 0 || tmpIndex >= indexList.count()) + { + tmpIndex = 0; + } + + if (column == DPC_Data && Qt::DisplayRole == role) + { + return indexList.at(tmpIndex); + } + else if (column == DPC_Data && Qt::EditRole == role) + { + return tmpIndex; + } + else + { + return VProperty::data(column, role); + } +} + +QWidget *VLineColorProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &options, + const QAbstractItemDelegate *delegate) +{ + Q_UNUSED(options); + Q_UNUSED(delegate); + QComboBox* tmpEditor = new QComboBox(parent); + tmpEditor->clear(); + + QMap::const_iterator i = colors.constBegin(); + while (i != colors.constEnd()) + { + QPixmap pix(16, 16); + pix.fill(QColor(i.key())); + tmpEditor->addItem(QIcon(pix), i.value(), QVariant(i.key())); + ++i; + } + + tmpEditor->setCurrentIndex(VProperty::d_ptr->VariantValue.toInt()); + connect(tmpEditor, static_cast(&QComboBox::currentIndexChanged), this, + &VLineColorProperty::currentIndexChanged); + + VProperty::d_ptr->editor = tmpEditor; + return VProperty::d_ptr->editor; +} + +QVariant VLineColorProperty::getEditorData(const QWidget *editor) const +{ + const QComboBox* tmpEditor = qobject_cast(editor); + if (tmpEditor) + { + return tmpEditor->currentIndex(); + } + + return QVariant(0); +} + +void VLineColorProperty::setColors(const QMap &colors) +{ + this->colors = colors; + indexList.clear(); + QMap::const_iterator i = colors.constBegin(); + while (i != colors.constEnd()) + { + indexList.append(i.key()); + ++i; + } +} + +QMap VLineColorProperty::getColors() const +{ + return colors; +} + +void VLineColorProperty::setValue(const QVariant &value) +{ + int tmpIndex = value.toInt(); + + if (tmpIndex < 0 || tmpIndex >= indexList.count()) + { + tmpIndex = 0; + } + + VProperty::d_ptr->VariantValue = tmpIndex; + VProperty::d_ptr->VariantValue.convert(QVariant::Int); + + if (VProperty::d_ptr->editor != nullptr) + { + setEditorData(VProperty::d_ptr->editor); + } +} + +QString VLineColorProperty::type() const +{ + return QStringLiteral("lineColor"); +} + +VProperty *VLineColorProperty::clone(bool include_children, VProperty *container) const +{ + return VProperty::clone(include_children, container ? container : new VLineColorProperty(getName())); +} + +int VLineColorProperty::IndexOfColor(const QMap &colors, const QString &color) +{ + QVector indexList; + QMap::const_iterator i = colors.constBegin(); + while (i != colors.constEnd()) + { + indexList.append(i.key()); + ++i; + } + return indexList.indexOf(color); +} + +void VLineColorProperty::currentIndexChanged(int index) +{ + Q_UNUSED(index) + UserChangeEvent *event = new UserChangeEvent(); + QCoreApplication::postEvent ( VProperty::d_ptr->editor, event ); +} diff --git a/src/libs/vpropertyexplorer/plugins/vlinecolorproperty.h b/src/libs/vpropertyexplorer/plugins/vlinecolorproperty.h new file mode 100644 index 000000000..416b2a1d3 --- /dev/null +++ b/src/libs/vpropertyexplorer/plugins/vlinecolorproperty.h @@ -0,0 +1,100 @@ +/************************************************************************ + ** + ** @file vlinecolorproperty.h + ** @author Roman Telezhynskyi + ** @date 7 2, 2015 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentine project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2015 Valentina project + ** All Rights Reserved. + ** + ** Valentina is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina 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 General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with Valentina. If not, see . + ** + *************************************************************************/ + +#ifndef VLINECOLORPROPERTY_H +#define VLINECOLORPROPERTY_H + +#include "../vproperty.h" + +#include +#include + +namespace VPE +{ + +class VPROPERTYEXPLORERSHARED_EXPORT VLineColorProperty : public VProperty +{ + Q_OBJECT +public: + //! Constructor + VLineColorProperty(const QString& name); + + //! Destructor + ~VLineColorProperty() {} + + //! Get the data how it should be displayed + virtual QVariant data (int column = DPC_Name, int role = Qt::DisplayRole) const; + + //! Returns an editor widget, or NULL if it doesn't supply one + //! \param parent The widget to which the editor will be added as a child + //! \options Render options + //! \delegate A pointer to the QAbstractItemDelegate requesting the editor. This can be used to connect signals and + //! slots. + virtual QWidget *createEditor(QWidget* parent, const QStyleOptionViewItem& options, + const QAbstractItemDelegate* delegate); + + //! Gets the data from the widget + virtual QVariant getEditorData(const QWidget* editor) const; + + //! Sets the colors + virtual void setColors(const QMap &colors); + + //! Get the settings. This function has to be implemented in a subclass in order to have an effect + virtual QMap getColors() const; + + //! Sets the value of the property + virtual void setValue(const QVariant& value); + + //! Returns a string containing the type of the property + virtual QString type() const; + + //! Clones this property + //! \param include_children Indicates whether to also clone the children + //! \param container If a property is being passed here, no new VProperty is being created but instead it is tried + //! to fill all the data into container. This can also be used when subclassing this function. + //! \return Returns the newly created property (or container, if it was not NULL) + virtual VProperty* clone(bool include_children = true, VProperty* container = nullptr) const; + + static int IndexOfColor(const QMap &colors, const QString &color); + +public slots: + void currentIndexChanged(int index); + +protected: + //! The list of possible options to choose from + QMap colors; + QVector indexList; + // No use of d-pointer in this case, because it is unlikely this will change. If it does, we can still add other + //members by reimplementing the VPropertyPrivate class without touching this header file. +private: + Q_DISABLE_COPY(VLineColorProperty) +}; + +} + +#endif // VLINECOLORPROPERTY_H diff --git a/src/libs/vpropertyexplorer/vpropertyexplorer.pri b/src/libs/vpropertyexplorer/vpropertyexplorer.pri index 871cff6d3..6f11c2705 100644 --- a/src/libs/vpropertyexplorer/vpropertyexplorer.pri +++ b/src/libs/vpropertyexplorer/vpropertyexplorer.pri @@ -28,7 +28,8 @@ SOURCES += \ $$PWD/plugins/vpointfproperty.cpp \ $$PWD/plugins/vobjectproperty.cpp \ $$PWD/stable.cpp \ - $$PWD/plugins/vlinetypeproperty.cpp + $$PWD/plugins/vlinetypeproperty.cpp \ + $$PWD/plugins/vlinecolorproperty.cpp HEADERS +=\ $$PWD/vpropertyexplorer_global.h \ @@ -69,4 +70,5 @@ HEADERS +=\ $$PWD/plugins/vobjectproperty.h \ $$PWD/vproperties.h \ $$PWD/stable.h \ - $$PWD/plugins/vlinetypeproperty.h + $$PWD/plugins/vlinetypeproperty.h \ + $$PWD/plugins/vlinecolorproperty.h