mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-28 05:00:20 +00:00
2b961414d7
In order not to waste CPU by constantly rerendering the chart, we must use these weird OpenGL QSGNode things. The interface is appallingly low-level and unfriendly. As a first test, try to convert the legend. Create a wrapper class that represents a rectangular item with a texture and that will certainly need some (lots of) optimization. Make sure that all low-level QSG-objects are only accessed in the rendering thread. This means that the wrapper has to maintain a notion of "dirtiness" of the state. I.e. which part of the QSG-objects have to be modified. From the low-level wrapper derive a class that draws a rounded rectangle for every resize. The child class of that must then paint on the rectangle after every resize. That looks all not very fortunate, but it displays a legend and will make it possible to move the legend without and drawing operations, only shifting around an OpenGL surface. The render thread goes through all chart-items and rerenders them if dirty. Currently, on deletion of these items, this list is not reset. I.e. currently it is not supported to remove individual items. Only the full scene can be cleared! Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
108 lines
3.4 KiB
C++
108 lines
3.4 KiB
C++
// SPDX-License-Identifier: GPL-2.0
|
|
#include "legend.h"
|
|
#include "statscolors.h"
|
|
#include "zvalues.h"
|
|
|
|
#include <QFontMetrics>
|
|
#include <QPen>
|
|
|
|
static const double legendBorderSize = 2.0;
|
|
static const double legendBoxBorderSize = 1.0;
|
|
static const double legendBoxBorderRadius = 4.0; // radius of rounded corners
|
|
static const double legendBoxScale = 0.8; // 1.0: text-height of the used font
|
|
static const double legendInternalBorderSize = 2.0;
|
|
static const QColor legendColor(0x00, 0x8e, 0xcc, 192); // Note: fourth argument is opacity
|
|
static const QColor legendBorderColor(Qt::black);
|
|
|
|
Legend::Legend(StatsView &view, const std::vector<QString> &names) :
|
|
ChartRectItem(view, QPen(legendBorderColor, legendBorderSize), QBrush(legendColor), legendBoxBorderRadius),
|
|
displayedItems(0), width(0.0), height(0.0),
|
|
font(QFont()) // Make configurable
|
|
{
|
|
entries.reserve(names.size());
|
|
QFontMetrics fm(font);
|
|
fontHeight = fm.height();
|
|
int idx = 0;
|
|
for (const QString &name: names)
|
|
entries.emplace_back(name, idx++, (int)names.size(), fm);
|
|
}
|
|
|
|
Legend::Entry::Entry(const QString &name, int idx, int numBins, const QFontMetrics &fm) :
|
|
name(name),
|
|
rectBrush(QBrush(binColor(idx, numBins)))
|
|
{
|
|
width = fm.height() + 2.0 * legendBoxBorderSize + fm.size(Qt::TextSingleLine, name).width();
|
|
}
|
|
|
|
void Legend::hide()
|
|
{
|
|
ChartRectItem::resize(QSizeF(1,1));
|
|
img->fill(Qt::transparent);
|
|
}
|
|
|
|
void Legend::resize()
|
|
{
|
|
if (entries.empty())
|
|
return hide();
|
|
|
|
QSizeF size = sceneSize();
|
|
|
|
// Silly heuristics: make the legend at most half as high and half as wide as the chart.
|
|
// Not sure if that makes sense - this might need some optimization.
|
|
int maxRows = static_cast<int>(size.height() / 2.0 - 2.0 * legendInternalBorderSize) / fontHeight;
|
|
if (maxRows <= 0)
|
|
return hide();
|
|
int numColumns = ((int)entries.size() - 1) / maxRows + 1;
|
|
int numRows = ((int)entries.size() - 1) / numColumns + 1;
|
|
|
|
double x = legendInternalBorderSize;
|
|
displayedItems = 0;
|
|
for (int col = 0; col < numColumns; ++col) {
|
|
double y = legendInternalBorderSize;
|
|
double nextX = x;
|
|
|
|
for (int row = 0; row < numRows; ++row) {
|
|
int idx = col * numRows + row;
|
|
if (idx >= (int)entries.size())
|
|
break;
|
|
entries[idx].pos = QPointF(x, y);
|
|
nextX = std::max(nextX, x + entries[idx].width);
|
|
y += fontHeight;
|
|
++displayedItems;
|
|
}
|
|
x = nextX;
|
|
width = nextX;
|
|
if (width >= size.width() / 2.0) // More than half the chart-width -> give up
|
|
break;
|
|
}
|
|
width += legendInternalBorderSize;
|
|
height = 2 * legendInternalBorderSize + numRows * fontHeight;
|
|
|
|
ChartRectItem::resize(QSizeF(width, height));
|
|
|
|
// Paint rectangles
|
|
painter->setPen(QPen(legendBorderColor, legendBoxBorderSize));
|
|
for (int i = 0; i < displayedItems; ++i) {
|
|
QPointF itemPos = entries[i].pos;
|
|
painter->setBrush(entries[i].rectBrush);
|
|
QRectF rect(itemPos, QSizeF(fontHeight, fontHeight));
|
|
// Decrease box size by legendBoxScale factor
|
|
double delta = fontHeight * (1.0 - legendBoxScale) / 2.0;
|
|
rect = rect.adjusted(delta, delta, -delta, -delta);
|
|
painter->drawRect(rect);
|
|
}
|
|
|
|
// Paint labels
|
|
painter->setPen(darkLabelColor); // QPainter uses pen not brush for text!
|
|
painter->setFont(font);
|
|
for (int i = 0; i < displayedItems; ++i) {
|
|
QPointF itemPos = entries[i].pos;
|
|
itemPos.rx() += fontHeight + 2.0 * legendBoxBorderSize;
|
|
QRectF rect(itemPos, QSizeF(entries[i].width, fontHeight));
|
|
painter->drawText(rect, entries[i].name);
|
|
}
|
|
|
|
// For now, place the legend in the top right corner.
|
|
QPointF pos(size.width() - width - 10.0, 10.0);
|
|
setPos(pos);
|
|
}
|