Commit 9ae27085 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Clean up ktexteditor_utils.h header

Make it lighter to include
parent 3c62dd41
Pipeline #229301 passed with stage
in 13 minutes and 12 seconds
......@@ -10,10 +10,12 @@
#include <QMenu>
#include <QPainter>
#include <QPainterPath>
#include <QScrollBar>
#include <QTextBlock>
#include <KLocalizedString>
#include <KSyntaxHighlighting/Format>
#include <KTextEditor/Editor>
void DiffSyntaxHighlighter::applyFormat(int offset, int length, const KSyntaxHighlighting::Format &format)
{
......
......@@ -12,6 +12,7 @@
#include <QMimeDatabase>
#include <QPainter>
#include <QPainterPath>
#include <QPointer>
#include <QRegularExpression>
#include <QScrollBar>
#include <QSyntaxHighlighter>
......@@ -22,6 +23,7 @@
#include <KSyntaxHighlighting/Definition>
#include <KSyntaxHighlighting/Format>
#include <KSyntaxHighlighting/Repository>
#include <KTextEditor/Editor>
DiffWidget::DiffWidget(DiffParams p, QWidget *parent)
: QWidget(parent)
......
......@@ -6,6 +6,16 @@
#include "ktexteditor_utils.h"
#include <KTextEditor/ConfigInterface>
#include <KTextEditor/Editor>
#include <KTextEditor/MainWindow>
#include <KTextEditor/View>
#include <QFontDatabase>
#include <QIcon>
#include <QPointer>
#include <QScrollBar>
#include <KActionCollection>
#include <KXMLGUIFactory>
#include <QMimeDatabase>
......@@ -13,6 +23,82 @@
namespace Utils
{
class KateScrollBarRestorerPrivate
{
public:
KateScrollBarRestorerPrivate(KTextEditor::View *view)
{
// Find KateScrollBar
const auto scrollBars = view->findChildren<QScrollBar *>();
kateScrollBar = [scrollBars] {
for (auto scrollBar : scrollBars) {
if (qstrcmp(scrollBar->metaObject()->className(), "KateScrollBar") == 0) {
return scrollBar;
}
}
return static_cast<QScrollBar *>(nullptr);
}();
if (kateScrollBar) {
oldScrollValue = kateScrollBar->value();
}
}
void restore()
{
if (restored) {
return;
}
if (kateScrollBar) {
kateScrollBar->setValue(oldScrollValue);
}
restored = true;
}
~KateScrollBarRestorerPrivate()
{
restore();
}
private:
QPointer<QScrollBar> kateScrollBar = nullptr;
int oldScrollValue = 0;
bool restored = false;
};
KateScrollBarRestorer::KateScrollBarRestorer(KTextEditor::View *view)
: d(new KateScrollBarRestorerPrivate(view))
{
}
void KateScrollBarRestorer::restore()
{
d->restore();
}
KateScrollBarRestorer::~KateScrollBarRestorer()
{
delete d;
}
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);
}
QFont viewFont(KTextEditor::View *view)
{
if (const auto ciface = qobject_cast<KTextEditor::ConfigInterface *>(view)) {
return ciface->configValue(QStringLiteral("font")).value<QFont>();
}
return editorFont();
}
QIcon iconForDocument(KTextEditor::Document *doc)
{
// simple modified indicator if modified
......
......@@ -5,84 +5,50 @@
#include "kateprivate_export.h"
#include <KTextEditor/ConfigInterface>
#include <KTextEditor/Editor>
#include <KTextEditor/MainWindow>
#include <KTextEditor/View>
#include <QString>
#include <QFontDatabase>
#include <QIcon>
#include <QPointer>
#include <QScrollBar>
QT_BEGIN_NAMESPACE
class QScrollBar;
class QAction;
class QFont;
class QIcon;
QT_END_NAMESPACE
namespace KTextEditor
{
class View;
class Document;
class MainWindow;
}
namespace Utils
{
// A helper class that maintains scroll position
struct KateScrollBarRestorer {
KateScrollBarRestorer(KTextEditor::View *view)
{
// Find KateScrollBar
const auto scrollBars = view->findChildren<QScrollBar *>();
kateScrollBar = [scrollBars] {
for (auto scrollBar : scrollBars) {
if (qstrcmp(scrollBar->metaObject()->className(), "KateScrollBar") == 0) {
return scrollBar;
}
}
return static_cast<QScrollBar *>(nullptr);
}();
struct KATE_PRIVATE_EXPORT KateScrollBarRestorer {
KateScrollBarRestorer(KTextEditor::View *view);
~KateScrollBarRestorer();
if (kateScrollBar) {
oldScrollValue = kateScrollBar->value();
}
}
void restore()
{
if (kateScrollBar) {
kateScrollBar->setValue(oldScrollValue);
}
restored = true;
}
~KateScrollBarRestorer()
{
if (!restored) {
restore();
}
}
void restore();
private:
QPointer<QScrollBar> kateScrollBar = nullptr;
int oldScrollValue = 0;
bool restored = false;
class KateScrollBarRestorerPrivate *const d = nullptr;
};
/**
* 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);
}
KATE_PRIVATE_EXPORT QFont editorFont();
/**
* 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();
}
KATE_PRIVATE_EXPORT QFont viewFont(KTextEditor::View *view);
inline QString fileNameFromPath(const QString &path)
/**
* @brief Given path "/home/user/xyz.txt" returns "xyz.txt"
*/
KATE_PRIVATE_EXPORT inline QString fileNameFromPath(const QString &path)
{
int lastSlash = path.lastIndexOf(QLatin1Char('/'));
return lastSlash == -1 ? path : path.mid(lastSlash + 1);
......
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