Skip to content

Commit

Permalink
TextEditChecker: move non-virtual private methods to d-pointer
Browse files Browse the repository at this point in the history
This also removes few static_cast of nullptr, as the abiguity is gone.
  • Loading branch information
pinotree committed Feb 27, 2021
1 parent df25a51 commit dc6fe56
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
2 changes: 0 additions & 2 deletions src/QtSpell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,7 @@ public slots:
QString getWord(int pos, int* start = 0, int* end = 0) const;
void insertWord(int start, int end, const QString& word);
bool isAttached() const;
void setTextEdit(TextEditProxy* textEdit);
bool eventFilter(QObject *obj, QEvent *event);
bool noSpellingPropertySet(const QTextCursor& cursor) const;

private slots:
void slotShowContextMenu(const QPoint& pos);
Expand Down
72 changes: 37 additions & 35 deletions src/TextEditChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,35 @@ TextEditChecker::TextEditChecker(QObject* parent)

TextEditChecker::~TextEditChecker()
{
setTextEdit(static_cast<TextEditProxy*>(nullptr));
Q_D(TextEditChecker);
d->setTextEdit(nullptr);
}

void TextEditChecker::setTextEdit(QTextEdit* textEdit)
{
setTextEdit(textEdit ? new TextEditProxyT<QTextEdit>(textEdit) : static_cast<TextEditProxyT<QTextEdit>*>(nullptr));
Q_D(TextEditChecker);
d->setTextEdit(textEdit ? new TextEditProxyT<QTextEdit>(textEdit) : nullptr);
}

void TextEditChecker::setTextEdit(QPlainTextEdit* textEdit)
{
setTextEdit(textEdit ? new TextEditProxyT<QPlainTextEdit>(textEdit) : static_cast<TextEditProxyT<QPlainTextEdit>*>(nullptr));
Q_D(TextEditChecker);
d->setTextEdit(textEdit ? new TextEditProxyT<QPlainTextEdit>(textEdit) : nullptr);
}

