ui: Refactored code component to handle all state changes in controller

This commit is contained in:
Eberhard Graether
2019-11-08 16:20:43 +01:00
parent 9d556599ab
commit 9281c237a7
32 changed files with 1542 additions and 1985 deletions
+103 -148
View File
@@ -9,7 +9,6 @@
#include "FilePath.h"
#include "logging.h"
#include "MessageChangeFileView.h"
#include "SourceLocationFile.h"
#include "QtCodeArea.h"
#include "QtCodeFileTitleBar.h"
@@ -20,8 +19,6 @@
QtCodeFileSingle::QtCodeFileSingle(QtCodeNavigator* navigator, QWidget* parent)
: m_navigator(navigator)
, m_area(nullptr)
, m_contentRequested(false)
, m_scrollRequested(false)
{
setObjectName("code_container");
@@ -44,11 +41,6 @@ QtCodeFileSingle::QtCodeFileSingle(QtCodeNavigator* navigator, QWidget* parent)
layout()->addWidget(m_areaWrapper);
}
QAbstractScrollArea* QtCodeFileSingle::getScrollArea()
{
return m_area;
}
void QtCodeFileSingle::clearFile()
{
setFileData(FileData());
@@ -65,110 +57,115 @@ void QtCodeFileSingle::clearCache()
m_fileDatas.clear();
m_filePaths.clear();
m_lastLocationFile.reset();
}
void QtCodeFileSingle::addCodeSnippet(const CodeSnippetParams& params)
bool QtCodeFileSingle::addFile(const CodeFileParams& params, bool useSingleFileCache)
{
if (!params.locationFile->isWhole())
if (!params.fileParams)
{
LOG_ERROR("Snippet params passed are not for whole file.");
return;
}
FileData file = getFileData(params.locationFile->getFilePath());
if (file.area)
{
setFileData(file);
return;
}
file.filePath = params.locationFile->getFilePath();
file.modificationTime = params.modificationTime;
file.isComplete = params.locationFile->isComplete();
file.isIndexed = params.locationFile->isIndexed();
if (params.reduced)
{
file.title = params.title;
}
file.area = new QtCodeArea(1, params.code, params.locationFile, m_navigator, !params.reduced, this);
connect(file.area->verticalScrollBar(), &QScrollBar::valueChanged, m_navigator, &QtCodeNavigator::scrolled);
m_fileDatas.emplace(file.filePath, file);
m_filePaths.push_back(file.filePath);
setFileData(file);
m_contentRequested = false;
if (m_filePaths.size() > 100)
{
FilePath toDelete = m_filePaths.front();
m_filePaths.pop_front();
auto it = m_fileDatas.find(toDelete);
if (it != m_fileDatas.end())
{
it->second.area->deleteLater();
m_fileDatas.erase(it);
}
}
}
void QtCodeFileSingle::updateCodeSnippet(const CodeSnippetParams& params)
{
auto it = m_fileDatas.find(params.locationFile->getFilePath());
if (it != m_fileDatas.end())
{
it->second.area->updateSourceLocations(params.locationFile);
}
}
void QtCodeFileSingle::requestFileContent(const FilePath& filePath)
{
if (m_contentRequested)
{
return;
}
FileData file = getFileData(filePath);
if (file.area)
{
setFileData(file);
return;
}
m_contentRequested = true;
MessageChangeFileView msg(
filePath,
MessageChangeFileView::FILE_MAXIMIZED,
MessageChangeFileView::VIEW_SINGLE,
true,
m_navigator->hasErrors()
);
msg.setSchedulerId(m_navigator->getSchedulerId());
msg.dispatch();
}
bool QtCodeFileSingle::requestScroll(
const FilePath& filePath, size_t lineNumber, Id locationId, bool animated, ScrollTarget target)
{
FileData file = getFileData(filePath);
if (file.area)
{
setFileData(file);
}
else
{
requestFileContent(filePath);
LOG_ERROR("File params have missing information.");
return false;
}
if (!m_scrollRequested)
std::shared_ptr<SourceLocationFile> locationFile = params.fileParams->locationFile;
FileData file = useSingleFileCache ? getFileData(locationFile->getFilePath()) : FileData();
if (file.area)
{
animated = false;
if (file.area == m_area)
{
return false;
}
setFileData(file);
return true;
}
if (!locationFile->isWhole())
{
LOG_ERROR("Snippet params passed are not for whole file.");
return false;
}
// prevent non cached file from being created again when currently displayed
if (locationFile == m_lastLocationFile && locationFile->getFilePath() == m_currentFilePath)
{
return false;
}
m_lastLocationFile = locationFile;
file.filePath = locationFile->getFilePath();
file.isComplete = locationFile->isComplete();
file.isIndexed = locationFile->isIndexed();
file.modificationTime = params.modificationTime;
if (params.fileParams->isOverview)
{
file.title = params.fileParams->title;
}
file.area = new QtCodeArea(1, params.fileParams->code, locationFile, m_navigator, !params.fileParams->isOverview, this);
connect(file.area->verticalScrollBar(), &QScrollBar::valueChanged, m_navigator, &QtCodeNavigator::scrolled);
setFileData(file);
updateRefCount(params.referenceCount);
if (useSingleFileCache)
{
m_fileDatas.emplace(file.filePath, file);
m_filePaths.push_back(file.filePath);
if (m_filePaths.size() > 100)
{
// TODO: don't delete files that were added multiple times
FilePath toDelete = m_filePaths.front();
m_filePaths.pop_front();
auto it = m_fileDatas.find(toDelete);
if (it != m_fileDatas.end())
{
it->second.area->deleteLater();
m_fileDatas.erase(it);
}
}
}
return true;
}
QAbstractScrollArea* QtCodeFileSingle::getScrollArea()
{
return m_area;
}
void QtCodeFileSingle::updateSourceLocations(const CodeSnippetParams& params)
{
if (m_currentFilePath == params.locationFile->getFilePath())
{
m_area->updateSourceLocations(params.locationFile);
}
}
void QtCodeFileSingle::updateFiles()
{
if (m_area)
{
m_area->updateContent();
m_area->show();
// Resizes the area to fix a bug where the area could be scrolled below the last line.
m_area->updateGeometry();
}
}
void QtCodeFileSingle::scrollTo(
const FilePath& filePath, size_t lineNumber, Id locationId, bool animated, CodeScrollParams::Target target)
{
if (m_currentFilePath != filePath)
{
return;
}
size_t endLineNumber = 0;
@@ -196,30 +193,6 @@ bool QtCodeFileSingle::requestScroll(
ensurePercentVisibleAnimated(percentA, percentB, animated, target);
m_area->ensureLocationIdVisible(locationId, width(), animated);
m_scrollRequested = true;
return true;
}
void QtCodeFileSingle::updateFiles()
{
if (m_area)
{
m_area->updateContent();
}
}
void QtCodeFileSingle::showContents()
{
if (m_area)
{
m_area->show();
updateRefCount(m_area->getActiveLocationCount());
// Resizes the area to fix a bug where the area could be scrolled below the last line.
m_area->updateGeometry();
}
}
void QtCodeFileSingle::onWindowFocus()
@@ -235,21 +208,6 @@ void QtCodeFileSingle::findScreenMatches(const std::wstring& query, std::vector<
}
}
std::vector<std::pair<FilePath, Id>> QtCodeFileSingle::getLocationIdsForTokenIds(const std::set<Id>& tokenIds) const
{
if (m_area)
{
std::vector<std::pair<FilePath, Id>> locationIds;
for (Id locationId : m_area->getLocationIdsForTokenIds(tokenIds))
{
locationIds.push_back(std::make_pair(getCurrentFilePath(), locationId));
}
return locationIds;
}
return { };
}
const FilePath& QtCodeFileSingle::getCurrentFilePath() const
{
return m_currentFilePath;
@@ -278,14 +236,13 @@ Id QtCodeFileSingle::getLocationIdOfFirstActiveLocationOfTokenId(Id tokenId) con
void QtCodeFileSingle::clickedSnippetButton()
{
m_navigator->requestScroll(m_currentFilePath, 0, 0, false, QtCodeNavigateable::SCROLL_TOP);
m_navigator->setMode(QtCodeNavigator::MODE_LIST);
MessageChangeFileView(
m_currentFilePath,
MessageChangeFileView::FILE_SNIPPETS,
MessageChangeFileView::VIEW_LIST,
true, // TODO: check if data is really needed
m_navigator->hasErrors(),
CodeScrollParams::toFile(m_currentFilePath, CodeScrollParams::Target::TOP),
true
).dispatch();
}
@@ -350,8 +307,6 @@ void QtCodeFileSingle::setFileData(const FileData& file)
// Resizes the area to fix a bug where the area could be scrolled below the last line.
m_area->updateGeometry();
m_scrollRequested = false;
}
else
{