ginebra2/Charms/ObjectCharm.cpp
changeset 9 b39122337a00
equal deleted inserted replaced
7:a1f515018ac1 9:b39122337a00
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 *
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not,
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 */
       
    20 
       
    21 #include "ObjectCharm.h"
       
    22 #include "Utilities.h"
       
    23 
       
    24 #include <QtGui>
       
    25 #include <QDebug>
       
    26 #include <QApplication>
       
    27 #include <QTimer>
       
    28 
       
    29 
       
    30 namespace GVA {
       
    31 
       
    32 // ----------------------------
       
    33 // ObjectCharm
       
    34 // ----------------------------
       
    35 
       
    36 ObjectCharm::ObjectCharm(QObject *object)
       
    37   : m_object(object)
       
    38 {
       
    39     m_object->installEventFilter(this);
       
    40     connect(m_object, SIGNAL(destroyed()), this, SLOT(onObjectDestroyed()));
       
    41 }
       
    42 
       
    43 void ObjectCharm::onObjectDestroyed() {
       
    44     deleteLater();
       
    45 }
       
    46 
       
    47 // ----------------------------
       
    48 // TouchCircleCharm
       
    49 // ----------------------------
       
    50 
       
    51 TouchCircleCharm::TouchCircleCharm(QObject *object, QGraphicsItem *parent)
       
    52     : ObjectCharm(object),
       
    53       m_timer(new QTimer())
       
    54 {
       
    55     m_item = new QGraphicsEllipseItem(QRect(0,0,20,20), parent);
       
    56     m_item->hide();
       
    57     m_item->setPen(QPen(QColor(Qt::black)));
       
    58     m_item->setBrush(QBrush(QColor(200, 0, 0)));
       
    59     m_item->setOpacity(0.7);
       
    60     m_item->setZValue(100);  // make sure it isn't obscured
       
    61 
       
    62     m_timer->setSingleShot(true);
       
    63     connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimer()));
       
    64 }
       
    65 
       
    66 TouchCircleCharm::~TouchCircleCharm() {
       
    67     delete m_item;
       
    68     delete m_timer;
       
    69 }
       
    70 
       
    71 bool TouchCircleCharm::eventFilter(QObject *object, QEvent *event) {
       
    72     switch (event->type()) {
       
    73         case QEvent::GraphicsSceneMousePress: {
       
    74             // Show the item.
       
    75             QGraphicsSceneMouseEvent * me = static_cast<QGraphicsSceneMouseEvent*>(event);
       
    76             m_item->setPos(me->scenePos().x() - m_item->rect().width()/2,
       
    77                            me->scenePos().y() - m_item->rect().height()/2);
       
    78 
       
    79             // Change the color to indicate click.
       
    80             m_item->setBrush(QBrush(QColor(0, 200, 0)));
       
    81 
       
    82             m_item->show();
       
    83             m_timer->stop();
       
    84             break;
       
    85         }
       
    86         case QEvent::GraphicsSceneMouseMove: {
       
    87             if(m_item->isVisible()) {
       
    88                 // Move it to new position.
       
    89                 QGraphicsSceneMouseEvent * me = static_cast<QGraphicsSceneMouseEvent*>(event);
       
    90                 m_item->setPos(me->scenePos().x() - m_item->rect().width()/2,
       
    91                                me->scenePos().y() - m_item->rect().height()/2);
       
    92 
       
    93                 // Change the color to indicate drag.
       
    94                 m_item->setBrush(QBrush(QColor(255, 255, 0)));
       
    95             }
       
    96             break;
       
    97         }
       
    98         case QEvent::GraphicsSceneMouseDoubleClick: {
       
    99             if(m_item->isVisible()) {
       
   100                 // Change the color to indicate double click.
       
   101                 m_item->setBrush(QBrush(QColor(0, 0, 200)));
       
   102 
       
   103                 m_timer->stop();
       
   104             }
       
   105             break;
       
   106         }
       
   107         case QEvent::GraphicsSceneMouseRelease: {
       
   108             if(m_item->isVisible()) {
       
   109                 // Change the color to indicate release.
       
   110                 m_item->setBrush(QBrush(QColor(200, 0, 0)));
       
   111 
       
   112                 // Start timer to hide it.
       
   113                 m_timer->start(2000);
       
   114             }
       
   115             break;
       
   116         }
       
   117 //        case QEvent::GraphicsSceneContextMenu: {
       
   118 //            if(m_item->isVisible()) {
       
   119 //                // Change the color to indicate context event.
       
   120 //                m_item->setBrush(QBrush(QColor(0, 200, 200)));
       
   121 //            }
       
   122 //            break;
       
   123 //        }
       
   124         default:
       
   125             break;
       
   126     }
       
   127     return object->eventFilter(object, event);
       
   128 }
       
   129 
       
   130 void TouchCircleCharm::onTimer() {   // slot
       
   131     m_item->hide();
       
   132 }
       
   133 
       
   134 }   // namespace GVA