UBGraphicsTextItem.cpp 12.5 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
25 26
 */

27

Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

Claudio Valerio's avatar
Claudio Valerio committed
30
#include <QtGui>
Claudio Valerio's avatar
Claudio Valerio committed
31
#include "UBGraphicsGroupContainerItem.h"
Claudio Valerio's avatar
Claudio Valerio committed
32 33 34 35 36 37 38 39
#include "UBGraphicsTextItem.h"
#include "UBGraphicsTextItemDelegate.h"
#include "UBGraphicsScene.h"
#include "UBGraphicsDelegateFrame.h"

#include "core/UBApplication.h"
#include "board/UBBoardController.h"
#include "board/UBBoardView.h"
40
#include "board/UBDrawingController.h"
Claudio Valerio's avatar
Claudio Valerio committed
41 42
#include "core/UBSettings.h"

43
#include "core/memcheck.h"
44
QColor UBGraphicsTextItem::lastUsedTextColor = QColor(Qt::black);
Claudio Valerio's avatar
Claudio Valerio committed
45

46 47
UBGraphicsTextItem::UBGraphicsTextItem(QGraphicsItem * parent)
    : QGraphicsTextItem(parent)
Ilia Ryabokon's avatar
Ilia Ryabokon committed
48
    , UBGraphicsItem()
49
    , mTypeTextHereLabel(tr("<Type Text Here>"))
Claudio Valerio's avatar
Claudio Valerio committed
50 51
    , mMultiClickState(0)
    , mLastMousePressTime(QTime::currentTime())
Nicolas Nenon's avatar
Nicolas Nenon committed
52
    , isActivatedTextEditor(true)
Claudio Valerio's avatar
Claudio Valerio committed
53
{
54
    setDelegate(new UBGraphicsTextItemDelegate(this, 0));
Claudio Valerio's avatar
Claudio Valerio committed
55

56 57
    // TODO claudio remove this because in contrast with the fact the frame should be created on demand.
    Delegate()->createControls();
58
    Delegate()->frame()->setOperationMode(UBGraphicsDelegateFrame::Resizing);
59 60
    Delegate()->setUBFlag(GF_FLIPPABLE_ALL_AXIS, false);
    Delegate()->setUBFlag(GF_REVOLVABLE, true);
Claudio Valerio's avatar
Claudio Valerio committed
61 62

    setData(UBGraphicsItemData::ItemLayerType, UBItemLayerType::Object);
shibakaneki's avatar
shibakaneki committed
63 64
    setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::ObjectItem)); //Necessary to set if we want z value to be assigned correctly

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

    setTextInteractionFlags(Qt::TextEditorInteraction);

70 71
    setUuid(QUuid::createUuid());

72
    connect(document(), SIGNAL(contentsChanged()), Delegate(), SLOT(contentsChanged()));
Claudio Valerio's avatar
Claudio Valerio committed
73 74
    connect(document(), SIGNAL(undoCommandAdded()), this, SLOT(undoCommandAdded()));

75 76
    connect(document()->documentLayout(), SIGNAL(documentSizeChanged(const QSizeF &)),
            this, SLOT(documentSizeChanged(const QSizeF &)));
Claudio Valerio's avatar
Claudio Valerio committed
77 78 79 80 81 82 83

}

UBGraphicsTextItem::~UBGraphicsTextItem()
{
}

84 85 86 87 88 89 90
void UBGraphicsTextItem::recolor()
{
    UBGraphicsTextItemDelegate * del = dynamic_cast<UBGraphicsTextItemDelegate*>(Delegate());
    if (del)
        del->recolor();
}

91 92 93 94 95 96 97 98 99
void UBGraphicsTextItem::setSelected(bool selected)
{
    if(selected){
        Delegate()->createControls();
        Delegate()->frame()->setOperationMode(UBGraphicsDelegateFrame::Resizing);
    }
    QGraphicsTextItem::setSelected(selected);
}

Claudio Valerio's avatar
Claudio Valerio committed
100 101 102 103
QVariant UBGraphicsTextItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    QVariant newValue = value;

104 105
    if(Delegate())
        newValue = Delegate()->itemChange(change, value);
Claudio Valerio's avatar
Claudio Valerio committed
106 107 108 109 110 111

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

void UBGraphicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
112

113 114
    // scene()->itemAt(pos) returns 0 if pos is not over text, but over text item, but mouse press comes.
    // It is a cludge...
115 116
    if (UBStylusTool::Play == UBDrawingController::drawingController()->stylusTool())
    {
117
        QGraphicsTextItem::mousePressEvent(event);
118 119 120 121 122
        event->accept();
        clearFocus();
        return;
    }

123
    if (Delegate())
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
124
    {
125 126
        Delegate()->mousePressEvent(event);
        if (Delegate() && parentItem() && UBGraphicsGroupContainerItem::Type == parentItem()->type())
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
127 128 129 130 131 132
        {
            UBGraphicsGroupContainerItem *group = qgraphicsitem_cast<UBGraphicsGroupContainerItem*>(parentItem());
            if (group)
            {
                QGraphicsItem *curItem = group->getCurrentItem();
                if (curItem && this != curItem)
133 134 135
                {
                    group->deselectCurrentItem();
                }
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
136 137
                group->setCurrentItem(this);
                this->setSelected(true);
138
                Delegate()->positionHandles();
139
            }
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
140 141 142

        }
    }
143

144 145
    if (!data(UBGraphicsItemData::ItemEditable).toBool()) {
        setTextInteractionFlags(Qt::NoTextInteraction);
146
        return;
147
    }
148

149 150
    setTextInteractionFlags(Qt::TextEditorInteraction);

Claudio Valerio's avatar
Claudio Valerio committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    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)
    {
Nicolas Nenon's avatar
Nicolas Nenon committed
168
        activateTextEditor(true);
Claudio Valerio's avatar
Claudio Valerio committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        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)
{
192
    if (!Delegate() || !Delegate()->mouseMoveEvent(event))
Claudio Valerio's avatar
Claudio Valerio committed
193 194 195 196 197
    {
        QGraphicsTextItem::mouseMoveEvent(event);
    }
}

198 199 200 201 202
void UBGraphicsTextItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{

}

Claudio Valerio's avatar
Claudio Valerio committed
203 204
void UBGraphicsTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
205 206
    // scene()->itemAt(pos) returns 0 if pos is not over text, but over text item, but mouse press comes.
    // It is a cludge...
207 208 209 210 211 212 213
    if (UBStylusTool::Play == UBDrawingController::drawingController()->stylusTool())
    {
        event->accept();
        clearFocus();
        return;
    }

Claudio Valerio's avatar
Claudio Valerio committed
214 215
    if (mMultiClickState == 1)
    {
216 217
        QGraphicsTextItem::mouseReleaseEvent(event);

218 219
        if (Delegate())
            Delegate()->mouseReleaseEvent(event);
Claudio Valerio's avatar
Claudio Valerio committed
220 221 222 223 224 225 226
    }
    else
    {
        event->accept();
    }
}

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
void UBGraphicsTextItem::keyPressEvent(QKeyEvent *event)
{
    if (Delegate() && !Delegate()->keyPressEvent(event)) {
        qDebug() << "UBGraphicsTextItem::keyPressEvent(QKeyEvent *event) has been rejected by delegate. Don't call base class method";
        return;
    }

    QGraphicsTextItem::keyPressEvent(event);
}

void UBGraphicsTextItem::keyReleaseEvent(QKeyEvent *event)
{
    if (Delegate() && !Delegate()->keyReleaseEvent(event)) {
        qDebug() << "UBGraphicsTextItem::keyPressEvent(QKeyEvent *event) has been rejected by delegate. Don't call base class method";
        return;
    }

    QGraphicsTextItem::keyReleaseEvent(event);
}

Claudio Valerio's avatar
Claudio Valerio committed
247 248 249 250 251 252 253 254 255
void UBGraphicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    // 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);

256
    if (widget == UBApplication::boardController->controlView()->viewport() && !isSelected())
Claudio Valerio's avatar
Claudio Valerio committed
257
    {
258
        setTextInteractionFlags(Qt::NoTextInteraction);
259 260 261 262 263 264
        if (toPlainText().isEmpty())
        {
            painter->setFont(font());
            painter->setPen(UBSettings::paletteColor);
            painter->drawText(boundingRect(), Qt::AlignCenter, mTypeTextHereLabel);
        }
Claudio Valerio's avatar
Claudio Valerio committed
265
    }
266 267

    Delegate()->postpaint(painter, option, widget);
Claudio Valerio's avatar
Claudio Valerio committed
268 269 270 271 272 273 274
}


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

275
    copyItemParameters(copy);
