UBGraphicsTextItemDelegate.cpp 13.8 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 266 267 268 269 270 271 272 273 274 275
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));
    }
}
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
276
    mEditableAction->setChecked(isEditable());
277 278 279 280 281 282

}
void UBGraphicsTextItemDelegate::updateMenuActionState()
{
    UBGraphicsItemDelegate::updateMenuActionState();
}
Ivan Ilin's avatar
Ivan Ilin committed
283 284 285
void UBGraphicsTextItemDelegate::positionHandles()
{
    UBGraphicsItemDelegate::positionHandles();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

    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();
306 307 308 309
            if (mToolBarItem->parentItem() && !mToolBarItem->parentItem()->data(UBGraphicsItemData::ItemLocked).toBool())
            {
                if (group && group->getCurrentItem() == mDelegated && group->isSelected())
                    mToolBarItem->show();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
310

311 312 313
                if (!group)
                     mToolBarItem->show();
            }
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
314 315 316 317 318 319 320 321

        }
    }
    else
    {
        mToolBarItem->hide();
    }

Ivan Ilin's avatar
Ivan Ilin committed
322 323
    setEditable(isEditable());
}
324

Aleksei Kanash's avatar
Aleksei Kanash committed
325
void UBGraphicsTextItemDelegate::ChangeTextSize(qreal factor, textChangeMode changeMode)
326
{
Aleksei Kanash's avatar
Aleksei Kanash committed
327
    if (0 == factor)
328 329
        return;

Aleksei Kanash's avatar
Aleksei Kanash committed
330 331 332 333 334
    UBGraphicsTextItem *item = dynamic_cast<UBGraphicsTextItem*>(delegated());

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

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    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());

350 351 352 353
    QFont curFont;
    bool bEndofTheSameBlock;
    int iBlockLen;
    int iPointSize;
354
    int iNextPointSize;
355
    int iCursorPos = startPos;
356

357
   // we search continuous blocks of the text with the same PointSize and allpy new settings for them.
Aleksei Kanash's avatar
Aleksei Kanash committed
358
    cursor.setPosition (startPos, QTextCursor::MoveAnchor);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    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);
376
            iNextPointSize = cursor.charFormat().font().pointSize();
377 378

            cursor.setPosition (iCursorPos+iBlockLen, QTextCursor::KeepAnchor);
379
            if ((iPointSize != iNextPointSize)||(iCursorPos+iBlockLen >= endPos))
380
                bEndofTheSameBlock = true;
381

382
        }while(!bEndofTheSameBlock);
383

384 385

        //setting new parameners
Aleksei Kanash's avatar
Aleksei Kanash committed
386
        int iNewPointSize = (changeSize == changeMode) ? (iPointSize + factor) : (iPointSize * factor);
387
        curFont.setPointSize( (iNewPointSize > 0)?iNewPointSize:1);
388 389 390
        textFormat.setFont(curFont);
        cursor.mergeCharFormat(textFormat);

391 392
        iCursorPos += iBlockLen;
        cursor.setPosition (iCursorPos, QTextCursor::MoveAnchor);
393 394
    }

395 396
    delegated()->setFont(curFont);
    UBSettings::settings()->setFontPointSize(iPointSize);
397 398 399 400 401 402
    //returning initial selection
    cursor.setPosition (anchorPos, QTextCursor::MoveAnchor);
    cursor.setPosition (cursorPos, QTextCursor::KeepAnchor);

    delegated()->setTextCursor(cursor);
}
Aleksei Kanash's avatar
Aleksei Kanash committed
403 404 405 406 407

void UBGraphicsTextItemDelegate::scaleTextSize(qreal multiplyer)
{
    ChangeTextSize(multiplyer, scaleSize);
}