• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kblackbox

kbbgraphicsitemonbox.cpp

Go to the documentation of this file.
00001 //
00002 // KBlackBox
00003 //
00004 // A simple game inspired by an emacs module
00005 //
00006 /***************************************************************************
00007  *   Copyright (c) 1999-2000, Robert Cimrman                               *
00008  *   cimrman3@students.zcu.cz                                              *
00009  *                                                                         *
00010  *   Copyright (c) 2007, Nicolas Roffet                                    *
00011  *   nicolas-kde@roffet.com                                                *
00012  *                                                                         *
00013  *                                                                         *
00014  *   This program is free software; you can redistribute it and/or modify  *
00015  *   it under the terms of the GNU General Public License as published by  *
00016  *   the Free Software Foundation; either version 2 of the License, or     *
00017  *   (at your option) any later version.                                   *
00018  *                                                                         *
00019  *   This program is distributed in the hope that it will be useful,       *
00020  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00021  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00022  *   GNU General Public License for more details.                          *
00023  *                                                                         *
00024  *   You should have received a copy of the GNU General Public License     *
00025  *   along with this program; if not, write to the                         *
00026  *   Free Software Foundation, Inc.,                                       *
00027  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA               *
00028  ***************************************************************************/
00029 
00030 #include "kbbgraphicsitemonbox.h"
00031 
00032 
00033 
00034 #include <QGraphicsScene>
00035 #include <QGraphicsSceneMouseEvent>
00036 
00037 
00038 #include "kbbgraphicsitem.h"
00039 #include "kbbgraphicsiteminteractioninfo.h"
00040 #include "kbbitemwithposition.h"
00041 #include "kbbscalablegraphicwidget.h"
00042 #include "kbbthememanager.h"
00043 
00044 
00045 
00046 //
00047 // Constructor / Destructor
00048 //
00049 
00050 KBBGraphicsItemOnBox::KBBGraphicsItemOnBox(KBBScalableGraphicWidget::itemType itemType, KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager, const int boxPosition, const int columns, const int rows) : KBBGraphicsItem(itemType, parent->scene(), themeManager), KBBItemWithPosition()
00051 {
00052     m_widget = parent;
00053     m_columns = columns;
00054     m_rows = rows;
00055     m_itemType = itemType;
00056     
00057     setBoxPosition(boxPosition);
00058 
00059     if (isMovable()) {
00060         setAcceptDrops(true);
00061         setFlag(QGraphicsItem::ItemIsMovable);
00062     }
00063 }
00064 
00065 
00066 
00067 //
00068 // Public
00069 //
00070 
00071 const int KBBGraphicsItemOnBox::position ()
00072 {
00073     return m_boxPosition;
00074 }
00075 
00076 
00077 
00078 //
00079 // Protected
00080 //
00081 
00082 void KBBGraphicsItemOnBox::removeInteractionInfos()
00083 {
00084 }
00085 
00086 
00087 
00088 //
00089 // Private
00090 //
00091 
00092 int KBBGraphicsItemOnBox::boxPosition(qreal x, qreal y)
00093 {
00094     int r = (int)((y - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO);
00095     int c = (int)((x - KBBScalableGraphicWidget::BORDER_SIZE)/KBBScalableGraphicWidget::RATIO);
00096 
00097     if ((r<0) || (r>=m_rows) || (c<0) || (c>=m_columns)) {
00098         if (m_boxPosition>=m_columns*m_rows)
00099             // Item is outside of the board
00100             return m_boxPosition;
00101         else
00102             return NO_POSITION;
00103     } else
00104         return c+r*m_columns;
00105 }
00106 
00107 
00108 bool KBBGraphicsItemOnBox::isMovable()
00109 {
00110     return ((m_itemType==KBBScalableGraphicWidget::playerBall) || (m_itemType==KBBScalableGraphicWidget::unsureBall) || (m_itemType==KBBScalableGraphicWidget::markerNothing));
00111 }
00112 
00113 
00114 void KBBGraphicsItemOnBox::mousePressEvent (QGraphicsSceneMouseEvent* event)
00115 {
00116     m_dragXPos = x();
00117     m_dragYPos = y();
00118     m_dragX = event->scenePos().x();
00119     m_dragY = event->scenePos().y();
00120 
00121     if (isMovable()) {
00122         setCursor(Qt::ClosedHandCursor);
00123         removeInteractionInfos();
00124     }
00125 }
00126 
00127 
00128 void KBBGraphicsItemOnBox::mouseReleaseEvent (QGraphicsSceneMouseEvent* event)
00129 {
00130     // Let's Qt handle the drag en drop
00131     QGraphicsItem::mouseReleaseEvent(event);
00132 
00133     qreal dropX = event->scenePos().x();
00134     qreal dropY = event->scenePos().y();
00135 
00136     if ((dropX==m_dragX) && (dropY==m_dragY)) {
00137         setCursor(Qt::ArrowCursor);
00138         if ((position()!=NO_POSITION) && (position()<(m_columns*m_rows)))
00139             m_widget->mouseBoxClick(event->button(), position());
00140     } else if (isMovable()) {
00141         setCursor(Qt::ArrowCursor);
00142 
00143         if ((boxPosition(dropX, dropY)==NO_POSITION) || (boxPosition(dropX, dropY)==boxPosition(m_dragX, m_dragY)) || (boxPosition(dropX, dropY)>=m_columns*m_rows))
00144             // Cancel move
00145             setPos(m_dragXPos, m_dragYPos);
00146         else {
00147             if (m_itemType==KBBScalableGraphicWidget::markerNothing)
00148                 setBoxPosition(m_widget->moveMarkerNothing(boxPosition(m_dragX, m_dragY), boxPosition(dropX, dropY)));
00149             else {
00150                 int newPos = m_widget->positionAfterMovingBall(boxPosition(m_dragX, m_dragY), boxPosition(dropX, dropY));
00151 
00152                 // if we do move from outside the board. Because in this case and if the move is OK, we will destroy ourself to put a "real" new ball over the board... So we cannot do anything else more after calling m_widget->moveBall()...
00153                 if ((m_boxPosition==NO_POSITION) || (m_boxPosition>=(m_columns*m_rows))) {
00154                     if (newPos==m_boxPosition)
00155                         setPos(m_dragXPos, m_dragYPos);
00156                     else
00157                         m_widget->moveBall(boxPosition(m_dragX, m_dragY), boxPosition(dropX, dropY));
00158                 } else
00159                     setBoxPosition(m_widget->moveBall(boxPosition(m_dragX, m_dragY), boxPosition(dropX, dropY)));
00160             }
00161         }
00162     }
00163 }
00164 
00165 
00166 void KBBGraphicsItemOnBox::setBoxPosition(int boxPosition)
00167 {
00168     m_boxPosition = boxPosition;
00169 
00170     if ((boxPosition!=NO_POSITION) && (boxPosition<m_columns*m_rows)) {
00171         QPointF p((qreal) (KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(boxPosition % m_columns)), (qreal) (KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(boxPosition / m_columns)));
00172         setPos(p);
00173     }
00174 }

kblackbox

Skip menu "kblackbox"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal