2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-09-05 23:45:05 +08:00
|
|
|
#ifndef PROJECT_H
|
|
|
|
#define PROJECT_H
|
|
|
|
|
2021-09-11 09:21:44 +08:00
|
|
|
#include <QAbstractItemModel>
|
2022-10-01 08:54:44 +08:00
|
|
|
#include <QHash>
|
|
|
|
#include <QSet>
|
2022-01-08 08:52:50 +08:00
|
|
|
#include <QSortFilterProxyModel>
|
2021-09-05 23:45:05 +08:00
|
|
|
#include <memory>
|
2021-09-16 12:03:10 +08:00
|
|
|
#include "projectoptions.h"
|
|
|
|
#include "utils.h"
|
2021-09-05 23:45:05 +08:00
|
|
|
|
|
|
|
class Project;
|
|
|
|
class Editor;
|
2021-09-06 08:45:53 +08:00
|
|
|
class CppParser;
|
2022-03-21 09:08:05 +08:00
|
|
|
class EditorList;
|
|
|
|
class QFileSystemWatcher;
|
2021-09-06 08:45:53 +08:00
|
|
|
|
2022-09-30 17:51:26 +08:00
|
|
|
enum ProjectModelNodeType {
|
|
|
|
DUMMY_HEADERS_FOLDER,
|
|
|
|
DUMMY_SOURCES_FOLDER,
|
|
|
|
DUMMY_OTHERS_FOLDER,
|
|
|
|
Folder,
|
|
|
|
File
|
2022-02-13 20:08:18 +08:00
|
|
|
};
|
|
|
|
|
2022-10-01 08:54:44 +08:00
|
|
|
struct ProjectModelItemRecord {
|
|
|
|
ProjectModelNodeType type;
|
|
|
|
QString fullPath;
|
|
|
|
};
|
|
|
|
|
2022-10-08 23:30:30 +08:00
|
|
|
class ProjectUnit;
|
|
|
|
|
2022-02-08 00:24:08 +08:00
|
|
|
struct ProjectModelNode;
|
|
|
|
using PProjectModelNode = std::shared_ptr<ProjectModelNode>;
|
|
|
|
struct ProjectModelNode {
|
2021-09-07 00:32:16 +08:00
|
|
|
QString text;
|
2022-02-08 00:24:08 +08:00
|
|
|
std::weak_ptr<ProjectModelNode> parent;
|
2022-10-08 23:30:30 +08:00
|
|
|
bool isUnit;
|
|
|
|
std::weak_ptr<ProjectUnit> pUnit;
|
2022-02-08 12:33:10 +08:00
|
|
|
int priority;
|
2022-02-08 00:24:08 +08:00
|
|
|
QList<PProjectModelNode> children;
|
2022-09-30 17:51:26 +08:00
|
|
|
ProjectModelNodeType folderNodeType;
|
2021-09-10 21:37:52 +08:00
|
|
|
int level;
|
2021-09-07 00:32:16 +08:00
|
|
|
};
|
|
|
|
|
2022-10-02 23:32:33 +08:00
|
|
|
struct ProjectEditorLayout {
|
|
|
|
QString filename;
|
|
|
|
int topLine;
|
|
|
|
int leftChar;
|
|
|
|
int caretX;
|
|
|
|
int caretY;
|
2022-10-09 22:19:18 +08:00
|
|
|
int order;
|
|
|
|
bool isFocused;
|
|
|
|
bool isOpen;
|
2022-10-02 23:32:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
using PProjectEditorLayout = std::shared_ptr<ProjectEditorLayout>;
|
|
|
|
|
2021-09-05 23:45:05 +08:00
|
|
|
class ProjectUnit {
|
|
|
|
|
|
|
|
public:
|
2021-09-07 00:32:16 +08:00
|
|
|
explicit ProjectUnit(Project* parent);
|
|
|
|
Project* parent() const;
|
2021-09-05 23:45:05 +08:00
|
|
|
const QString &fileName() const;
|
2021-10-23 19:27:50 +08:00
|
|
|
void setFileName(QString newFileName);
|
2021-09-05 23:45:05 +08:00
|
|
|
const QString &folder() const;
|
|
|
|
void setFolder(const QString &newFolder);
|
|
|
|
bool compile() const;
|
|
|
|
void setCompile(bool newCompile);
|
|
|
|
bool compileCpp() const;
|
|
|
|
void setCompileCpp(bool newCompileCpp);
|
|
|
|
bool overrideBuildCmd() const;
|
|
|
|
void setOverrideBuildCmd(bool newOverrideBuildCmd);
|
|
|
|
const QString &buildCmd() const;
|
|
|
|
void setBuildCmd(const QString &newBuildCmd);
|
|
|
|
bool link() const;
|
|
|
|
void setLink(bool newLink);
|
|
|
|
int priority() const;
|
|
|
|
void setPriority(int newPriority);
|
|
|
|
const QByteArray &encoding() const;
|
|
|
|
void setEncoding(const QByteArray &newEncoding);
|
|
|
|
|
2022-02-08 00:24:08 +08:00
|
|
|
PProjectModelNode &node();
|
|
|
|
void setNode(const PProjectModelNode &newNode);
|
2021-09-07 10:28:40 +08:00
|
|
|
|
2022-03-28 16:57:58 +08:00
|
|
|
bool FileMissing() const;
|
|
|
|
void setFileMissing(bool newDontSave);
|
|
|
|
|
2022-10-21 12:43:02 +08:00
|
|
|
void setNew(bool newNew);
|
|
|
|
|
2021-09-05 23:45:05 +08:00
|
|
|
private:
|
2021-09-07 00:32:16 +08:00
|
|
|
Project* mParent;
|
2021-09-05 23:45:05 +08:00
|
|
|
QString mFileName;
|
|
|
|
QString mFolder;
|
2022-10-21 12:43:02 +08:00
|
|
|
bool mNew;
|
2021-09-05 23:45:05 +08:00
|
|
|
bool mCompile;
|
|
|
|
bool mCompileCpp;
|
|
|
|
bool mOverrideBuildCmd;
|
|
|
|
QString mBuildCmd;
|
|
|
|
bool mLink;
|
|
|
|
int mPriority;
|
|
|
|
QByteArray mEncoding;
|
2022-02-08 00:24:08 +08:00
|
|
|
PProjectModelNode mNode;
|
2022-03-28 16:57:58 +08:00
|
|
|
bool mFileMissing;
|
2021-09-05 23:45:05 +08:00
|
|
|
};
|
|
|
|
|
2021-09-06 08:45:53 +08:00
|
|
|
using PProjectUnit = std::shared_ptr<ProjectUnit>;
|
|
|
|
|
2022-02-15 00:01:50 +08:00
|
|
|
class GitRepository;
|
|
|
|
class CustomFileIconProvider;
|
2022-02-08 00:24:08 +08:00
|
|
|
class ProjectModel : public QAbstractItemModel {
|
2022-02-07 22:05:19 +08:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2022-02-08 00:24:08 +08:00
|
|
|
explicit ProjectModel(Project* project, QObject* parent=nullptr);
|
2022-02-14 00:13:00 +08:00
|
|
|
~ProjectModel();
|
2021-09-11 09:21:44 +08:00
|
|
|
void beginUpdate();
|
|
|
|
void endUpdate();
|
|
|
|
private:
|
|
|
|
Project* mProject;
|
|
|
|
int mUpdateCount;
|
2022-02-15 00:01:50 +08:00
|
|
|
CustomFileIconProvider* mIconProvider;
|
2021-09-11 09:21:44 +08:00
|
|
|
|
2022-02-14 00:13:00 +08:00
|
|
|
|
2021-09-11 09:21:44 +08:00
|
|
|
// QAbstractItemModel interface
|
|
|
|
public:
|
|
|
|
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
|
|
|
|
QModelIndex parent(const QModelIndex &child) const override;
|
|
|
|
int rowCount(const QModelIndex &parent) const override;
|
|
|
|
int columnCount(const QModelIndex &parent) const override;
|
|
|
|
QVariant data(const QModelIndex &index, int role) const override;
|
2021-09-18 10:47:35 +08:00
|
|
|
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
|
|
|
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
2021-10-23 23:10:34 +08:00
|
|
|
|
2022-09-30 17:51:26 +08:00
|
|
|
QModelIndex getNodeIndex(ProjectModelNode *node) const;
|
2022-10-01 08:54:44 +08:00
|
|
|
QModelIndex getParentIndex(ProjectModelNode * node) const;
|
2022-09-30 17:51:26 +08:00
|
|
|
|
2022-10-02 23:32:33 +08:00
|
|
|
QModelIndex rootIndex() const;
|
|
|
|
|
2021-10-24 12:04:37 +08:00
|
|
|
private:
|
2022-09-30 17:51:26 +08:00
|
|
|
|
2021-10-23 23:10:34 +08:00
|
|
|
// QAbstractItemModel interface
|
|
|
|
public:
|
|
|
|
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override;
|
2021-10-24 00:17:08 +08:00
|
|
|
Qt::DropActions supportedDropActions() const override;
|
2021-10-24 12:04:37 +08:00
|
|
|
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
|
|
|
|
QMimeData *mimeData(const QModelIndexList &indexes) const override;
|
2022-01-08 08:52:50 +08:00
|
|
|
Project *project() const;
|
2022-02-15 21:39:17 +08:00
|
|
|
CustomFileIconProvider *iconProvider() const;
|
2022-10-01 08:54:44 +08:00
|
|
|
|
|
|
|
// QAbstractItemModel interface
|
|
|
|
public:
|
|
|
|
bool insertRows(int row, int count, const QModelIndex &parent) override;
|
|
|
|
bool removeRows(int row, int count, const QModelIndex &parent) override;
|
2022-01-08 08:52:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ProjectModelSortFilterProxy : public QSortFilterProxyModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ProjectModelSortFilterProxy(QObject *parent = nullptr);
|
|
|
|
// QSortFilterProxyModel interface
|
|
|
|
protected:
|
|
|
|
bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
|
2021-09-11 09:21:44 +08:00
|
|
|
};
|
|
|
|
|
2021-09-16 23:51:05 +08:00
|
|
|
class ProjectTemplate;
|
2021-09-05 23:45:05 +08:00
|
|
|
class Project : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2022-03-21 09:08:05 +08:00
|
|
|
explicit Project(const QString& filename, const QString& name,
|
|
|
|
EditorList* editorList,
|
|
|
|
QFileSystemWatcher* fileSystemWatcher,
|
|
|
|
QObject *parent = nullptr);
|
2022-10-09 22:19:18 +08:00
|
|
|
|
|
|
|
static std::shared_ptr<Project> load(const QString& filename,
|
|
|
|
EditorList* editorList,
|
|
|
|
QFileSystemWatcher* fileSystemWatcher,
|
|
|
|
QObject *parent = nullptr);
|
|
|
|
static std::shared_ptr<Project> create(const QString& filename,
|
|
|
|
const QString& name,
|
|
|
|
EditorList* editorList,
|
|
|
|
QFileSystemWatcher* fileSystemWatcher,
|
|
|
|
const std::shared_ptr<ProjectTemplate> pTemplate,
|
|
|
|
bool useCpp,
|
|
|
|
QObject *parent = nullptr);
|
|
|
|
|
2021-09-11 18:42:49 +08:00
|
|
|
~Project();
|
2021-09-09 11:47:04 +08:00
|
|
|
QString directory() const;
|
|
|
|
QString executable() const;
|
2021-09-06 08:45:53 +08:00
|
|
|
QString makeFileName();
|
2021-09-09 11:47:04 +08:00
|
|
|
bool modified() const;
|
2021-10-23 19:27:50 +08:00
|
|
|
void setFileName(QString value);
|
2021-09-07 00:32:16 +08:00
|
|
|
void setModified(bool value);
|
|
|
|
|
2022-10-02 23:32:33 +08:00
|
|
|
PProjectModelNode addFolder(PProjectModelNode parentFolder, const QString& s);
|
2021-09-07 00:32:16 +08:00
|
|
|
PProjectUnit addUnit(const QString& inFileName,
|
2022-10-02 13:29:51 +08:00
|
|
|
PProjectModelNode parentNode);
|
2022-01-27 18:34:18 +08:00
|
|
|
QString folder();
|
2021-09-12 22:45:00 +08:00
|
|
|
void buildPrivateResource(bool forceSave=false);
|
2022-10-08 23:30:30 +08:00
|
|
|
void closeUnit(PProjectUnit& unit);
|
2022-10-02 17:23:27 +08:00
|
|
|
PProjectUnit doAutoOpen();
|
2021-09-09 00:15:12 +08:00
|
|
|
bool fileAlreadyExists(const QString& s);
|
2022-05-12 15:28:08 +08:00
|
|
|
|
2022-10-01 08:54:44 +08:00
|
|
|
QString getNodePath(PProjectModelNode node);
|
2021-09-09 18:36:54 +08:00
|
|
|
void incrementBuildNumber();
|
2022-10-01 08:54:44 +08:00
|
|
|
|
2022-02-08 00:24:08 +08:00
|
|
|
PProjectUnit newUnit(PProjectModelNode parentNode,
|
2021-09-17 13:35:50 +08:00
|
|
|
const QString& customFileName="");
|
2022-10-08 23:30:30 +08:00
|
|
|
Editor* openUnit(PProjectUnit& unit, bool forceOpen=true);
|
|
|
|
Editor* openUnit(PProjectUnit& unit, const PProjectEditorLayout& layout);
|
2022-03-21 09:08:05 +08:00
|
|
|
Editor* unitEditor(const PProjectUnit& unit) const;
|
|
|
|
Editor* unitEditor(const ProjectUnit* unit) const;
|
2022-10-01 08:54:44 +08:00
|
|
|
|
|
|
|
QList<PProjectUnit> unitList();
|
2022-10-22 23:29:12 +08:00
|
|
|
QStringList unitFiles();
|
2022-10-01 08:54:44 +08:00
|
|
|
|
2022-02-08 12:33:10 +08:00
|
|
|
PProjectModelNode pointerToNode(ProjectModelNode * p, PProjectModelNode parent=PProjectModelNode());
|
2021-09-10 15:03:47 +08:00
|
|
|
void rebuildNodes();
|
2022-10-08 23:30:30 +08:00
|
|
|
bool removeUnit(PProjectUnit& unit, bool doClose, bool removeFile = false);
|
2022-02-08 00:24:08 +08:00
|
|
|
bool removeFolder(PProjectModelNode node);
|
2021-09-11 18:42:49 +08:00
|
|
|
void resetParserProjectFiles();
|
2021-09-10 21:37:52 +08:00
|
|
|
void saveAll(); // save [Project] and all [UnitX]
|
|
|
|
void saveLayout(); // save all [UnitX]
|
2021-09-10 23:41:38 +08:00
|
|
|
void saveOptions();
|
2022-10-08 23:30:30 +08:00
|
|
|
void renameUnit(PProjectUnit& unit, const QString& sFileName);
|
2021-09-07 10:28:40 +08:00
|
|
|
bool saveUnits();
|
2022-04-08 21:15:10 +08:00
|
|
|
|
2022-10-01 08:54:44 +08:00
|
|
|
PProjectUnit findUnit(const QString& filename);
|
|
|
|
PProjectUnit findUnit(const Editor* editor);
|
|
|
|
|
2022-01-13 17:15:57 +08:00
|
|
|
void associateEditor(Editor* editor);
|
|
|
|
void associateEditorToUnit(Editor* editor, PProjectUnit unit);
|
2022-05-12 15:28:08 +08:00
|
|
|
bool setCompileOption(const QString &key, const QString &value);
|
2022-05-14 11:21:59 +08:00
|
|
|
QString getCompileOption(const QString &key) const;
|
2022-05-12 15:28:08 +08:00
|
|
|
|
2021-09-10 23:41:38 +08:00
|
|
|
void updateFolders();
|
2021-10-25 21:36:23 +08:00
|
|
|
void setCompilerSet(int compilerSetIndex);
|
2021-09-09 00:15:12 +08:00
|
|
|
|
2022-08-07 21:41:57 +08:00
|
|
|
bool saveAsTemplate(const QString& templateFolder,
|
|
|
|
const QString& name,
|
|
|
|
const QString& description,
|
|
|
|
const QString& category);
|
2021-09-07 16:49:35 +08:00
|
|
|
|
|
|
|
std::shared_ptr<CppParser> cppParser();
|
2021-09-09 00:15:12 +08:00
|
|
|
const QString &filename() const;
|
|
|
|
|
2021-09-10 12:37:02 +08:00
|
|
|
const QString &name() const;
|
|
|
|
void setName(const QString &newName);
|
|
|
|
|
2022-02-08 00:24:08 +08:00
|
|
|
const PProjectModelNode &rootNode() const;
|
2021-09-10 12:37:02 +08:00
|
|
|
|
2021-09-14 17:33:47 +08:00
|
|
|
ProjectOptions &options();
|
2021-09-10 15:03:47 +08:00
|
|
|
|
2022-02-08 00:24:08 +08:00
|
|
|
ProjectModel* model() ;
|
2021-09-11 11:42:20 +08:00
|
|
|
|
2022-02-08 12:33:10 +08:00
|
|
|
ProjectModelType modelType() const;
|
|
|
|
void setModelType(ProjectModelType type);
|
|
|
|
|
2022-03-21 09:08:05 +08:00
|
|
|
EditorList *editorList() const;
|
|
|
|
|
|
|
|
QFileSystemWatcher *fileSystemWatcher() const;
|
|
|
|
|
2022-04-08 18:12:40 +08:00
|
|
|
QString fileSystemNodeFolderPath(const PProjectModelNode& node);
|
|
|
|
|
2022-06-16 21:34:31 +08:00
|
|
|
QStringList binDirs();
|
|
|
|
|
2022-10-08 23:30:30 +08:00
|
|
|
void renameFolderNode(PProjectModelNode node, const QString newName);
|
|
|
|
void loadUnitLayout(Editor *e);
|
2021-09-05 23:45:05 +08:00
|
|
|
signals:
|
2022-10-08 23:30:30 +08:00
|
|
|
void nodeRenamed();
|
2021-09-07 10:28:40 +08:00
|
|
|
void modifyChanged(bool value);
|
2022-10-08 23:30:30 +08:00
|
|
|
|
2021-09-07 00:32:16 +08:00
|
|
|
private:
|
2022-10-09 22:19:18 +08:00
|
|
|
bool assignTemplate(const std::shared_ptr<ProjectTemplate> aTemplate, bool useCpp);
|
2022-02-08 12:33:10 +08:00
|
|
|
void checkProjectFileForUpdate(SimpleIni& ini);
|
|
|
|
void createFolderNodes();
|
|
|
|
void createFileSystemFolderNodes();
|
2022-09-30 17:51:26 +08:00
|
|
|
void createFileSystemFolderNode(ProjectModelNodeType folderType, const QString& folderName, PProjectModelNode parent, const QSet<QString>& validFolders);
|
2022-10-02 13:29:51 +08:00
|
|
|
PProjectModelNode getParentFileSystemFolderNode(const QString& filename);
|
|
|
|
PProjectModelNode findFileSystemFolderNode(const QString& folderPath, ProjectModelNodeType nodeType);
|
|
|
|
PProjectModelNode getCustomeFolderNodeFromName(const QString& name);
|
2022-02-08 12:33:10 +08:00
|
|
|
void loadOptions(SimpleIni& ini);
|
2022-10-09 22:19:18 +08:00
|
|
|
//PProjectUnit
|
|
|
|
QHash<QString, PProjectEditorLayout> loadLayout();
|
2022-02-08 12:33:10 +08:00
|
|
|
|
2022-10-01 08:54:44 +08:00
|
|
|
PProjectModelNode makeNewFolderNode(
|
|
|
|
const QString& folderName,
|
|
|
|
PProjectModelNode newParent,
|
|
|
|
ProjectModelNodeType nodeType=ProjectModelNodeType::Folder,
|
|
|
|
int priority=0);
|
|
|
|
PProjectModelNode makeNewFileNode(
|
2022-10-08 23:30:30 +08:00
|
|
|
//const QString& fileName,
|
|
|
|
PProjectUnit unit,
|
2022-10-01 08:54:44 +08:00
|
|
|
int priority,
|
|
|
|
PProjectModelNode newParent);
|
2022-02-08 12:33:10 +08:00
|
|
|
PProjectModelNode makeProjectNode();
|
2021-09-07 00:32:16 +08:00
|
|
|
void open();
|
2022-02-08 00:24:08 +08:00
|
|
|
void removeFolderRecurse(PProjectModelNode node);
|
|
|
|
void updateFolderNode(PProjectModelNode node);
|
2021-10-25 21:36:23 +08:00
|
|
|
void updateCompilerSetType();
|
2021-09-12 19:50:44 +08:00
|
|
|
|
2021-09-06 08:45:53 +08:00
|
|
|
private:
|
2022-10-08 23:30:30 +08:00
|
|
|
QHash<QString,PProjectUnit> mUnits;
|
2021-09-06 08:45:53 +08:00
|
|
|
ProjectOptions mOptions;
|
|
|
|
QString mFilename;
|
|
|
|
QString mName;
|
|
|
|
bool mModified;
|
|
|
|
QStringList mFolders;
|
|
|
|
std::shared_ptr<CppParser> mParser;
|
2022-02-08 12:33:10 +08:00
|
|
|
PProjectModelNode mRootNode;
|
2022-10-02 13:29:51 +08:00
|
|
|
|
2022-09-30 17:51:26 +08:00
|
|
|
QHash<ProjectModelNodeType, PProjectModelNode> mSpecialNodes;
|
2022-02-08 12:33:10 +08:00
|
|
|
QHash<QString, PProjectModelNode> mFileSystemFolderNodes;
|
2022-10-02 13:29:51 +08:00
|
|
|
|
|
|
|
QList<PProjectModelNode> mCustomFolderNodes;
|
2022-02-08 00:24:08 +08:00
|
|
|
ProjectModel mModel;
|
2022-03-21 09:08:05 +08:00
|
|
|
EditorList *mEditorList;
|
|
|
|
QFileSystemWatcher* mFileSystemWatcher;
|
2021-09-05 23:45:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PROJECT_H
|