$search
00001 #include "arenasceneelement.h" 00002 00003 #include "../model/arenaelement.h" 00004 #include "../model/arenaelementtype.h" 00005 00006 #include "arenascene.h" 00007 00008 #include <QDebug> 00009 #include <QMimeData> 00010 #include <QDrag> 00011 #include <QPixmap> 00012 #include <QCursor> 00013 #include <QGraphicsSceneMouseEvent> 00014 #include <QPainter> 00015 #include <QStyleOptionGraphicsItem> 00016 00017 ArenaSceneElement::ArenaSceneElement(ArenaElement *element) 00018 : m_element(element) 00019 { 00020 setPixmap(element->type()->pixmap()); 00021 m_isEditorSample = false; 00022 setFlag(QGraphicsItem::ItemIsMovable, true); 00023 setFlag(QGraphicsItem::ItemIsSelectable, true); 00024 00025 connect(element, SIGNAL(posChanged(ArenaElement*, QPoint)), 00026 SLOT(slotPosChanged(ArenaElement*, QPoint))); 00027 connect(element, SIGNAL(rotationChanged(ArenaElement*, int)), 00028 SLOT(slotRotationChanged(ArenaElement*, int))); 00029 00030 //setTransformOriginPoint(pixmap().width() / 2.0, pixmap().width() / 2.0); 00031 00032 #if 0 00033 switch (element->type()->type()) 00034 { 00035 case ArenaElementType::WallType: 00036 //setTransformOriginPoint(-42.5, 12.5); 00037 break; 00038 case ArenaElementType::FloorType: 00039 case ArenaElementType::MountableItemType: 00040 case ArenaElementType::ItemType: 00041 setTransformOriginPoint(pixmap().width() / 2.0, pixmap().width() / 2.0); 00042 break; 00043 } 00044 #endif 00045 00046 slotPosChanged(element, element->pos()); 00047 slotRotationChanged(element, element->rotation()); 00048 } 00049 00050 void ArenaSceneElement::setIsEditorSample(bool isEditorSample) 00051 { 00052 m_isEditorSample = isEditorSample; 00053 setOffset(gridOffset()); 00054 } 00055 00056 QPointF ArenaSceneElement::gridOffset() 00057 { 00058 if (m_isEditorSample) 00059 return QPointF(0, 0); 00060 00061 // Offset of the pixmap before the rotation is performed! 00062 QPointF offset(-pixmap().width() / 2.0, -pixmap().height() / 2.0); 00063 00064 switch(m_element->type()->type()) 00065 { 00066 case ArenaElementType::WallType: 00067 offset += QPointF(0, -CELL_SIZE / 2); 00068 break; 00069 case ArenaElementType::FloorType: 00070 case ArenaElementType::MountableItemType: 00071 case ArenaElementType::ItemType: 00072 break; 00073 } 00074 00075 return offset; 00076 } 00077 00078 void ArenaSceneElement::slotRotationChanged(ArenaElement *element, int rotation) 00079 { 00080 setOffset(gridOffset()); 00081 setRotation(rotation); 00082 } 00083 00084 void ArenaSceneElement::mousePressEvent(QGraphicsSceneMouseEvent *event) 00085 { 00086 if (m_isEditorSample) 00087 { 00088 QMimeData *data = new QMimeData; 00089 data->setData("type", m_element->type()->name().toAscii()); 00090 data->setData("rotation", QString::number(m_element->rotation()).toAscii()); 00091 QDrag *drag = new QDrag(event->widget()); 00092 drag->setMimeData(data); 00093 drag->start(); 00094 00095 setCursor(Qt::OpenHandCursor); 00096 } 00097 00098 // Z value 1 is used to put the item being moved on top 00099 setZValue(1); 00100 event->accept(); 00101 QGraphicsPixmapItem::mousePressEvent(event); 00102 } 00103 00104 void ArenaSceneElement::slotPosChanged(ArenaElement *element, QPoint pos) 00105 { 00106 Q_ASSERT(element == m_element); 00107 00108 QPointF newPos = ArenaScene::gridToScene(pos + element->itemOffset()); 00109 setPos(newPos); 00110 }