File indexing completed on 2024-05-12 05:09:48

0001 /***************************************************************************
0002     Copyright (C) 2006-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "lineedit.h"
0026 
0027 #include <KStandardAction>
0028 #include <KActionCollection>
0029 #include <sonnet_version.h>
0030 #include <Sonnet/Dialog>
0031 #include <Sonnet/BackgroundChecker>
0032 
0033 #include <QMenu>
0034 #include <QContextMenuEvent>
0035 
0036 using Tellico::GUI::LineEdit;
0037 
0038 LineEdit::LineEdit(QWidget* parent_) : KLineEdit(parent_) //krazy:exclude=qclasses
0039     , m_allowSpellCheck(false)
0040     , m_enableSpellCheck(true)
0041     , m_sonnetDialog(nullptr) {
0042   m_spellAction = KStandardAction::spelling(this, &LineEdit::slotCheckSpelling, new KActionCollection(this));
0043 }
0044 
0045 void LineEdit::contextMenuEvent(QContextMenuEvent* event_) {
0046   QMenu* menu = createStandardContextMenu();
0047 
0048   if(!menu) {
0049     return;
0050   }
0051 
0052   if(m_allowSpellCheck && echoMode() == Normal && !isReadOnly()) {
0053     menu->addSeparator();
0054     menu->addAction(m_spellAction);
0055     m_spellAction->setEnabled(m_enableSpellCheck && !text().isEmpty());
0056   }
0057 
0058   menu->exec(event_->globalPos());
0059   delete menu;
0060 }
0061 
0062 void LineEdit::slotCheckSpelling() {
0063   delete m_sonnetDialog;
0064   m_sonnetDialog = new Sonnet::Dialog(new Sonnet::BackgroundChecker(this), this);
0065 
0066 
0067 #if SONNET_VERSION < QT_VERSION_CHECK(5, 65, 0)
0068   void (Sonnet::Dialog::* doneString)(const QString&) = &Sonnet::Dialog::done;
0069 #else
0070   void (Sonnet::Dialog::* doneString)(const QString&) = &Sonnet::Dialog::spellCheckDone;
0071 #endif
0072   connect(m_sonnetDialog, doneString,
0073           this, &LineEdit::slotSpellCheckDone);
0074   connect(m_sonnetDialog, &Sonnet::Dialog::misspelling,
0075           this, &LineEdit::spellCheckerMisspelling);
0076   connect(m_sonnetDialog, &Sonnet::Dialog::replace,
0077           this, &LineEdit::spellCheckerCorrected);
0078 
0079   if(hasSelectedText()) {
0080     m_sonnetDialog->setBuffer(selectedText());
0081   } else {
0082     m_sonnetDialog->setBuffer(text());
0083   }
0084 
0085   m_sonnetDialog->show();
0086 }
0087 
0088 void LineEdit::slotSpellCheckDone(const QString& newText) {
0089   if(newText != text()) {
0090     setText(newText);
0091   }
0092   m_sonnetDialog->hide();
0093   m_sonnetDialog->deleteLater();
0094   m_sonnetDialog = nullptr;
0095 }
0096 
0097 void LineEdit::spellCheckerMisspelling(const QString &text, int pos) {
0098   setSelection(pos, pos + text.length());
0099 }
0100 
0101 void LineEdit::spellCheckerCorrected(const QString& oldWord, int pos, const QString& newWord) {
0102   if(oldWord != newWord) {
0103     setSelection(pos, pos + oldWord.length());
0104     insert(newWord);
0105   }
0106 }