Skip to content
Snippets Groups Projects
Commit 1cbe6b5c authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

PySampleWidget update mechanism now is relying on UpdateTimer

parent 8dceceb9
No related branches found
No related tags found
No related merge requests found
......@@ -86,17 +86,11 @@ void InfoWidget::onDockVisibilityChange(bool is_visible)
{
Q_ASSERT(m_pySampleWidget);
if(isEditorVisible()) {
if(!is_visible) {
m_pySampleWidget->disableEditor();
} else {
//m_pySampleWidget->scheduleUpdate();
m_pySampleWidget->enableEditor();
}
if(!is_visible)
m_pySampleWidget->setEditorConnected(false);
else
m_pySampleWidget->setEditorConnected(true);
}
// if(status != isEditorVisible())
// m_pySampleWidget->setEditorEnabled(status);
}
void InfoWidget::onExpandButtonClicked()
......@@ -116,11 +110,11 @@ void InfoWidget::setEditorVisible(bool editor_status, bool dock_notify)
}
m_placeHolder->hide();
m_pySampleWidget->show();
m_pySampleWidget->enableEditor();
m_pySampleWidget->setEditorConnected(true);
} else {
m_cached_height = height();
m_pySampleWidget->hide();
m_pySampleWidget->disableEditor();
m_pySampleWidget->setEditorConnected(false);
m_placeHolder->show();
if(dock_notify) emit widgetHeightRequest(minimum_widget_height);
}
......
......@@ -22,13 +22,12 @@
#include "PythonSyntaxHighlighter.h"
#include "SampleModel.h"
#include "WarningSign.h"
#include "UpdateTimer.h"
#include <QScrollBar>
#include <QTextEdit>
#include <QTimer>
#include <QVBoxLayout>
namespace {
const int timer_interval_msec = 10;
const int accumulate_updates_during_msec = 20.;
const QString welcome_message =
......@@ -60,9 +59,8 @@ PySampleWidget::PySampleWidget(QWidget *parent)
, m_textEdit(new QTextEdit)
, m_sampleModel(0)
, m_instrumentModel(0)
, m_time_to_update(accumulate_updates_during_msec)
, m_n_of_sceduled_updates(-1)
, m_highlighter(0)
, m_updateTimer(new UpdateTimer(accumulate_updates_during_msec, this))
, m_warningSign(new WarningSign(m_textEdit))
{
m_textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
......@@ -73,10 +71,6 @@ PySampleWidget::PySampleWidget(QWidget *parent)
setLayout(mainLayout);
m_timer = new QTimer(this);
m_timer->setInterval(timer_interval_msec);
connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimerTimeout()));
m_textEdit->setHtml(welcomeMessage());
m_textEdit->setReadOnly(true);
QFont textFont("Monospace");
......@@ -94,7 +88,7 @@ void PySampleWidget::setSampleModel(SampleModel* sampleModel)
Q_ASSERT(sampleModel);
if (sampleModel != m_sampleModel) {
if (m_sampleModel)
disableEditor();
setEditorConnected(false);
m_sampleModel = sampleModel;
}
}
......@@ -107,20 +101,12 @@ void PySampleWidget::setInstrumentModel(InstrumentModel* instrumentModel)
void PySampleWidget::onModifiedRow(const QModelIndex&, int, int)
{
scheduleUpdate();
m_updateTimer->scheduleUpdate();
}
void PySampleWidget::onDataChanged(const QModelIndex&, const QModelIndex&)
{
scheduleUpdate();
}
//! Schedule subsequent update of the editor
void PySampleWidget::scheduleUpdate()
{
m_n_of_sceduled_updates++;
if (!m_timer->isActive())
m_timer->start();
m_updateTimer->scheduleUpdate();
}
//! Update the editor with the script content
......@@ -131,71 +117,43 @@ void PySampleWidget::updateEditor()
m_textEdit->setLineWrapMode(QTextEdit::NoWrap);
}
Q_ASSERT(!m_timer->isActive());
m_n_of_sceduled_updates = 0;
const int old_scrollbar_value = m_textEdit->verticalScrollBar()->value();
QString code_snippet = generateCodeSnippet();
if (m_warningSign->isShown())
m_textEdit->clear();
if (!code_snippet.isEmpty()) {
if (!code_snippet.isEmpty())
m_textEdit->setText(code_snippet);
}
else
m_textEdit->clear();
m_textEdit->verticalScrollBar()->setValue(old_scrollbar_value);
m_time_to_update = accumulate_updates_during_msec;
}
//! Disconnect from all signals to prevent editor update
void PySampleWidget::disableEditor()
void PySampleWidget::setEditorConnected(bool isConnected)
{
Q_ASSERT(m_sampleModel);
m_timer->stop();
disconnect(m_sampleModel, SIGNAL(rowsInserted(QModelIndex, int, int)), this,
SLOT(onModifiedRow(QModelIndex, int, int)));
disconnect(m_sampleModel, SIGNAL(rowsRemoved(QModelIndex, int, int)), this,
SLOT(onModifiedRow(QModelIndex, int, int)));
disconnect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
SLOT(onDataChanged(QModelIndex, QModelIndex)));
disconnect(m_sampleModel, SIGNAL(modelReset()), this, SLOT(updateEditor()));
}
void PySampleWidget::enableEditor()
{
Q_ASSERT(m_sampleModel);
if (isConnected) {
connect(m_sampleModel, SIGNAL(rowsInserted(QModelIndex, int, int)), this,
SLOT(onModifiedRow(QModelIndex, int, int)), Qt::UniqueConnection);
connect(m_sampleModel, SIGNAL(rowsRemoved(QModelIndex, int, int)), this,
SLOT(onModifiedRow(QModelIndex, int, int)), Qt::UniqueConnection);
connect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
SLOT(onDataChanged(QModelIndex, QModelIndex)), Qt::UniqueConnection);
connect(m_sampleModel, SIGNAL(modelReset()), this, SLOT(updateEditor()),
Qt::UniqueConnection);
connect(m_updateTimer, SIGNAL(timeToUpdate()), this, SLOT(updateEditor()),
Qt::UniqueConnection);
if (m_sampleModel->topItems().isEmpty()) {
// negative number would mean that editor was never used and still contains welcome message
// which we want to keep
if (m_n_of_sceduled_updates >= 0)
updateEditor();
} else {
updateEditor();
}
connect(m_sampleModel, SIGNAL(rowsInserted(QModelIndex, int, int)), this,
SLOT(onModifiedRow(QModelIndex, int, int)), Qt::UniqueConnection);
connect(m_sampleModel, SIGNAL(rowsRemoved(QModelIndex, int, int)), this,
SLOT(onModifiedRow(QModelIndex, int, int)), Qt::UniqueConnection);
connect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
SLOT(onDataChanged(QModelIndex, QModelIndex)), Qt::UniqueConnection);
connect(m_sampleModel, SIGNAL(modelReset()), this, SLOT(updateEditor()), Qt::UniqueConnection);
}
//! Triggers the update of the editor
void PySampleWidget::onTimerTimeout()
{
m_time_to_update -= timer_interval_msec;
if (m_time_to_update < 0) {
m_timer->stop();
updateEditor();
disconnect(m_sampleModel, SIGNAL(rowsInserted(QModelIndex, int, int)), this,
SLOT(onModifiedRow(QModelIndex, int, int)));
disconnect(m_sampleModel, SIGNAL(rowsRemoved(QModelIndex, int, int)), this,
SLOT(onModifiedRow(QModelIndex, int, int)));
disconnect(m_sampleModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this,
SLOT(onDataChanged(QModelIndex, QModelIndex)));
disconnect(m_sampleModel, SIGNAL(modelReset()), this, SLOT(updateEditor()));
disconnect(m_updateTimer, SIGNAL(timeToUpdate()), this, SLOT(updateEditor()));
}
}
......@@ -206,7 +164,7 @@ QString PySampleWidget::generateCodeSnippet()
m_warningSign->clear();
QString result;
foreach (SessionItem* sampleItem, m_sampleModel->topItems()) {
foreach (SessionItem* sampleItem, m_sampleModel->topItems(Constants::MultiLayerType)) {
DomainObjectBuilder builder;
try {
auto P_multilayer = builder.buildMultiLayer(*sampleItem);
......
......@@ -24,8 +24,8 @@ class SampleModel;
class InstrumentModel;
class QTextEdit;
class QModelIndex;
class QTimer;
class PythonSyntaxHighlighter;
class UpdateTimer;
class WarningSign;
//! The PySampleWidget displays Python script representing a MultiLayer at the bottom of SampleView
......@@ -44,14 +44,8 @@ public slots:
void onModifiedRow(const QModelIndex&, int, int);
void onDataChanged(const QModelIndex&, const QModelIndex&);
void scheduleUpdate();
void updateEditor();
void disableEditor();
void enableEditor();
private slots:
void onTimerTimeout();
void setEditorConnected(bool isConnected);
private:
QString generateCodeSnippet();
......@@ -60,10 +54,8 @@ private:
QTextEdit* m_textEdit;
SampleModel* m_sampleModel;
InstrumentModel* m_instrumentModel;
QTimer* m_timer;
int m_time_to_update;
int m_n_of_sceduled_updates;
PythonSyntaxHighlighter* m_highlighter;
UpdateTimer* m_updateTimer;
WarningSign* m_warningSign;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment