Minor fixes

This commit is contained in:
Max-Wilhelm Bruker 2010-10-14 15:50:19 +02:00
parent 970da7d214
commit c2d1f151c4
5 changed files with 49 additions and 45 deletions

View file

@ -42,32 +42,46 @@ void AbstractCardItem::pixmapUpdated()
update(); update();
} }
QSizeF AbstractCardItem::getTranslatedSize(QPainter *painter) const
{
return QSizeF(
painter->combinedTransform().map(QLineF(0, 0, boundingRect().width(), 0)).length(),
painter->combinedTransform().map(QLineF(0, 0, 0, boundingRect().height())).length()
);
}
void AbstractCardItem::transformPainter(QPainter *painter, const QSizeF &translatedSize)
{
QRectF totalBoundingRect = painter->combinedTransform().mapRect(boundingRect());
painter->resetTransform();
QTransform pixmapTransform;
pixmapTransform.translate(totalBoundingRect.width() / 2, totalBoundingRect.height() / 2);
pixmapTransform.rotate(tapAngle);
pixmapTransform.translate(-translatedSize.width() / 2, -translatedSize.height() / 2);
painter->setTransform(pixmapTransform);
QFont f;
int fontSize = translatedSize.height() / 6;
if (fontSize < 9)
fontSize = 9;
f.setPixelSize(fontSize);
painter->setFont(f);
}
void AbstractCardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/) void AbstractCardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
{ {
painter->save(); painter->save();
qreal w = painter->combinedTransform().map(QLineF(0, 0, boundingRect().width(), 0)).length(); QSizeF translatedSize = getTranslatedSize(painter);
qreal h = painter->combinedTransform().map(QLineF(0, 0, 0, boundingRect().height())).length();
QSizeF translatedSize(w, h);
QRectF totalBoundingRect = painter->combinedTransform().mapRect(boundingRect()); QRectF totalBoundingRect = painter->combinedTransform().mapRect(boundingRect());
qreal scaleFactor = translatedSize.width() / boundingRect().width();
QPixmap *translatedPixmap = info->getPixmap(translatedSize.toSize()); QPixmap *translatedPixmap = info->getPixmap(translatedSize.toSize());
painter->save(); painter->save();
if (translatedPixmap) { if (translatedPixmap) {
painter->resetTransform(); transformPainter(painter, translatedSize);
QTransform pixmapTransform;
pixmapTransform.translate(totalBoundingRect.width() / 2, totalBoundingRect.height() / 2);
pixmapTransform.rotate(tapAngle);
QPointF transPoint = QPointF(-w / 2, -h / 2);
pixmapTransform.translate(transPoint.x(), transPoint.y());
painter->setTransform(pixmapTransform);
painter->drawPixmap(QPointF(0, 0), *translatedPixmap); painter->drawPixmap(QPointF(0, 0), *translatedPixmap);
} else { } else {
QFont f;
int fontSize = h / 6;
if (fontSize < 9)
fontSize = 9;
f.setPixelSize(fontSize);
painter->setFont(f);
QString colorStr; QString colorStr;
if (!color.isEmpty()) if (!color.isEmpty())
colorStr = color; colorStr = color;
@ -98,25 +112,13 @@ void AbstractCardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *
} }
painter->setBrush(bgColor); painter->setBrush(bgColor);
QPen pen(Qt::black); QPen pen(Qt::black);
pen.setWidth(2);
painter->setPen(pen); painter->setPen(pen);
painter->drawRect(QRectF(1, 1, CARD_WIDTH - 2, CARD_HEIGHT - 2));
painter->drawRect(QRectF(0.5, 0.5, CARD_WIDTH - 1, CARD_HEIGHT - 1));
pen.setWidth(3); transformPainter(painter, translatedSize);
painter->setPen(pen);
painter->drawRect(QRectF(3, 3, CARD_WIDTH - 6, CARD_HEIGHT - 6));
painter->setPen(textColor); painter->setPen(textColor);
painter->drawText(QRectF(2 * scaleFactor, 2 * scaleFactor, translatedSize.width() - 4 * scaleFactor, translatedSize.height() - 4 * scaleFactor), Qt::AlignTop | Qt::AlignLeft | Qt::TextWrapAnywhere, name);
QRectF textRect = painter->combinedTransform().mapRect(QRectF(5, 5, CARD_WIDTH - 15, CARD_HEIGHT - 15));
painter->resetTransform();
QTransform pixmapTransform;
pixmapTransform.translate(totalBoundingRect.width() / 2, totalBoundingRect.height() / 2);
pixmapTransform.rotate(tapAngle);
QPointF transPoint = QPointF(-w / 2, -h / 2);
pixmapTransform.translate(transPoint.x(), transPoint.y());
painter->setTransform(pixmapTransform);
painter->drawText(textRect, Qt::AlignTop | Qt::AlignLeft | Qt::TextWrapAnywhere, name);
} }
painter->restore(); painter->restore();

