rename ProjectModel to ProjectLegacyModel

This commit is contained in:
Roy Qu 2022-02-07 22:05:19 +08:00
parent 6cbab4a646
commit 7ebcc2d635
4 changed files with 84 additions and 65 deletions

View File

@ -2468,8 +2468,8 @@ void MainWindow::buildContextMenus()
if (!current.isValid()) {
return;
}
FolderNode * node = static_cast<FolderNode*>(current.internalPointer());
PFolderNode folderNode = mProject->pointerToNode(node);
ProjectLegacyModelNode * node = static_cast<ProjectLegacyModelNode*>(current.internalPointer());
PProjectLegacyModelNode folderNode = mProject->pointerToNode(node);
if (!folderNode)
folderNode = mProject->rootNode();
if (folderNode->unitIndex>=0)
@ -2511,8 +2511,8 @@ void MainWindow::buildContextMenus()
if (!current.isValid()) {
return;
}
FolderNode * node = static_cast<FolderNode*>(current.internalPointer());
PFolderNode folderNode = mProject->pointerToNode(node);
ProjectLegacyModelNode * node = static_cast<ProjectLegacyModelNode*>(current.internalPointer());
PProjectLegacyModelNode folderNode = mProject->pointerToNode(node);
if (!folderNode)
return;
if (folderNode->unitIndex>=0)
@ -2900,8 +2900,8 @@ void MainWindow::onProjectViewContextMenu(const QPoint &pos)
int unitIndex = -1;
QModelIndex current = mProjectProxyModel->mapToSource(ui->projectView->selectionModel()->currentIndex());
if (current.isValid() && mProject) {
FolderNode * node = static_cast<FolderNode*>(current.internalPointer());
PFolderNode pNode = mProject->pointerToNode(node);
ProjectLegacyModelNode * node = static_cast<ProjectLegacyModelNode*>(current.internalPointer());
PProjectLegacyModelNode pNode = mProject->pointerToNode(node);
if (pNode) {
unitIndex = pNode->unitIndex;
onFolder = (unitIndex<0);
@ -3532,11 +3532,11 @@ void MainWindow::updateProjectView()
if (mProjectProxyModel->sourceModel()!=mProject->model()) {
mProjectProxyModel->setSourceModel(mProject->model());
mProjectProxyModel->sort(0);
connect(mProject->model(), &ProjectModel::dataChanged,
connect(mProject->model(), &ProjectLegacyModel::dataChanged,
this, &MainWindow::invalidateProjectProxyModel);
connect(mProject->model(), &ProjectModel::rowsRemoved,
connect(mProject->model(), &ProjectLegacyModel::rowsRemoved,
this, &MainWindow::invalidateProjectProxyModel);
connect(mProject->model(), &ProjectModel::rowsInserted,
connect(mProject->model(), &ProjectLegacyModel::rowsInserted,
this, &MainWindow::invalidateProjectProxyModel);
connect(mProject->model(), &QAbstractItemModel::modelReset,
ui->projectView,&QTreeView::expandAll);
@ -4953,7 +4953,7 @@ void MainWindow::on_projectView_doubleClicked(const QModelIndex &index)
QModelIndex sourceIndex = mProjectProxyModel->mapToSource(index);
if (!sourceIndex.isValid())
return;
FolderNode * node = static_cast<FolderNode*>(sourceIndex.internalPointer());
ProjectLegacyModelNode * node = static_cast<ProjectLegacyModelNode*>(sourceIndex.internalPointer());
if (!node)
return;
if (node->unitIndex>=0) {
@ -5106,11 +5106,11 @@ void MainWindow::on_actionAdd_to_project_triggered()
dialog.setAcceptMode(QFileDialog::AcceptOpen);
if (dialog.exec()) {
QModelIndex current = mProjectProxyModel->mapToSource(ui->projectView->currentIndex());
FolderNode * node = nullptr;
ProjectLegacyModelNode * node = nullptr;
if (current.isValid()) {
node = static_cast<FolderNode*>(current.internalPointer());
node = static_cast<ProjectLegacyModelNode*>(current.internalPointer());
}
PFolderNode folderNode = mProject->pointerToNode(node);
PProjectLegacyModelNode folderNode = mProject->pointerToNode(node);
foreach (const QString& filename, dialog.selectedFiles()) {
mProject->addUnit(filename,folderNode,false);
mProject->cppParser()->addFileToScan(filename);
@ -5135,8 +5135,8 @@ void MainWindow::on_actionRemove_from_project_triggered()
if (!index.isValid())
continue;
QModelIndex realIndex = mProjectProxyModel->mapToSource(index);
FolderNode * node = static_cast<FolderNode*>(realIndex.internalPointer());
PFolderNode folderNode = mProject->pointerToNode(node);
ProjectLegacyModelNode * node = static_cast<ProjectLegacyModelNode*>(realIndex.internalPointer());
PProjectLegacyModelNode folderNode = mProject->pointerToNode(node);
if (!folderNode)
continue;
selected.insert(folderNode->unitIndex);
@ -5326,9 +5326,9 @@ void MainWindow::newProjectUnitFile()
return;
int idx = -1;
QModelIndex current = mProjectProxyModel->mapToSource(ui->projectView->currentIndex());
FolderNode * node = nullptr;
ProjectLegacyModelNode * node = nullptr;
if (current.isValid()) {
node = static_cast<FolderNode*>(current.internalPointer());
node = static_cast<ProjectLegacyModelNode*>(current.internalPointer());
}
QString newFileName;
do {

View File

@ -1603,7 +1603,7 @@ ProjectOptions &Project::options()
return mOptions;
}
ProjectModel *Project::model()
ProjectLegacyModel *Project::legacyModel()
{
return &mModel;
}
@ -1822,14 +1822,14 @@ void ProjectUnit::setNode(const PFolderNode &newNode)
mNode = newNode;
}
ProjectModel::ProjectModel(Project *project, QObject *parent):
ProjectLegacyModel::ProjectLegacyModel(Project *project, QObject *parent):
QAbstractItemModel(parent),
mProject(project)
{
mUpdateCount = 0;
}
void ProjectModel::beginUpdate()
void ProjectLegacyModel::beginUpdate()
{
if (mUpdateCount==0) {
beginResetModel();
@ -1837,7 +1837,7 @@ void ProjectModel::beginUpdate()
mUpdateCount++;
}
void ProjectModel::endUpdate()
void ProjectLegacyModel::endUpdate()
{
mUpdateCount--;
if (mUpdateCount==0) {
@ -1845,12 +1845,12 @@ void ProjectModel::endUpdate()
}
}
Project *ProjectModel::project() const
Project *ProjectLegacyModel::project() const
{
return mProject;
}
QModelIndex ProjectModel::index(int row, int column, const QModelIndex &parent) const
QModelIndex ProjectLegacyModel::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid()) {
return createIndex(row,column,mProject->rootNode().get());
@ -1864,7 +1864,7 @@ QModelIndex ProjectModel::index(int row, int column, const QModelIndex &parent)
return createIndex(row,column,parentNode->children[row].get());
}
QModelIndex ProjectModel::parent(const QModelIndex &child) const
QModelIndex ProjectLegacyModel::parent(const QModelIndex &child) const
{
if (!child.isValid())
return QModelIndex();
@ -1874,7 +1874,7 @@ QModelIndex ProjectModel::parent(const QModelIndex &child) const
return getParentIndex(node);
}
int ProjectModel::rowCount(const QModelIndex &parent) const
int ProjectLegacyModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return 1;
@ -1886,12 +1886,12 @@ int ProjectModel::rowCount(const QModelIndex &parent) const
}
}
int ProjectModel::columnCount(const QModelIndex &) const
int ProjectLegacyModel::columnCount(const QModelIndex &) const
{
return 1;
}
QVariant ProjectModel::data(const QModelIndex &index, int role) const
QVariant ProjectLegacyModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
@ -1914,7 +1914,7 @@ QVariant ProjectModel::data(const QModelIndex &index, int role) const
return QVariant();
}
Qt::ItemFlags ProjectModel::flags(const QModelIndex &index) const
Qt::ItemFlags ProjectLegacyModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
@ -1931,7 +1931,7 @@ Qt::ItemFlags ProjectModel::flags(const QModelIndex &index) const
return flags;
}
bool ProjectModel::setData(const QModelIndex &index, const QVariant &value, int role)
bool ProjectLegacyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
@ -2033,7 +2033,7 @@ bool ProjectModel::setData(const QModelIndex &index, const QVariant &value, int
return false;
}
QModelIndex ProjectModel::getParentIndex(FolderNode * node) const
QModelIndex ProjectLegacyModel::getParentIndex(FolderNode * node) const
{
PFolderNode parent = node->parent.lock();
if (!parent) // root node
@ -2049,7 +2049,7 @@ QModelIndex ProjectModel::getParentIndex(FolderNode * node) const
return createIndex(row,0,parent.get());
}
bool ProjectModel::canDropMimeData(const QMimeData * data, Qt::DropAction action, int /*row*/, int /*column*/, const QModelIndex &parent) const
bool ProjectLegacyModel::canDropMimeData(const QMimeData * data, Qt::DropAction action, int /*row*/, int /*column*/, const QModelIndex &parent) const
{
if (!data || action != Qt::MoveAction)
@ -2089,12 +2089,12 @@ bool ProjectModel::canDropMimeData(const QMimeData * data, Qt::DropAction action
return true;
}
Qt::DropActions ProjectModel::supportedDropActions() const
Qt::DropActions ProjectLegacyModel::supportedDropActions() const
{
return Qt::MoveAction;
}
bool ProjectModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int /*row*/, int /*column*/, const QModelIndex &parent)
bool ProjectLegacyModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int /*row*/, int /*column*/, const QModelIndex &parent)
{
// check if the action is supported
if (!data || action != Qt::MoveAction)
@ -2151,7 +2151,7 @@ bool ProjectModel::dropMimeData(const QMimeData *data, Qt::DropAction action, in
return false;
}
QMimeData *ProjectModel::mimeData(const QModelIndexList &indexes) const
QMimeData *ProjectLegacyModel::mimeData(const QModelIndexList &indexes) const
{
if (indexes.count() <= 0)
return nullptr;
@ -2187,7 +2187,7 @@ bool ProjectModelSortFilterProxy::lessThan(const QModelIndex &source_left, const
{
if (!sourceModel())
return false;
ProjectModel* projectModel = dynamic_cast<ProjectModel*>(sourceModel());
ProjectLegacyModel* projectModel = dynamic_cast<ProjectLegacyModel*>(sourceModel());
FolderNode* pLeft=nullptr;
if (source_left.isValid())
pLeft = static_cast<FolderNode*>(source_left.internalPointer());
@ -2204,3 +2204,14 @@ bool ProjectModelSortFilterProxy::lessThan(const QModelIndex &source_left, const
return false;
return QString::compare(pLeft->text, pRight->text)<0;
}
ProjectModel::ProjectModel(Project *project, QObject *parent):QAbstractItemModel(parent),
mProject(project)
{
}
Project *ProjectModel::project() const
{
return mProject;
}

View File

@ -27,13 +27,13 @@ class Project;
class Editor;
class CppParser;
struct FolderNode;
using PFolderNode = std::shared_ptr<FolderNode>;
struct FolderNode {
struct ProjectLegacyModelNode;
using PProjectLegacyModelNode = std::shared_ptr<ProjectLegacyModelNode>;
struct ProjectLegacyModelNode {
QString text;
std::weak_ptr<FolderNode> parent;
std::weak_ptr<ProjectLegacyModelNode> parent;
int unitIndex;
QList<PFolderNode> children;
QList<PProjectLegacyModelNode> children;
int level;
};
@ -69,8 +69,8 @@ public:
void setModified(bool value);
bool save();
PFolderNode &node();
void setNode(const PFolderNode &newNode);
PProjectLegacyModelNode &node();
void setNode(const PProjectLegacyModelNode &newNode);
private:
Project* mParent;
@ -85,17 +85,25 @@ private:
bool mLink;
int mPriority;
QByteArray mEncoding;
PFolderNode mNode;
PProjectLegacyModelNode mNode;
};
using PProjectUnit = std::shared_ptr<ProjectUnit>;
class ProjectModel : public QAbstractItemModel {
class ProjectModel: public QAbstractItemModel {
Q_OBJECT
public:
explicit ProjectModel(Project* project, QObject* parent=nullptr);
explicit ProjectModel(Project* project, QObject* parent = nullptr);
Project *project() const;
private:
Project* mProject;
};
class ProjectLegacyModel : public QAbstractItemModel {
Q_OBJECT
public:
explicit ProjectLegacyModel(Project* project, QObject* parent=nullptr);
void beginUpdate();
void endUpdate();
private:
@ -113,7 +121,7 @@ public:
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
private:
QModelIndex getParentIndex(FolderNode * node) const;
QModelIndex getParentIndex(ProjectLegacyModelNode * node) const;
// QAbstractItemModel interface
public:
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override;
@ -150,7 +158,7 @@ public:
void addFolder(const QString& s);
PProjectUnit addUnit(const QString& inFileName,
PFolderNode parentNode,
PProjectLegacyModelNode parentNode,
bool rebuild);
QString folder();
void buildPrivateResource(bool forceSave=false);
@ -159,9 +167,9 @@ public:
void createFolderNodes();
void doAutoOpen();
bool fileAlreadyExists(const QString& s);
PFolderNode folderNodeFromName(const QString& name);
PProjectLegacyModelNode folderNodeFromName(const QString& name);
char getCompilerOption(const QString& optionString);
QString getFolderPath(PFolderNode node);
QString getFolderPath(PProjectLegacyModelNode node);
int getUnitFromString(const QString& s);
void incrementBuildNumber();
int indexInUnits(const QString& fileName) const;
@ -170,14 +178,14 @@ public:
void loadLayout(); // load all [UnitX]
void loadOptions(SimpleIni& ini);
void loadUnitLayout(Editor *e, int index); // load single [UnitX] cursor positions
PFolderNode makeNewFileNode(const QString& s, bool isFolder, PFolderNode newParent);
PFolderNode makeProjectNode();
PProjectUnit newUnit(PFolderNode parentNode,
PProjectLegacyModelNode makeNewFileNode(const QString& s, bool isFolder, PProjectLegacyModelNode newParent);
PProjectLegacyModelNode makeProjectNode();
PProjectUnit newUnit(PProjectLegacyModelNode parentNode,
const QString& customFileName="");
Editor* openUnit(int index);
void rebuildNodes();
bool removeUnit(int index, bool doClose, bool removeFile = false);
bool removeFolder(PFolderNode node);
bool removeFolder(PProjectLegacyModelNode node);
void resetParserProjectFiles();
void saveAll(); // save [Project] and all [UnitX]
void saveLayout(); // save all [UnitX]
@ -193,7 +201,7 @@ public:
void sortUnitsByAlpha();
void updateFolders();
void updateNodeIndexes();
PFolderNode pointerToNode(FolderNode * p, PFolderNode parent=PFolderNode());
PProjectLegacyModelNode pointerToNode(ProjectLegacyModelNode * p, PProjectLegacyModelNode parent=PProjectLegacyModelNode());
void setCompilerSet(int compilerSetIndex);
//void showOptions();
@ -206,11 +214,11 @@ public:
const QString &name() const;
void setName(const QString &newName);
const PFolderNode &rootNode() const;
const PProjectLegacyModelNode &rootNode() const;
ProjectOptions &options();
ProjectModel* model() ;
ProjectLegacyModel* legacyModel() ;
const QList<PProjectUnit> &units() const;
@ -219,8 +227,8 @@ signals:
void modifyChanged(bool value);
private:
void open();
void removeFolderRecurse(PFolderNode node);
void updateFolderNode(PFolderNode node);
void removeFolderRecurse(PProjectLegacyModelNode node);
void updateFolderNode(PProjectLegacyModelNode node);
void updateCompilerSetType();
private:
@ -231,9 +239,9 @@ private:
bool mModified;
QStringList mFolders;
std::shared_ptr<CppParser> mParser;
QList<PFolderNode> mFolderNodes;
PFolderNode mNode;
ProjectModel mModel;
QList<PProjectLegacyModelNode> mFolderNodes;
PProjectLegacyModelNode mNode;
ProjectLegacyModel mModel;
};
#endif // PROJECT_H

View File

@ -68,7 +68,7 @@ PProjectUnit ProjectFilesWidget::currentUnit()
QModelIndex index = ui->treeProject->currentIndex();
if (!index.isValid())
return PProjectUnit();
FolderNode* node = static_cast<FolderNode*>(index.internalPointer());
ProjectLegacyModelNode* node = static_cast<ProjectLegacyModelNode*>(index.internalPointer());
if (!node)
return PProjectUnit();
int i = node->unitIndex;
@ -136,7 +136,7 @@ void ProjectFilesWidget::on_treeProject_doubleClicked(const QModelIndex &index)
disableFileOptions();
return ;
}
FolderNode* node = static_cast<FolderNode*>(index.internalPointer());
ProjectLegacyModelNode* node = static_cast<ProjectLegacyModelNode*>(index.internalPointer());
if (!node) {
disableFileOptions();
return;