* Use unified showCodeSnippets method for setting file information on CodeView * Made async initial file and snippet expansion synced * Add ScrollParams API to CodeView * Regard scope size when scrolling to definition * Only use QtThreadedLambdaFunctor in QtCodeView now
452 lines
9.5 KiB
C++
452 lines
9.5 KiB
C++
#include "qt/element/QtCodeFile.h"
|
|
|
|
#include <QLabel>
|
|
#include <QPushButton>
|
|
#include <QVBoxLayout>
|
|
|
|
#include "utility/messaging/type/MessageChangeFileView.h"
|
|
|
|
#include "data/location/SourceLocationFile.h"
|
|
#include "qt/element/QtCodeFileTitleButton.h"
|
|
#include "qt/element/QtCodeNavigator.h"
|
|
#include "qt/element/QtCodeSnippet.h"
|
|
|
|
QtCodeFile::QtCodeFile(const FilePath& filePath, QtCodeNavigator* navigator)
|
|
: QFrame()
|
|
, m_navigator(navigator)
|
|
, m_filePath(filePath)
|
|
, m_isWholeFile(false)
|
|
, m_isCollapsed(true)
|
|
, m_contentRequested(false)
|
|
{
|
|
setObjectName("code_file");
|
|
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
|
|
|
|
QVBoxLayout* layout = new QVBoxLayout(this);
|
|
layout->setMargin(0);
|
|
layout->setSpacing(0);
|
|
layout->setAlignment(Qt::AlignTop);
|
|
setLayout(layout);
|
|
|
|
m_titleBar = new QPushButton(this);
|
|
m_titleBar->setObjectName("title_widget");
|
|
layout->addWidget(m_titleBar);
|
|
|
|
QHBoxLayout* titleLayout = new QHBoxLayout();
|
|
titleLayout->setMargin(0);
|
|
titleLayout->setSpacing(0);
|
|
titleLayout->setAlignment(Qt::AlignLeft);
|
|
m_titleBar->setLayout(titleLayout);
|
|
|
|
m_title = new QtCodeFileTitleButton(this);
|
|
if (!m_filePath.empty())
|
|
{
|
|
m_title->setFilePath(filePath);
|
|
}
|
|
|
|
titleLayout->addWidget(m_title);
|
|
|
|
m_titleBar->setMinimumHeight(m_title->height() + 4);
|
|
|
|
m_referenceCount = new QLabel(this);
|
|
m_referenceCount->setObjectName("references_label");
|
|
m_referenceCount->hide();
|
|
titleLayout->addWidget(m_referenceCount);
|
|
|
|
titleLayout->addStretch(3);
|
|
|
|
m_minimizeButton = new QPushButton(this);
|
|
m_minimizeButton->setObjectName("minimize_button");
|
|
m_minimizeButton->setToolTip("minimize");
|
|
m_minimizeButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // fixes layouting on Mac
|
|
titleLayout->addWidget(m_minimizeButton);
|
|
|
|
m_snippetButton = new QPushButton(this);
|
|
m_snippetButton->setObjectName("snippet_button");
|
|
m_snippetButton->setToolTip("show snippets");
|
|
m_snippetButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // fixes layouting on Mac
|
|
titleLayout->addWidget(m_snippetButton);
|
|
|
|
m_maximizeButton = new QPushButton(this);
|
|
m_maximizeButton->setObjectName("maximize_button");
|
|
m_maximizeButton->setToolTip("maximize");
|
|
m_maximizeButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); // fixes layouting on Mac
|
|
titleLayout->addWidget(m_maximizeButton);
|
|
|
|
m_minimizeButton->setEnabled(false);
|
|
m_snippetButton->setEnabled(false);
|
|
m_maximizeButton->setEnabled(false);
|
|
|
|
connect(m_titleBar, SIGNAL(clicked()), this, SLOT(clickedTitleBar()));
|
|
connect(m_minimizeButton, SIGNAL(clicked()), this, SLOT(clickedMinimizeButton()));
|
|
connect(m_snippetButton, SIGNAL(clicked()), this, SLOT(clickedSnippetButton()));
|
|
connect(m_maximizeButton, SIGNAL(clicked()), this, SLOT(clickedMaximizeButton()));
|
|
|
|
m_snippetLayout = new QVBoxLayout();
|
|
layout->addLayout(m_snippetLayout);
|
|
|
|
update();
|
|
}
|
|
|
|
QtCodeFile::~QtCodeFile()
|
|
{
|
|
}
|
|
|
|
void QtCodeFile::setModificationTime(const TimePoint modificationTime)
|
|
{
|
|
m_title->setModificationTime(modificationTime);
|
|
}
|
|
|
|
const FilePath& QtCodeFile::getFilePath() const
|
|
{
|
|
return m_filePath;
|
|
}
|
|
|
|
std::string QtCodeFile::getFileName() const
|
|
{
|
|
return m_filePath.fileName();
|
|
}
|
|
|
|
QtCodeSnippet* QtCodeFile::addCodeSnippet(const CodeSnippetParams& params)
|
|
{
|
|
m_isCollapsed = false;
|
|
|
|
std::shared_ptr<QtCodeSnippet> snippet(new QtCodeSnippet(params, m_navigator, this));
|
|
|
|
if (params.reduced)
|
|
{
|
|
m_title->setProject(params.title);
|
|
}
|
|
|
|
m_snippetLayout->addWidget(snippet.get());
|
|
|
|
if (params.locationFile->isWhole())
|
|
{
|
|
snippet->setProperty("isFirst", true);
|
|
snippet->setProperty("isLast", true);
|
|
|
|
m_fileSnippet = snippet;
|
|
if (!m_snippets.size())
|
|
{
|
|
m_fileSnippet->setIsActiveFile(true);
|
|
}
|
|
|
|
setMaximized();
|
|
if (params.refCount != -1)
|
|
{
|
|
updateRefCount(0);
|
|
}
|
|
|
|
return m_fileSnippet.get();
|
|
}
|
|
|
|
m_snippets.push_back(snippet);
|
|
|
|
setSnippets();
|
|
updateRefCount(params.refCount);
|
|
|
|
return snippet.get();
|
|
}
|
|
|
|
QtCodeSnippet* QtCodeFile::insertCodeSnippet(const CodeSnippetParams& params)
|
|
{
|
|
m_isCollapsed = false;
|
|
|
|
std::shared_ptr<QtCodeSnippet> snippet(new QtCodeSnippet(params, m_navigator, this));
|
|
|
|
size_t i = 0;
|
|
while (i < m_snippets.size())
|
|
{
|
|
uint start = snippet->getStartLineNumber();
|
|
uint end = snippet->getEndLineNumber();
|
|
|
|
std::shared_ptr<QtCodeSnippet> s = m_snippets[i];
|
|
|
|
if (s->getEndLineNumber() + 1 < start)
|
|
{
|
|
i++;
|
|
continue;
|
|
}
|
|
else if (s->getStartLineNumber() > end + 1)
|
|
{
|
|
break;
|
|
}
|
|
else if (s->getStartLineNumber() < start || s->getEndLineNumber() > end)
|
|
{
|
|
snippet = QtCodeSnippet::merged(snippet.get(), s.get(), m_navigator, this);
|
|
}
|
|
|
|
s->hide();
|
|
m_snippetLayout->removeWidget(s.get());
|
|
|
|
m_snippets.erase(m_snippets.begin() + i);
|
|
}
|
|
|
|
m_snippetLayout->insertWidget(i, snippet.get());
|
|
m_snippets.insert(m_snippets.begin() + i, snippet);
|
|
|
|
setSnippets();
|
|
|
|
return snippet.get();
|
|
}
|
|
|
|
QtCodeSnippet* QtCodeFile::getSnippetForLocationId(Id locationId) const
|
|
{
|
|
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
|
|
{
|
|
if (snippet->getLineNumberForLocationId(locationId))
|
|
{
|
|
return snippet.get();
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
QtCodeSnippet* QtCodeFile::getSnippetForLine(unsigned int line) const
|
|
{
|
|
if (m_fileSnippet && m_fileSnippet->isVisible())
|
|
{
|
|
return m_fileSnippet.get();
|
|
}
|
|
|
|
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
|
|
{
|
|
if (snippet->getStartLineNumber() <= line && line <= snippet->getEndLineNumber())
|
|
{
|
|
return snippet.get();
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
QtCodeSnippet* QtCodeFile::getFileSnippet() const
|
|
{
|
|
return m_fileSnippet.get();
|
|
}
|
|
|
|
std::pair<QtCodeSnippet*, Id> QtCodeFile::getFirstSnippetWithActiveLocationId(Id tokenId) const
|
|
{
|
|
std::pair<QtCodeSnippet*, Id> result(nullptr, 0);
|
|
|
|
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
|
|
{
|
|
Id locationId = snippet->getFirstActiveLocationId(tokenId);
|
|
if (locationId != 0)
|
|
{
|
|
result.first = snippet.get();
|
|
result.second = locationId;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool QtCodeFile::isCollapsed() const
|
|
{
|
|
return m_isCollapsed;
|
|
}
|
|
|
|
void QtCodeFile::requestContent()
|
|
{
|
|
if (!isCollapsed() || m_contentRequested)
|
|
{
|
|
updateContent();
|
|
return;
|
|
}
|
|
|
|
m_contentRequested = true;
|
|
|
|
MessageChangeFileView::FileState state =
|
|
m_isWholeFile ? MessageChangeFileView::FILE_MAXIMIZED : MessageChangeFileView::FILE_SNIPPETS;
|
|
|
|
MessageChangeFileView(m_filePath, state, isCollapsed(), m_navigator->hasErrors()).dispatch();
|
|
}
|
|
|
|
void QtCodeFile::updateContent()
|
|
{
|
|
updateSnippets();
|
|
|
|
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
|
|
{
|
|
snippet->updateContent();
|
|
}
|
|
|
|
if (m_fileSnippet)
|
|
{
|
|
m_fileSnippet->updateContent();
|
|
}
|
|
}
|
|
|
|
void QtCodeFile::setWholeFile(bool isWholeFile, int refCount)
|
|
{
|
|
m_isWholeFile = isWholeFile;
|
|
setMinimized();
|
|
|
|
updateRefCount(isWholeFile ? 0 : refCount);
|
|
}
|
|
|
|
void QtCodeFile::setIsComplete(bool isComplete)
|
|
{
|
|
m_title->setIsComplete(isComplete);
|
|
}
|
|
|
|
void QtCodeFile::setMinimized()
|
|
{
|
|
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
|
|
{
|
|
snippet->hide();
|
|
}
|
|
|
|
if (m_fileSnippet)
|
|
{
|
|
m_fileSnippet->hide();
|
|
}
|
|
|
|
m_minimizeButton->setEnabled(false);
|
|
m_snippetButton->setEnabled(m_snippets.size() || (isCollapsed() && !m_isWholeFile));
|
|
m_maximizeButton->setEnabled(true);
|
|
}
|
|
|
|
void QtCodeFile::setSnippets()
|
|
{
|
|
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
|
|
{
|
|
snippet->show();
|
|
}
|
|
|
|
if (m_fileSnippet)
|
|
{
|
|
m_fileSnippet->hide();
|
|
}
|
|
|
|
m_minimizeButton->setEnabled(true);
|
|
m_snippetButton->setEnabled(false);
|
|
m_maximizeButton->setEnabled(true);
|
|
}
|
|
|
|
void QtCodeFile::setMaximized()
|
|
{
|
|
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
|
|
{
|
|
snippet->hide();
|
|
}
|
|
|
|
if (m_fileSnippet)
|
|
{
|
|
m_fileSnippet->show();
|
|
}
|
|
|
|
m_minimizeButton->setEnabled(true);
|
|
m_snippetButton->setEnabled(m_snippets.size());
|
|
m_maximizeButton->setEnabled(false);
|
|
}
|
|
|
|
bool QtCodeFile::hasSnippets() const
|
|
{
|
|
return m_snippets.size() > 0;
|
|
}
|
|
|
|
void QtCodeFile::updateSnippets()
|
|
{
|
|
if (m_snippets.size() == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
int maxDigits = 1;
|
|
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
|
|
{
|
|
snippet->setProperty("isFirst", false);
|
|
snippet->setProperty("isLast", false);
|
|
|
|
maxDigits = qMax(maxDigits, snippet->lineNumberDigits());
|
|
}
|
|
|
|
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
|
|
{
|
|
snippet->updateLineNumberAreaWidthForDigits(maxDigits);
|
|
}
|
|
|
|
m_snippets.front()->setProperty("isFirst", true);
|
|
m_snippets.back()->setProperty("isLast", true);
|
|
}
|
|
|
|
void QtCodeFile::updateTitleBar()
|
|
{
|
|
m_title->updateTexts();
|
|
}
|
|
|
|
void QtCodeFile::clickedMinimizeButton() const
|
|
{
|
|
MessageChangeFileView(
|
|
m_filePath,
|
|
MessageChangeFileView::FILE_MINIMIZED,
|
|
false,
|
|
m_navigator->hasErrors()
|
|
).dispatch();
|
|
}
|
|
|
|
void QtCodeFile::clickedSnippetButton() const
|
|
{
|
|
MessageChangeFileView(
|
|
m_filePath,
|
|
MessageChangeFileView::FILE_SNIPPETS,
|
|
isCollapsed(),
|
|
m_navigator->hasErrors()
|
|
).dispatch();
|
|
}
|
|
|
|
void QtCodeFile::clickedMaximizeButton() const
|
|
{
|
|
MessageChangeFileView(
|
|
m_filePath,
|
|
MessageChangeFileView::FILE_MAXIMIZED,
|
|
!getFileSnippet(),
|
|
m_navigator->hasErrors()
|
|
).dispatch();
|
|
}
|
|
|
|
void QtCodeFile::clickedTitleBar()
|
|
{
|
|
if (m_minimizeButton->isEnabled())
|
|
{
|
|
clickedMinimizeButton();
|
|
}
|
|
else if (m_snippetButton->isEnabled())
|
|
{
|
|
clickedSnippetButton();
|
|
}
|
|
else
|
|
{
|
|
clickedMaximizeButton();
|
|
}
|
|
}
|
|
|
|
void QtCodeFile::updateRefCount(int refCount)
|
|
{
|
|
if (refCount > 0)
|
|
{
|
|
QString label = m_navigator->hasErrors() ? "error" : "reference";
|
|
if (refCount > 1)
|
|
{
|
|
label += "s";
|
|
}
|
|
|
|
size_t fatalErrorCount = m_navigator->getFatalErrorCountForFile(m_filePath);
|
|
if (fatalErrorCount > 0)
|
|
{
|
|
label += " (" + QString::number(fatalErrorCount) + " fatal)";
|
|
}
|
|
|
|
m_referenceCount->setText(QString::number(refCount) + " " + label);
|
|
m_referenceCount->show();
|
|
}
|
|
else
|
|
{
|
|
m_referenceCount->hide();
|
|
}
|
|
}
|