servatrice/cockatrice/src/handcounter.cpp
ctrlaltca b29bd9e070
Clang-format (#3028)
* 1/3 Add .clang-format file and travis compilation check

* 2/3 Run clang-format

* 3/3 Fix compilation problems due to include reordering

* 3bis/3 AfterControlStatement: false
2018-01-27 10:41:32 +01:00

49 lines
1.4 KiB
C++

#include "handcounter.h"
#include "cardzone.h"
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QPixmapCache>
HandCounter::HandCounter(QGraphicsItem *parent) : AbstractGraphicsItem(parent), number(0)
{
setCacheMode(DeviceCoordinateCache);
}
HandCounter::~HandCounter()
{
}
void HandCounter::updateNumber()
{
number = static_cast<CardZone *>(sender())->getCards().size();
update();
}
QRectF HandCounter::boundingRect() const
{
return QRectF(0, 0, 72, 72);
}
void HandCounter::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
{
painter->save();
QSize translatedSize = painter->combinedTransform().mapRect(boundingRect()).size().toSize();
QPixmap cachedPixmap;
if (!QPixmapCache::find("handCounter" + QString::number(translatedSize.width()), &cachedPixmap)) {
cachedPixmap = QPixmap("theme:hand").scaled(translatedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QPixmapCache::insert("handCounter" + QString::number(translatedSize.width()), cachedPixmap);
}
painter->resetTransform();
painter->drawPixmap(cachedPixmap.rect(), cachedPixmap, cachedPixmap.rect());
painter->restore();
paintNumberEllipse(number, 24, Qt::white, -1, -1, painter);
}
void HandCounter::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
emit showContextMenu(event->screenPos());
event->accept();
}
}