File: ScriptPanel.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (121 lines) | stat: -rw-r--r-- 3,628 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Sample/ScriptPanel.cpp
//! @brief     Implements class ScriptPanel.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Sample/ScriptPanel.h"
#include "GUI/Model/Sample/SampleItem.h"
#include "GUI/Model/ToCore/SampleToCore.h"
#include "GUI/Model/Type/UpdateTimer.h"
#include "GUI/View/Base/Fontsize.h"
#include "GUI/View/Info/CautionSign.h"
#include "GUI/View/Info/PythonSyntaxHighlighter.h"
#include "Sample/Multilayer/Sample.h"
#include "Sim/Export/ExportToPython.h"
#include <QScrollBar>
#include <QStackedWidget>
#include <QVBoxLayout>

namespace {

const int accumulateUpdatesDuringMsec = 20.;

} // namespace

ScriptPanel::ScriptPanel(QWidget* parent)
    : QWidget(parent)
    , m_text_edit(new QTextEdit)
    , m_highlighter(nullptr)
    , m_update_timer(new UpdateTimer(accumulateUpdatesDuringMsec, this))
    , m_caution_sign(new CautionSign(m_text_edit))
    , m_current_sample(nullptr)
{
    setWindowTitle("Python Script");
    setObjectName("ScriptPanel");

    m_text_edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    auto* mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addWidget(m_text_edit);

    m_text_edit->setReadOnly(true);
    QFont textFont("Monospace");
    m_text_edit->setFont(textFont);
    m_text_edit->setFontPointSize(GUI::Font::fontSizeRegular());

    connect(m_update_timer, &UpdateTimer::timeToUpdate, this, &ScriptPanel::updateEditor);
}

void ScriptPanel::setCurrentSample(SampleItem* sampleItem)
{
    m_current_sample = sampleItem;
    updateEditor();
}

void ScriptPanel::onSampleModified()
{
    if (isVisible())
        m_update_timer->scheduleUpdate();
}

void ScriptPanel::updateEditor()
{
    if (!m_highlighter) {
        m_highlighter = new PythonSyntaxHighlighter(m_text_edit->document());
        m_text_edit->setLineWrapMode(QTextEdit::NoWrap);
    }

    const int oldScrollbarValue = m_text_edit->verticalScrollBar()->value();

    const QString codeSnippet = generateCodeSnippet();
    if (!codeSnippet.isEmpty())
        m_text_edit->setText(codeSnippet);
    else
        m_text_edit->clear();

    m_text_edit->verticalScrollBar()->setValue(oldScrollbarValue);
}

void ScriptPanel::showEvent(QShowEvent*)
{
    m_update_timer->scheduleUpdate();
}

void ScriptPanel::hideEvent(QHideEvent*)
{
    m_update_timer->reset();
}

QString ScriptPanel::generateCodeSnippet()
{
    m_caution_sign->clear();

    if (m_current_sample == nullptr)
        return {};

    QString result;
    try {
        auto sample = GUI::ToCore::itemToSample(*m_current_sample);
        result.append(QString::fromStdString(Py::Export::sampleCode(*sample)));
    } catch (const std::exception& ex) {
        QString message =
            QString("Generation of Python Script failed. Code is not complete.\n\n"
                    "It can happen if sample requires further assembling or some of sample "
                    "parameters are not valid. See details below.\n\n%1")
                .arg(QString::fromStdString(ex.what()));

        m_caution_sign->setCautionMessage(message);
    }

    return result;
}