From bc4cddcd046a2348a0ca2e2be60e136b9a36573a Mon Sep 17 00:00:00 2001 From: Yury Shvedov Date: Sun, 15 Dec 2024 15:30:39 +0300 Subject: [PATCH] Macro: allow to specify extra system paths for macro This introduce new option `-E [ --macro-path]` to specify extra system paths of macros. The macro found in this paths will appear in `Macros` dialog at `System macros` tab. Change-Id: Ic21631ec0ebe8af5c7f42b4fe95400cfb67807d5 --- src/App/Application.cpp | 14 ++++++- src/App/FreeCADInit.py | 3 +- src/Gui/DlgMacroExecuteImp.cpp | 71 +++++++++++++++++++++------------- src/Gui/DlgMacroExecuteImp.h | 1 + 4 files changed, 59 insertions(+), 30 deletions(-) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 56ef0a38b2..1eb6ff50f8 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -2252,6 +2252,7 @@ void parseProgramOptions(int ac, char ** av, const string& exe, variables_map& v ("run-test,t", value()->implicit_value(""),"Run a given test case (use 0 (zero) to run all tests). If no argument is provided then return list of all available tests.") ("run-open,r", value()->implicit_value(""),"Run a given test case (use 0 (zero) to run all tests). If no argument is provided then return list of all available tests. Keeps UI open after test(s) complete.") ("module-path,M", value< vector >()->composing(),"Additional module paths") + ("macro-path,E", value< vector >()->composing(),"Additional macro paths") ("python-path,P", value< vector >()->composing(),"Additional python paths") ("disable-addon", value< vector >()->composing(),"Disable a given addon.") ("single-instance", "Allow to run a single instance of the application") @@ -2294,7 +2295,7 @@ void parseProgramOptions(int ac, char ** av, const string& exe, variables_map& v #endif ; - + //0000723: improper handling of qt specific command line arguments std::vector args; bool merge=false; @@ -2429,6 +2430,15 @@ void processProgramOptions(const variables_map& vm, std::map Macros = vm["macro-path"].as< vector >(); + string temp; + for (const auto & It : Macros) + temp += It + ";"; + temp.erase(temp.end()-1); + mConfig["AdditionalMacroPaths"] = temp; + } + if (vm.count("python-path")) { vector Paths = vm["python-path"].as< vector >(); for (const auto & It : Paths) @@ -2593,7 +2603,7 @@ void Application::initConfig(int argc, char ** argv) // extract home paths ExtractUserPath(); - + if (vm.count("safe-mode")) { SafeMode::StartSafeMode(); } diff --git a/src/App/FreeCADInit.py b/src/App/FreeCADInit.py index b020f0fd31..f4ee025aef 100644 --- a/src/App/FreeCADInit.py +++ b/src/App/FreeCADInit.py @@ -98,7 +98,8 @@ def InitApplications(): LibFcDir = os.path.realpath(LibFcDir) if (os.path.exists(LibFcDir) and not LibFcDir in libpaths): libpaths.append(LibFcDir) - AddPath = FreeCAD.ConfigGet("AdditionalModulePaths").split(";") + AddPath = FreeCAD.ConfigGet("AdditionalModulePaths").split(";") + \ + FreeCAD.ConfigGet("AdditionalMacroPaths").split(";") HomeMod = FreeCAD.getUserAppDataDir()+"Mod" HomeMod = os.path.realpath(HomeMod) MacroStd = App.getUserMacroDir(False) diff --git a/src/Gui/DlgMacroExecuteImp.cpp b/src/Gui/DlgMacroExecuteImp.cpp index 1abe1feb82..c008dc0b72 100644 --- a/src/Gui/DlgMacroExecuteImp.cpp +++ b/src/Gui/DlgMacroExecuteImp.cpp @@ -60,14 +60,30 @@ namespace Dialog class MacroItem: public QTreeWidgetItem { public: - MacroItem(QTreeWidget* widget, bool systemwide) + MacroItem(QTreeWidget* widget, bool systemwide, const QString& dirPath) : QTreeWidgetItem(widget) , systemWide(systemwide) + , dirPath(dirPath) {} + /** + * Acts same as setText method but additionally set toolTip with text of + * absolute file path. There may be different macros with same names from + * different system paths. So it could be helpful for user to show where + * exactly macro is placed. + */ + void setFileName(int column, const QString& text) + { + QFileInfo file(dirPath, text); + + setToolTip(column, file.absoluteFilePath()); + return QTreeWidgetItem::setText(column, text); + } + ~MacroItem() override = default; bool systemWide; + QString dirPath; }; } // namespace Dialog } // namespace Gui @@ -229,23 +245,37 @@ QStringList DlgMacroExecuteImp::filterFiles(const QString& folder) * Fills up the list with macro files found in the specified location * that have been filtered by both filename and by content */ +void DlgMacroExecuteImp::fillUpListForDir(const QString& dirPath, bool systemWide) +{ + QStringList filteredByContent = this->filterFiles(dirPath); + ui->userMacroListBox->clear(); + for (auto& fn : filteredByContent) { + auto* parent = systemWide ? ui->systemMacroListBox : ui->userMacroListBox; + auto item = new MacroItem(parent, systemWide, dirPath); + item->setFileName(0, fn); + } +} + +/** + * Fills up the list with macro files found in all system paths and specified by + * user location that have been filtered by both filename and by content + */ void DlgMacroExecuteImp::fillUpList() { - QStringList filteredByContent = this->filterFiles(this->macroPath); - ui->userMacroListBox->clear(); - for (auto fn : filteredByContent) { - auto item = new MacroItem(ui->userMacroListBox, false); - item->setText(0, fn); - } + fillUpListForDir(this->macroPath, false); QString dirstr = QString::fromStdString(App::Application::getHomePath()) + QString::fromLatin1("Macro"); - filteredByContent = this->filterFiles(dirstr); + fillUpListForDir(dirstr, true); - ui->systemMacroListBox->clear(); - for (auto fn : filteredByContent) { - auto item = new MacroItem(ui->systemMacroListBox, true); - item->setText(0, fn); + auto& config = App::Application::Config(); + auto additionalMacros = config.find("AdditionalMacroPaths"); + if (additionalMacros != config.end()) { + QString dirsstrs = QString::fromStdString(additionalMacros->second); + QStringList dirs = dirsstrs.split(QChar::fromLatin1(';')); + for (const auto& dirstr : dirs) { + fillUpListForDir(dirstr, true); + } } } @@ -392,17 +422,7 @@ void DlgMacroExecuteImp::accept() auto mitem = static_cast(item); - QDir dir; - - if (!mitem->systemWide) { - dir = QDir(this->macroPath); - } - else { - QString dirstr = - QString::fromStdString(App::Application::getHomePath()) + QString::fromLatin1("Macro"); - dir = QDir(dirstr); - } - + QDir dir(mitem->dirPath); QFileInfo fi(dir, item->text(0)); try { getMainWindow()->setCursor(Qt::WaitCursor); @@ -444,19 +464,15 @@ void DlgMacroExecuteImp::onFileChooserFileNameChanged(const QString& fn) */ void DlgMacroExecuteImp::onEditButtonClicked() { - QDir dir; QTreeWidgetItem* item = nullptr; int index = ui->tabMacroWidget->currentIndex(); if (index == 0) { // user-specific item = ui->userMacroListBox->currentItem(); - dir.setPath(this->macroPath); } else { // index == 1 system-wide item = ui->systemMacroListBox->currentItem(); - dir.setPath(QString::fromStdString(App::Application::getHomePath()) - + QString::fromLatin1("Macro")); } if (!item) { @@ -464,6 +480,7 @@ void DlgMacroExecuteImp::onEditButtonClicked() } auto mitem = static_cast(item); + QDir dir(mitem->dirPath); QString file = QString::fromLatin1("%1/%2").arg(dir.absolutePath(), item->text(0)); auto editor = new PythonEditor(); diff --git a/src/Gui/DlgMacroExecuteImp.h b/src/Gui/DlgMacroExecuteImp.h index 9ad454f77b..3be7b4b0a4 100644 --- a/src/Gui/DlgMacroExecuteImp.h +++ b/src/Gui/DlgMacroExecuteImp.h @@ -69,6 +69,7 @@ private: protected: void fillUpList(); + void fillUpListForDir(const QString& dirPath, bool systemWide); QStringList filterFiles(const QString&); protected: