diff options
author | Ben Boeckel <MathStuf@gmail.com> | 2009-03-19 00:50:59 -0400 |
---|---|---|
committer | Ben Boeckel <MathStuf@gmail.com> | 2009-03-19 00:51:12 -0400 |
commit | dc18b8610e03cbf83f5f58ddece4ad6c1aaf754c (patch) | |
tree | 5bdfdccf2be3514e452663d5cf65b3a5ee21223f /sigmodr | |
parent | ebac51218a9a95e40779f45b7a2a4a37b49c3807 (diff) | |
download | sigen-dc18b8610e03cbf83f5f58ddece4ad6c1aaf754c.tar.gz sigen-dc18b8610e03cbf83f5f58ddece4ad6c1aaf754c.tar.xz sigen-dc18b8610e03cbf83f5f58ddece4ad6c1aaf754c.zip |
Update TrainerItem to use MapItem
Diffstat (limited to 'sigmodr')
-rw-r--r-- | sigmodr/widgets/CMakeLists.txt | 2 | ||||
-rw-r--r-- | sigmodr/widgets/TrainerItem.cpp | 89 | ||||
-rw-r--r-- | sigmodr/widgets/TrainerItem.h | 25 |
3 files changed, 48 insertions, 68 deletions
diff --git a/sigmodr/widgets/CMakeLists.txt b/sigmodr/widgets/CMakeLists.txt index a9b6bbbf..4789f189 100644 --- a/sigmodr/widgets/CMakeLists.txt +++ b/sigmodr/widgets/CMakeLists.txt @@ -40,6 +40,7 @@ set(sigmodrwidgets_HEADERS TileUI.h TimeUI.h TrainerUI.h + TrainerItem.h TypechartModel.h TypeUI.h ValidationDialog.h @@ -80,6 +81,7 @@ set(sigmodrwidgets_SRCS TileUI.cpp TimeUI.cpp TrainerUI.cpp + TrainerItem.cpp TypechartModel.cpp TypeUI.cpp ValidationDialog.cpp diff --git a/sigmodr/widgets/TrainerItem.cpp b/sigmodr/widgets/TrainerItem.cpp index 55da4d10..f7d94f1f 100644 --- a/sigmodr/widgets/TrainerItem.cpp +++ b/sigmodr/widgets/TrainerItem.cpp @@ -19,100 +19,73 @@ #include "TrainerItem.h" // Sigmod includes -#include "../sigmod/MapTrainer.h" -#include "../sigmod/Sigmod.h" -#include "../sigmod/Sprite.h" - -// KDE includes -#include <KColorScheme> +#include <sigmod/MapTrainer.h> // Qt includes #include <QtGui/QPainter> #include <QtGui/QStyle> #include <QtGui/QStyleOptionGraphicsItem> -Sigmodr::TrainerItem::TrainerItem(Sigmod::MapTrainer* trainer, QObject* parent) : - QObject(parent), +using namespace Sigmod; +using namespace Sigmodr::Widgets; + +TrainerItem::TrainerItem(MapTrainer* trainer, QGraphicsScene* parent) : + MapItem(false, parent), m_trainer(trainer) { connect(m_trainer, SIGNAL(changed()), this, SLOT(trainerChanged())); trainerChanged(); setZValue(INT_MAX); - setAcceptHoverEvents(true); + QGraphicsSimpleTextItem* item = new QGraphicsSimpleTextItem(QString::number(m_trainer->id()), this); + QSizeF size = item->boundingRect().size() / 2; + item->setPos(-size.width(), -size.height()); + m_label = new QGraphicsSimpleTextItem(m_trainer->name(), this); + resetLabel(); } -QRectF Sigmodr::TrainerItem::boundingRect() const +QRectF TrainerItem::boundingRect() const { - return m_pixmap.rect(); + return QRect(m_trainer->position(), QSize(32, 32)); } -void Sigmodr::TrainerItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) +void TrainerItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { + Q_UNUSED(option) Q_UNUSED(widget) - if (m_pixmap.isNull()) - return; - if (!(flags() & QGraphicsItem::ItemIsSelectable)) - { - QPixmap temp(m_pixmap.size()); - temp.fill(Qt::transparent); - QPainter p; - p.begin(&temp); - p.setCompositionMode(QPainter::CompositionMode_DestinationIn); - p.fillRect(temp.rect(), QColor(0, 0, 0, 127)); - p.end(); - painter->drawPixmap(0, 0, temp); - } - painter->drawPixmap(0, 0, m_pixmap); - if (option->state & QStyle::State_MouseOver) - { - painter->setPen(QPen(KStatefulBrush(KColorScheme::View, (flags() & QGraphicsItem::ItemIsSelectable) ? KColorScheme::ActiveText : KColorScheme::InactiveText).brush((flags() & QGraphicsItem::ItemIsSelectable) ? QPalette::Active : QPalette::Inactive), 1)); - painter->drawText(m_pixmap.rect(), Qt::AlignHCenter | Qt::AlignVCenter, m_trainer->name()); - } - if (option->state & QStyle::State_HasFocus) - { - painter->setPen(QPen(KStatefulBrush(KColorScheme::Selection, KColorScheme::FocusColor).brush(QPalette::Active), 6)); - painter->drawRect(m_pixmap.rect()); - } - else if (option->state & QStyle::State_Selected) - { - painter->setPen(QPen(KStatefulBrush(KColorScheme::Selection, KColorScheme::ActiveBackground).brush(QPalette::Active), 6)); - painter->drawRect(m_pixmap.rect()); - } - else if ((option->state & QStyle::State_MouseOver) && (flags() & QGraphicsItem::ItemIsSelectable)) - { - painter->setPen(QPen(KStatefulBrush(KColorScheme::Selection, KColorScheme::HoverColor).brush(QPalette::Active), 6)); - painter->drawRect(m_pixmap.rect()); - } + makeTransparent(painter, 127); + painter->setBrush(QBrush(Qt::red)); + painter->setPen(QPen(Qt::black, 2)); + painter->drawRect(boundingRect()); } -void Sigmodr::TrainerItem::changeSprite(const int spriteId) -{ - const Sigmod::Sprite* sprite = m_trainer->sigmod()->spriteById(spriteId); - if (sprite) - m_pixmap.loadFromData(sprite->sprite()); - update(); -} - -void Sigmodr::TrainerItem::mousePressEvent(QGraphicsSceneMouseEvent* event) +void TrainerItem::mousePressEvent(QGraphicsSceneMouseEvent* event) { QGraphicsItem::mousePressEvent(event); update(); } -void Sigmodr::TrainerItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) +void TrainerItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) { QGraphicsItem::mouseMoveEvent(event); m_trainer->setPosition(scenePos().toPoint()); } -void Sigmodr::TrainerItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) +void TrainerItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) { QGraphicsItem::mouseReleaseEvent(event); update(); } -void Sigmodr::TrainerItem::trainerChanged() +void TrainerItem::trainerChanged() { setPos(m_trainer->position()); + m_label->setText(m_trainer->name()); + resetLabel(); update(); } + +void TrainerItem::resetLabel() +{ + QSizeF size = m_label->boundingRect().size() / 2 - QSize(32, 32) / 2; + m_label->setPos(-size.width(), -size.height()); +} diff --git a/sigmodr/widgets/TrainerItem.h b/sigmodr/widgets/TrainerItem.h index 5733194b..719cc431 100644 --- a/sigmodr/widgets/TrainerItem.h +++ b/sigmodr/widgets/TrainerItem.h @@ -1,5 +1,5 @@ /* - * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,11 +15,11 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef SIGMODR_TRAINERITEM -#define SIGMODR_TRAINERITEM +#ifndef SIGMODRWIDGETS_TRAINERITEM +#define SIGMODRWIDGETS_TRAINERITEM -// Qt includes -#include <QtGui/QGraphicsItem> +// Sigmodr widget includes +#include "MapItem.h" // Forward declarations namespace Sigmod @@ -29,28 +29,33 @@ class MapTrainer; namespace Sigmodr { -class TrainerItem : public QObject, public QGraphicsItem +namespace Widgets +{ +class SIGMODRWIDGETS_NO_EXPORT TrainerItem : public MapItem { Q_OBJECT public: - TrainerItem(Sigmod::MapTrainer* trainer, QObject* parent); + TrainerItem(Sigmod::MapTrainer* trainer, QGraphicsScene* parent); QRectF boundingRect() const; void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0); - public slots: - void changeSprite(const int spriteId); protected: void mousePressEvent(QGraphicsSceneMouseEvent* event); void mouseMoveEvent(QGraphicsSceneMouseEvent* event); void mouseReleaseEvent(QGraphicsSceneMouseEvent* event); + + void resizeBy(const QPointF& size); protected slots: void trainerChanged(); private: - QPixmap m_pixmap; + void resetLabel(); + Sigmod::MapTrainer* m_trainer; + QGraphicsSimpleTextItem* m_label; }; } +} #endif |