X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fuisupport%2Fmultilineedit.cpp;h=ac92235104057f846aed6c26b415ab4fbf76cf6c;hp=a4d95b0dbebf7071e97d8cc9cccc25a0bfbd33cc;hb=e2188dc438be6f3eb0d9cdf47d28821aefe9835e;hpb=694f9bfbf7f1af19108461c7e00d133e55082bce diff --git a/src/uisupport/multilineedit.cpp b/src/uisupport/multilineedit.cpp index a4d95b0d..ac922351 100644 --- a/src/uisupport/multilineedit.cpp +++ b/src/uisupport/multilineedit.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-2012 by the Quassel Project * + * Copyright (C) 2005-2018 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,11 +15,10 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ #include -#include #include #include @@ -32,30 +31,27 @@ const int leftMargin = 3; MultiLineEdit::MultiLineEdit(QWidget *parent) - : MultiLineEditParent(parent), - _idx(0), - _mode(SingleLine), - _singleLine(true), - _minHeight(1), - _maxHeight(5), - _scrollBarsEnabled(true), - _pasteProtectionEnabled(true), - _emacsMode(false), - _lastDocumentHeight(-1) -{ -#if QT_VERSION >= 0x040500 - document()->setDocumentMargin(0); // new in Qt 4.5 and we really don't want it here -#endif + : MultiLineEditParent(parent) +{ + document()->setDocumentMargin(0); setAcceptRichText(false); #ifdef HAVE_KDE enableFindReplace(false); #endif +#if defined HAVE_SONNET && !defined HAVE_KDE + _spellCheckDecorator = new Sonnet::SpellCheckDecorator(this); + highlighter()->setActive(highlighter()->checkerEnabledByDefault()); +#endif + setMode(SingleLine); - setWordWrapEnabled(false); + setLineWrapEnabled(false); reset(); + // Prevent QTextHtmlImporter::appendNodeText from eating whitespace + document()->setDefaultStyleSheet("span { white-space: pre-wrap; }"); + connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged())); _mircColorMap["00"] = "#ffffff"; @@ -77,11 +73,38 @@ MultiLineEdit::MultiLineEdit(QWidget *parent) } -MultiLineEdit::~MultiLineEdit() +#if defined HAVE_SONNET && !defined HAVE_KDE +Sonnet::Highlighter *MultiLineEdit::highlighter() const { + return _spellCheckDecorator->highlighter(); } +void MultiLineEdit::setSpellCheckEnabled(bool enabled) +{ + highlighter()->setActive(enabled); + if (enabled) { + highlighter()->slotRehighlight(); + } +} + +void MultiLineEdit::contextMenuEvent(QContextMenuEvent *event) +{ + QMenu *menu = createStandardContextMenu(); + menu->addSeparator(); + + auto action = menu->addAction(tr("Auto Spell Check")); + action->setCheckable(true); + action->setChecked(highlighter()->isActive()); + connect(action, SIGNAL(toggled(bool)), this, SLOT(setSpellCheckEnabled(bool))); + + menu->exec(event->globalPos()); + delete menu; +} + +#endif + + void MultiLineEdit::setCustomFont(const QFont &font) { setFont(font); @@ -98,6 +121,13 @@ void MultiLineEdit::setMode(Mode mode) } +void MultiLineEdit::setLineWrapEnabled(bool enable) +{ + setLineWrapMode(enable ? WidgetWidth : NoWrap); + updateSizeHint(); +} + + void MultiLineEdit::setMinHeight(int lines) { if (lines == _minHeight) @@ -161,13 +191,18 @@ void MultiLineEdit::updateSizeHint() // use the style to determine a decent size int h = qMin(qMax((int)document()->size().height() + scrollBarHeight, minPixelHeight), maxPixelHeight) + 2 * frameWidth(); - QStyleOptionFrameV2 opt; + + QStyleOptionFrame opt; opt.initFrom(this); opt.rect = QRect(0, 0, 100, h); opt.lineWidth = lineWidth(); opt.midLineWidth = midLineWidth(); opt.state |= QStyle::State_Sunken; - QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), this); + QWidget *widget = this; +#ifdef Q_OS_MAC + widget = 0; +#endif + QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), widget); if (s != _sizeHint) { _sizeHint = s; updateGeometry(); @@ -178,7 +213,7 @@ void MultiLineEdit::updateSizeHint() QSize MultiLineEdit::sizeHint() const { if (!_sizeHint.isValid()) { - MultiLineEdit *that = const_cast(this); + auto *that = const_cast(this); that->updateSizeHint(); } return _sizeHint; @@ -197,23 +232,6 @@ void MultiLineEdit::setEmacsMode(bool enable) } -void MultiLineEdit::setSpellCheckEnabled(bool enable) -{ -#ifdef HAVE_KDE - setCheckSpellingEnabled(enable); -#else - Q_UNUSED(enable) -#endif -} - - -void MultiLineEdit::setWordWrapEnabled(bool enable) -{ - setLineWrapMode(enable ? WidgetWidth : NoWrap); - updateSizeHint(); -} - - void MultiLineEdit::setPasteProtectionEnabled(bool enable, QWidget *) { _pasteProtectionEnabled = enable; @@ -280,7 +298,7 @@ bool MultiLineEdit::event(QEvent *e) { // We need to make sure that global shortcuts aren't eaten if (e->type() == QEvent::ShortcutOverride) { - QKeyEvent *event = static_cast(e); + auto *event = static_cast(e); QKeySequence key = QKeySequence(event->key() | event->modifiers()); foreach(QAction *action, GraphicalUi::actionCollection()->actions()) { if (action->shortcuts().contains(key)) { @@ -296,18 +314,7 @@ bool MultiLineEdit::event(QEvent *e) void MultiLineEdit::keyPressEvent(QKeyEvent *event) { - // Workaround the fact that Qt < 4.5 doesn't know InsertLineSeparator yet -#if QT_VERSION >= 0x040500 if (event == QKeySequence::InsertLineSeparator) { -#else - -# ifdef Q_WS_MAC - if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::META) { -# else - if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::SHIFT) { -# endif -#endif - if (_mode == SingleLine) { event->accept(); on_returnPressed(); @@ -564,22 +571,18 @@ QString MultiLineEdit::convertRichtextToMircCodes() cursor.clearSelection(); } - if (color) { - color = false; + + if (color) mircText.append('\x03'); - } - if (underline) { - underline = false; + + if (underline) mircText.append('\x1f'); - } - if (italic) { - italic = false; + + if (italic) mircText.append('\x1d'); - } - if (bold) { - bold = false; + + if (bold) mircText.append('\x02'); - } return mircText; } @@ -684,8 +687,12 @@ void MultiLineEdit::on_returnPressed() } -void MultiLineEdit::on_returnPressed(const QString &text) +void MultiLineEdit::on_returnPressed(QString text) { + if (_completionSpace && text.endsWith(" ")) { + text.chop(1); + } + if (!text.isEmpty()) { foreach(const QString &line, text.split('\n', QString::SkipEmptyParts)) { if (line.isEmpty()) @@ -704,6 +711,8 @@ void MultiLineEdit::on_returnPressed(const QString &text) void MultiLineEdit::on_textChanged() { + _completionSpace = qMax(_completionSpace - 1, 0); + QString newText = text(); newText.replace("\r\n", "\n"); newText.replace('\r', '\n'); @@ -718,7 +727,7 @@ void MultiLineEdit::on_textChanged() QString msg = tr("Do you really want to paste %n line(s)?", "", lines.count()); msg += "

"; for (int i = 0; i < 4; i++) { - msg += Qt::escape(lines[i].left(40)); + msg += lines[i].left(40).toHtmlEscaped(); if (lines[i].count() > 40) msg += "..."; msg += "
"; @@ -726,7 +735,7 @@ void MultiLineEdit::on_textChanged() msg += "...

"; QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No); question.setDefaultButton(QMessageBox::No); -#ifdef Q_WS_MAC +#ifdef Q_OS_MAC question.setWindowFlags(question.windowFlags() | Qt::Sheet); #endif if (question.exec() != QMessageBox::Yes) @@ -782,3 +791,12 @@ void MultiLineEdit::showHistoryEntry() setTextCursor(cursor); updateScrollBars(); } + + +void MultiLineEdit::addCompletionSpace() +{ + // Inserting the space emits textChanged, which should not disable removal + _completionSpace = 2; + insertPlainText(" "); +} +