mirror of
https://github.com/OPM/ResInsight.git
synced 2025-01-24 15:26:48 -06:00
#5882 Add color gradient as icon for legend palettes.
This commit is contained in:
parent
90596be3ee
commit
9130030d7b
@ -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;
|
||||
}
|
||||
|
@ -56,6 +56,8 @@ public:
|
||||
|
||||
cvf::Color3ubArray colorArray() const;
|
||||
|
||||
caf::IconProvider paletteIconProvider() const;
|
||||
|
||||
public:
|
||||
caf::PdmFieldHandle* userDescriptionField() override;
|
||||
|
||||
|
@ -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 )
|
||||
|
@ -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,11 +117,34 @@ 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))
|
||||
if (!m_backgroundColorStrings.empty())
|
||||
{
|
||||
pixmap.fill(QColor(m_backgroundColorString));
|
||||
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);
|
||||
|
||||
if (!m_iconResourceString.isEmpty())
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user