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
This commit is contained in:
Yury Shvedov
2024-12-15 15:30:39 +03:00
committed by Yorik van Havre
parent 91d7c875a5
commit bc4cddcd04
4 changed files with 59 additions and 30 deletions

View File

@@ -2252,6 +2252,7 @@ void parseProgramOptions(int ac, char ** av, const string& exe, variables_map& v
("run-test,t", value<string>()->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<string>()->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<string> >()->composing(),"Additional module paths")
("macro-path,E", value< vector<string> >()->composing(),"Additional macro paths")
("python-path,P", value< vector<string> >()->composing(),"Additional python paths")
("disable-addon", value< vector<string> >()->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<std::string> args;
bool merge=false;
@@ -2429,6 +2430,15 @@ void processProgramOptions(const variables_map& vm, std::map<std::string,std::st
mConfig["AdditionalModulePaths"] = temp;
}
if (vm.count("macro-path")) {
vector<string> Macros = vm["macro-path"].as< vector<string> >();
string temp;
for (const auto & It : Macros)
temp += It + ";";
temp.erase(temp.end()-1);
mConfig["AdditionalMacroPaths"] = temp;
}
if (vm.count("python-path")) {
vector<string> Paths = vm["python-path"].as< vector<string> >();
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();
}

View File

@@ -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)

View File

@@ -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<MacroItem*>(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<MacroItem*>(item);
QDir dir(mitem->dirPath);
QString file = QString::fromLatin1("%1/%2").arg(dir.absolutePath(), item->text(0));
auto editor = new PythonEditor();

View File

@@ -69,6 +69,7 @@ private:
protected:
void fillUpList();
void fillUpListForDir(const QString& dirPath, bool systemWide);
QStringList filterFiles(const QString&);
protected: