UBGraphicsTextItemDelegate.cpp 14.4 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2 3
 * 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
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
Claudio Valerio's avatar
Claudio Valerio committed
5
 * (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
 */

#include <QtGui>
#include <QtSvg>

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
19
#include "core/UBApplication.h"
Claudio Valerio's avatar
Claudio Valerio committed
20
#include "UBGraphicsGroupContainerItem.h"
Claudio Valerio's avatar
Claudio Valerio committed
21 22
#include "UBGraphicsTextItemDelegate.h"
#include "UBGraphicsScene.h"
Claudio Valerio's avatar
Claudio Valerio committed
23 24
#include "gui/UBResources.h"

Claudio Valerio's avatar
Claudio Valerio committed
25 26 27 28
#include "domain/UBGraphicsTextItem.h"
#include "domain/UBGraphicsDelegateFrame.h"
#include "core/UBSettings.h"

29
#include "board/UBBoardController.h"
Claudio Valerio's avatar
Claudio Valerio committed
30

31 32
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
33
const int UBGraphicsTextItemDelegate::sMinPixelSize = 8;
34
const int UBGraphicsTextItemDelegate::sMinPointSize = 8;
Claudio Valerio's avatar
Claudio Valerio committed
35 36

UBGraphicsTextItemDelegate::UBGraphicsTextItemDelegate(UBGraphicsTextItem* pDelegated, QObject * parent)
37
    : UBGraphicsItemDelegate(pDelegated,0, parent, true)
Claudio Valerio's avatar
Claudio Valerio committed
38
    , mLastFontPixelSize(-1)
39
    , delta(5)
Claudio Valerio's avatar
Claudio Valerio committed
40
{
Ivan Ilin's avatar
Ivan Ilin committed
41 42 43 44 45 46 47 48 49 50 51
    delegated()->setData(UBGraphicsItemData::ItemEditable, QVariant(true));
    delegated()->setPlainText("");

    QTextCursor curCursor = delegated()->textCursor();
    QTextCharFormat format;
    QFont font(createDefaultFont());
    font.setPointSize(UBSettings::settings()->fontPointSize());

    format.setFont(font);
    curCursor.mergeCharFormat(format);
    delegated()->setTextCursor(curCursor);
52
    delegated()->setFont(font);
Ivan Ilin's avatar
Ivan Ilin committed
53 54 55 56

    delegated()->adjustSize();
    delegated()->contentsChanged();

Claudio Valerio's avatar
Claudio Valerio committed
57 58 59 60 61 62 63 64
    // NOOP
}

UBGraphicsTextItemDelegate::~UBGraphicsTextItemDelegate()
{
    // NOOP
}

Ivan Ilin's avatar
Ivan Ilin committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
QFont UBGraphicsTextItemDelegate::createDefaultFont()
{
    QTextCharFormat textFormat;

    QString fFamily = UBSettings::settings()->fontFamily();
    if (!fFamily.isEmpty())
        textFormat.setFontFamily(fFamily);

    bool bold = UBSettings::settings()->isBoldFont();
    if (bold)
        textFormat.setFontWeight(QFont::Bold);

    bool italic = UBSettings::settings()->isItalicFont();
    if (italic)
        textFormat.setFontItalic(true);

    QFont font(fFamily, -1, bold ? QFont::Bold : -1, italic);
82 83 84 85 86 87 88 89 90
//    int pixSize = UBSettings::settings()->fontPixelSize();
//    if (pixSize > 0) {
//        mLastFontPixelSize = pixSize;
//        font.setPixelSize(pixSize);
//    }
    int pointSize = UBSettings::settings()->fontPointSize();
    if (pointSize > 0) {
//        mLastFontPixelSize = pointSize;
        font.setPointSize(pointSize);
Ivan Ilin's avatar
Ivan Ilin committed
91 92 93 94 95
    }

    return font;
}

Claudio Valerio's avatar
Claudio Valerio committed
96 97 98 99
void UBGraphicsTextItemDelegate::buildButtons()
{
    UBGraphicsItemDelegate::buildButtons();

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
100 101 102 103
    mFontButton = new DelegateButton(":/images/font.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
    mColorButton = new DelegateButton(":/images/color.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
    mDecreaseSizeButton = new DelegateButton(":/images/minus.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
    mIncreaseSizeButton = new DelegateButton(":/images/plus.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
Claudio Valerio's avatar
Claudio Valerio committed
104 105 106 107 108 109

    connect(mFontButton, SIGNAL(clicked(bool)), this, SLOT(pickFont()));
    connect(mColorButton, SIGNAL(clicked(bool)), this, SLOT(pickColor()));
    connect(mDecreaseSizeButton, SIGNAL(clicked(bool)), this, SLOT(decreaseSize()));
    connect(mIncreaseSizeButton, SIGNAL(clicked(bool)), this, SLOT(increaseSize()));

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
110 111 112
    QList<QGraphicsItem*> itemsOnToolBar;
    itemsOnToolBar << mFontButton << mColorButton << mDecreaseSizeButton << mIncreaseSizeButton;
    mToolBarItem->setItemsOnToolBar(itemsOnToolBar);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
113
    mToolBarItem->setShifting(true);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
114
    mToolBarItem->setVisibleOnBoard(true);
Claudio Valerio's avatar
Claudio Valerio committed
115 116 117 118 119 120 121 122
}

void UBGraphicsTextItemDelegate::contentsChanged()
{
    positionHandles();
    delegated()->contentsChanged();
}

123 124
// This method is used to filter the available fonts. Only the web-compliant fonts
// will remain in the font list.
Claudio Valerio's avatar
Claudio Valerio committed
125 126 127 128
void UBGraphicsTextItemDelegate::customize(QFontDialog &fontDialog)
{
    fontDialog.setOption(QFontDialog::DontUseNativeDialog);

129
    if (UBSettings::settings()->isDarkBackground()) {
Claudio Valerio's avatar
Claudio Valerio committed
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
        fontDialog.setStyleSheet("background-color: white;");
    }

    QListView *fontNameListView;
    QList<QListView*> listViews = fontDialog.findChildren<QListView*>();
    if (listViews.count() > 0)
    {
        fontNameListView = listViews.at(0);
        foreach (QListView* listView, listViews)
        {
            if (listView->pos().x() < fontNameListView->pos().x())
                fontNameListView = listView;
        }
    }
    if (fontNameListView)
    {
        QStringListModel *stringListModel = dynamic_cast<QStringListModel*>(fontNameListView->model());
        if (stringListModel)
        {
            QStringList dialogFontNames = stringListModel->stringList();
            QStringList safeWebFontNames;
            safeWebFontNames.append("Arial");
            safeWebFontNames.append("Arial Black");
            safeWebFontNames.append("Comic Sans MS");
            safeWebFontNames.append("Courier New");
            safeWebFontNames.append("Georgia");
            safeWebFontNames.append("Impact");
            safeWebFontNames.append("Times New Roman");
            safeWebFontNames.append("Trebuchet MS");
            safeWebFontNames.append("Verdana");
Claudio Valerio's avatar
Claudio Valerio committed
160 161

            QStringList customFontList =  UBResources::resources()->customFontList();
Claudio Valerio's avatar
Claudio Valerio committed
162
            int index = 0;
Claudio Valerio's avatar
Claudio Valerio committed
163 164
            foreach (QString dialogFontName, dialogFontNames){
                if (safeWebFontNames.contains(dialogFontName, Qt::CaseInsensitive) || customFontList.contains(dialogFontName, Qt::CaseSensitive))
Claudio Valerio's avatar
Claudio Valerio committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
                    index++;
                else
                    stringListModel->removeRow(index);
            }
        }
    }
    QList<QComboBox*> comboBoxes = fontDialog.findChildren<QComboBox*>();
    if (comboBoxes.count() > 0)
        comboBoxes.at(0)->setEnabled(false);
}


void UBGraphicsTextItemDelegate::pickFont()
{
    if (mDelegated && mDelegated->scene() && mDelegated->scene()->views().size() > 0)
    {
Ivan Ilin's avatar
Ivan Ilin committed
181
        QFontDialog fontDialog(delegated()->textCursor().charFormat().font(), mDelegated->scene()->views().at(0));
Claudio Valerio's avatar
Claudio Valerio committed
182 183 184 185 186 187 188 189
        customize(fontDialog);

        if (fontDialog.exec())
        {
            QFont selectedFont = fontDialog.selectedFont();
            UBSettings::settings()->setFontFamily(selectedFont.family());
            UBSettings::settings()->setBoldFont(selectedFont.bold());
            UBSettings::settings()->setItalicFont(selectedFont.italic());
Ivan Ilin's avatar
Ivan Ilin committed
190
            UBSettings::settings()->setFontPointSize(selectedFont.pointSize());
Claudio Valerio's avatar
Claudio Valerio committed
191

192 193 194 195 196 197
            //setting format for selected item
            QTextCursor curCursor = delegated()->textCursor();
            QTextCharFormat format;
            format.setFont(selectedFont);
            curCursor.mergeCharFormat(format);

198 199
            delegated()->setTextCursor(curCursor);
            delegated()->setFont(selectedFont);
200
            delegated()->setSelected(true);
Claudio Valerio's avatar
Claudio Valerio committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
            delegated()->document()->adjustSize();
            delegated()->contentsChanged();
        }
    }
}

void UBGraphicsTextItemDelegate::pickColor()
{
    if (mDelegated && mDelegated->scene() && mDelegated->scene()->views().size() > 0)
    {
        QColorDialog colorDialog(delegated()->defaultTextColor(), mDelegated->scene()->views().at(0));
        colorDialog.setWindowTitle(tr("Text Color"));
        if (UBSettings::settings()->isDarkBackground())
        {
            colorDialog.setStyleSheet("background-color: white;");
        }

        if (colorDialog.exec())
        {
            QColor selectedColor = colorDialog.selectedColor();
            delegated()->setDefaultTextColor(selectedColor);
222 223 224 225 226 227 228
//            delegated()->setColorOnDarkBackground(selectedColor);
//            delegated()->setColorOnLightBackground(selectedColor);
            QTextCursor curCursor = delegated()->textCursor();
            QTextCharFormat format;
            format.setForeground(QBrush(selectedColor));
            curCursor.mergeCharFormat(format);
            delegated()->setTextCursor(curCursor);
Claudio Valerio's avatar
Claudio Valerio committed
229 230 231 232 233 234 235 236 237 238 239

            UBGraphicsTextItem::lastUsedTextColor = selectedColor;

            delegated()->setSelected(true);
            delegated()->contentsChanged();
        }
    }
}

void UBGraphicsTextItemDelegate::decreaseSize()
{
Aleksei Kanash's avatar
Aleksei Kanash committed
240
    ChangeTextSize(-delta, changeSize);
Claudio Valerio's avatar
Claudio Valerio committed
241 242 243 244
}

void UBGraphicsTextItemDelegate::increaseSize()
{
Aleksei Kanash's avatar
Aleksei Kanash committed
245
   ChangeTextSize(delta, changeSize);
Claudio Valerio's avatar
Claudio Valerio committed
246 247 248 249 250 251
}

UBGraphicsTextItem* UBGraphicsTextItemDelegate::delegated()
{
    return static_cast<UBGraphicsTextItem*>(mDelegated);
}
252 253 254 255 256 257 258 259 260 261 262 263 264 265
void UBGraphicsTextItemDelegate::setEditable(bool editable)
{
    if (editable) {
        delegated()->setTextInteractionFlags(Qt::TextEditorInteraction);
        mDelegated->setData(UBGraphicsItemData::ItemEditable, QVariant(true));
    } else {
        QTextCursor cursor(delegated()->document());
        cursor.clearSelection();
        delegated()->setTextCursor(cursor);

        delegated()->setTextInteractionFlags(Qt::NoTextInteraction);
        mDelegated->setData(UBGraphicsItemData::ItemEditable, QVariant(false));
    }
}
Ilia Ryabokon's avatar
Ilia Ryabokon committed
266 267 268 269 270
void UBGraphicsTextItemDelegate::remove(bool canUndo)
{
    UBGraphicsItemDelegate::remove(canUndo);
}

271 272 273 274 275 276 277 278 279 280
bool UBGraphicsTextItemDelegate::isEditable()
{
    return mDelegated->data(UBGraphicsItemData::ItemEditable).toBool();
}
void UBGraphicsTextItemDelegate::decorateMenu(QMenu *menu)
{
    UBGraphicsItemDelegate::decorateMenu(menu);

    mEditableAction = menu->addAction(tr("Editable"), this, SLOT(setEditable(bool)));
    mEditableAction->setCheckable(true);
Ivan Ilin's avatar
Ivan Ilin committed
281
    mEditableAction->setChecked(isEditable());
282 283 284 285 286 287

}
void UBGraphicsTextItemDelegate::updateMenuActionState()
{
    UBGraphicsItemDelegate::updateMenuActionState();
}
Ivan Ilin's avatar
Ivan Ilin committed
288 289 290
void UBGraphicsTextItemDelegate::positionHandles()
{
    UBGraphicsItemDelegate::positionHandles();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

    if (mDelegated->isSelected() || (mDelegated->parentItem() && UBGraphicsGroupContainerItem::Type == mDelegated->parentItem()->type())) 
    {
        if (mToolBarItem->isVisibleOnBoard())
        {
            qreal AntiScaleRatio = 1 / (UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom());    
            mToolBarItem->setScale(AntiScaleRatio);
            QRectF toolBarRect = mToolBarItem->rect();
            toolBarRect.setWidth(delegated()->boundingRect().width()/AntiScaleRatio);
            mToolBarItem->setRect(toolBarRect);           
            mToolBarItem->positionHandles();
            mToolBarItem->update();
            if (mToolBarItem->isShifting())
                mToolBarItem->setPos(0,-mToolBarItem->boundingRect().height()*AntiScaleRatio);
            else
                mToolBarItem->setPos(0, 0);

            UBGraphicsGroupContainerItem *group = qgraphicsitem_cast<UBGraphicsGroupContainerItem*>(mDelegated->parentItem());

            mToolBarItem->hide();
311
            if (mToolBarItem->parentItem())
312 313 314
            {
                if (group && group->getCurrentItem() == mDelegated && group->isSelected())
                    mToolBarItem->show();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
315

316 317 318
                if (!group)
                     mToolBarItem->show();
            }
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
319 320 321 322 323 324 325

        }
    }
    else
    {
        mToolBarItem->hide();
    }
Ivan Ilin's avatar
Ivan Ilin committed
326
}
327

Aleksei Kanash's avatar
Aleksei Kanash committed
328
void UBGraphicsTextItemDelegate::ChangeTextSize(qreal factor, textChangeMode changeMode)
329
{
330 331 332 333 334 335
    if (scaleSize == changeMode)
    {
        if (1 == factor)
            return;
    }
    else
Aleksei Kanash's avatar
Aleksei Kanash committed
336
    if (0 == factor)
337 338
        return;

Aleksei Kanash's avatar
Aleksei Kanash committed
339 340 341 342 343
    UBGraphicsTextItem *item = dynamic_cast<UBGraphicsTextItem*>(delegated());

    if (item && (QString() == item->toPlainText()))
        return;

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    QTextCursor cursor = delegated()->textCursor();
    QTextCharFormat textFormat;

    int anchorPos = cursor.anchor();
    int cursorPos = cursor.position();

    if (0 == anchorPos-cursorPos)
    {
        cursor.setPosition (0, QTextCursor::MoveAnchor);
        cursor.setPosition (cursor.document()->characterCount()-1, QTextCursor::KeepAnchor);
    }

    int startPos = qMin(cursor.anchor(), cursor.position());
    int endPos = qMax(cursor.anchor(), cursor.position());

359 360 361 362
    QFont curFont;
    bool bEndofTheSameBlock;
    int iBlockLen;
    int iPointSize;
363
    int iNextPointSize;
364
    int iCursorPos = startPos;
365

366
   // we search continuous blocks of the text with the same PointSize and allpy new settings for them.
Aleksei Kanash's avatar
Aleksei Kanash committed
367
    cursor.setPosition (startPos, QTextCursor::MoveAnchor);
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    while(iCursorPos < endPos)
    {   
        bEndofTheSameBlock = false;
        iBlockLen = 0; 

        cursor.setPosition (iCursorPos+1, QTextCursor::KeepAnchor);
        iPointSize = cursor.charFormat().font().pointSize();

        cursor.setPosition (iCursorPos, QTextCursor::KeepAnchor);

        curFont = cursor.charFormat().font();

        do
        {
            iBlockLen++;

            cursor.setPosition (iCursorPos+iBlockLen+1, QTextCursor::KeepAnchor);
385
            iNextPointSize = cursor.charFormat().font().pointSize();
386 387

            cursor.setPosition (iCursorPos+iBlockLen, QTextCursor::KeepAnchor);
388
            if ((iPointSize != iNextPointSize)||(iCursorPos+iBlockLen >= endPos))
389
                bEndofTheSameBlock = true;
390

391
        }while(!bEndofTheSameBlock);
392

393 394

        //setting new parameners
Aleksei Kanash's avatar
Aleksei Kanash committed
395
        int iNewPointSize = (changeSize == changeMode) ? (iPointSize + factor) : (iPointSize * factor);
396
        curFont.setPointSize( (iNewPointSize > 0)?iNewPointSize:1);
397 398 399
        textFormat.setFont(curFont);
        cursor.mergeCharFormat(textFormat);

400 401
        iCursorPos += iBlockLen;
        cursor.setPosition (iCursorPos, QTextCursor::MoveAnchor);
402 403
    }

404 405
    delegated()->setFont(curFont);
    UBSettings::settings()->setFontPointSize(iPointSize);
406 407 408 409 410 411
    //returning initial selection
    cursor.setPosition (anchorPos, QTextCursor::MoveAnchor);
    cursor.setPosition (cursorPos, QTextCursor::KeepAnchor);

    delegated()->setTextCursor(cursor);
}
Aleksei Kanash's avatar
Aleksei Kanash committed
412 413 414 415

void UBGraphicsTextItemDelegate::scaleTextSize(qreal multiplyer)
{
    ChangeTextSize(multiplyer, scaleSize);
416
}
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
417 418 419 420 421 422 423 424 425 426

QVariant UBGraphicsTextItemDelegate::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
    if (change == QGraphicsItem::ItemSelectedChange)
    {
        if (delegated()->isSelected())
        {
            QTextCursor c = delegated()->textCursor();
            if (c.hasSelection())
            {
Ilia Ryabokon's avatar
Ilia Ryabokon committed
427 428
                c.clearSelection();
                delegated()->setTextCursor(c);
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
429 430 431 432
            }
        }
    }
    return UBGraphicsItemDelegate::itemChange(change, value);
433
}