Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a way to display log in a ScriptDialog #157

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/API/knut/scriptdialog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Knut
||**[firstStep](#firstStep)**(string firstStep)|
||**[nextStep](#nextStep)**(string title)|
||**[runSteps](#runSteps)**(function generator)|
||**[setDisplayLogs](#setDisplayLogs)**(const QString &level)|
||**[setStepCount](#setStepCount)**(int stepCount)|
||**[verify](#verify)**(bool value, string message = {})|

Expand Down Expand Up @@ -171,6 +172,11 @@ function convert() {
}
```

#### <a name="setDisplayLogs"></a>**setDisplayLogs**(const QString &level)

This method enables real-time logging display in the progress dialog by configuring a QPlainTextEdit widget as a log
output panel. It filters the logs to show only those that are of the specified log level or higher.

#### <a name="setStepCount"></a>**setStepCount**(int stepCount)

Sets the number of steps to show in the progress bar.
Expand Down
1 change: 1 addition & 0 deletions examples/ex_gui_interactive.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ScriptDialog {
// This can be done using the `stepCount` property of ScriptDialog
// Note: if you don't set the step count, a moving scrollbar will be displayed
setStepCount(5);
setDisplayLogs("info");

// 3) Initialize the first step using `firstStep` and run the different commands for this step
firstStep("Step 1...");
Expand Down
38 changes: 38 additions & 0 deletions src/core/scriptdialogitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

#include "scriptdialogitem.h"
#include "loghighlighter.h"
#include "scriptdialogitem_p.h"
#include "scriptprogressdialog.h"
#include "scriptrunner.h"
Expand Down Expand Up @@ -44,6 +45,7 @@
#include <QToolButton>
#include <QUiLoader>
#include <QVBoxLayout>
#include <spdlog/sinks/qt_sinks.h>

namespace Core {

Expand Down Expand Up @@ -431,6 +433,42 @@ void ScriptDialogItem::verify(bool value, QString message)
ScriptRunner::verify(this, value, message);
}

/*!
* \qmlmethod ScriptDialog::setDisplayLogs(const QString &level)
* This method enables real-time logging display in the progress dialog by configuring a QPlainTextEdit widget as a log
* output panel. It filters the logs to show only those that are of the specified log level or higher.
*/
void ScriptDialogItem::setDisplayLogs(const QString &level)
{
showProgressDialog();

if (!m_progressDialog)
return;

QPlainTextEdit *logPanel = m_progressDialog->logsWidget();
logPanel->show();
logPanel->setReadOnly(true);

new Core::LogHighlighter(logPanel->document());

const std::vector<QString> levels = {"trace", "debug", "info", "warn", "error", "critical"};
const auto it = std::find(levels.begin(), levels.end(), level);
const int index = (it != levels.end()) ? std::distance(levels.begin(), it) : 0;

spdlog::level::level_enum logLevel = static_cast<spdlog::level::level_enum>(index);

auto logger = spdlog::default_logger();

auto currentLogLevel = logger->level();
auto logLevelMin = static_cast<spdlog::level::level_enum>(std::min(static_cast<int>(currentLogLevel), index));

logger->set_level(logLevelMin);

auto sink = std::make_shared<spdlog::sinks::qt_sink_mt>(logPanel, "appendPlainText");
sink->set_level(logLevel);
logger->sinks().push_back(sink);
}

void ScriptDialogItem::showProgressDialog()
{
// If there's no interaction or the progress bar is already displayed, do nothing
Expand Down
2 changes: 2 additions & 0 deletions src/core/scriptdialogitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class ScriptDialogItem : public QDialog
Q_INVOKABLE void compare(const QJSValue &actual, const QJSValue &expected, QString message = {});
Q_INVOKABLE void verify(bool value, QString message = {});

Q_INVOKABLE void setDisplayLogs(const QString &level);

public slots:
void setStepCount(int stepCount);
void done(int code) override;
Expand Down
6 changes: 6 additions & 0 deletions src/core/scriptprogressdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ScriptProgressDialog::ScriptProgressDialog(QWidget *parent)
setAttribute(Qt::WA_DeleteOnClose);

ui->buttonBox->button(QDialogButtonBox::Yes)->setText(tr("Continue"));
ui->logsWidget->hide();
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &ScriptProgressDialog::apply);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &ScriptProgressDialog::abort);
}
Expand Down Expand Up @@ -63,3 +64,8 @@ void ScriptProgressDialog::setReadOnly(bool readOnly)
{
ui->buttonBox->setEnabled(!readOnly);
}

QPlainTextEdit *ScriptProgressDialog::logsWidget()
{
return ui->logsWidget;
}
3 changes: 3 additions & 0 deletions src/core/scriptprogressdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Ui {
class ScriptProgressDialog;
}
class QPlainTextEdit;

class ScriptProgressDialog : public QDialog
{
Expand All @@ -34,6 +35,8 @@ class ScriptProgressDialog : public QDialog

int value() const;

QPlainTextEdit *logsWidget();

signals:
void apply();
void abort();
Expand Down
5 changes: 4 additions & 1 deletion src/core/scriptprogressdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>360</width>
<height>94</height>
<height>189</height>
</rect>
</property>
<property name="minimumSize">
Expand Down Expand Up @@ -48,6 +48,9 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QPlainTextEdit" name="logsWidget"/>
</item>
</layout>
</widget>
<resources>
Expand Down
Loading