UBGraphicsTextItem.cpp 8.65 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
 */

#include <QtGui>
#include "UBGraphicsTextItem.h"
#include "UBGraphicsTextItemDelegate.h"
#include "UBGraphicsScene.h"
#include "UBGraphicsDelegateFrame.h"

#include "core/UBApplication.h"
#include "board/UBBoardController.h"
#include "board/UBBoardView.h"
#include "core/UBSettings.h"

27
#include "core/memcheck.h"
Claudio Valerio's avatar
Claudio Valerio committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

QColor UBGraphicsTextItem::lastUsedTextColor;

UBGraphicsTextItem::UBGraphicsTextItem(QGraphicsItem * parent)
    : QGraphicsTextItem(parent)
    , mMultiClickState(0)
    , mLastMousePressTime(QTime::currentTime())
{
    mDelegate = new UBGraphicsTextItemDelegate(this, 0);
    mDelegate->init();

    mDelegate->frame()->setOperationMode(UBGraphicsDelegateFrame::Resizing);

    mTypeTextHereLabel = tr("<Type Text Here>");

Ivan Ilin's avatar
Ivan Ilin committed
43 44 45 46 47 48 49 50 51 52
//    if (lastUsedTextColor.isValid())
//    {
//        setDefaultTextColor(lastUsedTextColor);
//        setColorOnDarkBackground(lastUsedTextColor);
//        setColorOnLightBackground(lastUsedTextColor);
//    }
//    else
//    {
//        QColor colorOnDarkBG = UBApplication::boardController->penColorOnDarkBackground();
//        QColor colorOnLightBG = UBApplication::boardController->penColorOnLightBackground();
Claudio Valerio's avatar
Claudio Valerio committed
53

Ivan Ilin's avatar
Ivan Ilin committed
54 55 56
//        setColorOnDarkBackground(colorOnDarkBG);
//        setColorOnLightBackground(colorOnLightBG);
//    }
Claudio Valerio's avatar
Claudio Valerio committed
57 58

    setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Object);
59
//    setData(UBGraphicsItemData::ItemEditable, QVariant(true));
Claudio Valerio's avatar
Claudio Valerio committed
60 61

    setFlag(QGraphicsItem::ItemIsSelectable, true);
62
//    setFlag(QGraphicsItem::ItemIsMovable, true);
Claudio Valerio's avatar
Claudio Valerio committed
63 64 65 66 67 68 69
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

    setTextInteractionFlags(Qt::TextEditorInteraction);

    connect(document(), SIGNAL(contentsChanged()), mDelegate, SLOT(contentsChanged()));
    connect(document(), SIGNAL(undoCommandAdded()), this, SLOT(undoCommandAdded()));

70 71
    connect(document()->documentLayout(), SIGNAL(documentSizeChanged(const QSizeF &)),
            this, SLOT(documentSizeChanged(const QSizeF &)));
Claudio Valerio's avatar
Claudio Valerio committed
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

}

UBGraphicsTextItem::~UBGraphicsTextItem()
{
    if (mDelegate)
    {
        delete mDelegate;
    }
}

QVariant UBGraphicsTextItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (QGraphicsItem::ItemSelectedChange == change)
    {
        bool selected = value.toBool();

        if (selected)
        {
            setTextInteractionFlags(Qt::TextEditorInteraction);
        }
        else
        {
            QTextCursor tc = textCursor();
            tc.clearSelection();
            setTextCursor(tc);
            setTextInteractionFlags(Qt::NoTextInteraction);
        }
    }

    QVariant newValue = value;

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

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

void UBGraphicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
112 113 114 115 116 117
    if (mDelegate)
        mDelegate->mousePressEvent(event);

    if (!data(UBGraphicsItemData::ItemEditable).toBool())
        return;

Claudio Valerio's avatar
Claudio Valerio committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    int elapsed = mLastMousePressTime.msecsTo(QTime::currentTime());

    if (elapsed < UBApplication::app()->doubleClickInterval())
    {
        mMultiClickState++;
        if (mMultiClickState > 3)
            mMultiClickState = 1;
    }
    else
    {
        mMultiClickState = 1;
    }

    mLastMousePressTime = QTime::currentTime();

    if (mMultiClickState == 1)
    {
135
//        setTextInteractionFlags(Qt::TextEditorInteraction);
Claudio Valerio's avatar
Claudio Valerio committed
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        QGraphicsTextItem::mousePressEvent(event);
        setFocus();
    }
    else if (mMultiClickState == 2)
    {
        QTextCursor tc= textCursor();
        tc.select(QTextCursor::WordUnderCursor);
        setTextCursor(tc);
    }
    else if (mMultiClickState == 3)
    {
        QTextCursor tc= textCursor();
        tc.select(QTextCursor::Document);
        setTextCursor(tc);
    }
    else
    {
        mMultiClickState = 0;
    }
}

void UBGraphicsTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (!mDelegate || !mDelegate->mouseMoveEvent(event))
    {
        QGraphicsTextItem::mouseMoveEvent(event);
    }
}

void UBGraphicsTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (mMultiClickState == 1)
    {
        if (mDelegate)
            mDelegate->mouseReleaseEvent(event);

        QGraphicsTextItem::mouseReleaseEvent(event);
    }
    else
    {
        event->accept();
    }
}

void UBGraphicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QColor color = UBSettings::settings()->isDarkBackground() ? mColorOnDarkBackground : mColorOnLightBackground;
    setDefaultTextColor(color);

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

    QGraphicsTextItem::paint(painter, &styleOption, widget);

    if (widget == UBApplication::boardController->controlView()->viewport() &&
            !isSelected() && toPlainText().isEmpty())
    {
        painter->setFont(font());
        painter->setPen(UBSettings::paletteColor);
        painter->drawText(boundingRect(), Qt::AlignCenter, mTypeTextHereLabel);
    }
}


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

Ivan Ilin's avatar
Ivan Ilin committed
206
    copy->setHtml(toHtml());
Claudio Valerio's avatar
Claudio Valerio committed
207
    copy->setPos(this->pos());
208 209
//    copy->setZValue(this->zValue());
    UBGraphicsItem::assignZValue(copy, this->zValue());
Claudio Valerio's avatar
Claudio Valerio committed
210 211 212 213 214
    copy->setTransform(this->transform());
    copy->setFlag(QGraphicsItem::ItemIsMovable, true);
    copy->setFlag(QGraphicsItem::ItemIsSelectable, true);
    copy->setData(UBGraphicsItemData::ItemLayerType, this->data(UBGraphicsItemData::ItemLayerType));
    copy->setData(UBGraphicsItemData::ItemLocked, this->data(UBGraphicsItemData::ItemLocked));
Ivan Ilin's avatar
Ivan Ilin committed
215 216 217 218 219
    copy->setData(UBGraphicsItemData::ItemEditable, data(UBGraphicsItemData::ItemEditable).toBool());
//    copy->setDefaultTextColor(this->defaultTextColor());
//    copy->setFont(this->font());
//    copy->setColorOnDarkBackground(this->colorOnDarkBackground());
//    copy->setColorOnLightBackground(this->colorOnLightBackground());
Claudio Valerio's avatar
Claudio Valerio committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    copy->setTextWidth(this->textWidth());
    copy->setTextHeight(this->textHeight());

    copy->setSourceUrl(this->sourceUrl());

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

   return copy;
}


QRectF UBGraphicsTextItem::boundingRect() const
{
    qreal width = textWidth();
    qreal height = textHeight();
    return QRectF(QPointF(), QSizeF(width, height));
}


QPainterPath UBGraphicsTextItem::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}

void UBGraphicsTextItem::setTextWidth(qreal width)
{
    QFontMetrics fm(font());
    qreal strictMin = fm.height();
    qreal newWidth = qMax(strictMin, width);

    if (toPlainText().isEmpty())
    {
        qreal minWidth = fm.width(mTypeTextHereLabel);
        newWidth = qMax(minWidth, newWidth);
    }

    QGraphicsTextItem::setTextWidth(newWidth);
}


void UBGraphicsTextItem::setTextHeight(qreal height)
{
    QFontMetrics fm(font());
    qreal minHeight = fm.height() + document()->documentMargin() * 2;
    mTextHeight = qMax(minHeight, height);
    update();
shibakaneki's avatar
shibakaneki committed
268
    setFocus();
Claudio Valerio's avatar
Claudio Valerio committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
}


qreal UBGraphicsTextItem::textHeight() const
{
    return mTextHeight;
}


void UBGraphicsTextItem::contentsChanged()
{
    if (scene())
    {
        scene()->setModified(true);
    }

    if (toPlainText().isEmpty())
    {
        setTextWidth(textWidth());
    }
}


UBGraphicsScene* UBGraphicsTextItem::scene()
{
    return static_cast<UBGraphicsScene*>(QGraphicsItem::scene());
}


void UBGraphicsTextItem::resize(qreal w, qreal h)
{
    setTextWidth(w);
    setTextHeight(h);

    if (mDelegate)
        mDelegate->positionHandles();
}


QSizeF UBGraphicsTextItem::size() const
{
    return QSizeF(textWidth(), textHeight());
}


void UBGraphicsTextItem::undoCommandAdded()
{
    emit textUndoCommandAdded(this);
}


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

void UBGraphicsTextItem::documentSizeChanged(const QSizeF & newSize)
{
    resize(newSize.width(), newSize.height());
}