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

libkdepim

  • sources
  • kde-4.12
  • kdepim
  • libkdepim
  • widgets
spellchecklineedit.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2012-2013 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 <QContextMenuEvent>
35 #include <QStyle>
36 #include <QApplication>
37 #include <QMenu>
38 #include <QStyleOptionFrameV2>
39 
40 #include <KLocale>
41 
42 using namespace KPIM;
43 
44 
45 class SpellCheckLineEdit::Private
46 {
47 public:
48  Private()
49  : activateLanguageMenu(true),
50  speller(0)
51  {
52  }
53  ~Private()
54  {
55  delete speller;
56  }
57 
58  QString configFile;
59  bool activateLanguageMenu;
60  Sonnet::Speller* speller;
61 };
62 
63 SpellCheckLineEdit::SpellCheckLineEdit(QWidget* parent, const QString& configFile)
64  : KTextEdit( parent ),
65  d( new Private )
66 {
67  d->configFile = configFile;
68 
69  enableFindReplace(false);
70 #ifdef HAVE_SHOWTABACTION
71  showTabAction(false);
72 #endif
73  setAcceptRichText(false);
74  setTabChangesFocus( true );
75  // widget may not be resized vertically
76  setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::Fixed));
77  setLineWrapMode(NoWrap);
78  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
79  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
80  setCheckSpellingEnabledInternal( true );
81  document()->adjustSize();
82 
83  document()->setDocumentMargin(2);
84 
85  connect(this, SIGNAL(aboutToShowContextMenu(QMenu*)), this, SLOT(insertLanguageMenu(QMenu*)));
86 }
87 
88 SpellCheckLineEdit::~SpellCheckLineEdit()
89 {
90  delete d;
91 }
92 
93 bool SpellCheckLineEdit::activateLanguageMenu() const
94 {
95  return d->activateLanguageMenu;
96 }
97 
98 void SpellCheckLineEdit::setActivateLanguageMenu(bool activate)
99 {
100  d->activateLanguageMenu = activate;
101 }
102 
103 
104 void SpellCheckLineEdit::createHighlighter()
105 {
106  Sonnet::Highlighter *highlighter = new Sonnet::Highlighter(this, d->configFile);
107  highlighter->setAutomatic( false );
108 
109  KTextEdit::setHighlighter(highlighter);
110 
111  if (!spellCheckingLanguage().isEmpty()) {
112  setSpellCheckingLanguage( spellCheckingLanguage() );
113  }
114 }
115 
116 void SpellCheckLineEdit::keyPressEvent(QKeyEvent *e)
117 {
118  if (e->key() == Qt::Key_Enter ||
119  e->key() == Qt::Key_Return ||
120  e->key() == Qt::Key_Down) {
121  emit focusDown();
122  return;
123  } else if (e->key() == Qt::Key_Up) {
124  emit focusUp();
125  return;
126  }
127  KTextEdit::keyPressEvent(e);
128 }
129 
130 QSize SpellCheckLineEdit::sizeHint() const
131 {
132  QFontMetrics fm(font());
133 
134  const int h = document()->size().toSize().height() - fm.descent() + 2 * frameWidth();
135 
136  QStyleOptionFrameV2 opt;
137  opt.initFrom(this);
138  opt.rect = QRect(0, 0, 100, h);
139  opt.lineWidth = lineWidth();
140  opt.midLineWidth = 0;
141  opt.state |= QStyle::State_Sunken;
142 
143  QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), this);
144 
145  return s;
146 }
147 
148 QSize SpellCheckLineEdit::minimumSizeHint() const
149 {
150  return sizeHint();
151 }
152 
153 void SpellCheckLineEdit::insertFromMimeData(const QMimeData * source)
154 {
155  if(!source)
156  return;
157 
158  setFocus();
159 
160  // Copy text from the clipboard (paste)
161  QString pasteText = source->text();
162 
163  // is there any text in the clipboard?
164  if (!pasteText.isEmpty()) {
165  // replace \r with \n to make xterm pastes happy
166  pasteText.replace(QLatin1Char( '\r' ),QLatin1Char( '\n' ));
167  // remove blank lines
168  while(pasteText.contains(QLatin1String( "\n\n" )))
169  pasteText.replace(QLatin1String( "\n\n" ),QLatin1String( "\n" ));
170 
171  QRegExp reTopSpace(QLatin1String( "^ *\n" ));
172  while(pasteText.contains(reTopSpace))
173  pasteText.remove(reTopSpace);
174 
175  QRegExp reBottomSpace(QLatin1String( "\n *$" ));
176  while(pasteText.contains(reBottomSpace))
177  pasteText.remove(reBottomSpace);
178 
179  // does the text contain at least one newline character?
180  if(pasteText.contains(QLatin1Char( '\n' )))
181  pasteText.remove(QLatin1Char( '\n' ));
182 
183  insertPlainText(pasteText);
184  ensureCursorVisible();
185  return;
186  } else {
187  KTextEdit::insertFromMimeData(source);
188  }
189 }
190 
191 static inline QString i18n_kdelibs4(const char *str) { return ki18n(str).toString(QLatin1String("kdelibs4")); }
192 
193 void SpellCheckLineEdit::insertLanguageMenu(QMenu* contextMenu)
194 {
195  if (!checkSpellingEnabled())
196  return;
197 
198  if (!d->activateLanguageMenu)
199  return;
200 
201  QAction* spellCheckAction = 0;
202 
203  foreach (QAction* action, contextMenu->actions())
204  {
205  if (action->text() == i18n_kdelibs4("Auto Spell Check"))
206  {
207  spellCheckAction = action;
208  break;
209  }
210  }
211 
212  if (spellCheckAction)
213  {
214  QMenu* languagesMenu = new QMenu(i18n("Spell Checking Language"), contextMenu);
215  QActionGroup* languagesGroup = new QActionGroup(languagesMenu);
216  languagesGroup->setExclusive(true);
217 
218  if (!d->speller)
219  d->speller = new Sonnet::Speller();
220 
221  QMapIterator<QString, QString> i(d->speller->availableDictionaries());
222  QAction* languageAction = 0;
223 
224  while (i.hasNext())
225  {
226  i.next();
227 
228  languageAction = languagesMenu->addAction(i.key());
229  languageAction->setCheckable(true);
230  languageAction->setChecked(spellCheckingLanguage() == i.value() || (spellCheckingLanguage().isEmpty()
231  && d->speller->defaultLanguage() == i.value()));
232  languageAction->setData(i.value());
233  languageAction->setActionGroup(languagesGroup);
234  connect(languageAction, SIGNAL(triggered(bool)), this, SLOT(languageSelected()));
235  }
236 
237  contextMenu->insertMenu(spellCheckAction, languagesMenu);
238  }
239 }
240 
241 void SpellCheckLineEdit::languageSelected()
242 {
243  QAction* languageAction = static_cast<QAction*>(QObject::sender());
244  setSpellCheckingLanguage(languageAction->data().toString());
245 }
246 
247 
248 #include "spellchecklineedit.moc"
KPIM::SpellCheckLineEdit::minimumSizeHint
QSize minimumSizeHint() const
Definition: spellchecklineedit.cpp:148
KPIM::SpellCheckLineEdit::setActivateLanguageMenu
void setActivateLanguageMenu(bool activate)
Definition: spellchecklineedit.cpp:98
QWidget
KPIM::SpellCheckLineEdit::insertLanguageMenu
void insertLanguageMenu(QMenu *contextMenu)
Definition: spellchecklineedit.cpp:193
KPIM::SpellCheckLineEdit::createHighlighter
void createHighlighter()
Definition: spellchecklineedit.cpp:104
KPIM::SpellCheckLineEdit::languageSelected
void languageSelected()
Definition: spellchecklineedit.cpp:241
QMimeData
KPIM::SpellCheckLineEdit::sizeHint
QSize sizeHint() const
Definition: spellchecklineedit.cpp:130
i18n_kdelibs4
static QString i18n_kdelibs4(const char *str)
Definition: spellchecklineedit.cpp:191
KPIM::SpellCheckLineEdit::~SpellCheckLineEdit
~SpellCheckLineEdit()
Destructor.
Definition: spellchecklineedit.cpp:88
KPIM::SpellCheckLineEdit::activateLanguageMenu
bool activateLanguageMenu() const
Definition: spellchecklineedit.cpp:93
QMenu
KPIM::SpellCheckLineEdit::SpellCheckLineEdit
SpellCheckLineEdit(QWidget *parent, const QString &configFile)
Constructs a SpellCheckLineEdit object.
Definition: spellchecklineedit.cpp:63
spellchecklineedit.h
KPIM::SpellCheckLineEdit::focusDown
void focusDown()
KPIM::SpellCheckLineEdit::keyPressEvent
void keyPressEvent(QKeyEvent *)
Definition: spellchecklineedit.cpp:116
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:153
KTextEdit
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:03 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

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