Commit 45b950d7 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Make sure foreground is colored correctly and theme updates are caught correctly

parent ff1eb3a9
......@@ -144,12 +144,11 @@ void MatchModel::addMatches(const QUrl &fileUrl, const QVector<KateSearchMatch>
endInsertRows();
}
void MatchModel::setMatchColors(const QColor &foreground, const QColor &background, const QColor &replaceBackground, const QColor &lineNrBackground)
void MatchModel::setMatchColors(const QString &foreground, const QString &background, const QString &replaceBackground)
{
m_foregroundColor = foreground;
m_searchBackgroundColor = background;
m_replaceHighlightColor = replaceBackground;
m_lineNumberBackgroundColor = lineNrBackground;
}
KateSearchMatch *MatchModel::matchFromIndex(const QModelIndex &matchIndex)
......@@ -446,6 +445,7 @@ static QString nbsFormated(int number, int width)
{
QString str = QString::number(number);
int strWidth = str.size();
str.reserve(width);
while (strWidth < width) {
str = QStringLiteral("&nbsp;") + str;
strWidth++;
......@@ -538,11 +538,11 @@ QString MatchModel::matchToHtmlString(const Match &match) const
matchStr = QLatin1String("<i><s>") + matchStr + QLatin1String("</s></i> ");
}
matchStr = QStringLiteral("<span style=\"background-color:%1; color:%2;\">%3</span>")
.arg(m_searchBackgroundColor.name(), m_foregroundColor.name(), matchStr);
.arg(m_searchBackgroundColor, m_foregroundColor, matchStr);
if (!replaceStr.isEmpty()) {
matchStr += QStringLiteral("<span style=\"background-color:%1; color:%2;\">%3</span>")
.arg(m_replaceHighlightColor.name(), m_foregroundColor.name(), replaceStr);
.arg(m_replaceHighlightColor, m_foregroundColor, replaceStr);
}
matchStr.replace(QLatin1Char('\n'), QStringLiteral("\\n"));
......@@ -559,8 +559,8 @@ QString MatchModel::matchToHtmlString(const Match &match) const
post = post.toHtmlEscaped();
// (line:col)[space][space] ...Line text pre [highlighted match] Line text post....
QString displayText = QStringLiteral("<span style=\"background-color:%1; color:%2;\">&nbsp;<b>%3:%4:</b></span>&nbsp;")
.arg(m_lineNumberBackgroundColor.name()).arg(m_foregroundColor.name())
QString displayText = QStringLiteral("<span style=\"color:%1;\">&nbsp;<b>%2:%3</b></span>&nbsp;")
.arg(m_foregroundColor)
.arg(nbsFormated(match.range.start().line() + 1, 3))
.arg(nbsFormated(match.range.start().column() + 1, 3)) + pre + matchStr + post;
......
......@@ -83,7 +83,7 @@ public:
void setDocumentManager(KTextEditor::Application *manager);
void setMatchColors(const QColor &foreground, const QColor &background, const QColor &replaceBackground, const QColor &lineNrBackground);
void setMatchColors(const QString &foreground, const QString &background, const QString &replaceBackground);
void setSearchPlace(MatchModel::SearchPlaces searchPlace);
......@@ -169,10 +169,9 @@ private:
QVector<MatchFile> m_matchFiles;
QHash<QUrl, int> m_matchFileIndexHash;
QColor m_searchBackgroundColor;
QColor m_foregroundColor;
QColor m_replaceHighlightColor;
QColor m_lineNumberBackgroundColor;
QString m_searchBackgroundColor;
QString m_foregroundColor;
QString m_replaceHighlightColor;
Qt::CheckState m_infoCheckState = Qt::Checked;
SearchPlaces m_searchPlace = CurrentFile;
......
......@@ -52,6 +52,7 @@ void SPHtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option
painter->translate(clip.topLeft() - QPoint(0, 0));
}
QAbstractTextDocumentLayout::PaintContext pcontext;
pcontext.palette.setColor(QPalette::Text, options.palette.text().color());
doc.documentLayout()->draw(painter, pcontext);
painter->restore();
......
......@@ -176,17 +176,31 @@ Results::Results(QWidget *parent)
treeView->setItemDelegate(new SPHtmlDelegate(treeView));
treeView->setModel(&matchModel);
auto updateColors = [this](KTextEditor::Editor* e){
if (!e)
return;
const auto theme = e->theme();
auto bg = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::BackgroundColor));
auto hl = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::TextSelection));
auto search = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::SearchHighlight));
auto replace = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::ReplaceHighlight));
auto fg = QColor::fromRgba(theme.textColor(KSyntaxHighlighting::Theme::Normal));
auto pal = treeView->palette();
pal.setColor(QPalette::Base, bg);
pal.setColor(QPalette::Highlight, hl);
pal.setColor(QPalette::Text, fg);
matchModel.setMatchColors(fg.name(), search.name(), replace.name());
treeView->setPalette(pal);
emit colorsChanged();
};
auto e = KTextEditor::Editor::instance();
auto bg = QColor::fromRgba(e->theme().editorColor(KSyntaxHighlighting::Theme::BackgroundColor));
auto fg = QColor::fromRgba(e->theme().textColor(KSyntaxHighlighting::Theme::Normal));
auto hl = QColor::fromRgba(e->theme().editorColor(KSyntaxHighlighting::Theme::CurrentLine));
auto pal = treeView->palette();
pal.setColor(QPalette::Base, bg);
pal.setColor(QPalette::Highlight, hl);
pal.setColor(QPalette::Text, fg);
pal.setColor(QPalette::HighlightedText, fg);
treeView->setPalette(pal);
connect(e, &KTextEditor::Editor::configChanged, this, updateColors);
updateColors(e);
}
K_PLUGIN_FACTORY_WITH_JSON(KatePluginSearchFactory, "katesearch.json", registerPlugin<KatePluginSearch>();)
......@@ -791,7 +805,7 @@ void KatePluginSearchView::stopClicked()
* search so that if the user changes the theme, he can see the new colors
* on the next search
*/
void KatePluginSearchView::updateSearchColors()
void KatePluginSearchView::updateViewColors()
{
auto* view = m_mainWindow->activeView();
KTextEditor::ConfigInterface *ciface = qobject_cast<KTextEditor::ConfigInterface *>(view);
......@@ -800,15 +814,11 @@ void KatePluginSearchView::updateSearchColors()
QColor searchBackgroundColor = ciface->configValue(QStringLiteral("search-highlight-color")).value<QColor>();
if (!searchBackgroundColor.isValid())
searchBackgroundColor = Qt::yellow;
QColor replaceHighlightColor = ciface->configValue(QStringLiteral("replace-highlight-color")).value<QColor>();
if (!replaceHighlightColor.isValid())
replaceHighlightColor = Qt::green;
m_replaceHighlightColor = ciface->configValue(QStringLiteral("replace-highlight-color")).value<QColor>();
if (!m_replaceHighlightColor.isValid())
m_replaceHighlightColor = Qt::green;
QColor foregroundColor = view->defaultStyleAttribute(KTextEditor::dsNormal)->foreground().color();
QColor lineNrBackgroundColor = ciface->configValue(QStringLiteral("icon-border-color")).value<QColor>();
if (!lineNrBackgroundColor.isValid())
lineNrBackgroundColor = view->defaultStyleAttribute(KTextEditor::dsNormal)->background().color();
if (!m_resultAttr)
m_resultAttr = new KTextEditor::Attribute();
// reset colors at the start of search
......@@ -821,10 +831,6 @@ void KatePluginSearchView::updateSearchColors()
if (delegate) {
delegate->setDisplayFont(ciface->configValue(QStringLiteral("font")).value<QFont>());
}
m_curResults->matchModel.setMatchColors(foregroundColor,
searchBackgroundColor,
replaceHighlightColor,
lineNrBackgroundColor);
}
}
}
......@@ -896,7 +902,7 @@ void KatePluginSearchView::startSearch()
return;
}
updateSearchColors();
updateViewColors();
m_curResults->regExp = reg;
m_curResults->useRegExp = m_ui.useRegExp->isChecked();
......@@ -1020,7 +1026,7 @@ void KatePluginSearchView::startSearchWhileTyping()
if (!m_searchDiskFilesDone || !m_searchOpenFilesDone) {
return;
}
updateSearchColors();
updateViewColors();
m_isSearchAsYouType = true;
......@@ -1780,6 +1786,9 @@ void KatePluginSearchView::addTab()
}
Results *res = new Results();
connect(res, &Results::colorsChanged, this, [this](){
updateViewColors();
});
res->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
res->treeView->setRootIsDecorated(false);
......
......@@ -60,6 +60,9 @@ public:
int searchPlaceIndex = 0;
QString treeRootText;
MatchModel matchModel;
Q_SIGNALS:
void colorsChanged();
};
// This class keeps the focus inside the S&R plugin when pressing tab/shift+tab by overriding focusNextPrevChild()
......@@ -176,7 +179,7 @@ protected:
private:
QStringList filterFiles(const QStringList &files) const;
void updateSearchColors();
void updateViewColors();
void onResize(const QSize &size);
......
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