Commit b53085ab authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

start with more message handling, type, body

parent 8485730c
......@@ -128,6 +128,7 @@ void GitWidget::sendMessage(const QString &message, bool warn)
// FIXME: proper attributes
// if finished => remove the other case below!
QVariantMap genericMessage;
genericMessage.insert(QStringLiteral("type"), warn ? QStringLiteral("Warning") : QStringLiteral("Info"));
genericMessage.insert(QStringLiteral("plainText"), message);
Q_EMIT m_pluginView->message(genericMessage);
......
......@@ -6,6 +6,8 @@
#include "kateoutputview.h"
#include <KLocalizedString>
#include <QTreeView>
#include <QVBoxLayout>
......@@ -17,14 +19,46 @@ KateOutputView::KateOutputView(KateMainWindow *mainWindow, QWidget *parent)
// TODO: e.g. filter and such!
QVBoxLayout *layout = new QVBoxLayout(this);
m_messagesTreeView = new QTreeView(this);
m_messagesTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_messagesTreeView->setHeaderHidden(true);
m_messagesTreeView->setRootIsDecorated(false);
m_messagesTreeView->setModel(&m_messagesModel);
layout->addWidget(m_messagesTreeView);
// we want a special delegate to render the message body, as that might be plain text
// mark down or HTML
m_messagesTreeView->setItemDelegateForColumn(1, &m_messageBodyDelegate);
}
void KateOutputView::slotMessage(const QVariantMap &message)
{
// first dummy implementation: just add message 1:1 as text to output
if (message.contains(QStringLiteral("plainText"))) {
m_messagesModel.appendRow(new QStandardItem(message.value(QStringLiteral("plainText")).toString().trimmed()));
/**
* type column: shows the type, icons for some types only
*/
auto typeColumn = new QStandardItem();
const auto typeString = message.value(QStringLiteral("type")).toString();
if (typeString == QLatin1String("Error")) {
typeColumn->setText(i18nc("@info", "Error"));
typeColumn->setIcon(QIcon::fromTheme(QStringLiteral("data-error")));
} else if (typeString == QLatin1String("Warning")) {
typeColumn->setText(i18nc("@info", "Warning"));
typeColumn->setIcon(QIcon::fromTheme(QStringLiteral("data-warning")));
} else if (typeString == QLatin1String("Info")) {
typeColumn->setText(i18nc("@info", "Info"));
typeColumn->setIcon(QIcon::fromTheme(QStringLiteral("data-information")));
} else {
typeColumn->setText(i18nc("@info", "Log"));
}
/**
* body column, formatted text
* we just set the full message as attribute
*/
auto bodyColumn = new QStandardItem();
bodyColumn->setData(QVariant::fromValue(message));
/**
* add new message to model as one row
*/
m_messagesModel.appendRow({typeColumn, bodyColumn});
}
......@@ -4,15 +4,62 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef __KATE_OUTPUT_VIEW_H__
#define __KATE_OUTPUT_VIEW_H__
#ifndef KATE_OUTPUT_VIEW_H
#define KATE_OUTPUT_VIEW_H
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QTextDocument>
#include <QWidget>
class KateMainWindow;
class QTreeView;
/**
* Delegate to render the message body.
*/
class KateOutputMessageStyledDelegate : public QStyledItemDelegate
{
Q_OBJECT
private:
/**
* setup text document from data
*/
static void setupText(QTextDocument &doc, const QVariant &data)
{
const auto message = data.toMap();
printf("MUHH\n");
if (message.contains(QStringLiteral("plainText"))) {
printf("MUHH1\n");
doc.setPlainText(message.value(QStringLiteral("plainText")).toString());
} else if (message.contains(QStringLiteral("markDown"))) {
printf("MUHH2\n");
doc.setMarkdown(message.value(QStringLiteral("markDown")).toString());
} else if (message.contains(QStringLiteral("html"))) {
printf("MUHH4\n");
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.data());
doc.drawContents(painter);
}
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &index) const override
{
QTextDocument doc;
setupText(doc, index.data());
return doc.size().toSize();
}
};
/**
* Widget to output stuff e.g. for plugins.
*/
......@@ -32,6 +79,31 @@ public Q_SLOTS:
/**
* slot for incoming messages
* @param message incoming message we shall handle
*
* details of message format:
*
* IMPORTANT: at the moment, most stuff is key/value with strings, if the format is more finalized, one can introduce better type for more efficiency/type
* safety
*
* message body, can have different formats, lookup order will be:
*
* message["plainText"] => plain text string to display
* message["markDown"] => markdown string to display, we use QTextDocument with MarkdownDialectGitHub
* message["html"] => HTML string to display, we use QTextDocument, that means only a subset of HTML https://doc.qt.io/qt-5/richtext-html-subset.html
*
* the first thing found will be used.
*
* message type, we support at the moment
*
* message["type"] = "Error"
* message["type"] = "Warning"
* message["type"] = "Info"
* message["type"] = "Log"
*
* this is take from https://microsoft.github.io/language-server-protocol/specification#window_showMessage MessageType of LSP
*
* will lead to appropriate icons/... in the output view
*
*/
void slotMessage(const QVariantMap &message);
......@@ -51,6 +123,11 @@ private:
* Our message model, at the moment a standard item model
*/
QStandardItemModel m_messagesModel;
/**
* Our special output delegate for the message body
*/
KateOutputMessageStyledDelegate m_messageBodyDelegate;
};
#endif
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