2021-01-01 21:03:26 +00:00
|
|
|
#include "informationbox.h"
|
|
|
|
#include "statscolors.h"
|
2021-01-13 14:17:54 +00:00
|
|
|
#include "statsview.h"
|
2021-01-01 21:03:26 +00:00
|
|
|
#include "zvalues.h"
|
|
|
|
|
|
|
|
#include <QFontMetrics>
|
|
|
|
|
|
|
|
static const int informationBorder = 2;
|
2021-01-03 11:49:26 +00:00
|
|
|
static const double informationBorderRadius = 4.0; // Radius of rounded corners
|
2021-01-01 21:03:26 +00:00
|
|
|
static const int distanceFromPointer = 10; // Distance to place box from mouse pointer or scatter item
|
|
|
|
|
2021-01-13 14:17:54 +00:00
|
|
|
InformationBox::InformationBox(StatsView &v) :
|
2021-01-13 15:19:27 +00:00
|
|
|
ChartRectItem(v, ChartZValue::InformationBox,
|
2021-02-16 16:05:39 +00:00
|
|
|
QPen(v.getCurrentTheme().informationBorderColor, informationBorder),
|
|
|
|
QBrush(v.getCurrentTheme().informationColor), informationBorderRadius),
|
|
|
|
theme(v.getCurrentTheme()),
|
2021-03-05 08:42:10 +00:00
|
|
|
width(0.0),
|
|
|
|
height(0.0)
|
2021-01-01 21:03:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void InformationBox::setText(const std::vector<QString> &text, QPointF pos)
|
|
|
|
{
|
2021-01-13 14:17:54 +00:00
|
|
|
QFontMetrics fm(font);
|
|
|
|
double fontHeight = fm.height();
|
2021-01-01 21:03:26 +00:00
|
|
|
|
2021-01-13 14:17:54 +00:00
|
|
|
std::vector<double> widths;
|
|
|
|
widths.reserve(text.size());
|
|
|
|
width = 0.0;
|
2021-01-01 21:03:26 +00:00
|
|
|
for (const QString &s: text) {
|
2021-01-13 14:17:54 +00:00
|
|
|
widths.push_back(static_cast<double>(fm.size(Qt::TextSingleLine, s).width()));
|
|
|
|
width = std::max(width, widths.back());
|
2021-01-01 21:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
width += 4.0 * informationBorder;
|
2021-01-13 14:17:54 +00:00
|
|
|
height = widths.size() * fontHeight + 4.0 * informationBorder;
|
|
|
|
|
|
|
|
ChartRectItem::resize(QSizeF(width, height));
|
2021-01-01 21:03:26 +00:00
|
|
|
|
2021-02-16 16:05:39 +00:00
|
|
|
painter->setPen(QPen(theme.darkLabelColor)); // QPainter uses QPen to set text color!
|
2021-01-13 14:17:54 +00:00
|
|
|
double y = 2.0 * informationBorder;
|
|
|
|
for (size_t i = 0; i < widths.size(); ++i) {
|
|
|
|
QRectF rect(2.0 * informationBorder, y, widths[i], fontHeight);
|
|
|
|
painter->drawText(rect, text[i]);
|
|
|
|
y += fontHeight;
|
|
|
|
}
|
2021-01-01 21:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InformationBox::setPos(QPointF pos)
|
|
|
|
{
|
2021-01-13 14:17:54 +00:00
|
|
|
QSizeF size = sceneSize();
|
2021-01-01 21:03:26 +00:00
|
|
|
|
|
|
|
double x = pos.x() + distanceFromPointer;
|
2021-01-13 14:17:54 +00:00
|
|
|
if (x + width >= size.width()) {
|
|
|
|
if (pos.x() - width >= 0.0)
|
2021-01-06 13:33:06 +00:00
|
|
|
x = pos.x() - width;
|
|
|
|
else
|
|
|
|
x = pos.x() - width / 2.0;
|
|
|
|
}
|
2021-01-01 21:03:26 +00:00
|
|
|
double y = pos.y() + distanceFromPointer;
|
2021-01-13 14:17:54 +00:00
|
|
|
if (y + height >= size.height()) {
|
|
|
|
if (pos.y() - height >= 0.0)
|
2021-01-06 13:33:06 +00:00
|
|
|
y = pos.y() - height;
|
|
|
|
else
|
|
|
|
y = pos.y() - height / 2.0;
|
|
|
|
}
|
2021-01-01 21:03:26 +00:00
|
|
|
|
2021-01-13 14:17:54 +00:00
|
|
|
ChartRectItem::setPos(QPointF(x, y));
|
2021-01-01 21:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to stay within three-thirds of the chart height
|
|
|
|
int InformationBox::recommendedMaxLines() const
|
|
|
|
{
|
|
|
|
QFontMetrics fm(font);
|
2021-01-13 14:17:54 +00:00
|
|
|
int maxHeight = static_cast<int>(sceneSize().height());
|
2021-01-01 21:03:26 +00:00
|
|
|
return maxHeight * 2 / fm.height() / 3;
|
|
|
|
}
|