View file

@ -43,6 +43,8 @@ public:
void setTapped(bool _tapped, bool canAnimate = false); void setTapped(bool _tapped, bool canAnimate = false);
void processHoverEvent(); void processHoverEvent();
protected: protected:
QSizeF getTranslatedSize(QPainter *painter) const;
void transformPainter(QPainter *painter, const QSizeF &translatedSize);
void mousePressEvent(QGraphicsSceneMouseEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverEnterEvent(QGraphicsSceneHoverEvent *event);

View file

@ -2,6 +2,7 @@
#include <QPainter> #include <QPainter>
#include <QMenu> #include <QMenu>
#include <QAction> #include <QAction>
#include <QDebug>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include "carditem.h" #include "carditem.h"
@ -171,16 +172,15 @@ void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
++i; ++i;
} }
if (!pt.isEmpty()) { if (!pt.isEmpty()) {
QFont font("Times"); painter->save();
font.setPixelSize(16); QSizeF translatedSize = getTranslatedSize(painter);
painter->setFont(font); transformPainter(painter, translatedSize);
QPen pen(Qt::white); painter->setBackground(Qt::black);
QBrush brush(Qt::black);
painter->setBackground(brush);
painter->setBackgroundMode(Qt::OpaqueMode); painter->setBackgroundMode(Qt::OpaqueMode);
painter->setPen(pen); painter->setPen(Qt::white);
painter->drawText(QRectF(0, 0, boundingRect().width() - 5, boundingRect().height() - 5), Qt::AlignRight | Qt::AlignBottom, pt); painter->drawText(QRectF(2, 2, translatedSize.width() - 4, translatedSize.height() - 4), Qt::AlignRight | Qt::AlignBottom, pt);
painter->restore();
} }
if (getBeingPointedAt()) if (getBeingPointedAt())
painter->fillRect(boundingRect(), QBrush(QColor(255, 0, 0, 100))); painter->fillRect(boundingRect(), QBrush(QColor(255, 0, 0, 100)));

View file

@ -28,7 +28,7 @@ void PlayerTarget::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*o
QRectF translatedRect = painter->combinedTransform().mapRect(boundingRect()); QRectF translatedRect = painter->combinedTransform().mapRect(boundingRect());
QSize translatedSize = translatedRect.size().toSize(); QSize translatedSize = translatedRect.size().toSize();
QPixmap cachedPixmap; QPixmap cachedPixmap;
const QString cacheKey = "avatar" + QString::number(translatedSize.width()) + "_" + QString::number(fullPixmap.cacheKey()); const QString cacheKey = "avatar" + QString::number(translatedSize.width()) + "_" + QString::number(info->getUserLevel()) + "_" + QString::number(fullPixmap.cacheKey());
#if QT_VERSION >= 0x040600 #if QT_VERSION >= 0x040600
if (!QPixmapCache::find(cacheKey, &cachedPixmap)) { if (!QPixmapCache::find(cacheKey, &cachedPixmap)) {
#else #else

View file

@ -156,7 +156,7 @@ ServerInfo_User *Servatrice::getUserData(const QString &name)
query.prepare("select admin, country, avatar_bmp from " + dbPrefix + "_users where name = :name and active = 1"); query.prepare("select admin, country, avatar_bmp from " + dbPrefix + "_users where name = :name and active = 1");
query.bindValue(":name", name); query.bindValue(":name", name);
if (!execSqlQuery(query)) if (!execSqlQuery(query))
return new ServerInfo_User(name); return new ServerInfo_User(name, ServerInfo_User::IsUser);
if (query.next()) { if (query.next()) {
bool is_admin = query.value(0).toInt(); bool is_admin = query.value(0).toInt();
@ -174,9 +174,9 @@ ServerInfo_User *Servatrice::getUserData(const QString &name)
avatarBmp avatarBmp
); );
} else } else
return new ServerInfo_User(name); return new ServerInfo_User(name, ServerInfo_User::IsUser);
} else } else
return new ServerInfo_User(name); return new ServerInfo_User(name, ServerInfo_User::IsUser);
} }
const QString Servatrice::versionString = "Servatrice 0.20101009"; const QString Servatrice::versionString = "Servatrice 0.20101009";