Commit f1faada8 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Fix color when we change theme

parent 34f5ad47
Pipeline #160209 failed with stages
in 2 minutes and 14 seconds
......@@ -24,6 +24,7 @@
#include <Sonnet/Dialog>
#include <sonnet/backgroundchecker.h>
#include <KColorScheme>
#include <QApplication>
#include <QClipboard>
#include <QDBusConnection>
......@@ -75,6 +76,7 @@ public:
QString spellCheckingLanguage;
QTextDocumentFragment originalDoc;
PlainTextEditor::SupportFeatures supportFeatures;
QColor mReadOnlyBackgroundColor;
int mInitialFontSize = 0;
bool customPalette = false;
bool activateLanguageMenu = true;
......@@ -88,10 +90,20 @@ PlainTextEditor::PlainTextEditor(QWidget *parent)
KCursor::setAutoHideCursor(this, true, false);
setSpellCheckingConfigFileName(QString());
d->mInitialFontSize = font().pointSize();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
connect(qApp, &QApplication::paletteChanged, this, &PlainTextEditor::regenerateColorScheme);
#endif
regenerateColorScheme();
}
PlainTextEditor::~PlainTextEditor() = default;
void PlainTextEditor::regenerateColorScheme()
{
d->mReadOnlyBackgroundColor = KColorScheme(QPalette::Disabled, KColorScheme::View).background().color();
updateReadOnlyColor();
}
void PlainTextEditor::addIgnoreWords(const QStringList &lst)
{
d->ignoreSpellCheckingWords = lst;
......@@ -291,6 +303,16 @@ bool PlainTextEditor::webShortcutSupport() const
return d->supportFeatures & AllowWebShortcut;
}
void PlainTextEditor::updateReadOnlyColor()
{
if (isReadOnly()) {
QPalette p = palette();
p.setColor(QPalette::Base, d->mReadOnlyBackgroundColor);
p.setColor(QPalette::Window, d->mReadOnlyBackgroundColor);
setPalette(p);
}
}
void PlainTextEditor::setReadOnly(bool readOnly)
{
if (!readOnly && hasFocus() && d->checkSpellingEnabled && !d->richTextDecorator) {
......@@ -303,13 +325,8 @@ void PlainTextEditor::setReadOnly(bool readOnly)
if (readOnly) {
clearDecorator();
d->customPalette = testAttribute(Qt::WA_SetPalette);
QPalette p = palette();
QColor color = p.color(QPalette::Disabled, QPalette::Window);
p.setColor(QPalette::Base, color);
p.setColor(QPalette::Window, color);
setPalette(p);
updateReadOnlyColor();
} else {
if (d->customPalette && testAttribute(Qt::WA_SetPalette)) {
QPalette p = palette();
......@@ -431,6 +448,12 @@ bool PlainTextEditor::event(QEvent *ev)
return true;
}
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
else if (e->type() == QEvent::ApplicationPaletteChange) {
regenerateColorScheme();
}
#endif
return QPlainTextEdit::event(ev);
}
......
......@@ -111,6 +111,8 @@ private:
void deleteEndOfLine();
void moveLineUpDown(bool moveUp);
void moveCursorBeginUpDown(bool moveUp);
void regenerateColorScheme();
void updateReadOnlyColor();
class PlainTextEditorPrivate;
std::unique_ptr<PlainTextEditorPrivate> const d;
};
......
......@@ -29,6 +29,7 @@
#include <texttospeech/texttospeech.h>
#endif
#include <KColorScheme>
#include <QApplication>
#include <QClipboard>
#include <QContextMenuEvent>
......@@ -100,6 +101,7 @@ public:
Sonnet::Speller *speller = nullptr;
KIO::KUriFilterSearchProviderActions *const webshortcutMenuManager;
RichTextEditor::SupportFeatures supportFeatures;
QColor mReadOnlyBackgroundColor;
int mInitialFontSize;
bool customPalette = false;
bool checkSpellingEnabled = false;
......@@ -115,10 +117,20 @@ RichTextEditor::RichTextEditor(QWidget *parent)
KCursor::setAutoHideCursor(this, true, false);
setSpellCheckingConfigFileName(QString());
d->mInitialFontSize = font().pointSize();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
connect(qApp, &QApplication::paletteChanged, this, &RichTextEditor::regenerateColorScheme);
#endif
regenerateColorScheme();
}
RichTextEditor::~RichTextEditor() = default;
void RichTextEditor::regenerateColorScheme()
{
d->mReadOnlyBackgroundColor = KColorScheme(QPalette::Disabled, KColorScheme::View).background().color();
updateReadOnlyColor();
}
void RichTextEditor::setDefaultFontSize(int val)
{
d->mInitialFontSize = val;
......@@ -393,6 +405,16 @@ void RichTextEditor::slotUndoableClear()
cursor.endEditBlock();
}
void RichTextEditor::updateReadOnlyColor()
{
if (isReadOnly()) {
QPalette p = palette();
p.setColor(QPalette::Base, d->mReadOnlyBackgroundColor);
p.setColor(QPalette::Window, d->mReadOnlyBackgroundColor);
setPalette(p);
}
}
void RichTextEditor::setReadOnly(bool readOnly)
{
if (!readOnly && hasFocus() && checkSpellingEnabled() && !d->richTextDecorator) {
......@@ -405,13 +427,8 @@ void RichTextEditor::setReadOnly(bool readOnly)
if (readOnly) {
clearDecorator();
d->customPalette = testAttribute(Qt::WA_SetPalette);
QPalette p = palette();
QColor color = p.color(QPalette::Disabled, QPalette::Window);
p.setColor(QPalette::Base, color);
p.setColor(QPalette::Window, color);
setPalette(p);
updateReadOnlyColor();
} else {
if (d->customPalette && testAttribute(Qt::WA_SetPalette)) {
QPalette p = palette();
......@@ -736,6 +753,11 @@ void RichTextEditor::wheelEvent(QWheelEvent *event)
event->accept();
return;
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
else if (e->type() == QEvent::ApplicationPaletteChange) {
regenerateColorScheme();
}
#endif
QTextEdit::wheelEvent(event);
}
......
......@@ -133,6 +133,8 @@ private:
void checkSpelling(bool force);
void moveLineUpDown(bool moveUp);
void moveCursorBeginUpDown(bool moveUp);
void regenerateColorScheme();
void updateReadOnlyColor();
class RichTextEditorPrivate;
std::unique_ptr<RichTextEditorPrivate> const d;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment