richtextcomposer.cpp 20.8 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2015-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
18 19 20 21 22 23 24 25 26
*/

#include "richtextcomposer.h"
#include "richtextcomposercontroler.h"
#include "richtextcomposeractions.h"
#include "richtextcomposerimages.h"
#include "richtextcomposeremailquotehighlighter.h"
#include "nestedlisthelper_p.h"
#include "richtextexternalcomposer.h"
27 28
#include "grantleebuilder/plaintextmarkupbuilder.h"
#include "grantleebuilder/markupdirector.h"
29 30 31 32 33 34 35
#include <QTextBlock>
#include <QTextLayout>
#include <QClipboard>

#include "richtextcomposeremailquotedecorator.h"

#include <KActionCollection>
36
#include <QAction>
37 38 39 40 41 42 43 44 45
#include <QFileInfo>
#include <QMimeData>

using namespace KPIMTextEdit;

class Q_DECL_HIDDEN RichTextComposer::RichTextComposerPrivate
{
public:
    RichTextComposerPrivate(RichTextComposer *qq)
Laurent Montel's avatar
Laurent Montel committed
46
        : q(qq)
47 48 49
    {
        composerControler = new RichTextComposerControler(q, q);
        richTextComposerActions = new RichTextComposerActions(composerControler, q);
Laurent Montel's avatar
Laurent Montel committed
50
        externalComposer = new KPIMTextEdit::RichTextExternalComposer(q, q);
51 52 53 54
        q->connect(externalComposer, &RichTextExternalComposer::externalEditorClosed, qq, &RichTextComposer::externalEditorClosed);
        q->connect(externalComposer, &RichTextExternalComposer::externalEditorStarted, qq, &RichTextComposer::externalEditorStarted);
        q->connect(q, &RichTextComposer::textModeChanged, q, &RichTextComposer::slotTextModeChanged);
    }
Laurent Montel's avatar
Laurent Montel committed
55

56
    QString quotePrefix;
Laurent Montel's avatar
Laurent Montel committed
57 58 59 60
    RichTextComposerControler *composerControler = nullptr;
    RichTextComposerActions *richTextComposerActions = nullptr;
    KPIMTextEdit::RichTextExternalComposer *externalComposer = nullptr;
    RichTextComposer *q = nullptr;
Laurent Montel's avatar
Laurent Montel committed
61
    RichTextComposer::Mode mode = RichTextComposer::Plain;
Laurent Montel's avatar
Laurent Montel committed
62
    bool forcePlainTextMarkup = false;
63 64 65
    struct UndoHtmlVersion {
        QString originalHtml;
        QString plainText;
Laurent Montel's avatar
Laurent Montel committed
66 67
        Q_REQUIRED_RESULT bool isValid() const
        {
68 69
            return !originalHtml.isEmpty() && !plainText.isEmpty();
        }
Laurent Montel's avatar
Laurent Montel committed
70 71 72

        void clear()
        {
73 74 75 76 77
            originalHtml.clear();
            plainText.clear();
        }
    };
    UndoHtmlVersion undoHtmlVersion;
78
    bool blockClearUndoHtmlVersion = false;
79
    QMetaObject::Connection mRichTextChangedConnection;
80 81 82
};

