Commit 5caa244a authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

fixup rendering

parent 10e1a4e5
......@@ -8,9 +8,59 @@
#include <KLocalizedString>
#include <QPainter>
#include <QTextDocument>
#include <QTreeView>
#include <QVBoxLayout>
/**
* setup text document from data
*/
static void setupText(QTextDocument &doc, const QModelIndex &index)
{
/**
* fill in right variant of text
* we always trim spaces away, to avoid ugly empty line at the start/end
*/
const auto message = index.data(KateOutputMessageStyledDelegate::MessageRole).toMap();
if (message.contains(QStringLiteral("plainText"))) {
doc.setPlainText(message.value(QStringLiteral("plainText")).toString().trimmed());
} else if (message.contains(QStringLiteral("markDown"))) {
doc.setMarkdown(message.value(QStringLiteral("markDown")).toString().trimmed());
} else if (message.contains(QStringLiteral("html"))) {
doc.setHtml(message.value(QStringLiteral("html")).toString().trimmed());
}
}
void KateOutputMessageStyledDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem options = option;
initStyleOption(&options, index);
// paint background
if (option.state & QStyle::State_Selected) {
painter->fillRect(option.rect, option.palette.highlight());
} else {
painter->fillRect(option.rect, option.palette.base());
}
options.text = QString();
options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
painter->translate(option.rect.x(), option.rect.y());
QTextDocument doc;
setupText(doc, index);
doc.drawContents(painter);
}
QSize KateOutputMessageStyledDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &index) const
{
QTextDocument doc;
setupText(doc, index);
return doc.size().toSize();
}
KateOutputView::KateOutputView(KateMainWindow *mainWindow, QWidget *parent)
: QWidget(parent)
, m_mainWindow(mainWindow)
......
......@@ -9,7 +9,6 @@
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QTextDocument>
#include <QWidget>
class KateMainWindow;
......@@ -28,38 +27,12 @@ public:
*/
static constexpr int MessageRole = Qt::UserRole + 1;
private:
/**
* setup text document from data
*/
static void setupText(QTextDocument &doc, const QModelIndex &index)
{
const auto message = index.data(KateOutputMessageStyledDelegate::MessageRole).toMap();
if (message.contains(QStringLiteral("plainText"))) {
doc.setPlainText(message.value(QStringLiteral("plainText")).toString());
} else if (message.contains(QStringLiteral("markDown"))) {
doc.setMarkdown(message.value(QStringLiteral("markDown")).toString());
} else if (message.contains(QStringLiteral("html"))) {
doc.setHtml(message.value(QStringLiteral("html")).toString());
}
}
public:
KateOutputMessageStyledDelegate() = default;
void paint(QPainter *painter, const QStyleOptionViewItem &, const QModelIndex &index) const override
{
QTextDocument doc;
setupText(doc, index);
doc.drawContents(painter);
}
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &index) const override
{
QTextDocument doc;
setupText(doc, index);
return doc.size().toSize();
}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &index) const override;
};
/**
......
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