Claudio Valerio's avatar
Claudio Valerio committed
276 277 278 279 280 281

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

   return copy;
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
void UBGraphicsTextItem::copyItemParameters(UBItem *copy) const
{
    UBGraphicsTextItem *cp = dynamic_cast<UBGraphicsTextItem*>(copy);
    if (cp)
    {
        cp->setHtml(toHtml());
        cp->setPos(this->pos());
        cp->setTransform(this->transform());
        cp->setFlag(QGraphicsItem::ItemIsMovable, true);
        cp->setFlag(QGraphicsItem::ItemIsSelectable, true);
        cp->setData(UBGraphicsItemData::ItemLayerType, this->data(UBGraphicsItemData::ItemLayerType));
        cp->setData(UBGraphicsItemData::ItemLocked, this->data(UBGraphicsItemData::ItemLocked));
        cp->setData(UBGraphicsItemData::ItemEditable, data(UBGraphicsItemData::ItemEditable).toBool());
        cp->setTextWidth(this->textWidth());
        cp->setTextHeight(this->textHeight());

        cp->setSourceUrl(this->sourceUrl());
299
        cp->setZValue(this->zValue());
300 301
    }
}
Claudio Valerio's avatar
Claudio Valerio committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

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)
{
320 321 322 323 324 325
    qreal titleBarWidth = 0;
    UBGraphicsTextItemDelegate * del = dynamic_cast<UBGraphicsTextItemDelegate*>(Delegate());
    if (del)
        titleBarWidth = del->titleBarWidth();

    qreal newWidth = qMax(titleBarWidth, width);
Claudio Valerio's avatar
Claudio Valerio committed
326 327 328 329 330 331 332 333 334 335 336

    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
337
    setFocus();
Claudio Valerio's avatar
Claudio Valerio committed
338 339 340 341 342 343 344 345
}


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

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
/**
 * @brief Get the ratio between font size in pixels and points.
 * @return The ratio of pixel size to point size of the first character, or 0 if the text item is empty.
 *
 * Qt may display fonts differently on different platforms -- on the same display,
 * the same point size may be displayed at different pixel sizes. This function returns the
 * ratio of pixel size to point size, based on the first character in the text item.
 */
qreal UBGraphicsTextItem::pixelsPerPoint() const
{
    QTextCursor cursor = textCursor();
    if (cursor.isNull())
        return 0;

    cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
    cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);

    QFont f = cursor.charFormat().font();
    qDebug() << "ppp. Font: " << f;
    QFontInfo fi(cursor.charFormat().font());

    qreal pixelSize = fi.pixelSize();
    qreal pointSize = fi.pointSizeF();

    //qDebug() << "Pixel size: " << pixelSize;
    //qDebug() << "Point size: " << pointSize;

    if (pointSize == 0)
        return 0;

    return pixelSize/pointSize;
}

Claudio Valerio's avatar
Claudio Valerio committed
379 380 381 382 383 384 385 386 387 388

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

    if (toPlainText().isEmpty())
    {
389
        resize(QFontMetrics(font()).width(mTypeTextHereLabel),QFontMetrics(font()).height());
Claudio Valerio's avatar
Claudio Valerio committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403
    }
}


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


void UBGraphicsTextItem::resize(qreal w, qreal h)
{
    setTextWidth(w);
    setTextHeight(h);
404 405
    if (Delegate())
        Delegate()->positionHandles();
Claudio Valerio's avatar
Claudio Valerio committed
406 407 408 409 410 411 412 413
}


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

root's avatar
root committed
414 415 416 417 418 419
void UBGraphicsTextItem::setUuid(const QUuid &pUuid)
{
    UBItem::setUuid(pUuid);
    setData(UBGraphicsItemData::ItemUuid, QVariant(pUuid)); //store item uuid inside the QGraphicsItem to fast operations with Items on the scene
}

Claudio Valerio's avatar
Claudio Valerio committed
420 421 422 423 424 425 426 427 428 429 430

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


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

Nicolas Nenon's avatar
Nicolas Nenon committed
432
void UBGraphicsTextItem::activateTextEditor(bool activate)
433
{
Nicolas Nenon's avatar
Nicolas Nenon committed
434 435 436 437 438 439 440 441 442 443 444
    qDebug() << textInteractionFlags();

    this->isActivatedTextEditor = activate;

    if(!activate){
        setTextInteractionFlags(Qt::TextSelectableByMouse);
    }else{
        setTextInteractionFlags(Qt::TextEditorInteraction);
    }

    qDebug() <<  textInteractionFlags();
445
}