1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <Python.h>
#include <QPainter>
#include <QStyleOption>
#include <QVBoxLayout>
#include "script/frame.h"
#include "script/editor.h"
#include "app/colors.h"
////////////////////////////////////////////////////////////////////////////////
ScriptFrame::ScriptFrame(Script* script, QWidget* parent)
: QWidget(parent), editor(new ScriptEditor(script, this)),
output(new QPlainTextEdit), error(new QPlainTextEdit)
{
for (auto txt : {output, error})
{
txt->document()->setDefaultFont(editor->document()->defaultFont());
txt->setReadOnly(true);
txt->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
txt->setStyleSheet(editor->styleSheet());
txt->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Fixed));
}
setStyleSheet(QString(
"QWidget { "
" background-color: %1;"
" border: none"
"}").arg(Colors::base01.name()));
auto layout = new QVBoxLayout;
layout->addWidget(editor);
layout->addWidget(output);
layout->addWidget(error);
layout->setSpacing(10);
layout->setContentsMargins(20, 0, 20, 0);
setLayout(layout);
}
////////////////////////////////////////////////////////////////////////////////
void ScriptFrame::resizeEvent(QResizeEvent* event)
{
resizePanes();
QWidget::resizeEvent(event);
}
void ScriptFrame::resizePanes()
{
for (auto txt : {output, error})
{
int lines = txt->document()->size().height() + 1;
QFontMetrics fm(txt->document()->defaultFont());
txt->setFixedHeight(std::min(height()/3, lines * fm.lineSpacing()));
}
}
////////////////////////////////////////////////////////////////////////////////
void ScriptFrame::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
////////////////////////////////////////////////////////////////////////////////
void ScriptFrame::setOutput(QString out)
{
if (out.isEmpty())
{
output->hide();
}
else
{
output->setPlainText(out);
output->show();
}
resizePanes();
}
void ScriptFrame::setError(QString err)
{
if (err.isEmpty())
{
error->hide();
}
else
{
error->setPlainText(err);
error->show();
}
resizePanes();
}
|