- enhancement: keep current position in the class browser after contents modified

This commit is contained in:
Roy Qu 2022-10-23 16:31:05 +08:00
parent 471b3d6f26
commit b4deb9299b
5 changed files with 72 additions and 1 deletions

View File

@ -11,6 +11,7 @@ Red Panda C++ Version 2.0
- fix: correctly display statements whose parent is not in the current file - fix: correctly display statements whose parent is not in the current file
- fix: statements is the class browser is correctly sorted - fix: statements is the class browser is correctly sorted
- enhancement: Weither double click on the class browser should goto definition/declaration, depends on the current cursor position - enhancement: Weither double click on the class browser should goto definition/declaration, depends on the current cursor position
- enhancement: keep current position in the class browser after contents modified
Red Panda C++ Version 1.5 Red Panda C++ Version 1.5

View File

@ -362,6 +362,11 @@ MainWindow::MainWindow(QWidget *parent)
ui->classBrowser->setModel(&mClassBrowserModel); ui->classBrowser->setModel(&mClassBrowserModel);
delete m; delete m;
connect(&mClassBrowserModel, &ClassBrowserModel::refreshStarted,
this, &MainWindow::onClassBrowserRefreshStart);
connect(&mClassBrowserModel, &ClassBrowserModel::refreshEnd,
this, &MainWindow::onClassBrowserRefreshEnd);
connect(&mFileSystemWatcher,&QFileSystemWatcher::fileChanged, connect(&mFileSystemWatcher,&QFileSystemWatcher::fileChanged,
this, &MainWindow::onFileChanged); this, &MainWindow::onFileChanged);
@ -4123,6 +4128,33 @@ void MainWindow::onClassBrowserChangeScope()
} }
} }
void MainWindow::onClassBrowserRefreshStart()
{
mClassBrowserCurrentStatement="";
QModelIndex index = ui->classBrowser->currentIndex();
if (!index.isValid())
return ;
ClassBrowserNode * node = static_cast<ClassBrowserNode*>(index.internalPointer());
if (!node)
return ;
PStatement statement = node->statement;
if (!statement) {
return;
}
mClassBrowserCurrentStatement=statement->fullName;
}
void MainWindow::onClassBrowserRefreshEnd()
{
QModelIndex index = mClassBrowserModel.modelIndexForStatement(mClassBrowserCurrentStatement);
if (index.isValid()) {
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
ui->classBrowser->expandRecursively(index);
#endif
ui->classBrowser->setCurrentIndex(index);
}
}
void MainWindow::onProjectSwitchCustomViewMode() void MainWindow::onProjectSwitchCustomViewMode()
{ {
mProject->setModelType(ProjectModelType::Custom); mProject->setModelType(ProjectModelType::Custom);

View File

@ -348,6 +348,8 @@ private slots:
void onClassBrowserSortByType(); void onClassBrowserSortByType();
void onClassBrowserSortByName(); void onClassBrowserSortByName();
void onClassBrowserChangeScope(); void onClassBrowserChangeScope();
void onClassBrowserRefreshStart();
void onClassBrowserRefreshEnd();
void onProjectSwitchCustomViewMode(); void onProjectSwitchCustomViewMode();
void onProjectSwitchFileSystemViewMode(); void onProjectSwitchFileSystemViewMode();
@ -767,6 +769,8 @@ private:
OJProblemModel mOJProblemModel; OJProblemModel mOJProblemModel;
int mOJProblemSetNameCounter; int mOJProblemSetNameCounter;
QString mClassBrowserCurrentStatement;
bool mCheckSyntaxInBack; bool mCheckSyntaxInBack;
bool mShouldRemoveAllSettings; bool mShouldRemoveAllSettings;
PCompileSuccessionTask mCompileSuccessionTask; PCompileSuccessionTask mCompileSuccessionTask;

View File

@ -205,6 +205,8 @@ void ClassBrowserModel::clear()
beginResetModel(); beginResetModel();
mRoot->children.clear(); mRoot->children.clear();
mNodes.clear(); mNodes.clear();
mNodeIndex.clear();
mProcessedStatements.clear();
mDummyStatements.clear(); mDummyStatements.clear();
mScopeNodes.clear(); mScopeNodes.clear();
endResetModel(); endResetModel();
@ -218,12 +220,14 @@ void ClassBrowserModel::fillStatements()
return; return;
mUpdating = true; mUpdating = true;
} }
emit refreshStarted();
beginResetModel(); beginResetModel();
clear(); clear();
{ {
auto action = finally([this]{ auto action = finally([this]{
endResetModel(); endResetModel();
mUpdating = false; mUpdating = false;
emit refreshEnd();
}); });
if (!mParser) if (!mParser)
return; return;
@ -247,6 +251,8 @@ PClassBrowserNode ClassBrowserModel::addChild(ClassBrowserNode *node, const PSta
// newNode->childrenFetched = false; // newNode->childrenFetched = false;
node->children.append(newNode.get()); node->children.append(newNode.get());
mNodes.append(newNode); mNodes.append(newNode);
mNodeIndex.insert(statement->fullName,newNode);
mProcessedStatements.insert(statement.get());
if (statement->kind == StatementKind::skClass if (statement->kind == StatementKind::skClass
|| statement->kind == StatementKind::skNamespace) || statement->kind == StatementKind::skNamespace)
mScopeNodes.insert(statement->fullName,newNode); mScopeNodes.insert(statement->fullName,newNode);
@ -312,6 +318,10 @@ void ClassBrowserModel::filterChildren(ClassBrowserNode *node, const StatementMa
if (mClassBrowserType==ProjectClassBrowserType::WholeProject if (mClassBrowserType==ProjectClassBrowserType::WholeProject
&& !statement->inProject) && !statement->inProject)
continue; continue;
if (mProcessedStatements.contains(statement.get()))
continue;
if (statement->kind == StatementKind::skBlock) if (statement->kind == StatementKind::skBlock)
continue; continue;
if (statement->isInherited && !pSettings->ui().classBrowserShowInherited()) if (statement->isInherited && !pSettings->ui().classBrowserShowInherited())
@ -421,6 +431,24 @@ void ClassBrowserModel::setCurrentFiles(const QStringList &newCurrentFiles)
mCurrentFiles = newCurrentFiles; mCurrentFiles = newCurrentFiles;
} }
QModelIndex ClassBrowserModel::modelIndexForStatement(const QString &fullname)
{
QMutexLocker locker(&mMutex);
if (mUpdating)
return QModelIndex();
PClassBrowserNode node=mNodeIndex.value(fullname,PClassBrowserNode());
if (!node)
return QModelIndex();
ClassBrowserNode *parentNode=node->parent;
if (!parentNode)
return QModelIndex();
int row=parentNode->children.indexOf(node.get());
if (row<0)
return QModelIndex();
return createIndex(row,0,node.get());
}
ProjectClassBrowserType ClassBrowserModel::classBrowserType() const ProjectClassBrowserType ClassBrowserModel::classBrowserType() const
{ {
return mClassBrowserType; return mClassBrowserType;
@ -465,7 +493,7 @@ void ClassBrowserModel::endUpdate()
mUpdateCount--; mUpdateCount--;
if (mUpdateCount == 0) { if (mUpdateCount == 0) {
if (mParser && !mParser->parsing()) { if (mParser && !mParser->parsing()) {
this->fillStatements(); fillStatements();
} }
} }
} }

View File

@ -65,6 +65,10 @@ public:
const QStringList &currentFiles() const; const QStringList &currentFiles() const;
void setCurrentFiles(const QStringList &newCurrentFiles); void setCurrentFiles(const QStringList &newCurrentFiles);
QModelIndex modelIndexForStatement(const QString& fullname);
signals:
void refreshStarted();
void refreshEnd();
public slots: public slots:
void fillStatements(); void fillStatements();
private: private:
@ -78,6 +82,8 @@ private:
ClassBrowserNode * mRoot; ClassBrowserNode * mRoot;
QHash<QString,PStatement> mDummyStatements; QHash<QString,PStatement> mDummyStatements;
QHash<QString,PClassBrowserNode> mScopeNodes; QHash<QString,PClassBrowserNode> mScopeNodes;
QHash<QString,PClassBrowserNode> mNodeIndex;
QSet<Statement*> mProcessedStatements;
QVector<PClassBrowserNode> mNodes; QVector<PClassBrowserNode> mNodes;
PCppParser mParser; PCppParser mParser;
bool mUpdating; bool mUpdating;