Pimcommon

spellchecklineedit.cpp
1 /*
2  * SPDX-FileCopyrightText: 2011-2020 Laurent Montel <[email protected]>
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  */
6 
7 #include "spellchecklineedit.h"
8 #include <QStyleOptionFrame>
9 #include <QKeyEvent>
10 #include <QStyle>
11 #include <QMimeData>
12 
13 using namespace PimCommon;
14 
16  : KPIMTextEdit::RichTextEditor(parent)
17 {
18  setSpellCheckingConfigFileName(configFile);
19  setSearchSupport(false);
20  setAllowTabSupport(false);
21  setAcceptRichText(false);
22  setTabChangesFocus(true);
23  // widget may not be resized vertically
28  setCheckSpellingEnabled(true);
29  document()->adjustSize();
30 
32 }
33 
35 {
36 }
37 
38 void SpellCheckLineEdit::keyPressEvent(QKeyEvent *e)
39 {
40  if (e->key() == Qt::Key_Enter
41  || e->key() == Qt::Key_Return
42  || e->key() == Qt::Key_Down) {
43  Q_EMIT focusDown();
44  return;
45  } else if (e->key() == Qt::Key_Up) {
46  Q_EMIT focusUp();
47  return;
48  }
49  RichTextEditor::keyPressEvent(e);
50 }
51 
52 QSize SpellCheckLineEdit::sizeHint() const
53 {
54  QFontMetrics fm(font());
55 
56  const int h = document()->size().toSize().height() - fm.descent() + 2 * frameWidth();
57 
59  opt.initFrom(this);
60  opt.rect = QRect(0, 0, 100, h);
61  opt.lineWidth = lineWidth();
62  opt.midLineWidth = 0;
63  opt.state |= QStyle::State_Sunken;
64 
65  QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h), this);
66 
67  return s;
68 }
69 
70 QSize SpellCheckLineEdit::minimumSizeHint() const
71 {
72  return sizeHint();
73 }
74 
75 void SpellCheckLineEdit::insertFromMimeData(const QMimeData *source)
76 {
77  if (!source) {
78  return;
79  }
80 
81  setFocus();
82 
83  // Copy text from the clipboard (paste)
84  QString pasteText = source->text();
85 
86  // is there any text in the clipboard?
87  if (!pasteText.isEmpty()) {
88  // replace \r with \n to make xterm pastes happy
89  pasteText.replace(QLatin1Char('\r'), QLatin1Char('\n'));
90  // remove blank lines
91  while (pasteText.contains(QLatin1String("\n\n"))) {
92  pasteText.replace(QLatin1String("\n\n"), QLatin1String("\n"));
93  }
94 
95  QRegExp reTopSpace(QStringLiteral("^ *\n"));
96  while (pasteText.contains(reTopSpace)) {
97  pasteText.remove(reTopSpace);
98  }
99 
100  QRegExp reBottomSpace(QStringLiteral("\n *$"));
101  while (pasteText.contains(reBottomSpace)) {
102  pasteText.remove(reBottomSpace);
103  }
104 
105  // does the text contain at least one newline character?
106  pasteText.replace(QLatin1Char('\n'), QLatin1Char(' '));
107 
108  insertPlainText(pasteText);
110  return;
111  } else {
112  RichTextEditor::insertFromMimeData(source);
113  }
114 }
ScrollBarAlwaysOff
QStyle * style() const const
folderdialogacltab.h
void setTabChangesFocus(bool b)
void setDocumentMargin(qreal margin)
int descent() const const
QString & remove(int position, int n)
int lineWidth() const const
~SpellCheckLineEdit() override
Destructor.
void initFrom(const QWidget *widget)
QString text() const const
void focusUp()
Emitted when the user uses the up arrow in the first line.
void setFocus()
void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy)
bool isEmpty() const const
void setSizePolicy(QSizePolicy)
int key() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
const QFont & font() const const
void setLineWrapMode(QTextEdit::LineWrapMode mode)
QString & replace(int position, int n, QChar after)
void ensureCursorVisible()
void adjustSize()
int frameWidth() const const
QTextDocument * document() const const
void setVerticalScrollBarPolicy(Qt::ScrollBarPolicy)
void insertPlainText(const QString &text)
virtual QSize sizeFromContents(QStyle::ContentsType type, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget) const const =0
Key_Enter
void setAcceptRichText(bool accept)
Q_EMITQ_EMIT
SpellCheckLineEdit(QWidget *parent, const QString &configFile)
Constructs a SpellCheckLineEdit object.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 14 2020 23:15:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.