statistics: collect colors in a StatsTheme class

To enable rudimentary theming, collect all colors in a new
theme class. The class has to be passed down to the various
items.

In general the items save a reference to the them in the
constructor. Alternatively, they might also just query
the StatsView everytime they need to access a color.
For now, it's hard the say what is preferred: a reference
per item or a function call per invokation?

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-02-16 17:05:39 +01:00 committed by Dirk Hohndel
parent 56e02dbcc0
commit b5aac29cea
25 changed files with 272 additions and 166 deletions

View file

@ -64,7 +64,7 @@ void ChartPixmapItem::setPositionDirty()
markDirty();
}
void ChartPixmapItem::render()
void ChartPixmapItem::render(const StatsTheme &)
{
if (!node) {
createNode(view.w()->createImageNode());
@ -137,30 +137,25 @@ static QSGTexture *createScatterTexture(StatsView &view, const QColor &color, co
return view.w()->createTextureFromImage(img, QQuickWindow::TextureHasAlphaChannel);
}
static QSGTexture *scatterItemTexture = nullptr;
static QSGTexture *scatterItemSelectedTexture = nullptr;
static QSGTexture *scatterItemHighlightedTexture = nullptr;
static QSGTexture *selectedTexture = nullptr; // A checkerboard pattern.
QSGTexture *ChartScatterItem::getTexture() const
QSGTexture *ChartScatterItem::getTexture(const StatsTheme &theme) const
{
switch (highlight) {
default:
case Highlight::Unselected:
return scatterItemTexture;
return theme.scatterItemTexture;
case Highlight::Selected:
return scatterItemSelectedTexture;
return theme.scatterItemSelectedTexture;
case Highlight::Highlighted:
return scatterItemHighlightedTexture;
return theme.scatterItemHighlightedTexture;
}
}
void ChartScatterItem::render()
void ChartScatterItem::render(const StatsTheme &theme)
{
if (!scatterItemTexture) {
scatterItemTexture = register_global(createScatterTexture(view, fillColor, borderColor));
scatterItemSelectedTexture = register_global(createScatterTexture(view, selectedColor, selectedBorderColor));
scatterItemHighlightedTexture = register_global(createScatterTexture(view, highlightedColor, highlightedBorderColor));
if (!theme.scatterItemTexture) {
theme.scatterItemTexture = register_global(createScatterTexture(view, theme.fillColor, theme.borderColor));
theme.scatterItemSelectedTexture = register_global(createScatterTexture(view, theme.selectedColor, theme.selectedBorderColor));
theme.scatterItemHighlightedTexture = register_global(createScatterTexture(view, theme.highlightedColor, theme.highlightedBorderColor));
}
if (!node) {
createNode(view.w()->createImageNode());
@ -169,7 +164,7 @@ void ChartScatterItem::render()
}
updateVisible();
if (textureDirty) {
node->node->setTexture(getTexture());
node->node->setTexture(getTexture(theme));
textureDirty = false;
}
if (positionDirty) {
@ -288,7 +283,7 @@ ChartPieItem::ChartPieItem(StatsView &v, ChartZValue z, double borderWidth) : Ch
{
}
static QBrush makeBrush(QColor fill, bool selected)
static QBrush makeBrush(QColor fill, bool selected, const StatsTheme &theme)
{
if (!selected)
return QBrush(fill);
@ -296,18 +291,18 @@ static QBrush makeBrush(QColor fill, bool selected)
img.fill(fill);
for (int x = 0; x < selectionOverlayPixelSize; ++x) {
for (int y = 0; y < selectionOverlayPixelSize; ++y) {
img.setPixelColor(x, y, selectionOverlayColor);
img.setPixelColor(x, y, theme.selectionOverlayColor);
img.setPixelColor(x + selectionOverlayPixelSize, y + selectionOverlayPixelSize,
selectionOverlayColor);
theme.selectionOverlayColor);
}
}
return QBrush(img);
}
void ChartPieItem::drawSegment(double from, double to, QColor fill, QColor border, bool selected)
void ChartPieItem::drawSegment(double from, double to, QColor fill, QColor border, bool selected, const StatsTheme &theme)
{
painter->setPen(QPen(border, borderWidth));
painter->setBrush(makeBrush(fill, selected));
painter->setBrush(makeBrush(fill, selected, theme));
// For whatever obscure reason, angles of pie pieces are given as 16th of a degree...?
// Angles increase CCW, whereas pie charts usually are read CW. Therfore, startAngle
// is dervied from "from" and subtracted from the origin angle at 12:00.
@ -353,7 +348,7 @@ void setPoint(QSGGeometry::TexturedPoint2D &v, const QPointF &p, const QPointF &
static_cast<float>(t.x()), static_cast<float>(t.y()));
}
void ChartLineItem::render()
void ChartLineItem::render(const StatsTheme &)
{
if (!node) {
geometry.reset(new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 2));
@ -384,7 +379,7 @@ void ChartLineItem::render()
positionDirty = materialDirty = false;
}
void ChartRectLineItem::render()
void ChartRectLineItem::render(const StatsTheme &)
{
if (!node) {
geometry.reset(new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4));
@ -427,19 +422,19 @@ ChartBarItem::~ChartBarItem()
{
}
QSGTexture *ChartBarItem::getSelectedTexture() const
QSGTexture *ChartBarItem::getSelectedTexture(const StatsTheme &theme) const
{
if (!selectedTexture) {
if (!theme.selectedTexture) {
QImage img(2, 2, QImage::Format_ARGB32);
img.fill(Qt::transparent);
img.setPixelColor(0, 0, selectionOverlayColor);
img.setPixelColor(1, 1, selectionOverlayColor);
selectedTexture = register_global(view.w()->createTextureFromImage(img, QQuickWindow::TextureHasAlphaChannel));
img.setPixelColor(0, 0, theme.selectionOverlayColor);
img.setPixelColor(1, 1, theme.selectionOverlayColor);
theme.selectedTexture = register_global(view.w()->createTextureFromImage(img, QQuickWindow::TextureHasAlphaChannel));
}
return selectedTexture;
return theme.selectedTexture;
}
void ChartBarItem::render()
void ChartBarItem::render(const StatsTheme &theme)
{
if (!node) {
createNode(view.w()->createRectangleNode());
@ -483,7 +478,7 @@ void ChartBarItem::render()
selectionGeometry.reset(new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4));
selectionGeometry->setDrawingMode(QSGGeometry::DrawTriangleFan);
selectionMaterial.reset(new QSGTextureMaterial);
selectionMaterial->setTexture(getSelectedTexture());
selectionMaterial->setTexture(getSelectedTexture(theme));
selectionMaterial->setHorizontalWrapMode(QSGTexture::Repeat);
selectionMaterial->setVerticalWrapMode(QSGTexture::Repeat);
selectionNode.reset(new QSGGeometryNode);
@ -556,12 +551,12 @@ ChartBoxItem::~ChartBoxItem()
{
}
void ChartBoxItem::render()
void ChartBoxItem::render(const StatsTheme &theme)
{
// Remember old dirty values, since ChartBarItem::render() will clear them
bool oldPositionDirty = positionDirty;
bool oldColorDirty = colorDirty;
ChartBarItem::render(); // This will create the base node, so no need to check for that.
ChartBarItem::render(theme); // This will create the base node, so no need to check for that.
if (!whiskersNode) {
whiskersGeometry.reset(new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 10));
whiskersGeometry->setDrawingMode(QSGGeometry::DrawLines);