X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fuisupport%2Fmultilineedit.cpp;h=1f9515e5a14589464c3bc2a97c481d1af98786a8;hp=5b81ab32d279a02bf088f400852658356f05ca8c;hb=72cf394e3d73d9885eabf09c958ea106da72a8c2;hpb=b819fa39f3053f047f5389456570d00020051688 diff --git a/src/uisupport/multilineedit.cpp b/src/uisupport/multilineedit.cpp index 5b81ab32..1f9515e5 100644 --- a/src/uisupport/multilineedit.cpp +++ b/src/uisupport/multilineedit.cpp @@ -55,6 +55,7 @@ MultiLineEdit::MultiLineEdit(QWidget *parent) #endif setMode(SingleLine); + setWordWrapEnabled(false); reset(); connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged())); @@ -65,7 +66,7 @@ MultiLineEdit::~MultiLineEdit() { void MultiLineEdit::setCustomFont(const QFont &font) { setFont(font); - computeSizeHint(); + updateSizeHint(); } void MultiLineEdit::setMode(Mode mode) { @@ -80,7 +81,7 @@ void MultiLineEdit::setMinHeight(int lines) { return; _minHeight = lines; - computeSizeHint(); + updateSizeHint(); } void MultiLineEdit::setMaxHeight(int lines) { @@ -88,10 +89,10 @@ void MultiLineEdit::setMaxHeight(int lines) { return; _maxHeight = lines; - computeSizeHint(); + updateSizeHint(); } -void MultiLineEdit::enableScrollBars(bool enable) { +void MultiLineEdit::setScrollBarsEnabled(bool enable) { if(_scrollBarsEnabled == enable) return; @@ -114,11 +115,12 @@ void MultiLineEdit::updateScrollBars() { } void MultiLineEdit::resizeEvent(QResizeEvent *event) { - updateScrollBars(); QTextEdit::resizeEvent(event); + updateSizeHint(); + updateScrollBars(); } -void MultiLineEdit::computeSizeHint() { +void MultiLineEdit::updateSizeHint() { QFontMetrics fm(font()); int minPixelHeight = fm.lineSpacing() * _minHeight; int maxPixelHeight = fm.lineSpacing() * _maxHeight; @@ -142,7 +144,7 @@ void MultiLineEdit::computeSizeHint() { QSize MultiLineEdit::sizeHint() const { if(!_sizeHint.isValid()) { MultiLineEdit *that = const_cast(this); - that->computeSizeHint(); + that->updateSizeHint(); } return _sizeHint; } @@ -151,6 +153,23 @@ QSize MultiLineEdit::minimumSizeHint() const { return sizeHint(); } +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; +} + void MultiLineEdit::historyMoveBack() { addToHistory(text(), true); @@ -169,6 +188,9 @@ void MultiLineEdit::historyMoveForward() { showHistoryEntry(); else reset(); // equals clear() in this case + } else { + addToHistory(text()); + reset(); } } @@ -275,8 +297,12 @@ void MultiLineEdit::keyPressEvent(QKeyEvent *event) { } void MultiLineEdit::on_returnPressed() { - if(!text().isEmpty()) { - foreach(const QString &line, text().split('\n', QString::SkipEmptyParts)) { + on_returnPressed(text()); +} + +void MultiLineEdit::on_returnPressed(const QString & text) { + if(!text.isEmpty()) { + foreach(const QString &line, text.split('\n', QString::SkipEmptyParts)) { if(line.isEmpty()) continue; addToHistory(line); @@ -291,8 +317,39 @@ void MultiLineEdit::on_textChanged() { QString newText = text(); newText.replace("\r\n", "\n"); newText.replace('\r', '\n'); - if(_mode == SingleLine) - newText.replace('\n', ' '); + if(_mode == SingleLine) { + if(!pasteProtectionEnabled()) + newText.replace('\n', ' '); + else if(newText.contains('\n')) { + QStringList lines = newText.split('\n', QString::SkipEmptyParts); + clear(); + + if(lines.count() >= 4) { + QString msg = tr("Do you really want to paste %n lines?", "", lines.count()); + msg += "

"; + for(int i = 0; i < 4; i++) { + msg += Qt::escape(lines[i].left(40)); + if(lines[i].count() > 40) + msg += "..."; + msg += "
"; + } + msg += "...

"; + QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No); + question.setDefaultButton(QMessageBox::No); +#ifdef Q_WS_MAC + question.setWindowFlags(question.windowFlags() | Qt::Sheet); +#endif + if(question.exec() != QMessageBox::Yes) + return; + } + + foreach(QString line, lines) { + clear(); + insert(line); + on_returnPressed(); + } + } + } _singleLine = (newText.indexOf('\n') < 0); @@ -300,7 +357,8 @@ void MultiLineEdit::on_textChanged() { _lastDocumentHeight = document()->size().height(); on_documentHeightChanged(_lastDocumentHeight); } - computeSizeHint(); + updateSizeHint(); + ensureCursorVisible(); } void MultiLineEdit::on_documentHeightChanged(qreal) {