#5882 Add color gradient as icon for legend palettes.

This commit is contained in:
Gaute Lindkvist 2020-05-29 09:46:44 +02:00
parent 90596be3ee
commit 9130030d7b
5 changed files with 92 additions and 10 deletions

View File

@ -17,6 +17,9 @@
/////////////////////////////////////////////////////////////////////////////////
#include "RimColorLegend.h"
#include "RiaColorTools.h"
#include "RimColorLegendItem.h"
CAF_PDM_SOURCE_INIT( RimColorLegend, "ColorLegend" );
@ -119,3 +122,20 @@ cvf::Color3ubArray RimColorLegend::colorArray() const
}
return colorArray;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::IconProvider RimColorLegend::paletteIconProvider() const
{
std::vector<QString> colorNames;
std::vector<RimColorLegendItem*> legendItems = colorLegendItems();
for ( auto legendItem : legendItems )
{
QColor color = RiaColorTools::toQColor( legendItem->color() );
colorNames.push_back( color.name() );
}
caf::IconProvider iconProvider;
iconProvider.setBackgroundColorGradient( colorNames );
return iconProvider;
}

View File

@ -56,6 +56,8 @@ public:
cvf::Color3ubArray colorArray() const;
caf::IconProvider paletteIconProvider() const;
public:
caf::PdmFieldHandle* userDescriptionField() override;

View File

@ -1091,7 +1091,10 @@ QList<caf::PdmOptionItemInfo>
for ( RimColorLegend* colorLegend : colorLegends )
{
options.push_back( caf::PdmOptionItemInfo( colorLegend->colorLegendName(), colorLegend ) );
options.push_back( caf::PdmOptionItemInfo( colorLegend->colorLegendName(),
colorLegend,
false,
colorLegend->paletteIconProvider() ) );
}
}
else if ( fieldNeedingOptions == &m_rangeMode )

View File

@ -36,6 +36,7 @@
#include "cafIconProvider.h"
#include <QApplication>
#include <QLinearGradient>
#include <QPainter>
using namespace caf;
@ -74,7 +75,7 @@ IconProvider::IconProvider(const IconProvider& rhs)
: m_active(rhs.m_active)
, m_iconResourceString(rhs.m_iconResourceString)
, m_overlayResourceString(rhs.m_overlayResourceString)
, m_backgroundColorString(rhs.m_backgroundColorString)
, m_backgroundColorStrings(rhs.m_backgroundColorStrings)
{
copyPixmap(rhs);
}
@ -87,7 +88,7 @@ IconProvider& IconProvider::operator=(const IconProvider& rhs)
m_active = rhs.m_active;
m_iconResourceString = rhs.m_iconResourceString;
m_overlayResourceString = rhs.m_overlayResourceString;
m_backgroundColorString = rhs.m_backgroundColorString;
m_backgroundColorStrings = rhs.m_backgroundColorStrings;
copyPixmap(rhs);
return *this;
@ -116,10 +117,33 @@ std::unique_ptr<QIcon> IconProvider::icon(const QSize& size /*= QSize(16, 16)*/)
QPixmap pixmap(size);
bool validIcon = false;
if (!m_backgroundColorString.isEmpty() && QColor::isValidColor(m_backgroundColorString))
{
pixmap.fill(QColor(m_backgroundColorString));
validIcon = true;
if (!m_backgroundColorStrings.empty())
{
if (m_backgroundColorStrings.size() == 1u && QColor::isValidColor(m_backgroundColorStrings.front()))
{
pixmap.fill(QColor(m_backgroundColorStrings.front()));
validIcon = true;
}
else
{
validIcon = true;
QLinearGradient gradient (QPointF(0.0f, 0.0f), QPoint(size.width(), 0.0f));
for (size_t i = 0; i < m_backgroundColorStrings.size(); ++i)
{
if (!QColor::isValidColor(m_backgroundColorStrings[i]))
{
validIcon = false;
break;
}
QColor color (m_backgroundColorStrings[i]);
float frac = i / ((float) m_backgroundColorStrings.size() - 1.0);
gradient.setColorAt(frac, color);
}
QBrush gradientBrush(gradient);
QPainter painter (&pixmap);
painter.fillRect(0, 0, size.width(), size.height(), gradientBrush);
}
}
else pixmap.fill(Qt::transparent);
@ -171,7 +195,15 @@ void IconProvider::setOverlayResourceString(const QString& overlayResourceString
//--------------------------------------------------------------------------------------------------
void IconProvider::setBackgroundColorString(const QString& colorName)
{
m_backgroundColorString = colorName;
m_backgroundColorStrings = { colorName };
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void IconProvider::setBackgroundColorGradient(const std::vector<QString>& colorNames)
{
m_backgroundColorStrings = colorNames;
}
//--------------------------------------------------------------------------------------------------
@ -183,7 +215,7 @@ bool caf::IconProvider::valid() const
{
if (m_pixmap && !m_pixmap->isNull()) return true;
if (!m_backgroundColorString.isEmpty() && QColor::isValidColor(m_backgroundColorString))
if (backgroundColorsAreValid())
{
return true;
}
@ -223,3 +255,24 @@ void IconProvider::copyPixmap(const IconProvider& rhs)
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool IconProvider::backgroundColorsAreValid() const
{
if (!m_backgroundColorStrings.empty())
{
bool validBackgroundColors = true;
for (QString colorName : m_backgroundColorStrings)
{
if (!QColor::isValidColor(colorName))
{
validBackgroundColors = false;
break;
}
}
return validBackgroundColors;
}
return false;
}

View File

@ -41,6 +41,7 @@
#include <QString>
#include <memory>
#include <vector>
class QIcon;
class QPixmap;
@ -68,18 +69,21 @@ public:
void setIconResourceString(const QString& iconResourceString);
void setOverlayResourceString(const QString& overlayResourceString);
void setBackgroundColorString(const QString& colorName);
void setBackgroundColorGradient(const std::vector<QString>& colorNames);
void setPixmap(const QPixmap& pixmap);
private:
static bool isGuiApplication();
void copyPixmap(const IconProvider& rhs);
bool backgroundColorsAreValid() const;
private:
bool m_active;
QString m_iconResourceString;
QString m_overlayResourceString;
QString m_backgroundColorString;
std::vector<QString> m_backgroundColorStrings;
std::unique_ptr<QPixmap> m_pixmap;
};