KTextAddons

grammarresulttextedit.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "grammarresulttextedit.h"
8#include "grammarresultutil.h"
9#include "textgrammarcheck_debug.h"
10#include <KStatefulBrush>
11
12#include <KLocalizedString>
13#include <KStandardAction>
14#include <QDesktopServices>
15
16#include <KColorScheme>
17#include <QAction>
18#include <QApplication>
19#include <QContextMenuEvent>
20#include <QMenu>
21#include <QPainter>
22using namespace TextGrammarCheck;
23GrammarResultTextEdit::GrammarResultTextEdit(QWidget *parent)
24 : QTextEdit(parent)
25{
26 setReadOnly(true);
27 setAcceptRichText(false);
28 generalPaletteChanged();
29}
30
31GrammarResultTextEdit::~GrammarResultTextEdit() = default;
32
33void GrammarResultTextEdit::paintEvent(QPaintEvent *event)
34{
35 if (document()->isEmpty()) {
36 const QString label = i18n("Any text to check.");
37
38 QPainter p(viewport());
39
40 QFont font = p.font();
41 font.setItalic(true);
42 p.setFont(font);
43
44 if (!mTextColor.isValid()) {
45 generalPaletteChanged();
46 }
47 p.setPen(mTextColor);
48
49 p.drawText(QRect(0, 0, width(), height()), Qt::AlignCenter, label);
50 } else {
52 }
53}
54
55bool GrammarResultTextEdit::event(QEvent *ev)
56{
58 generalPaletteChanged();
59 }
60 return QTextEdit::event(ev);
61}
62
63void GrammarResultTextEdit::generalPaletteChanged()
64{
65 const QPalette palette = viewport()->palette();
66 QColor color = palette.text().color();
67 color.setAlpha(128);
68 mTextColor = color;
70 mNegativeTextColor = bgBrush.brush(palette).color();
71}
72
73void GrammarResultTextEdit::applyGrammarResult(const QVector<GrammarError> &infos)
74{
75 GrammarResultUtil::applyGrammarResult(infos, document(), mNegativeTextColor);
76}
77
78void GrammarResultTextEdit::contextMenuEvent(QContextMenuEvent *event)
79{
81 if (popup) {
83 if (cursor.charFormat().hasProperty(GrammarResultUtil::TextInfo::ReplaceFormatInfo)) {
84 const auto act = cursor.charFormat().property(GrammarResultUtil::TextInfo::ReplaceFormatInfo).value<TextGrammarCheck::GrammarAction>();
85 const QStringList sugg = act.suggestions();
86 if (!sugg.isEmpty()) {
87 popup->addSeparator();
88 QMenu *popupReplacement = popup->addMenu(i18n("Replacement"));
89 for (const QString &str : sugg) {
90 QAction *actReplacement = popupReplacement->addAction(str);
91 connect(actReplacement, &QAction::triggered, this, [this, act, str]() {
92 slotReplaceWord(act, str);
93 });
94 }
95 const QStringList lst = act.infoUrls();
96 if (!lst.isEmpty()) {
97 QMenu *popupUrlInfo = popup->addMenu(i18n("Online Grammar Information"));
98 for (const QString &str : lst) {
99 QAction *actUrlInfo = popupUrlInfo->addAction(str);
100 connect(actUrlInfo, &QAction::triggered, this, [this, str]() {
101 slotOpenGrammarUrlInfo(str);
102 });
103 }
104 }
105 } else {
106 qCDebug(TEXTGRAMMARCHECK_LOG) << " no suggestion " << act;
107 }
108 }
109 popup->addSeparator();
110 QAction *checkAgainAct = popup->addAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18n("Check Again"));
111 connect(checkAgainAct, &QAction::triggered, this, &GrammarResultTextEdit::checkAgain);
112 popup->addSeparator();
113 QAction *configureAct = popup->addAction(QIcon::fromTheme(QStringLiteral("settings-configure")), i18n("Configureā€¦"));
114 connect(configureAct, &QAction::triggered, this, &GrammarResultTextEdit::configure);
115 popup->addSeparator();
116 QAction *closeAct = KStandardAction::close(this, &GrammarResultTextEdit::closeChecker, this);
117 closeAct->setShortcut({});
118 popup->addAction(closeAct);
119 popup->exec(event->globalPos());
120 delete popup;
121 }
122}
123
124void GrammarResultTextEdit::slotOpenGrammarUrlInfo(const QString &url)
125{
127}
128
129void GrammarResultTextEdit::slotReplaceWord(const TextGrammarCheck::GrammarAction &act, const QString &replacementWord)
130{
131 TextGrammarCheck::GrammarAction actWithReplacement = act;
132 actWithReplacement.setReplacement(replacementWord);
133 GrammarResultUtil::replaceWord(actWithReplacement, replacementWord, document());
134 Q_EMIT replaceText(actWithReplacement);
135}
136
137#include "moc_grammarresulttextedit.cpp"
QBrush brush(const QPalette &) const
QString i18n(const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
QAction * close(const QObject *recvr, const char *slot, QObject *parent)
QString label(StandardShortcut id)
virtual bool event(QEvent *event) override
QWidget * viewport() const const
void setShortcut(const QKeySequence &shortcut)
void triggered(bool checked)
const QColor & color() const const
bool isValid() const const
void setAlpha(int alpha)
bool openUrl(const QUrl &url)
ApplicationPaletteChange
Type type() const const
void setItalic(bool enable)
QIcon fromTheme(const QString &name)
bool isEmpty() const const
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * addMenu(QMenu *menu)
QAction * addSeparator()
QAction * exec()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
AlignCenter
QMenu * createStandardContextMenu()
QTextCursor cursorForPosition(const QPoint &pos) const const
virtual void paintEvent(QPaintEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.