69 lines
1.7 KiB
C++
69 lines
1.7 KiB
C++
#include "StatusPill.h"
|
|
|
|
#include <QGuiApplication>
|
|
#include <QHBoxLayout>
|
|
#include <QLabel>
|
|
#include <QPainter>
|
|
#include <QPixmap>
|
|
#include <QRectF>
|
|
|
|
namespace
|
|
{
|
|
|
|
// Diameter of the status dot, in device-independent pixels.
|
|
const int kDotSizePx = 8;
|
|
|
|
QPixmap renderDot(const QColor& fill, const QColor& outline)
|
|
{
|
|
const qreal dpr = qApp ? qApp->devicePixelRatio() : 1.0;
|
|
QPixmap pixmap(static_cast<int>(kDotSizePx * dpr),
|
|
static_cast<int>(kDotSizePx * dpr));
|
|
pixmap.setDevicePixelRatio(dpr);
|
|
pixmap.fill(Qt::transparent);
|
|
|
|
QPainter painter(&pixmap);
|
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
|
painter.setPen(outline.isValid() ? QPen(outline) : QPen(Qt::NoPen));
|
|
painter.setBrush(fill);
|
|
// Inset by half the pen width so the outline stays inside the pixmap.
|
|
painter.drawEllipse(QRectF(0.5, 0.5, kDotSizePx - 1.0, kDotSizePx - 1.0));
|
|
return pixmap;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
StatusPill::StatusPill(QWidget* parent)
|
|
: QWidget(parent)
|
|
{
|
|
QHBoxLayout* layout = new QHBoxLayout(this);
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
layout->setSpacing(4);
|
|
|
|
m_dotLabel = new QLabel(this);
|
|
m_captionLabel = new QLabel(this);
|
|
|
|
layout->addWidget(m_dotLabel);
|
|
layout->addWidget(m_captionLabel);
|
|
|
|
hide();
|
|
}
|
|
|
|
void StatusPill::setStatus(const QColor& dotColor, const QColor& outlineColor,
|
|
const QString& caption)
|
|
{
|
|
if (dotColor.isValid())
|
|
{
|
|
m_dotLabel->setPixmap(renderDot(dotColor, outlineColor));
|
|
m_dotLabel->show();
|
|
}
|
|
else
|
|
{
|
|
m_dotLabel->hide();
|
|
}
|
|
|
|
m_captionLabel->setText(caption);
|
|
m_captionLabel->setVisible(!caption.isEmpty());
|
|
setVisible(dotColor.isValid() || !caption.isEmpty());
|
|
}
|