UBGraphicsCurtainItem.cpp 4.91 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2 3 4 5
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Claudio Valerio's avatar
Claudio Valerio committed
6
 *
Claudio Valerio's avatar
Claudio Valerio committed
7 8 9 10 11 12 13
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 */

#include "UBGraphicsCurtainItem.h"

#include <QtGui>

#include "domain/UBGraphicsScene.h"

#include "UBGraphicsCurtainItemDelegate.h"
#include "core/UBApplication.h"
#include "core/UBApplicationController.h"
#include "core/UBDisplayManager.h" // TODO UB 4.x clean this dependency
#include "core/UBSettings.h"
#include "board/UBBoardController.h"
#include "board/UBBoardView.h"

30 31
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
const QColor                        UBGraphicsCurtainItem::sDrawColor = Qt::white;
const QColor          UBGraphicsCurtainItem::sDarkBackgroundDrawColor = Qt::black;
const QColor               UBGraphicsCurtainItem::sOpaqueControlColor = QColor(191,191,191,255);
const QColor UBGraphicsCurtainItem::sDarkBackgroundOpaqueControlColor = QColor(63,63,63,255);

UBGraphicsCurtainItem::UBGraphicsCurtainItem(QGraphicsItem* parent)
    : QGraphicsRectItem(parent)
    , mDelegate(0)
{
    mDelegate = new UBGraphicsCurtainItemDelegate(this, 0);
    mDelegate->init();
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);

#if QT_VERSION >= 0x040600 // needs Qt 4.6.0 or better
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
#endif

    setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Tool);
    setPen(Qt::NoPen);
}

UBGraphicsCurtainItem::~UBGraphicsCurtainItem()
{
    delete mDelegate;
}

QVariant UBGraphicsCurtainItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == QGraphicsItem::ItemSelectedHasChanged && QGraphicsRectItem::scene() && isSelected())
        setZValue(UBGraphicsScene::toolLayerStart + UBGraphicsScene::toolOffsetCurtain);

    QVariant newValue = value;

    if (mDelegate)
    {
        newValue = mDelegate->itemChange(change, value);
    }

    return QGraphicsRectItem::itemChange(change, newValue);
}

void UBGraphicsCurtainItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (mDelegate->mousePressEvent(event))
    {
        //NOOP
    }
    else
    {
        QGraphicsRectItem::mousePressEvent(event);
    }
}

void UBGraphicsCurtainItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (mDelegate->mouseMoveEvent(event))
    {
        // NOOP;
    }
    else
    {
        QGraphicsRectItem::mouseMoveEvent(event);
    }
}

void UBGraphicsCurtainItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    mDelegate->mouseReleaseEvent(event);
    QGraphicsRectItem::mouseReleaseEvent(event);
}


void UBGraphicsCurtainItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QColor color = drawColor();

    if(widget == UBApplication::boardController->controlView()->viewport())
    {
        color = UBSettings::paletteColor;
        if(!UBApplication::applicationController->displayManager()->hasDisplay())
        {
            color = opaqueControlColor();
        }
    }

    // Never draw the rubber band, we draw our custom selection with the DelegateFrame
    QStyleOptionGraphicsItem styleOption = QStyleOptionGraphicsItem(*option);
    styleOption.state &= ~QStyle::State_Selected;

    painter->fillRect(rect(), color);
}


UBItem* UBGraphicsCurtainItem::deepCopy() const
{
   UBGraphicsCurtainItem* copy = new UBGraphicsCurtainItem();

   copy->setRect(this->rect());
   copy->setPos(this->pos());
   copy->setBrush(this->brush());
   copy->setPen(this->pen());
   copy->setZValue(this->zValue());
   copy->setTransform(this->transform());
   copy->setFlag(QGraphicsItem::ItemIsMovable, true);
   copy->setFlag(QGraphicsItem::ItemIsSelectable, true);
   copy->setData(UBGraphicsItemData::ItemLayerType, this->data(UBGraphicsItemData::ItemLayerType));

   // TODO UB 4.7 ... complete all members ?

   return copy;
}


QColor UBGraphicsCurtainItem::drawColor() const
{
    UBGraphicsScene* pScene = static_cast<UBGraphicsScene*>(QGraphicsRectItem::scene());
    return pScene->isDarkBackground() ? sDarkBackgroundDrawColor : sDrawColor;
}


QColor UBGraphicsCurtainItem::opaqueControlColor() const
{
    UBGraphicsScene* pScene = static_cast<UBGraphicsScene*>(QGraphicsRectItem::scene());
    return pScene->isDarkBackground() ? sDarkBackgroundOpaqueControlColor : sOpaqueControlColor;
}


void UBGraphicsCurtainItem::remove()
{
    if (mDelegate)
        mDelegate->remove(true);
}


void UBGraphicsCurtainItem::triggerRemovedSignal()
{
    emit removed();
}