• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkdepim

  • sources
  • kde-4.14
  • kdepim
  • libkdepim
  • widgets
spellchecklineedit.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015 Montel Laurent <montel@kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of this program with any edition of
19  * the Qt library by Trolltech AS, Norway (or with modified versions
20  * of Qt that use the same license as Qt), and distribute linked
21  * combinations including the two. You must obey the GNU General
22  * Public License in all respects for all of the code used other than
23  * Qt. If you modify this file, you may extend this exception to
24  * your version of the file, but you are not obligated to do so. If
25  * you do not wish to do so, delete this exception statement from
26  * your version.
27  */
28 
29 #include "spellchecklineedit.h"
30 
31 #include <sonnet/speller.h>
32 
33 #include <QKeyEvent>
34 #include <QStyle>
35 #include <QApplication>
36 #include <QMenu>
37 #include <QStyleOptionFrameV2>
38 
39 #include <KLocale>
40 
41 using namespace KPIM;
42 
43 
44 class SpellCheckLineEdit::Private
45 {
46 public:
47  Private()
48  : activateLanguageMenu(true),
49  speller(0)
50  {
51  }
52  ~Private()
53  {
54  delete speller;
55  }
56 
57  QString configFile;
58  bool activateLanguageMenu;
59  Sonnet::Speller* speller;
60 };
61 
62 SpellCheckLineEdit::SpellCheckLineEdit(QWidget* parent, const QString& configFile)
63  : KTextEdit( parent ),
64  d( new Private )
65 {
66  d->configFile = configFile;
67 
68  enableFindReplace(false);
69  showTabAction(false);
70  setAcceptRichText(false);
71  setTabChangesFocus( true );
72  // widget may not be resized vertically
73  setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::Fixed));
74  setLineWrapMode(NoWrap);
75  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
76  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
77  setCheckSpellingEnabledInternal( true );
78  document()->adjustSize();
79 
80  document()->setDocumentMargin(2);
81 
82  connect(this, SIGNAL(aboutToShowContextMenu(QMenu*)), this, SLOT(insertLanguageMenu(QMenu*)));
83 }
84 
85 SpellCheckLineEdit::~SpellCheckLineEdit()
86 {
87  delete d;
88 }
89 
90 bool SpellCheckLineEdit::activateLanguageMenu() const
91 {
92  return d->activateLanguageMenu;
93 }
94 
95 void SpellCheckLineEdit::setActivateLanguageMenu(bool activate)
96 {
97  d->activateLanguageMenu = activate;
98 }
99 
100 
101 void SpellCheckLineEdit::createHighlighter()
102 {
103  Sonnet::Highlighter *highlighter = new Sonnet::Highlighter(this, d->configFile);
104  highlighter->setAutomatic( false );
105 
106  KTextEdit::setHighlighter(highlighter);
107 
108  if (!spellCheckingLanguage().isEmpty()) {
109  setSpellCheckingLanguage( spellCheckingLanguage() );
110  }
111 }
112 
113 void SpellCheckLineEdit::keyPressEvent(QKeyEvent *e)
114 {
115  if (e->key() == Qt::Key_Enter ||
116  e->key() == Qt::Key_Return ||
117  e->key() == Qt::Key_Down) {
118  emit focusDown();
119  return;
120  } else if (e->key() == Qt::Key_Up) {
121  emit focusUp();
122  return;
123  }
124  KTextEdit::keyPressEvent(e);
125 }
126 
127 QSize SpellCheckLineEdit::sizeHint() const
128 {
129  QFontMetrics fm(font());
130 
131  const int h = document()->size().toSize().height() - fm.descent() + 2 * frameWidth();
132 
133  QStyleOptionFrameV2 opt;
134  opt.initFrom(this);
135  opt.rect = QRect(0, 0, 100, h);
136  opt.lineWidth = lineWidth();
137  opt.midLineWidth = 0;
138  opt.state |= QStyle::State_Sunken;
139 
140  QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), this);
141 
142  return s;
143 }
144 
145 QSize SpellCheckLineEdit::minimumSizeHint() const
146 {
147  return sizeHint();
148 }
149 
150 void SpellCheckLineEdit::insertFromMimeData(const QMimeData * source)
151 {
152  if(!source)
153  return;
154 
155  setFocus();
156 
157  // Copy text from the clipboard (paste)
158  QString pasteText = source->text();
159 
160  // is there any text in the clipboard?
161  if (!pasteText.isEmpty()) {
162  // replace \r with \n to make xterm pastes happy
163  pasteText.replace(QLatin1Char( '\r' ),QLatin1Char( '\n' ));
164  // remove blank lines
165  while(pasteText.contains(QLatin1String( "\n\n" )))
166  pasteText.replace(QLatin1String( "\n\n" ),QLatin1String( "\n" ));
167 
168  QRegExp reTopSpace(QLatin1String( "^ *\n" ));
169  while(pasteText.contains(reTopSpace))
170  pasteText.remove(reTopSpace);
171 
172  QRegExp reBottomSpace(QLatin1String( "\n *$" ));
173  while(pasteText.contains(reBottomSpace))
174  pasteText.remove(reBottomSpace);
175 
176  // does the text contain at least one newline character?
177  if(pasteText.contains(QLatin1Char( '\n' )))
178  pasteText.replace(QLatin1Char( '\n' ), QLatin1Char(' '));
179 
180  insertPlainText(pasteText);
181  ensureCursorVisible();
182  return;
183  } else {
184  KTextEdit::insertFromMimeData(source);
185  }
186 }
187 
188 static inline QString i18n_kdelibs4(const char *str) { return ki18n(str).toString(QLatin1String("kdelibs4")); }
189 
190 void SpellCheckLineEdit::insertLanguageMenu(QMenu* contextMenu)
191 {
192  if (!checkSpellingEnabled())
193  return;
194 
195  if (!d->activateLanguageMenu)
196  return;
197 
198  QAction* spellCheckAction = 0;
199 
200  foreach (QAction* action, contextMenu->actions())
201  {
202  if (action->text() == i18n_kdelibs4("Auto Spell Check"))
203  {
204  spellCheckAction = action;
205  break;
206  }
207  }
208 
209  if (spellCheckAction)
210  {
211  QMenu* languagesMenu = new QMenu(i18n("Spell Checking Language"), contextMenu);
212  QActionGroup* languagesGroup = new QActionGroup(languagesMenu);
213  languagesGroup->setExclusive(true);
214 
215  if (!d->speller)
216  d->speller = new Sonnet::Speller();
217 
218  QMapIterator<QString, QString> i(d->speller->availableDictionaries());
219  QAction* languageAction = 0;
220 
221  while (i.hasNext())
222  {
223  i.next();
224 
225  languageAction = languagesMenu->addAction(i.key());
226  languageAction->setCheckable(true);
227  languageAction->setChecked(spellCheckingLanguage() == i.value() || (spellCheckingLanguage().isEmpty()
228  && d->speller->defaultLanguage() == i.value()));
229  languageAction->setData(i.value());
230  languageAction->setActionGroup(languagesGroup);
231  connect(languageAction, SIGNAL(triggered(bool)), this, SLOT(languageSelected()));
232  }
233 
234  contextMenu->insertMenu(spellCheckAction, languagesMenu);
235  }
236 }
237 
238 void SpellCheckLineEdit::languageSelected()
239 {
240  QAction* languageAction = static_cast<QAction*>(QObject::sender());
241  setSpellCheckingLanguage(languageAction->data().toString());
242 }
243 
244 
QAction::text
text
QWidget
KPIM::SpellCheckLineEdit::minimumSizeHint
QSize minimumSizeHint() const
Definition: spellchecklineedit.cpp:145
QActionGroup
KPIM::SpellCheckLineEdit::setActivateLanguageMenu
void setActivateLanguageMenu(bool activate)
Definition: spellchecklineedit.cpp:95
QObject::sender
QObject * sender() const
QAction::data
QVariant data() const
QSizePolicy
QMenu::addAction
void addAction(QAction *action)
QFontMetrics::descent
int descent() const
QFontMetrics
QString::remove
QString & remove(int position, int n)
QMimeData
QApplication::globalStrut
QSize globalStrut()
QStyleOption::initFrom
void initFrom(const QWidget *widget)
KPIM::SpellCheckLineEdit::insertLanguageMenu
void insertLanguageMenu(QMenu *contextMenu)
Definition: spellchecklineedit.cpp:190
QRegExp
QRect
QMapIterator
QMimeData::text
QString text() const
QMenu::insertMenu
QAction * insertMenu(QAction *before, QMenu *menu)
KPIM::SpellCheckLineEdit::createHighlighter
void createHighlighter()
Definition: spellchecklineedit.cpp:101
QString::isEmpty
bool isEmpty() const
KPIM::SpellCheckLineEdit::languageSelected
void languageSelected()
Definition: spellchecklineedit.cpp:238
QString
KPIM::SpellCheckLineEdit::sizeHint
QSize sizeHint() const
Definition: spellchecklineedit.cpp:127
QKeyEvent::key
int key() const
QMenu
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QSize
QLatin1Char
i18n_kdelibs4
static QString i18n_kdelibs4(const char *str)
Definition: spellchecklineedit.cpp:188
KPIM::SpellCheckLineEdit::~SpellCheckLineEdit
~SpellCheckLineEdit()
Destructor.
Definition: spellchecklineedit.cpp:85
KPIM::SpellCheckLineEdit::activateLanguageMenu
bool activateLanguageMenu() const
Definition: spellchecklineedit.cpp:90
QString::replace
QString & replace(int position, int n, QChar after)
QKeyEvent
KPIM::SpellCheckLineEdit::SpellCheckLineEdit
SpellCheckLineEdit(QWidget *parent, const QString &configFile)
Constructs a SpellCheckLineEdit object.
Definition: spellchecklineedit.cpp:62
spellchecklineedit.h
QLatin1String
KPIM::SpellCheckLineEdit::focusDown
void focusDown()
QAction
KPIM::SpellCheckLineEdit::keyPressEvent
void keyPressEvent(QKeyEvent *)
Definition: spellchecklineedit.cpp:113
QWidget::actions
QList< QAction * > actions() const
QActionGroup::setExclusive
void setExclusive(bool)
QStyleOptionFrameV2
QVariant::toString
QString toString() const
KPIM::SpellCheckLineEdit::focusUp
void focusUp()
Emitted when the user uses the up arrow in the first line.
KPIM::SpellCheckLineEdit::insertFromMimeData
void insertFromMimeData(const QMimeData *source)
Definition: spellchecklineedit.cpp:150
KTextEdit
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:50 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal