logic: Return to last scroll position in code view on undo
* fixed scrolling on undo * refactored autoscrolling in code view * save scroll position of user with MessageScrollCode
This commit is contained in:
@@ -173,6 +173,11 @@ std::shared_ptr<TokenLocationFile> QtCodeArea::getTokenLocationFile() const
|
||||
return m_locationFile;
|
||||
}
|
||||
|
||||
QtCodeFile* QtCodeArea::getFile() const
|
||||
{
|
||||
return m_fileWidget;
|
||||
}
|
||||
|
||||
void QtCodeArea::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter(m_lineNumberArea);
|
||||
|
||||
@@ -71,6 +71,7 @@ public:
|
||||
uint getEndLineNumber() const;
|
||||
|
||||
std::shared_ptr<TokenLocationFile> getTokenLocationFile() const;
|
||||
QtCodeFile* getFile() const;
|
||||
|
||||
void lineNumberAreaPaintEvent(QPaintEvent* event);
|
||||
int lineNumberDigits() const;
|
||||
|
||||
@@ -265,13 +265,13 @@ QtCodeSnippet* QtCodeFile::findFirstActiveSnippet() const
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool QtCodeFile::openCollapsedActiveSnippet() const
|
||||
bool QtCodeFile::isCollapsedActiveFile() const
|
||||
{
|
||||
bool isActiveFile = false;
|
||||
if (m_locationFile)
|
||||
{
|
||||
std::vector<Id> ids = getActiveTokenIds();
|
||||
|
||||
bool isActiveFile = false;
|
||||
m_locationFile->forEachTokenLocation(
|
||||
[&](TokenLocation* location)
|
||||
{
|
||||
@@ -281,15 +281,9 @@ bool QtCodeFile::openCollapsedActiveSnippet() const
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (isActiveFile)
|
||||
{
|
||||
showSnippets();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return isActiveFile;
|
||||
}
|
||||
|
||||
void QtCodeFile::updateContent()
|
||||
@@ -430,7 +424,7 @@ void QtCodeFile::clickedMaximizeButton() const
|
||||
).dispatch();
|
||||
}
|
||||
|
||||
void QtCodeFile::showSnippets() const
|
||||
void QtCodeFile::requestSnippets() const
|
||||
{
|
||||
if (m_snippetsRequested)
|
||||
{
|
||||
@@ -450,6 +444,11 @@ void QtCodeFile::showSnippets() const
|
||||
msg.dispatch();
|
||||
}
|
||||
|
||||
bool QtCodeFile::hasSnippets() const
|
||||
{
|
||||
return m_snippets.size() > 0;
|
||||
}
|
||||
|
||||
void QtCodeFile::handleMessage(MessageWindowFocus* message)
|
||||
{
|
||||
updateTitleBar();
|
||||
|
||||
@@ -59,7 +59,7 @@ public:
|
||||
);
|
||||
|
||||
QtCodeSnippet* findFirstActiveSnippet() const;
|
||||
bool openCollapsedActiveSnippet() const;
|
||||
bool isCollapsedActiveFile() const;
|
||||
|
||||
void updateContent();
|
||||
|
||||
@@ -69,7 +69,8 @@ public:
|
||||
void setSnippets();
|
||||
void setMaximized();
|
||||
|
||||
void showSnippets() const;
|
||||
void requestSnippets() const;
|
||||
bool hasSnippets() const;
|
||||
|
||||
public slots:
|
||||
void clickedMinimizeButton() const;
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
#include <QPropertyAnimation>
|
||||
#include <QScrollBar>
|
||||
#include <QTimer>
|
||||
#include <QVariant>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
#include "utility/file/FileSystem.h"
|
||||
#include "utility/messaging/type/MessageScrollCode.h"
|
||||
|
||||
#include "data/location/TokenLocationFile.h"
|
||||
#include "qt/element/QtCodeFile.h"
|
||||
@@ -13,6 +15,8 @@
|
||||
|
||||
QtCodeFileList::QtCodeFileList(QWidget* parent)
|
||||
: QScrollArea(parent)
|
||||
, m_scrollToFile(nullptr)
|
||||
, m_value(0)
|
||||
{
|
||||
setObjectName("code_file_list_base");
|
||||
|
||||
@@ -28,6 +32,7 @@ QtCodeFileList::QtCodeFileList(QWidget* parent)
|
||||
setWidgetResizable(true);
|
||||
setWidget(m_frame.get());
|
||||
|
||||
connect(this->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(scrolled(int)));
|
||||
connect(this, SIGNAL(shouldScrollToSnippet(QtCodeSnippet*)), this, SLOT(scrollToSnippet(QtCodeSnippet*)), Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
@@ -112,37 +117,26 @@ void QtCodeFileList::showActiveTokenIds()
|
||||
updateFiles();
|
||||
}
|
||||
|
||||
bool QtCodeFileList::scrollToFirstActiveSnippet()
|
||||
void QtCodeFileList::showFirstActiveSnippet(bool scrollTo)
|
||||
{
|
||||
updateFiles();
|
||||
|
||||
QtCodeSnippet* snippet = nullptr;
|
||||
for (std::shared_ptr<QtCodeFile> file: m_files)
|
||||
{
|
||||
snippet = file->findFirstActiveSnippet();
|
||||
if (snippet)
|
||||
{
|
||||
if (!snippet->isVisible())
|
||||
{
|
||||
file->showSnippets();
|
||||
}
|
||||
QtCodeSnippet* snippet = getFirstActiveSnippet();
|
||||
|
||||
emit shouldScrollToSnippet(snippet);
|
||||
return true;
|
||||
}
|
||||
if (!snippet)
|
||||
{
|
||||
expandActiveSnippetFile(scrollTo);
|
||||
return;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void QtCodeFileList::expandActiveSnippetFile()
|
||||
{
|
||||
for (std::shared_ptr<QtCodeFile> file: m_files)
|
||||
if (!snippet->isVisible())
|
||||
{
|
||||
if (file->openCollapsedActiveSnippet())
|
||||
{
|
||||
return;
|
||||
}
|
||||
snippet->getFile()->setSnippets();
|
||||
}
|
||||
|
||||
if (scrollTo)
|
||||
{
|
||||
emit shouldScrollToSnippet(snippet);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,11 +187,36 @@ void QtCodeFileList::showContents()
|
||||
}
|
||||
}
|
||||
|
||||
void QtCodeFileList::scrollToValue(int value)
|
||||
{
|
||||
m_value = value;
|
||||
QTimer::singleShot(100, this, SLOT(setValue()));
|
||||
}
|
||||
|
||||
void QtCodeFileList::scrollToActiveFileIfRequested()
|
||||
{
|
||||
if (m_scrollToFile && m_scrollToFile->hasSnippets())
|
||||
{
|
||||
showFirstActiveSnippet(true);
|
||||
m_scrollToFile = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void QtCodeFileList::scrolled(int value)
|
||||
{
|
||||
MessageScrollCode(value).dispatch();
|
||||
}
|
||||
|
||||
void QtCodeFileList::scrollToSnippet(QtCodeSnippet* snippet)
|
||||
{
|
||||
this->ensureWidgetVisibleAnimated(snippet, snippet->getFirstActiveLineRect());
|
||||
}
|
||||
|
||||
void QtCodeFileList::setValue()
|
||||
{
|
||||
this->verticalScrollBar()->setValue(m_value);
|
||||
}
|
||||
|
||||
QtCodeFile* QtCodeFileList::getFile(const FilePath filePath)
|
||||
{
|
||||
QtCodeFile* file = nullptr;
|
||||
@@ -225,6 +244,21 @@ QtCodeFile* QtCodeFileList::getFile(const FilePath filePath)
|
||||
return file;
|
||||
}
|
||||
|
||||
QtCodeSnippet* QtCodeFileList::getFirstActiveSnippet() const
|
||||
{
|
||||
QtCodeSnippet* snippet = nullptr;
|
||||
for (std::shared_ptr<QtCodeFile> file: m_files)
|
||||
{
|
||||
snippet = file->findFirstActiveSnippet();
|
||||
if (snippet)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return snippet;
|
||||
}
|
||||
|
||||
void QtCodeFileList::updateFiles()
|
||||
{
|
||||
for (std::shared_ptr<QtCodeFile> file: m_files)
|
||||
@@ -233,6 +267,24 @@ void QtCodeFileList::updateFiles()
|
||||
}
|
||||
}
|
||||
|
||||
void QtCodeFileList::expandActiveSnippetFile(bool scrollTo)
|
||||
{
|
||||
for (std::shared_ptr<QtCodeFile> file: m_files)
|
||||
{
|
||||
if (file->isCollapsedActiveFile())
|
||||
{
|
||||
file->requestSnippets();
|
||||
|
||||
if (scrollTo)
|
||||
{
|
||||
m_scrollToFile = file.get();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QtCodeFileList::ensureWidgetVisibleAnimated(QWidget *childWidget, QRectF rect)
|
||||
{
|
||||
if (!widget()->isAncestorOf(childWidget))
|
||||
@@ -259,7 +311,7 @@ void QtCodeFileList::ensureWidgetVisibleAnimated(QWidget *childWidget, QRectF re
|
||||
if (scrollBar && value != 0)
|
||||
{
|
||||
QPropertyAnimation* anim = new QPropertyAnimation(scrollBar, "value");
|
||||
anim->setDuration(500);
|
||||
anim->setDuration(300);
|
||||
anim->setStartValue(scrollBar->value());
|
||||
anim->setEndValue(scrollBar->value() + value);
|
||||
anim->setEasingCurve(QEasingCurve::InOutQuad);
|
||||
|
||||
@@ -55,8 +55,7 @@ public:
|
||||
|
||||
void showActiveTokenIds();
|
||||
|
||||
bool scrollToFirstActiveSnippet();
|
||||
void expandActiveSnippetFile();
|
||||
void showFirstActiveSnippet(bool scrollTo);
|
||||
|
||||
void focusTokenIds(const std::vector<Id>& focusedTokenIds);
|
||||
void defocusTokenIds();
|
||||
@@ -66,15 +65,21 @@ public:
|
||||
void setFileMaximized(const FilePath path);
|
||||
|
||||
void showContents();
|
||||
void scrollToValue(int value);
|
||||
void scrollToActiveFileIfRequested();
|
||||
|
||||
private slots:
|
||||
void scrolled(int value);
|
||||
void scrollToSnippet(QtCodeSnippet* snippet);
|
||||
void setValue();
|
||||
|
||||
private:
|
||||
QtCodeFile* getFile(const FilePath filePath);
|
||||
QtCodeSnippet* getFirstActiveSnippet() const;
|
||||
|
||||
void updateFiles();
|
||||
|
||||
void expandActiveSnippetFile(bool scrollTo);
|
||||
void ensureWidgetVisibleAnimated(QWidget *childWidget, QRectF rect);
|
||||
|
||||
std::shared_ptr<QFrame> m_frame;
|
||||
@@ -83,6 +88,9 @@ private:
|
||||
std::vector<Id> m_activeTokenIds;
|
||||
std::vector<Id> m_focusedTokenIds;
|
||||
std::vector<std::string> m_errorMessages;
|
||||
|
||||
QtCodeFile* m_scrollToFile;
|
||||
int m_value;
|
||||
};
|
||||
|
||||
#endif // QT_CODE_FILE_LIST
|
||||
|
||||
@@ -110,6 +110,11 @@ QtCodeSnippet::~QtCodeSnippet()
|
||||
{
|
||||
}
|
||||
|
||||
QtCodeFile* QtCodeSnippet::getFile() const
|
||||
{
|
||||
return m_codeArea->getFile();
|
||||
}
|
||||
|
||||
uint QtCodeSnippet::getStartLineNumber() const
|
||||
{
|
||||
return m_codeArea->getStartLineNumber();
|
||||
|
||||
@@ -32,6 +32,8 @@ public:
|
||||
);
|
||||
virtual ~QtCodeSnippet();
|
||||
|
||||
QtCodeFile* getFile() const;
|
||||
|
||||
uint getStartLineNumber() const;
|
||||
uint getEndLineNumber() const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user