RichTextComposer::RichTextComposer(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
83 84
    : KPIMTextEdit::RichTextEditor(parent)
    , d(new RichTextComposerPrivate(this))
85 86
{
    setAcceptRichText(false);
87
    d->mRichTextChangedConnection = connect(this, &RichTextComposer::textChanged, this, [this]() {
88 89 90 91 92 93
        if (!d->blockClearUndoHtmlVersion && d->undoHtmlVersion.isValid() && (d->mode == RichTextComposer::Plain)) {
            if (toPlainText() != d->undoHtmlVersion.plainText) {
                d->undoHtmlVersion.clear();
            }
        }
    });
94 95 96 97
}

RichTextComposer::~RichTextComposer()
{
98
    disconnect(d->mRichTextChangedConnection);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    delete d;
}

KPIMTextEdit::RichTextExternalComposer *RichTextComposer::externalComposer() const
{
    return d->externalComposer;
}

KPIMTextEdit::RichTextComposerControler *RichTextComposer::composerControler() const
{
    return d->composerControler;
}

KPIMTextEdit::RichTextComposerActions *RichTextComposer::composerActions() const
{
    return d->richTextComposerActions;
}

Laurent Montel's avatar
Laurent Montel committed
117
QVector<QAction *> RichTextComposer::richTextActionList() const
118 119 120 121
{
    return d->richTextComposerActions->richTextActionList();
}

122 123
void RichTextComposer::setEnableActions(bool state)
{
124 125
    const QVector<QAction *> richAction = richTextActionList();
    for (QAction *act : richAction) {
126 127 128 129
        act->setEnabled(state);
    }
}

130 131 132 133 134 135 136
void RichTextComposer::createActions(KActionCollection *ac)
{
    d->richTextComposerActions->createActions(ac);
}

void RichTextComposer::updateHighLighter()
{
Laurent Montel's avatar
Laurent Montel committed
137
    KPIMTextEdit::RichTextComposerEmailQuoteHighlighter *hlighter = qobject_cast<KPIMTextEdit::RichTextComposerEmailQuoteHighlighter *>(highlighter());
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    if (hlighter) {
        hlighter->toggleSpellHighlighting(checkSpellingEnabled());
    }
}

void RichTextComposer::clearDecorator()
{
    //Nothing
}

void RichTextComposer::createHighlighter()
{
    KPIMTextEdit::RichTextComposerEmailQuoteHighlighter *highlighter = new KPIMTextEdit::RichTextComposerEmailQuoteHighlighter(this);
    highlighter->toggleSpellHighlighting(checkSpellingEnabled());
    setHighlighterColors(highlighter);
    setHighlighter(highlighter);
}

void RichTextComposer::setHighlighterColors(KPIMTextEdit::RichTextComposerEmailQuoteHighlighter *highlighter)
{
    Q_UNUSED(highlighter);
}

void RichTextComposer::setUseExternalEditor(bool use)
{
    d->externalComposer->setUseExternalEditor(use);
}

void RichTextComposer::setExternalEditorPath(const QString &path)
{
    d->externalComposer->setExternalEditorPath(path);
}

bool RichTextComposer::checkExternalEditorFinished()
{
    return d->externalComposer->checkExternalEditorFinished();
}

void RichTextComposer::killExternalEditor()
{
    d->externalComposer->killExternalEditor();
}

RichTextComposer::Mode RichTextComposer::textMode() const
{
    return d->mode;
}

void RichTextComposer::enableWordWrap(int wrapColumn)
{
    setWordWrapMode(QTextOption::WordWrap);
    setLineWrapMode(QTextEdit::FixedColumnWidth);
    setLineWrapColumnOrWidth(wrapColumn);
}

void RichTextComposer::disableWordWrap()
{
    setLineWrapMode(QTextEdit::WidgetWidth);
}

int RichTextComposer::linePosition() const
{
    const QTextCursor cursor = textCursor();
    const QTextDocument *doc = document();
    QTextBlock block = doc->begin();
    int lineCount = 0;

    // Simply using cursor.block.blockNumber() would not work since that does not
    // take word-wrapping into account, i.e. it is possible to have more than one
    // line in a block.
    //
    // What we have to do therefore is to iterate over the blocks and count the
    // lines in them. Once we have reached the block where the cursor is, we have
    // to iterate over each line in it, to find the exact line in the block where
    // the cursor is.
    while (block.isValid()) {
        const QTextLayout *layout = block.layout();

        // If the current block has the cursor in it, iterate over all its lines
        if (block == cursor.block()) {
            // Special case: Cursor at end of single non-wrapped line, exit early
            // in this case as the logic below can't handle it
            if (block.lineCount() == layout->lineCount()) {
                return lineCount;
            }

            const int cursorBasePosition = cursor.position() - block.position();
            const int numberOfLine(layout->lineCount());
            for (int i = 0; i < numberOfLine; ++i) {
                QTextLine line = layout->lineAt(i);
Laurent Montel's avatar
Laurent Montel committed
228 229
                if (cursorBasePosition >= line.textStart()
                    && cursorBasePosition < line.textStart() + line.textLength()) {
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
                    break;
                }
                lineCount++;
            }
            return lineCount;
        } else {
            // No, cursor is not in the current block
            lineCount += layout->lineCount();
        }

        block = block.next();
    }

    // Only gets here if the cursor block can't be found, shouldn't happen except
    // for an empty document maybe
    return lineCount;
}

int RichTextComposer::columnNumber() const
{
    const QTextCursor cursor = textCursor();
    return cursor.columnNumber();
}

void RichTextComposer::forcePlainTextMarkup(bool force)
{
    d->forcePlainTextMarkup = force;
}

void RichTextComposer::insertPlainTextImplementation()
{
    if (d->forcePlainTextMarkup) {
262
        KPIMTextEdit::PlainTextMarkupBuilder *pb = new KPIMTextEdit::PlainTextMarkupBuilder();
263
        pb->setQuotePrefix(defaultQuoteSign());
264
        KPIMTextEdit::MarkupDirector *pmd = new KPIMTextEdit::MarkupDirector(pb);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
        pmd->processDocument(document());
        const QString plainText = pb->getResult();
        document()->setPlainText(plainText);
        delete pmd;
        delete pb;
    } else {
        document()->setPlainText(document()->toPlainText());
    }
}

void RichTextComposer::slotChangeInsertMode()
{
    setOverwriteMode(!overwriteMode());
    Q_EMIT insertModeChanged();
}

void RichTextComposer::activateRichText()
{
    if (d->mode == RichTextComposer::Plain) {
        setAcceptRichText(true);
        d->mode = RichTextComposer::Rich;
286 287 288
        if (d->undoHtmlVersion.isValid() && (toPlainText() == d->undoHtmlVersion.plainText)) {
            setHtml(d->undoHtmlVersion.originalHtml);
            d->undoHtmlVersion.clear();
289
#if 0 //Need to investigate it
290
        } else {
291 292 293 294 295
            //try to import markdown
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
            document()->setMarkdown(toPlainText(), QTextDocument::MarkdownDialectCommonMark);
#endif
#endif
296
        }
297 298 299 300 301 302 303 304
        Q_EMIT textModeChanged(d->mode);
    }
}

void RichTextComposer::switchToPlainText()
{
    if (d->mode == RichTextComposer::Rich) {
        d->mode = RichTextComposer::Plain;
305
        d->blockClearUndoHtmlVersion = true;
306
        d->undoHtmlVersion.originalHtml = toHtml();
307 308 309
        // TODO: Warn the user about this?
        insertPlainTextImplementation();
        setAcceptRichText(false);
310
        d->undoHtmlVersion.plainText = toPlainText();
311
        d->blockClearUndoHtmlVersion = false;
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
        Q_EMIT textModeChanged(d->mode);
    }
}

QString RichTextComposer::textOrHtml() const
{
    if (textMode() == Rich) {
        return d->composerControler->toCleanHtml();
    } else {
        return toPlainText();
    }
}

void RichTextComposer::setTextOrHtml(const QString &text)
{
    // might be rich text
    if (Qt::mightBeRichText(text)) {
        if (d->mode == RichTextComposer::Plain) {
            activateRichText();
        }
        setHtml(text);
    } else {
        setPlainText(text);
    }
}

void RichTextComposer::evaluateReturnKeySupport(QKeyEvent *event)
{
Laurent Montel's avatar
Laurent Montel committed
340
    if (event->key() == Qt::Key_Return) {
341
        QTextCursor cursor = textCursor();
342 343
        const int oldPos = cursor.position();
        const int blockPos = cursor.block().position();
344 345 346 347 348

        //selection all the line.
        cursor.movePosition(QTextCursor::StartOfBlock);
        cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
        QString lineText = cursor.selectedText();
Laurent Montel's avatar
Laurent Montel committed
349 350
        if (((oldPos - blockPos) > 0)
            && ((oldPos - blockPos) < int(lineText.length()))) {
351 352 353
            bool isQuotedLine = false;
            int bot = 0; // bot = begin of text after quote indicators
            while (bot < lineText.length()) {
Laurent Montel's avatar
Laurent Montel committed
354 355
                if ((lineText[bot] == QChar::fromLatin1('>'))
                    || (lineText[bot] == QChar::fromLatin1('|'))) {
356 357 358 359 360 361 362 363 364 365 366 367
                    isQuotedLine = true;
                    ++bot;
                } else if (lineText[bot].isSpace()) {
                    ++bot;
                } else {
                    break;
                }
            }
            evaluateListSupport(event);
            // duplicate quote indicators of the previous line before the new
            // line if the line actually contained text (apart from the quote
            // indicators) and the cursor is behind the quote indicators
Laurent Montel's avatar
Laurent Montel committed
368 369 370
            if (isQuotedLine
                && (bot != lineText.length())
                && ((oldPos - blockPos) >= int(bot))) {
371 372 373 374 375 376 377 378 379
                // The cursor position might have changed unpredictably if there was selected
                // text which got replaced by a new line, so we query it again:
                cursor.movePosition(QTextCursor::StartOfBlock);
                cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
                QString newLine = cursor.selectedText();

                // remove leading white space from the new line and instead
                // add the quote indicators of the previous line
                int leadingWhiteSpaceCount = 0;
Laurent Montel's avatar
Laurent Montel committed
380 381
                while ((leadingWhiteSpaceCount < newLine.length())
                       && newLine[leadingWhiteSpaceCount].isSpace()) {
382 383
                    ++leadingWhiteSpaceCount;
                }
384
                newLine.replace(0, leadingWhiteSpaceCount, lineText.left(bot));
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
                cursor.insertText(newLine);
                //cursor.setPosition( cursor.position() + 2 );
                cursor.movePosition(QTextCursor::StartOfBlock);
                setTextCursor(cursor);
            }
        } else {
            evaluateListSupport(event);
        }
    } else {
        evaluateListSupport(event);
    }
}

void RichTextComposer::evaluateListSupport(QKeyEvent *event)
{
    bool handled = false;
    if (textCursor().currentList()) {
        // handled is False if the key press event was not handled or not completely
        // handled by the Helper class.
        handled = d->composerControler->nestedListHelper()->handleBeforeKeyPressEvent(event);
    }

407 408
    // If a line was merged with previous (next) one, with different heading level,
    // the style should also be adjusted accordingly (i.e. merged)
Laurent Montel's avatar
Laurent Montel committed
409 410 411 412
    if ((event->key() == Qt::Key_Backspace && textCursor().atBlockStart()
         && (textCursor().blockFormat().headingLevel() != textCursor().block().previous().blockFormat().headingLevel()))
        || (event->key() == Qt::Key_Delete && textCursor().atBlockEnd()
            && (textCursor().blockFormat().headingLevel() != textCursor().block().next().blockFormat().headingLevel()))) {
413 414 415 416 417 418 419 420 421 422 423 424
        QTextCursor cursor = textCursor();
        cursor.beginEditBlock();
        if (event->key() == Qt::Key_Delete) {
            cursor.deleteChar();
        } else {
            cursor.deletePreviousChar();
        }
        d->composerControler->setHeadingLevel(cursor.blockFormat().headingLevel());
        cursor.endEditBlock();
        handled = true;
    }

425 426 427 428
    if (!handled) {
        KPIMTextEdit::RichTextEditor::keyPressEvent(event);
    }

429 430
    // Match the behavior of office suites: newline after header switches to normal text
    if ((event->key() == Qt::Key_Return)
Laurent Montel's avatar
Laurent Montel committed
431 432
        && (textCursor().blockFormat().headingLevel() > 0)
        && (textCursor().atBlockEnd())) {
433 434
        // it should be undoable together with actual "return" keypress
        textCursor().joinPreviousEditBlock();
435
        d->composerControler->setHeadingLevel(0);
436
        textCursor().endEditBlock();
437 438
    }

439 440 441 442 443 444 445 446
    if (textCursor().currentList()) {
        d->composerControler->nestedListHelper()->handleAfterKeyPressEvent(event);
    }
    Q_EMIT cursorPositionChanged();
}

bool RichTextComposer::processKeyEvent(QKeyEvent *e)
{
Laurent Montel's avatar
Laurent Montel committed
447 448 449 450 451 452 453 454 455
    if (d->externalComposer->useExternalEditor()
        && (e->key() != Qt::Key_Shift)
        && (e->key() != Qt::Key_Control)
        && (e->key() != Qt::Key_Meta)
        && (e->key() != Qt::Key_CapsLock)
        && (e->key() != Qt::Key_NumLock)
        && (e->key() != Qt::Key_ScrollLock)
        && (e->key() != Qt::Key_Alt)
        && (e->key() != Qt::Key_AltGr)) {
456 457 458 459 460 461
        if (!d->externalComposer->isInProgress()) {
            d->externalComposer->startExternalEditor();
        }
        return true;
    }

Laurent Montel's avatar
Laurent Montel committed
462 463 464
    if (e->key() == Qt::Key_Up && e->modifiers() != Qt::ShiftModifier
        && textCursor().block().position() == 0
        && textCursor().block().layout()->lineForTextPosition(textCursor().position()).lineNumber() == 0) {
465 466 467 468 469 470
        textCursor().clearSelection();
        Q_EMIT focusUp();
    } else if (e->key() == Qt::Key_Backtab && e->modifiers() == Qt::ShiftModifier) {
        textCursor().clearSelection();
        Q_EMIT focusUp();
    } else {
Laurent Montel's avatar
Laurent Montel committed
471
        if (!processModifyText(e)) {
Laurent Montel's avatar
Laurent Montel committed
472 473
            evaluateReturnKeySupport(e);
        }
474 475 476 477
    }
    return true;
}

Laurent Montel's avatar
Laurent Montel committed
478
bool RichTextComposer::processModifyText(QKeyEvent *event)
Laurent Montel's avatar
Laurent Montel committed
479 480 481 482 483
{
    Q_UNUSED(event);
    return false;
}

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
void RichTextComposer::keyPressEvent(QKeyEvent *e)
{
    processKeyEvent(e);
}

Sonnet::SpellCheckDecorator *RichTextComposer::createSpellCheckDecorator()
{
    return new KPIMTextEdit::RichTextComposerEmailQuoteDecorator(this);
}

QString RichTextComposer::smartQuote(const QString &msg)
{
    return msg;
}

void RichTextComposer::setQuotePrefixName(const QString &quotePrefix)
{
    d->quotePrefix = quotePrefix;
}

QString RichTextComposer::quotePrefixName() const
{
    if (!d->quotePrefix.simplified().isEmpty()) {
        return d->quotePrefix;
    } else {
        return QStringLiteral(">");
    }
}

513
int RichTextComposer::quoteLength(const QString &line, bool oneQuote) const
514 515 516 517 518 519 520 521 522 523 524 525 526
{
    if (!d->quotePrefix.simplified().isEmpty()) {
        if (line.startsWith(d->quotePrefix)) {
            return d->quotePrefix.length();
        } else {
            return 0;
        }
    } else {
        bool quoteFound = false;
        int startOfText = -1;
        const int lineLength(line.length());
        for (int i = 0; i < lineLength; ++i) {
            if (line[i] == QLatin1Char('>') || line[i] == QLatin1Char('|')) {
527 528 529
                if (quoteFound && oneQuote) {
                    break;
                }
530 531 532 533 534 535 536
                quoteFound = true;
            } else if (line[i] != QLatin1Char(' ')) {
                startOfText = i;
                break;
            }
        }
        if (quoteFound) {
537
            //We found a quote but it's just quote element => 1 => remove 1 char.
538
            if (startOfText == -1) {
539
                startOfText = 1;
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
            }
            return startOfText;
        } else {
            return 0;
        }
    }
}

void RichTextComposer::setCursorPositionFromStart(unsigned int pos)
{
    d->composerControler->setCursorPositionFromStart(pos);
}

bool RichTextComposer::isLineQuoted(const QString &line) const
{
    return quoteLength(line) > 0;
}

const QString RichTextComposer::defaultQuoteSign() const
{
    if (!d->quotePrefix.simplified().isEmpty()) {
        return d->quotePrefix;
    } else {
        return QStringLiteral("> ");
    }
}

void RichTextComposer::insertFromMimeData(const QMimeData *source)
{
    // Add an image if that is on the clipboard
    if (textMode() == RichTextComposer::Rich && source->hasImage()) {
        const QImage image = qvariant_cast<QImage>(source->imageData());
        QFileInfo fi;
        d->composerControler->composerImages()->insertImage(image, fi);
        return;
    }

    // Attempt to paste HTML contents into the text edit in plain text mode,
    // prevent this and prevent plain text instead.
    if (textMode() == RichTextComposer::Plain && source->hasHtml()) {
        if (source->hasText()) {
            insertPlainText(source->text());
            return;
        }
    }

586 587 588
    if (textMode() == RichTextComposer::Rich) {
        if (source->hasText()) {
            const QString sourceText = source->text();
589 590 591 592 593 594 595 596 597 598 599 600
            if (sourceText.startsWith(QLatin1String("http://"))
                || sourceText.startsWith(QLatin1String("https://"))
                || sourceText.startsWith(QLatin1String("ftps://"))
                || sourceText.startsWith(QLatin1String("ftp://"))
                || sourceText.startsWith(QLatin1String("mailto:"))
                || sourceText.startsWith(QLatin1String("smb://"))
                || sourceText.startsWith(QLatin1String("file://"))
                || sourceText.startsWith(QLatin1String("webdavs://"))
                || sourceText.startsWith(QLatin1String("imaps://"))
                || sourceText.startsWith(QLatin1String("sftp://"))
                || sourceText.startsWith(QLatin1String("fish://"))
                || sourceText.startsWith(QLatin1String("tel:"))
Laurent Montel's avatar
Laurent Montel committed
601
                ) {
602 603 604 605 606 607
                insertHtml(QStringLiteral("<a href=\"%1\">%1</a> ").arg(sourceText));
                return;
            }
        }
    }

608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    KPIMTextEdit::RichTextEditor::insertFromMimeData(source);
}

bool RichTextComposer::canInsertFromMimeData(const QMimeData *source) const
{
    if (source->hasHtml() && textMode() == RichTextComposer::Rich) {
        return true;
    }

    if (source->hasText()) {
        return true;
    }

    if (textMode() == RichTextComposer::Rich && source->hasImage()) {
        return true;
    }

    return KPIMTextEdit::RichTextEditor::canInsertFromMimeData(source);
}

void RichTextComposer::mouseReleaseEvent(QMouseEvent *event)
{
    if (d->composerControler->painterActive()) {
        d->composerControler->disablePainter();
        d->richTextComposerActions->uncheckActionFormatPainter();
    }
    KPIMTextEdit::RichTextEditor::mouseReleaseEvent(event);
}

void RichTextComposer::slotTextModeChanged(KPIMTextEdit::RichTextComposer::Mode mode)
{
    d->composerControler->textModeChanged(mode);
    d->richTextComposerActions->textModeChanged(mode);
}