void TextEditChecker::setTextEdit(TextEditProxy *textEdit)
void TextEditCheckerPrivate::setTextEdit(TextEditProxy *newTextEdit)
{
Q_D(TextEditChecker);
if(d->textEdit){
disconnect(d->textEdit, &TextEditProxy::editDestroyed, this, &TextEditChecker::slotDetachTextEdit);
disconnect(d->textEdit, &TextEditProxy::textChanged, this, &TextEditChecker::slotCheckDocumentChanged);
disconnect(d->textEdit, &TextEditProxy::customContextMenuRequested, this, &TextEditChecker::slotShowContextMenu);
disconnect(d->textEdit->document(), &QTextDocument::contentsChange, this, &TextEditChecker::slotCheckRange);
d->textEdit->setContextMenuPolicy(d->oldContextMenuPolicy);
d->textEdit->removeEventFilter(this);
Q_Q(TextEditChecker);
if(textEdit){
QObject::disconnect(textEdit, &TextEditProxy::editDestroyed, q, &TextEditChecker::slotDetachTextEdit);
QObject::disconnect(textEdit, &TextEditProxy::textChanged, q, &TextEditChecker::slotCheckDocumentChanged);
QObject::disconnect(textEdit, &TextEditProxy::customContextMenuRequested, q, &TextEditChecker::slotShowContextMenu);
QObject::disconnect(textEdit->document(), &QTextDocument::contentsChange, q, &TextEditChecker::slotCheckRange);
textEdit->setContextMenuPolicy(oldContextMenuPolicy);
textEdit->removeEventFilter(q);

// Remove spelling format
QTextCursor cursor = d->textEdit->textCursor();
QTextCursor cursor = textEdit->textCursor();
cursor.movePosition(QTextCursor::Start);
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
QTextCharFormat fmt = cursor.charFormat();
Expand All @@ -142,22 +145,22 @@ void TextEditChecker::setTextEdit(TextEditProxy *textEdit)
fmt.setUnderlineStyle(defaultFormat.underlineStyle());
cursor.setCharFormat(fmt);
}
bool undoWasEnabled = d->undoRedoStack != nullptr;
setUndoRedoEnabled(false);
delete d->textEdit;
d->document = nullptr;
d->textEdit = textEdit;
if(d->textEdit){
d->document = d->textEdit->document();
connect(d->textEdit, &TextEditProxy::editDestroyed, this, &TextEditChecker::slotDetachTextEdit);
connect(d->textEdit, &TextEditProxy::textChanged, this, &TextEditChecker::slotCheckDocumentChanged);
connect(d->textEdit, &TextEditProxy::customContextMenuRequested, this, &TextEditChecker::slotShowContextMenu);
connect(d->textEdit->document(), &QTextDocument::contentsChange, this, &TextEditChecker::slotCheckRange);
d->oldContextMenuPolicy = d->textEdit->contextMenuPolicy();
setUndoRedoEnabled(undoWasEnabled);
d->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
d->textEdit->installEventFilter(this);
checkSpelling();
bool undoWasEnabled = undoRedoStack != nullptr;
q->setUndoRedoEnabled(false);
delete textEdit;
document = nullptr;
textEdit = newTextEdit;
if(textEdit){
document = textEdit->document();
QObject::connect(textEdit, &TextEditProxy::editDestroyed, q, &TextEditChecker::slotDetachTextEdit);
QObject::connect(textEdit, &TextEditProxy::textChanged, q, &TextEditChecker::slotCheckDocumentChanged);
QObject::connect(textEdit, &TextEditProxy::customContextMenuRequested, q, &TextEditChecker::slotShowContextMenu);
QObject::connect(textEdit->document(), &QTextDocument::contentsChange, q, &TextEditChecker::slotCheckRange);
oldContextMenuPolicy = textEdit->contextMenuPolicy();
q->setUndoRedoEnabled(undoWasEnabled);
textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
textEdit->installEventFilter(q);
q->checkSpelling();
}
}

Expand Down Expand Up @@ -215,7 +218,7 @@ void TextEditChecker::checkSpelling(int start, int end)
cursor.moveWordEnd(QTextCursor::KeepAnchor);
bool correct;
QString word = cursor.selectedText();
if(noSpellingPropertySet(cursor)) {
if(d->noSpellingPropertySet(cursor)) {
correct = true;
qDebug() << "Skipping word:" << word << "(" << cursor.anchor() << "-" << cursor.position() << ")";
} else {
Expand All @@ -241,19 +244,18 @@ void TextEditChecker::checkSpelling(int start, int end)
d->textEdit->document()->blockSignals(false);
}

bool TextEditChecker::noSpellingPropertySet(const QTextCursor &cursor) const
bool TextEditCheckerPrivate::noSpellingPropertySet(const QTextCursor &cursor) const
{
Q_D(const TextEditChecker);
if(d->noSpellingProperty < QTextFormat::UserProperty) {
if(noSpellingProperty < QTextFormat::UserProperty) {
return false;
}
if(cursor.charFormat().intProperty(d->noSpellingProperty) == 1) {
if(cursor.charFormat().intProperty(noSpellingProperty) == 1) {
return true;
}
const QVector<QTextLayout::FormatRange>& formats = cursor.block().layout()->formats();
int pos = cursor.positionInBlock();
foreach(const QTextLayout::FormatRange& range, formats) {
if(pos > range.start && pos <= range.start + range.length && range.format.intProperty(d->noSpellingProperty) == 1) {
if(pos > range.start && pos <= range.start + range.length && range.format.intProperty(noSpellingProperty) == 1) {
return true;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/TextEditChecker_p.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class TextEditCheckerPrivate : public CheckerPrivate
TextEditCheckerPrivate();
virtual ~TextEditCheckerPrivate();

void setTextEdit(TextEditProxy* newTextEdit);
bool noSpellingPropertySet(const QTextCursor& cursor) const;

TextEditProxy* textEdit = nullptr;
QTextDocument* document = nullptr;
UndoRedoStack* undoRedoStack = nullptr;
Expand Down

0 comments on commit dc6fe56

Please sign in to comment.