diff --git a/RedPandaIDE/compiler/compiler.cpp b/RedPandaIDE/compiler/compiler.cpp index 9f95235e..3e43ea4c 100644 --- a/RedPandaIDE/compiler/compiler.cpp +++ b/RedPandaIDE/compiler/compiler.cpp @@ -403,7 +403,7 @@ QString Compiler::parseFileIncludesForAutolink( PCppParser& parser) { QString result; - QString baseName = baseFileName(filename); + QString baseName = extractFileName(filename); if (parsedFiles.contains(filename)) return result; parsedFiles.insert(filename); diff --git a/RedPandaIDE/debugger.cpp b/RedPandaIDE/debugger.cpp index 3a9b12cb..4d066d52 100644 --- a/RedPandaIDE/debugger.cpp +++ b/RedPandaIDE/debugger.cpp @@ -1468,7 +1468,7 @@ QVariant BreakpointModel::data(const QModelIndex &index, int role) const case Qt::DisplayRole: switch (index.column()) { case 0: { - return baseFileName(breakpoint->filename); + return extractFileName(breakpoint->filename); } case 1: if (breakpoint->line>0) diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 97da4af0..f4e45d33 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -46,8 +46,6 @@ const char* SaveException::what() const noexcept { return mReasonBuffer; } -int Editor::newfileCount=0; - Editor::Editor(QWidget *parent): Editor(parent,QObject::tr("untitled"),ENCODING_SYSTEM_DEFAULT,false,true,nullptr) { @@ -69,14 +67,13 @@ Editor::Editor(QWidget *parent, const QString& filename, mActiveBreakpointLine(-1), mLastIdCharPressed(0), mCurrentWord(), - mSelectionWord(), + mCurrentTipType(TipType::None), mOldSelectionWord(), - mCurrentTipType(TipType::None) + mSelectionWord() { mUseCppSyntax = pSettings->editor().defaultFileCpp(); if (mFilename.isEmpty()) { - newfileCount++; - mFilename = tr("untitled%1").arg(newfileCount); + mFilename = tr("untitled")+QString("%1").arg(getNewFileNumber()); } QFileInfo fileInfo(mFilename); if (mParentPageControl!=nullptr) { @@ -2196,6 +2193,17 @@ QString Editor::getHintForFunction(const PStatement &statement, const PStatement return result; } +void Editor::setInProject(bool newInProject) +{ + if (mInProject == newInProject) + return; + if (mInProject) { + initParser(); + } else { + mParser = pMainWindow->project()->cppParser(); + } +} + void Editor::gotoDeclaration(const BufferCoord &pos) { // Exit early, don't bother creating a stream (which is slow) diff --git a/RedPandaIDE/editor.h b/RedPandaIDE/editor.h index d748f00f..83f98434 100644 --- a/RedPandaIDE/editor.h +++ b/RedPandaIDE/editor.h @@ -198,7 +198,6 @@ private: private: - static int newfileCount; QByteArray mEncodingOption; // the encoding type set by the user QByteArray mFileEncoding; // the real encoding of the file (auto detected) QString mFilename; @@ -253,6 +252,8 @@ public: bool event(QEvent *event) override; // QWidget interface + void setInProject(bool newInProject); + protected: void mouseReleaseEvent(QMouseEvent *event) override; }; diff --git a/RedPandaIDE/project.cpp b/RedPandaIDE/project.cpp index 166cfa4d..06cbe5cf 100644 --- a/RedPandaIDE/project.cpp +++ b/RedPandaIDE/project.cpp @@ -49,13 +49,13 @@ QString Project::executable() const } else { switch(mOptions.type) { case ProjectType::StaticLib: - exeFileName = changeFileExt(baseFileName(mFilename),STATIC_LIB_EXT); + exeFileName = changeFileExt(extractFileName(mFilename),STATIC_LIB_EXT); break; case ProjectType::DynamicLib: - exeFileName = changeFileExt(baseFileName(mFilename),DYNAMIC_LIB_EXT); + exeFileName = changeFileExt(extractFileName(mFilename),DYNAMIC_LIB_EXT); break; default: - exeFileName = changeFileExt(baseFileName(mFilename),EXECUTABLE_EXT); + exeFileName = changeFileExt(extractFileName(mFilename),EXECUTABLE_EXT); } } QString exePath; @@ -147,7 +147,7 @@ void Project::open() newUnit->setEditor(nullptr); newUnit->setNew(false); newUnit->setParent(this); - newUnit->setNode(makeNewFileNode(baseFileName(newUnit->fileName()), false, folderNodeFromName(newUnit->folder()))); + newUnit->setNode(makeNewFileNode(extractFileName(newUnit->fileName()), false, folderNodeFromName(newUnit->folder()))); newUnit->node()->unitIndex = mUnits.count(); mUnits.append(newUnit); } @@ -192,6 +192,81 @@ PFolderNode Project::makeProjectNode() node->text = mName; } +int Project::newUnit(bool newProject, PFolderNode parentNode, const QString customFileName) +{ + PProjectUnit newUnit = std::make_shared(); + + // Select folder to add unit to + if (!parentNode) + parentNode = mNode; // project root node + + if (parentNode->unitIndex>=0) { //it's a file + parentNode = mNode; + } + QString s; + QDir dir(directory()); + // Find unused 'new' filename + if (customFileName.isEmpty()) { + do { + s = dir.absoluteFilePath(tr("untitled")+QString("%1").arg(getNewFileNumber())); + } while (fileExists(s)); + } else { + s = dir.absoluteFilePath(customFileName); + } + // Add + int result = mUnits.count(); + mUnits.append(newUnit); + + // Set all properties + newUnit->setFileName(s); + newUnit->setNew(true); + newUnit->setEditor(nullptr); + newUnit->setFolder(getFolderPath(parentNode)); + newUnit->setNode(makeNewFileNode(extractFileName(newUnit->fileName()), + false, parentNode)); + newUnit->node()->unitIndex = result; + //parentNode.Expand(True); + newUnit->setCompile(true); + newUnit->setCompileCpp(mOptions.useGPP); + newUnit->setLink(true); + newUnit->setPriority(1000); + newUnit->setOverrideBuildCmd(false); + newUnit->setBuildCmd(""); + newUnit->setModified(true); + return result; +} + +Editor *Project::openUnit(int index) +{ + if ((index < 0) || (index >= mUnits.count())) + return nullptr; + + PProjectUnit unit = mUnits[index]; + + if (!unit->fileName().isEmpty()) { + QDir dir(directory()); + QString fullPath = dir.absoluteFilePath(unit->fileName()); + Editor * editor = pMainWindow->editorList()->getOpenedEditorByFilename(fullPath); + if (editor) {//already opened in the editors + editor->setInProject(true); + return editor; + } + try + fEditor := MainForm.EditorList.NewEditor(FullPath, Encoding, true, false); + fEditor.InProject := True; + Encoding := fEditor.EncodingOption; + LoadUnitLayout(fEditor, index); + Result := fEditor; + except + MessageDlg(Format(Lang[ID_ERR_OPENFILE], [Filename]), mtError, [mbOK], 0); + end; + + } + + if FileName <> '' then begin + end; +} + void Project::addFolder(const QString &s) { if (mFolders.indexOf(s)<0) { @@ -221,7 +296,7 @@ PProjectUnit Project::addUnit(const QString &inFileName, PFolderNode parentNode, newUnit->setNew(false); newUnit->setEditor(nullptr); newUnit->setFolder(getFolderPath(parentNode)); - newUnit->setNode(makeNewFileNode(baseFileName(newUnit->fileName()), false, parentNode)); + newUnit->setNode(makeNewFileNode(extractFileName(newUnit->fileName()), false, parentNode)); newUnit->node()->unitIndex = mUnits.count(); mUnits.append(newUnit); @@ -347,10 +422,10 @@ void Project::buildPrivateResource(bool forceSave) "1 24 \"" + genMakePath2( includeTrailingPathDelimiter(mOptions.exeOutput) - + baseFileName(executable())) + + extractFileName(executable())) + ".Manifest\""); else - contents.append("1 24 \"" + baseFileName(executable()) + ".Manifest\""); + contents.append("1 24 \"" + extractFileName(executable()) + ".Manifest\""); } if (mOptions.includeVersionInfo) { @@ -482,7 +557,7 @@ void Project::buildPrivateResource(bool forceSave) // create private header file QString hFile = changeFileExt(rcFile, H_EXT); contents.clear(); - QString def = baseFileName(rcFile); + QString def = extractFileName(rcFile); def.replace(".","_"); contents.append("/* THIS FILE WILL BE OVERWRITTEN BY DEV-C++ */"); contents.append("/* DO NOT EDIT ! */"); @@ -804,7 +879,7 @@ void Project::loadOptions() mOptions.versionInfo.legalCopyright = mIniFile->value("LegalCopyright","").toString(); mOptions.versionInfo.legalTrademarks = mIniFile->value("LegalTrademarks","").toString(); mOptions.versionInfo.originalFilename = mIniFile->value("OriginalFilename", - baseFileName(executable())).toString(); + extractFileName(executable())).toString(); mOptions.versionInfo.productName = mIniFile->value("ProductName", mName).toString(); mOptions.versionInfo.productVersion = mIniFile->value("ProductVersion", "0.1.1.1").toString(); mOptions.versionInfo.autoIncBuildNr = mIniFile->value("AutoIncBuildNr", false).toBool(); @@ -862,6 +937,26 @@ int Project::indexInUnits(const Editor *editor) const return indexInUnits(editor->filename()); } +const PFolderNode &Project::node() const +{ + return mNode; +} + +void Project::setNode(const PFolderNode &newNode) +{ + mNode = newNode; +} + +const QString &Project::name() const +{ + return mName; +} + +void Project::setName(const QString &newName) +{ + mName = newName; +} + std::shared_ptr &Project::iniFile() { return mIniFile; @@ -1051,7 +1146,7 @@ bool ProjectUnit::save() result = mEditor->save(); } if (mNode) { - mNode->text = baseFileName(mFileName); + mNode->text = extractFileName(mFileName); } return result; } diff --git a/RedPandaIDE/project.h b/RedPandaIDE/project.h index f98cf815..0013612c 100644 --- a/RedPandaIDE/project.h +++ b/RedPandaIDE/project.h @@ -148,7 +148,6 @@ public: bool modified() const; void setFileName(const QString& value); void setModified(bool value); - PFolderNode makeNewFileNode(const QString& s, bool isFolder, PFolderNode newParent); void addFolder(const QString& s); PProjectUnit addUnit(const QString& inFileName, @@ -168,18 +167,20 @@ public: QString listUnitStr(const QChar& separator); void loadLayout(); // load all [UnitX] void loadOptions(); - + PFolderNode makeNewFileNode(const QString& s, bool isFolder, PFolderNode newParent); + PFolderNode makeProjectNode(); int newUnit(bool newProject, PFolderNode parentNode, const QString customFileName); - void updateFolders(); Editor* openUnit(int index); + + + void updateFolders(); void saveUnitAs(int i, const QString& sFileName); // save single [UnitX] void saveAll(); // save [Project] and all [UnitX] void loadUnitLayout(Editor *e, int index); // load single [UnitX] cursor positions void saveLayout(); // save all [UnitX] void saveUnitLayout(Editor* e, int index); // save single [UnitX] cursor positions - PFolderNode makeProjectNode(); void saveOptions(); bool saveUnits(); // procedure Open; @@ -200,6 +201,12 @@ public: std::shared_ptr &iniFile(); void setIniFile(const std::shared_ptr &newIniFile); + const QString &name() const; + void setName(const QString &newName); + + const PFolderNode &node() const; + void setNode(const PFolderNode &newNode); + signals: void nodesChanged(); void modifyChanged(bool value); diff --git a/RedPandaIDE/utils.cpp b/RedPandaIDE/utils.cpp index 792743da..256a087d 100644 --- a/RedPandaIDE/utils.cpp +++ b/RedPandaIDE/utils.cpp @@ -616,7 +616,7 @@ void logToFile(const QString &s, const QString &filename, bool append) } } -QString baseFileName(const QString &fileName) +QString extractFileName(const QString &fileName) { QFileInfo fileInfo(fileName); return fileInfo.fileName(); @@ -670,3 +670,22 @@ QString getSizeString(int size) return QString("%1 ").arg(size / 1024.0 / 1024.0 / 1024.0)+QObject::tr("GB"); } } + +int getNewFileNumber() +{ + static int count = 0; + count++; + return count; +} + +QString extractFilePath(const QString &filePath) +{ + QFileInfo info(filePath); + return info.path(); +} + +QString extractAbsoluteFilePath(const QString &filePath) +{ + QFileInfo info(filePath); + return info.absoluteFilePath(); +} diff --git a/RedPandaIDE/utils.h b/RedPandaIDE/utils.h index 063b1f21..b61ed770 100644 --- a/RedPandaIDE/utils.h +++ b/RedPandaIDE/utils.h @@ -162,9 +162,13 @@ bool findComplement(const QString& s, int increment); void logToFile(const QString& s, const QString& filename, bool append=true); -QString baseFileName(const QString& fileName); +QString extractFileName(const QString& fileName); +QString extractFilePath(const QString& filePath); +QString extractAbsoluteFilePath(const QString& filePath); QString getSizeString(int size); +int getNewFileNumber(); + class CppParser; void resetCppParser(std::shared_ptr parser); diff --git a/RedPandaIDE/widgets/filepropertiesdialog.cpp b/RedPandaIDE/widgets/filepropertiesdialog.cpp index 389b04c1..a433bd40 100644 --- a/RedPandaIDE/widgets/filepropertiesdialog.cpp +++ b/RedPandaIDE/widgets/filepropertiesdialog.cpp @@ -96,7 +96,7 @@ QVariant FilePropertiesModel::data(const QModelIndex &index, int role) const if (row>=0 && row < pMainWindow->editorList()->pageCount()) { Editor *editor = (*(pMainWindow->editorList()))[row]; if (editor) { - return baseFileName(editor->filename()); + return extractFileName(editor->filename()); } } } diff --git a/RedPandaIDE/widgets/issuestable.cpp b/RedPandaIDE/widgets/issuestable.cpp index 29320acf..d085c2a6 100644 --- a/RedPandaIDE/widgets/issuestable.cpp +++ b/RedPandaIDE/widgets/issuestable.cpp @@ -183,7 +183,7 @@ QVariant IssuesModel::data(const QModelIndex &index, int role) const switch (index.column()) { case 0: { if (role == Qt::DisplayRole) - return baseFileName(issue->filename); + return extractFileName(issue->filename); else return issue->filename; } diff --git a/RedPandaIDE/widgets/searchresultview.cpp b/RedPandaIDE/widgets/searchresultview.cpp index 75010c8e..09c00887 100644 --- a/RedPandaIDE/widgets/searchresultview.cpp +++ b/RedPandaIDE/widgets/searchresultview.cpp @@ -289,7 +289,7 @@ QVariant SearchResultListModel::data(const QModelIndex &index, int role) const } else if (results->searchType == SearchType::FindOccurences) { return tr("References to symbol \'%1\' at '%2':%3") .arg(results->keyword) - .arg(baseFileName(results->filename)) + .arg(extractFileName(results->filename)) .arg(results->symbolLine); } }