Commit d30fed99 authored by Martin Seher's avatar Martin Seher Committed by Milian Wolff
Browse files

Apply color scheme to code browser tooltip

do not force black on white palette
adept colors when dark color scheme is used
  - make dark text colors brighter
  - when background color is set keep bg and fg colors

Also changes the colors in the quick open menu

BUG: 257378
parent 165f50cd
......@@ -16,6 +16,9 @@
#include <KLocalizedString>
#include "../duchainlock.h"
#include <util/widgetcolorizer.h>
#include <debug.h>
namespace {
......@@ -85,13 +88,6 @@ void AbstractNavigationWidget::initBrowser(int height)
Q_UNUSED(height);
d->m_browser = new QTextBrowser;
// since we can embed arbitrary HTML we have to make sure it stays readable by forcing a black-white palette
QPalette p;
p.setColor(QPalette::AlternateBase, Qt::white);
p.setColor(QPalette::Base, Qt::white);
p.setColor(QPalette::Text, Qt::black);
d->m_browser->setPalette(p);
d->m_browser->setOpenLinks(false);
d->m_browser->setOpenExternalLinks(false);
......@@ -204,6 +200,8 @@ void AbstractNavigationWidget::update()
d->m_browser->setHtml(html);
WidgetColorizer::convertDocumentToDarkTheme(d->m_browser->document());
d->m_currentText = html;
d->m_idealTextSize = QSize();
......
......@@ -14,6 +14,10 @@
#include <QPainter>
#include <QPalette>
#include <QTreeView>
#include <QTextDocument>
#include <QApplication>
#include <QTextCharFormat>
#include <QTextFrame>
using namespace KDevelop;
......@@ -64,3 +68,70 @@ bool WidgetColorizer::colorizeByProject()
{
return KSharedConfig::openConfig()->group("UiSettings").readEntry("ColorizeByProject", true);
}
void WidgetColorizer::convertDocumentToDarkTheme(QTextDocument* doc)
{
const auto palette = QApplication::palette();
const auto bgcolor = palette.color(QPalette::Base);
const auto fgcolor = palette.color(QPalette::Text);
if (fgcolor.value() < bgcolor.value())
return;
auto cur = QTextCursor(doc);
std::vector<std::pair<QTextCursor, QTextCharFormat> > cursors;
std::function<void(QTextFrame*, QColor, QColor, bool)> iterateFrame = [&iterateFrame, &cur, &cursors](QTextFrame* frame, QColor fgcolor, QColor bgcolor, bool bgSet) {
for (auto it = frame->begin(); it != frame->end(); ++it) {
if (auto frame = it.currentFrame()) {
auto fmt = it.currentFrame()->frameFormat();
if (fmt.hasProperty(QTextFormat::BackgroundBrush)) {
auto bg = fmt.background().color();
iterateFrame(frame, fgcolor, bg, true);
} else {
iterateFrame(frame, fgcolor, bgcolor, bgSet);
}
}
if (it.currentBlock().isValid()) {
for (auto jt = it.currentBlock().begin(); jt != it.currentBlock().end(); ++jt) {
auto fragment = jt.fragment();
auto text = fragment.text().trimmed();
if (!text.isEmpty()) {
auto fmt = fragment.charFormat();
if (!bgSet && !fmt.hasProperty(QTextFormat::BackgroundBrush)) {
if (!fmt.hasProperty(QTextFormat::ForegroundBrush) || fmt.foreground().color() == Qt::black)
fmt.setForeground(fgcolor);
else if (fmt.foreground().color().valueF() < 0.7)
fmt.setForeground(WidgetColorizer::blendForeground(fmt.foreground().color(), 1.0, fgcolor, fmt.background().color()));
} else {
QColor bg = fmt.hasProperty(QTextFormat::BackgroundBrush) ? fmt.background().color() : bgcolor;
QColor fg = fmt.hasProperty(QTextFormat::ForegroundBrush) ? fmt.foreground().color() : fgcolor;
if (bg.valueF() > 0.3) {
if (fmt.hasProperty(QTextFormat::BackgroundBrush) && bg.valueF() > 0.5 && bg.hsvSaturationF() < 0.08) {
bg = QColor::fromHsv(bg.hsvHue(), bg.hsvSaturation(), 255 - bg.value() );
fmt.setBackground(bg);
if (fg.valueF() < 0.7) {
fmt.setForeground(WidgetColorizer::blendForeground(fg, 1.0, fgcolor, bg));
}
} else if (fg.valueF() > 0.5 && fg.hsvSaturationF() < 0.08) {
fg = QColor::fromHsv(fg.hsvHue(), fg.hsvSaturation(), 255 - fg.value() );
fmt.setForeground(fg);
}
}
}
cur.setPosition(fragment.position());
cur.setPosition(fragment.position() + fragment.length(), QTextCursor::KeepAnchor);
cursors.push_back(std::make_pair(cur, fmt));
}
}
}
}
};
iterateFrame(doc->rootFrame(), fgcolor, bgcolor, false);
for (auto p : cursors)
p.first.setCharFormat(p.second);
}
......@@ -16,6 +16,7 @@ class QPainter;
class QRect;
class QPalette;
class QTreeView;
class QTextDocument;
namespace KDevelop {
......@@ -68,6 +69,11 @@ KDEVPLATFORMUTIL_EXPORT QColor colorForId(uint id, const QPalette& activePalette
* files belonging to projects.
*/
KDEVPLATFORMUTIL_EXPORT bool colorizeByProject();
/**
* Convert colors in the @p doc to make them readable if the current palette is using a dark theme
*/
KDEVPLATFORMUTIL_EXPORT void convertDocumentToDarkTheme(QTextDocument* doc);
}
}
......@@ -40,16 +40,18 @@ void ExpandingTree::drawRow(QPainter* painter, const QStyleOptionViewItem& optio
if (eModel->isPartiallyExpanded(sourceIndex) != ExpandingWidgetModel::ExpansionType::NotExpanded) {
QRect rect = eModel->partialExpandRect(sourceIndex);
if (rect.isValid()) {
painter->fillRect(rect, QBrush(0xffffffff));
QStyleOption opt;
QAbstractTextDocumentLayout::PaintContext ctx;
// since arbitrary HTML can be shown use a black on white color scheme here
ctx.palette = QPalette(Qt::black, Qt::white);
opt.rect = rect;
style()->drawPrimitive(QStyle::PE_FrameLineEdit, &opt, painter);
ctx.clip = QRectF(0, 0, rect.width(), rect.height());
painter->setViewTransformEnabled(true);
painter->translate(rect.left(), rect.top());
m_drawText.setHtml(eModel->partialExpandText(sourceIndex));
WidgetColorizer::convertDocumentToDarkTheme(&m_drawText);
m_drawText.setPageSize(QSizeF(rect.width(), rect.height()));
m_drawText.documentLayout()->draw(painter, ctx);
......
Supports Markdown
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