Commit 0377d9d0 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Introduce ktexteditor utils

The idea is to place common ktexteditor related utils here to reduce
duplication.
parent f52f75fc
Pipeline #121139 passed with stage
in 4 minutes and 40 seconds
......@@ -15,6 +15,7 @@
#include <QDebug>
#include <kfts_fuzzy_match.h>
#include <ktexteditor_utils.h>
class LineNumberDelegate : public QStyledItemDelegate
{
......@@ -281,6 +282,10 @@ AsmView::AsmView(QWidget *parent)
c = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::BackgroundColor));
palette.setColor(QPalette::Base, c);
setPalette(palette);
auto model = static_cast<AsmViewModel *>(this->model());
model->setFont(Utils::editorFont());
};
updateColors();
connect(KTextEditor::Editor::instance(), &KTextEditor::Editor::configChanged, this, updateColors);
......
......@@ -500,11 +500,6 @@ void CEWidget::doCompile()
return;
}
if (auto ciface = qobject_cast<KTextEditor::ConfigInterface *>(m_textEditor)) {
auto font = ciface->configValue(QStringLiteral("font")).value<QFont>();
m_model->setFont(font);
}
const QString text = doc->text();
if (text.isEmpty()) {
return;
......
......@@ -28,6 +28,8 @@
#include <KSyntaxHighlighting/Repository>
#include <KSyntaxHighlighting/State>
#include <ktexteditor_utils.h>
using KSyntaxHighlighting::AbstractHighlighter;
using KSyntaxHighlighting::Format;
......@@ -167,6 +169,8 @@ public:
const QColor normal = theme.textColor(KSyntaxHighlighting::Theme::Normal);
pal.setColor(QPalette::Text, normal);
setPalette(pal);
setFont(Utils::editorFont());
};
updateColors(KTextEditor::Editor::instance());
connect(KTextEditor::Editor::instance(), &KTextEditor::Editor::configChanged, this, updateColors);
......@@ -230,10 +234,6 @@ public:
m_view->focusProxy()->removeEventFilter(this);
}
m_view = view;
// update font
auto ciface = qobject_cast<KTextEditor::ConfigInterface *>(m_view);
auto font = ciface->configValue(QStringLiteral("font")).value<QFont>();
setFont(font);
m_view->focusProxy()->installEventFilter(this);
}
......
......@@ -16,3 +16,8 @@ target_sources(
plugin.qrc
)
target_include_directories(
katebuildplugin
PUBLIC
${CMAKE_SOURCE_DIR}/shared
)
......@@ -54,6 +54,8 @@
#include "SelectTargetView.h"
#include <ktexteditor_utils.h>
K_PLUGIN_FACTORY_WITH_JSON(KateBuildPluginFactory, "katebuildplugin.json", registerPlugin<KateBuildPlugin>();)
static const QString DefConfigCmd = QStringLiteral("cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ../");
......@@ -774,15 +776,9 @@ bool KateBuildView::startProcess(const QString &dir, const QString &command)
m_buildUi.displayModeSlider->setValue(0);
m_win->showToolView(m_toolView);
KTextEditor::View *kv = m_win->activeView();
if (kv) {
KTextEditor::ConfigInterface *ciface = qobject_cast<KTextEditor::ConfigInterface *>(kv);
if (ciface) {
QFont font = ciface->configValue(QStringLiteral("font")).value<QFont>();
m_buildUi.errTreeWidget->setFont(font);
m_buildUi.plainTextEdit->setFont(font);
}
}
QFont font = Utils::editorFont();
m_buildUi.errTreeWidget->setFont(font);
m_buildUi.plainTextEdit->setFont(font);
// set working directory
m_make_dir = dir;
......
......@@ -20,6 +20,7 @@
#include <QStyledItemDelegate>
#include <kfts_fuzzy_match.h>
#include <ktexteditor_utils.h>
static constexpr int SymbolInfoRole = Qt::UserRole + 1;
......@@ -165,14 +166,6 @@ private:
QFont monoFont;
};
static QFont getViewFont(KTextEditor::MainWindow *mainWindow)
{
auto view = mainWindow->activeView();
auto ciface = qobject_cast<KTextEditor::ConfigInterface *>(view);
Q_ASSERT(ciface);
return ciface->configValue(QStringLiteral("font")).value<QFont>();
}
GotoSymbolHUDDialog::GotoSymbolHUDDialog(KTextEditor::MainWindow *mainWindow, QSharedPointer<LSPClientServer> server)
: QuickDialog(nullptr, mainWindow->window())
, model(new QStandardItemModel(this))
......@@ -186,7 +179,7 @@ GotoSymbolHUDDialog::GotoSymbolHUDDialog(KTextEditor::MainWindow *mainWindow, QS
m_treeView.setModel(model);
auto delegate = new GotoSymbolHUDStyleDelegate(this);
delegate->setColors();
delegate->setFont(getViewFont(mainWindow));
delegate->setFont(Utils::editorFont());
m_treeView.setItemDelegate(delegate);
connect(&m_lineEdit, &QLineEdit::textChanged, this, &GotoSymbolHUDDialog::slotTextChanged);
......
......@@ -56,10 +56,12 @@
#include <QTextCodec>
#include <QTimer>
#include <QTreeView>
#include <kfts_fuzzy_match.h>
#include <unordered_map>
#include <utility>
#include <kfts_fuzzy_match.h>
#include <ktexteditor_utils.h>
namespace RangeData
{
enum {
......@@ -915,15 +917,6 @@ public:
clearAllDiagnosticsMarks();
}
QFont getEditorFont()
{
auto ciface = qobject_cast<KTextEditor::ConfigInterface *>(m_mainWindow->activeView());
if (ciface) {
return ciface->configValue(QStringLiteral("font")).value<QFont>();
}
return QFont();
}
void configureTreeView(QTreeView *treeView)
{
treeView->setHeaderHidden(true);
......@@ -933,7 +926,7 @@ public:
treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
// styling
treeView->setItemDelegate(new LocationTreeDelegate(treeView, getEditorFont()));
treeView->setItemDelegate(new LocationTreeDelegate(treeView, Utils::editorFont()));
// context menu
treeView->setContextMenuPolicy(Qt::CustomContextMenu);
......
......@@ -21,13 +21,13 @@
#include <KTextEditor/ConfigInterface>
#include <KTextEditor/Editor>
#include <KTextEditor/View>
#include <KSyntaxHighlighting/Definition>
#include <KSyntaxHighlighting/Repository>
#include <KSyntaxHighlighting/SyntaxHighlighter>
#include <KWindowSystem>
#include <ktexteditor_utils.h>
class Tooltip : public QTextBrowser
{
Q_OBJECT
......@@ -58,7 +58,6 @@ public:
m_view = view;
hl.setDefinition(KTextEditor::Editor::instance()->repository().definitionForFileName(m_view->document()->url().toString()));
updateFont();
if (m_view && m_view->focusProxy()) {
m_view->focusProxy()->installEventFilter(this);
......@@ -94,7 +93,7 @@ public:
pal.setColor(QPalette::Text, normal);
setPalette(pal);
updateFont();
setFont(Utils::editorFont());
};
updateColors(KTextEditor::Editor::instance());
connect(KTextEditor::Editor::instance(), &KTextEditor::Editor::configChanged, this, updateColors);
......@@ -134,15 +133,6 @@ public:
return false;
}
void updateFont()
{
if (!m_view)
return;
auto ciface = qobject_cast<KTextEditor::ConfigInterface *>(m_view);
auto font = ciface->configValue(QStringLiteral("font")).value<QFont>();
setFont(font);
}
Q_SLOT void hideTooltip()
{
deleteLater();
......
......@@ -16,6 +16,8 @@
#include <KColorScheme>
#include <KLocalizedString>
#include <ktexteditor_utils.h>
class BadLengthHighlighter : public QSyntaxHighlighter
{
public:
......@@ -60,13 +62,15 @@ static void changeTextColorToRed(QLineEdit *lineEdit, const QColor &red)
QCoreApplication::sendEvent(lineEdit, &event);
}
GitCommitDialog::GitCommitDialog(const QString &lastCommit, const QFont &font, QWidget *parent, Qt::WindowFlags f)
GitCommitDialog::GitCommitDialog(const QString &lastCommit, QWidget *parent, Qt::WindowFlags f)
: QDialog(parent, f)
{
Q_ASSERT(parent);
setWindowTitle(i18n("Commit Changes"));
QFont font = Utils::editorFont();
ok.setText(i18n("Commit"));
cancel.setText(i18n("Cancel"));
......
......@@ -19,7 +19,7 @@ class GitCommitDialog : public QDialog
{
Q_OBJECT
public:
explicit GitCommitDialog(const QString &lastCommit, const QFont &font, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
explicit GitCommitDialog(const QString &lastCommit, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
QString subject() const;
QString description() const;
......
......@@ -677,15 +677,7 @@ void GitWidget::openCommitChangesDialog(bool amend)
return sendMessage(i18n("Nothing to commit. Please stage your changes first."), true);
}
auto ciface = qobject_cast<KTextEditor::ConfigInterface *>(m_mainWin->activeView());
QFont font;
if (ciface) {
font = ciface->configValue(QStringLiteral("font")).value<QFont>();
} else {
font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
}
GitCommitDialog *dialog = new GitCommitDialog(m_commitMessage, font, this);
GitCommitDialog *dialog = new GitCommitDialog(m_commitMessage, this);
if (amend) {
dialog->setAmendingCommit();
......
......@@ -14,14 +14,13 @@
#include <KTextEditor/MainWindow>
#include <KTextEditor/View>
#include <ktexteditor_utils.h>
PushPullDialog::PushPullDialog(KTextEditor::MainWindow *mainWindow, const QString &repoPath)
: QuickDialog(nullptr, mainWindow->window())
, m_repo(repoPath)
{
auto ciface = qobject_cast<KTextEditor::ConfigInterface *>(mainWindow->activeView());
Q_ASSERT(ciface);
m_lineEdit.setFont(ciface->configValue(QStringLiteral("font")).value<QFont>());
m_lineEdit.setFont(Utils::editorFont());
loadLastExecutedCommands();
}
......
......@@ -18,6 +18,12 @@ target_link_libraries(
KF5::TextEditor
)
target_include_directories(
katesearchplugin
PUBLIC
${CMAKE_SOURCE_DIR}/shared
)
ki18n_wrap_ui(katesearchplugin search.ui results.ui MatchExportDialog.ui)
target_sources(
......
......@@ -129,10 +129,3 @@ bool Results::replaceSingleMatch(KTextEditor::Document *doc, const QModelIndex &
return matchModel.replaceSingleMatch(doc, sourceIndex, regExp, replaceString);
}
void Results::setDisplayFont(const QFont &f)
{
if (treeView->itemDelegate()) {
auto *delegate = static_cast<SPHtmlDelegate *>(treeView->itemDelegate());
delegate->setDisplayFont(f);
}
}
......@@ -33,7 +33,6 @@ public:
QModelIndex lastMatch() const;
KTextEditor::Range matchRange(const QModelIndex &matchIndex) const;
bool replaceSingleMatch(KTextEditor::Document *doc, const QModelIndex &matchIndex, const QRegularExpression &regExp, const QString &replaceString);
void setDisplayFont(const QFont &);
Q_SIGNALS:
void colorsChanged();
......
......@@ -7,11 +7,13 @@
#include "htmldelegate.h"
#include <KLocalizedString>
#include <KTextEditor/Editor>
#include <QAbstractTextDocumentLayout>
#include <QModelIndex>
#include <QPainter>
#include <QTextCharFormat>
#include <QTextDocument>
#include <ktexteditor_utils.h>
// make list spacing resemble the default list spacing
// (which would not be the case with default QTextDocument margin)
......@@ -20,6 +22,10 @@ static const int s_ItemMargin = 1;
SPHtmlDelegate::SPHtmlDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
connect(KTextEditor::Editor::instance(), &KTextEditor::Editor::configChanged, this, [this] {
m_font = Utils::editorFont();
});
m_font = Utils::editorFont();
}
SPHtmlDelegate::~SPHtmlDelegate()
......
......@@ -20,10 +20,6 @@ public:
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setDisplayFont(const QFont &font)
{
m_font = font;
}
private:
QFont m_font;
......
......@@ -37,6 +37,8 @@
#include <QPoint>
#include <QScrollBar>
#include <ktexteditor_utils.h>
static QUrl localFileDirUp(const QUrl &url)
{
if (!url.isLocalFile()) {
......@@ -889,10 +891,6 @@ void KatePluginSearchView::updateViewColors()
m_resultAttr->clear();
m_resultAttr->setBackground(search);
m_resultAttr->setForeground(fg);
if (m_curResults) {
m_curResults->setDisplayFont(ciface->configValue(QStringLiteral("font")).value<QFont>());
}
}
}
......
......@@ -28,6 +28,7 @@
#include <QVBoxLayout>
#include <kfts_fuzzy_match.h>
#include <ktexteditor_utils.h>
class KateOutputTreeView : public QTreeView
{
......@@ -237,14 +238,7 @@ void KateOutputView::readConfig()
pal.setColor(QPalette::Text, QColor::fromRgba(theme.textColor(KSyntaxHighlighting::Theme::Normal)));
m_messagesTreeView->setPalette(pal);
// remove later in favor or Editor::font
QFont font;
if (const auto ciface = qobject_cast<KTextEditor::ConfigInterface *>(m_mainWindow->activeView())) {
font = ciface->configValue(QStringLiteral("font")).value<QFont>();
} else {
font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
}
m_messagesTreeView->setFont(font);
m_messagesTreeView->setFont(Utils::editorFont());
}
void KateOutputView::slotMessage(const QVariantMap &message)
......
/*
SPDX-FileCopyrightText: 2022 Waqar Ahmed <waqar.17a@gmail.com>
SPDX-License-Identifier: MIT
*/
#include <KTextEditor/ConfigInterface>
#include <KTextEditor/Editor>
#include <KTextEditor/MainWindow>
#include <KTextEditor/View>
#include <QFontDatabase>
namespace Utils
{
/**
* returns the current active global font
*/
inline QFont editorFont()
{
if (KTextEditor::Editor::instance()) {
return KTextEditor::Editor::instance()->font();
}
qWarning() << __func__ << "Editor::instance() is null! falling back to system fixed font";
return QFontDatabase::systemFont(QFontDatabase::FixedFont);
}
/**
* returns the font for view @p view
*/
inline QFont viewFont(KTextEditor::View *view)
{
if (const auto ciface = qobject_cast<KTextEditor::ConfigInterface *>(view)) {
return ciface->configValue(QStringLiteral("font")).value<QFont>();
}
return editorFont();
}
}
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