logplus/qtpropertybrowser/ColorSchemeComboBox.cpp
DESKTOP-450PEFP\mainc 5ece38108e 左侧数据树 色板属性添加颜色,可以配置不同的色板列表
图像道属性实现,最大值,最小值,色板等属性
波列道实现逻辑,x 与 y 轴不对调
2026-05-10 22:21:26 +08:00

506 lines
15 KiB
C++

#include <QObject>
// #include <QtGui/QAbstractItemView>
#include <QPen>
#include <QPainter>
#include <qtpropertybrowser.h>
#include "ColorSchemeComboBox.h"
#include "QtColorSchemeComboBox.h"
// #include "QtColorTableData.h"
template <class Value, class PrivateData>
static Value getData(const QMap<const QtProperty *, PrivateData> &propertyMap,
Value PrivateData::*data,
const QtProperty *property, const Value &defaultValue = Value())
{
typedef QMap<const QtProperty *, PrivateData> PropertyToData;
typedef PropertyToData::const_iterator PropertyToDataConstIterator;
const PropertyToDataConstIterator it = propertyMap.constFind(property);
if (it == propertyMap.constEnd())
return defaultValue;
return it.value().*data;
}
template <class Value, class PrivateData>
static Value getValue(const QMap<const QtProperty *, PrivateData> &propertyMap,
const QtProperty *property, const Value &defaultValue = Value())
{
return getData<Value>(propertyMap, &PrivateData::val, property, defaultValue);
}
QtColorSchemeComboBoxFactory::QtColorSchemeComboBoxFactory(QObject *parent)
: QtAbstractEditorFactory<QtColorSchemeComboBoxPropertyManager>(parent), d_ptr(new QtColorSchemeComboBoxFactoryPrivate())
{
d_ptr->q_ptr = this;
}
/*!
Destroys this factory, and all the widgets it has created.
*/
QtColorSchemeComboBoxFactory::~QtColorSchemeComboBoxFactory()
{
qDeleteAll(d_ptr->m_editorToProperty.keys());
}
/*!
\internal
Reimplemented from the QtAbstractEditorFactory class.
*/
void QtColorSchemeComboBoxFactory::connectPropertyManager(QtColorSchemeComboBoxPropertyManager *manager)
{
connect(manager, SIGNAL(valueChanged(QtProperty*,int,bool)),
this, SLOT(slotPropertyChanged(QtProperty*,int,bool)));
connect(manager, SIGNAL(enumNamesChanged(QtProperty*,QStringList)),
this, SLOT(slotEnumNamesChanged(QtProperty*,QStringList)));
}
// void QtColorSchemeComboBoxFactory::rendererLinearEnum(QComboBox *editor)
// {
// for (int ls = Qt::SolidLine; ls < Qt::CustomDashLine; ls++)
// {
// for(int lw = Qt::SolidLine; lw < Qt::CustomDashLine; lw++){
// QSize rectSize(200,15);
// QPixmap pix(rectSize);
// pix.fill(Qt::white);
//
// QBrush brush(Qt::black);
// QPen pen(brush,(Qt::PenStyle)lw,(Qt::PenStyle)ls);
// QPainter painter(&pix);
// painter.setPen(pen);
// painter.drawLine(2,7,500,7);
// editor->setIconSize(rectSize);
// editor->addItem(QIcon(pix),"");
//
// }
// }
// }
/*!
\internal
Reimplemented from the QtAbstractEditorFactory class.
*/
QWidget *QtColorSchemeComboBoxFactory::createEditor(QtColorSchemeComboBoxPropertyManager *manager, QtProperty *property,
QWidget *parent)
{
//QtColorSchemeComboBox * m_pColorSchemeBox = new QtColorSchemeComboBox();
//return m_pColorSchemeBox;
QtColorSchemeComboBox * pColorSchemeCombox = d_ptr->createEditor(property,parent);
// QtColorTableData colorMap("Color Scheme",true);
// colorMap.SetCurrentSchemeIndex(manager->value(property));
int useclr= manager->useColorTag(property);
QList<QtSchemeColor> colorList = QtColorTableData::getInstance()->GetSchemeList(useclr);
//QList<QtSchemeColor> schemeColor = manager->schemeColor(property);
pColorSchemeCombox->setSchemeColor(colorList);
pColorSchemeCombox->setCurrentIndex(manager->value(property));
//QComboBox *editor = d_ptr->createEditor(property, parent);
//pColorSchemeCombox->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
//pColorSchemeCombox->view()->setTextElideMode(Qt::ElideRight);
QStringList enumNames = manager->enumNames(property);
// QSize rectSize(200,15);
// editor->setIconSize(rectSize);
// editor->addItems(enumNames);
// rendererLinearEnum(editor);
//
QMap<int, QIcon> enumIcons = manager->enumIcons(property);
const int enumNamesCount = enumNames.count();
const int enumIconCount = enumIcons.count();
// for (int i = 0; i < enumIconCount; i++)
// editor->setItemIcon(i, enumIcons.value(i));
// editor->setCurrentIndex(manager->value(property));
//
connect(pColorSchemeCombox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotSetValue(int)));
connect(pColorSchemeCombox, SIGNAL(destroyed(QObject*)),
this, SLOT(slotEditorDestroyed(QObject*)));
return pColorSchemeCombox;
// return editor;
}
/*!
\internal
Reimplemented from the QtAbstractEditorFactory class.
*/
void QtColorSchemeComboBoxFactory::disconnectPropertyManager(QtColorSchemeComboBoxPropertyManager *manager)
{
disconnect(manager, SIGNAL(valueChanged(QtProperty*,int)),
this, SLOT(slotPropertyChanged(QtProperty*,int)));
disconnect(manager, SIGNAL(enumNamesChanged(QtProperty*,QStringList)),
this, SLOT(slotEnumNamesChanged(QtProperty*,QStringList)));
}
void QtColorSchemeComboBoxFactoryPrivate::slotPropertyChanged(QtProperty *property, int value)
{
if (!m_createdEditors.contains(property))
return;
QListIterator<QtColorSchemeComboBox *> itEditor(m_createdEditors[property]);
while (itEditor.hasNext()) {
QtColorSchemeComboBox *editor = itEditor.next();
editor->blockSignals(true);
editor->setCurrentIndex(value);
editor->blockSignals(false);
}
}
void QtColorSchemeComboBoxFactoryPrivate::slotEnumNamesChanged(QtProperty *property,
const QStringList &enumNames)
{
if (!m_createdEditors.contains(property))
return;
QtColorSchemeComboBoxPropertyManager *manager = q_ptr->propertyManager(property);
if (!manager)
return;
QMap<int, QIcon> enumIcons = manager->enumIcons(property);
QListIterator<QtColorSchemeComboBox *> itEditor(m_createdEditors[property]);
while (itEditor.hasNext()) {
QtColorSchemeComboBox *editor = itEditor.next();
editor->blockSignals(true);
editor->clear();
editor->addItems(enumNames);
const int nameCount = enumNames.count();
for (int i = 0; i < nameCount; i++)
editor->setItemIcon(i, enumIcons.value(i));
editor->setCurrentIndex(manager->value(property));
editor->blockSignals(false);
}
}
void QtColorSchemeComboBoxFactoryPrivate::slotEnumIconsChanged(QtProperty *property,
const QMap<int, QIcon> &enumIcons)
{
if (!m_createdEditors.contains(property))
return;
QtColorSchemeComboBoxPropertyManager *manager = q_ptr->propertyManager(property);
if (!manager)
return;
const QStringList enumNames = manager->enumNames(property);
QListIterator<QtColorSchemeComboBox *> itEditor(m_createdEditors[property]);
while (itEditor.hasNext()) {
QtColorSchemeComboBox *editor = itEditor.next();
editor->blockSignals(true);
const int nameCount = enumNames.count();
for (int i = 0; i < nameCount; i++)
editor->setItemIcon(i, enumIcons.value(i));
editor->setCurrentIndex(manager->value(property));
editor->blockSignals(false);
}
}
void QtColorSchemeComboBoxFactoryPrivate::slotSetValue(int value)
{
QObject *object = q_ptr->sender();
const QMap<QtColorSchemeComboBox *, QtProperty *>::ConstIterator ecend = m_editorToProperty.constEnd();
for (QMap<QtColorSchemeComboBox *, QtProperty *>::ConstIterator itEditor = m_editorToProperty.constBegin(); itEditor != ecend; ++itEditor)
if (itEditor.key() == object) {
QtProperty *property = itEditor.value();
QtColorSchemeComboBoxPropertyManager *manager = q_ptr->propertyManager(property);
if (!manager)
return;
manager->setValue(property, value,false);
return;
}
}
QtColorSchemeComboBoxPropertyManager::QtColorSchemeComboBoxPropertyManager(QObject *parent)
: QtAbstractPropertyManager(parent), d_ptr(new QtColorSchemeComboBoxPropertyManagerPrivate)
{
d_ptr->q_ptr = this;
}
/*!
Destroys this manager, and all the properties it has created.
*/
QtColorSchemeComboBoxPropertyManager::~QtColorSchemeComboBoxPropertyManager()
{
clear();
}
/*!
Returns the given \a property's value which is an index in the
list returned by enumNames()
If the given property is not managed by this manager, this
function returns -1.
\sa enumNames(), setValue()
*/
int QtColorSchemeComboBoxPropertyManager::value(const QtProperty *property) const
{
return getValue<int>(d_ptr->m_values, property, -1);
}
/*!
Returns the given \a property's list of enum names.
\sa value(), setEnumNames()
*/
QStringList QtColorSchemeComboBoxPropertyManager::enumNames(const QtProperty *property) const
{
return getData<QStringList>(d_ptr->m_values, &QtColorSchemeComboBoxPropertyManagerPrivate::Data::enumNames, property, QStringList());
}
/*!
Returns the given \a property's map of enum values to their icons.
\sa value(), setEnumIcons()
*/
QMap<int, QIcon> QtColorSchemeComboBoxPropertyManager::enumIcons(const QtProperty *property) const
{
return getData<QMap<int, QIcon> >(d_ptr->m_values, &QtColorSchemeComboBoxPropertyManagerPrivate::Data::enumIcons, property, QMap<int, QIcon>());
}
int QtColorSchemeComboBoxPropertyManager::useColorTag(const QtProperty *property) const
{
return getData<int>(d_ptr->m_values, &QtColorSchemeComboBoxPropertyManagerPrivate::Data::nUseColorTag, property, 0);
}
/*!
\reimp
*/
QString QtColorSchemeComboBoxPropertyManager::valueText(const QtProperty *property) const
{
const QtColorSchemeComboBoxPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property);
if (it == d_ptr->m_values.constEnd())
return QString();
const QtColorSchemeComboBoxPropertyManagerPrivate::Data &data = it.value();
const int v = data.val;
if (v >= 0 && v < data.enumNames.count())
return data.enumNames.at(v);
return QString();
}
bool QtColorSchemeComboBoxPropertyManager::IsColorScheme(const QtProperty *property) const
{
return true;
}
/*!
\reimp
*/
QIcon QtColorSchemeComboBoxPropertyManager::valueIcon(const QtProperty *property) const
{
const QtColorSchemeComboBoxPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property);
if (it == d_ptr->m_values.constEnd())
return QIcon();
const QtColorSchemeComboBoxPropertyManagerPrivate::Data &data = it.value();
const int v = data.val;
return data.enumIcons.value(v);
}
/*!
\fn void QtColorSchemeComboBoxPropertyManager::setValue(QtProperty *property, int value)
Sets the value of the given \a property to \a value.
The specified \a value must be less than the size of the given \a
property's enumNames() list, and larger than (or equal to) 0.
\sa value(), valueChanged()
*/
void QtColorSchemeComboBoxPropertyManager::setValue(QtProperty *property, int val,bool islineStyle)
{
const QtColorSchemeComboBoxPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property);
if (it == d_ptr->m_values.end())
return;
QtColorSchemeComboBoxPropertyManagerPrivate::Data data = it.value();
if (val >= data.enumNames.count())
return;
if (val < 0 && data.enumNames.count() > 0)
return;
if (val < 0)
val = -1;
if (data.val == val)
return;
data.val = val;
it.value() = data;
emit propertyChanged(property);
emit valueChanged(property, val,islineStyle);
}
/*!
Sets the given \a property's list of enum names to \a
enumNames. The \a property's current value is reset to 0
indicating the first item of the list.
If the specified \a enumNames list is empty, the \a property's
current value is set to -1.
\sa enumNames(), enumNamesChanged()
*/
void QtColorSchemeComboBoxPropertyManager::setEnumNames(QtProperty *property, const QStringList &enumNames)
{
const QtColorSchemeComboBoxPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property);
if (it == d_ptr->m_values.end())
return;
QtColorSchemeComboBoxPropertyManagerPrivate::Data data = it.value();
if (data.enumNames == enumNames)
return;
data.enumNames = enumNames;
data.val = -1;
if (enumNames.count() > 0)
data.val = 0;
it.value() = data;
emit enumNamesChanged(property, data.enumNames);
emit propertyChanged(property);
emit valueChanged(property, data.val,false);
}
/*!
Sets the given \a property's map of enum values to their icons to \a
enumIcons.
Each enum value can have associated icon. This association is represented with passed \a enumIcons map.
\sa enumNames(), enumNamesChanged()
*/
void QtColorSchemeComboBoxPropertyManager::setEnumIcons(QtProperty *property, const QMap<int, QIcon> &enumIcons)
{
const QtColorSchemeComboBoxPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property);
if (it == d_ptr->m_values.end())
return;
it.value().enumIcons = enumIcons;
emit enumIconsChanged(property, it.value().enumIcons);
emit propertyChanged(property);
}
void QtColorSchemeComboBoxPropertyManager::setUseColorTag(QtProperty *property, int useColorTag)
{
const QtColorSchemeComboBoxPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property);
if (it == d_ptr->m_values.end())
return;
QtColorSchemeComboBoxPropertyManagerPrivate::Data& data = it.value();
data.nUseColorTag = useColorTag;
}
QPixmap QtColorSchemeComboBoxPropertyManager::getColorLabelPixmap(const QVector<QtColorItem> &colorList, int colorNumber)
{
int w = 64, h = 32;
QPixmap pixmap(w, h);
QBrush brush(QColor(255, 0, 0));
pixmap.fill(QColor(255, 0, 0));
//QVector<QtColorItem> newColorList=paiInpolation(colorList,colorNumber);
QPainter painter;
painter.begin(&pixmap);
float colorStep = colorNumber * 1. / w;
QPen Pen;
for (int x = 0; x < w; x++)
{
// 色标位置
int clrIndex = (int)((x)* colorStep);
if (colorList.size() <= x) break;
Pen.setColor(QColor(colorList[clrIndex].color));
painter.setPen(Pen);
painter.drawLine(x, 0, x, h - 1);
}
painter.end();
return pixmap;
}
void QtColorSchemeComboBoxPropertyManager::setLinear(QtProperty *property, int nclr)
{
QStringList enumNames;
QMap<int, QIcon> enumIcons;
//QtColorTableData colorMap("Color Scheme",true);
QList<QtSchemeColor> colorList = QtColorTableData::getInstance()->GetSchemeList(nclr);
int nCount = colorList.size();
//样式值
///QList<Qt::PenStyle> linears;
// for(int index = 0; index < nCount; index++)
// {
// linears.push_back((Qt::PenStyle)(index));
// }
for(int i = 0 ; i < nCount; i++)
{
enumNames.push_back(colorList.at(i).schemeName);
//colorMap.
QPixmap pix = getColorLabelPixmap(colorList.at(i).colorList, colorList.at(i).colorList.count());
// pix.fill(Qt::white);
// QBrush brush(Qt::black);
// QPen pen(brush,2, linears.at(i));
// QPainter painter(&pix);
// painter.setPen(pen);
// painter.drawLine(2,7,500,7);
enumIcons[i] = QIcon(pix);
}
setEnumNames(property, enumNames);
setEnumIcons(property, enumIcons);
setUseColorTag(property, nclr);
}
void QtColorSchemeComboBoxPropertyManager::setLinear(QtProperty *property, QList<QtSchemeColor> colorList)
{
QStringList enumNames;
QMap<int, QIcon> enumIcons;
int nCount = colorList.size();
for (int i = 0; i < nCount; i++)
{
enumNames.push_back(colorList.at(i).schemeName);
QPixmap pix = getColorLabelPixmap(colorList.at(i).colorList, colorList.at(i).colorList.count());
enumIcons[i] = QIcon(pix);
}
setEnumNames(property, enumNames);
setEnumIcons(property, enumIcons);
}
/*!
\reimp
*/
void QtColorSchemeComboBoxPropertyManager::initializeProperty(QtProperty *property)
{
d_ptr->m_values[property] = QtColorSchemeComboBoxPropertyManagerPrivate::Data();
}
/*!
\reimp
*/
void QtColorSchemeComboBoxPropertyManager::uninitializeProperty(QtProperty *property)
{
d_ptr->m_values.remove(property);
}