View Issue Details

IDProjectCategoryView StatusLast Update
0003173FreeCADFeaturepublic2018-08-26 18:38
ReporterSam Assigned Towmayer  
PrioritynoneSeverityfeatureReproducibilityalways
Status closedResolutionfixed 
Product Version0.17 
Fixed in Version0.17 
Summary0003173: Allow renaming macros from the macro dialog
DescriptionForum thread: [Feature Request] Rename macro

It occurred to me that it'd save me some time, and probably a bunch of others, if you were able to rename macros from the macro dialog. A rename button, or F2 on the name.

So, I did some simple hacking to add a rename button.

The code to do it was pretty much already there and I just hacked it around into a new function.

While I was there I tidied up some white space and made the button enablement/clickability a bit saner.

This looks like old code: It looks a bit loose, and and the style is inconsistent. Some more things could be tidied up while I'm here if you like.

One thing I'd kind of like to add is a preference for the default option in the macro dialog. I'd prefer the default was edit, not execute; I prefer to look before I leap.

Also, I still haven't read the FreeCAD style guide, or patch submission guidelines, apart from the sticky at the top of the developers forum. If it pisses you off that I've gone off half cocked like this, I don't blame you! I'll get around to it OK?
Additional Information
OS: Ubuntu 16.04.3 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.11937 (Git)
Build type: Debug
Branch: master
Hash: 2751a3e8476c9b12d6982c7301bedd35a61fee4a
Python version: 2.7.12
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.1.0
Locale: English/UnitedKingdom (en_GB)
TagsNo tags attached.
FreeCAD Information

Activities

Sam

2017-08-30 22:22

reporter  

0001-Add-a-rename-option-to-the-macro-dialog.patch.txt (7,368 bytes)   
From d6240300ed1b77c9ed808ac8efa8b7625d7fe639 Mon Sep 17 00:00:00 2001
Message-Id: <d6240300ed1b77c9ed808ac8efa8b7625d7fe639.1504098250.git.aaron@pelly.co>
From: apelly <aaron@pelly.co>
Date: Thu, 31 Aug 2017 01:03:42 +1200
Subject: [PATCH] Add a rename option to the macro dialog

Also took out a touch of trailing white space.
---
 src/Gui/DlgMacroExecute.ui     | 13 ++++++++
 src/Gui/DlgMacroExecuteImp.cpp | 76 +++++++++++++++++++++++++++++++++++++++---
 src/Gui/DlgMacroExecuteImp.h   |  1 +
 3 files changed, 85 insertions(+), 5 deletions(-)

diff --git a/src/Gui/DlgMacroExecute.ui b/src/Gui/DlgMacroExecute.ui
index 1b6e526..319e916 100644
--- a/src/Gui/DlgMacroExecute.ui
+++ b/src/Gui/DlgMacroExecute.ui
@@ -246,12 +246,25 @@
        </item>
        <item>
         <widget class="QPushButton" name="editButton">
+         <property name="enabled">
+          <bool>false</bool>
+         </property>
          <property name="text">
           <string>Edit</string>
          </property>
         </widget>
        </item>
        <item>
+        <widget class="QPushButton" name="renameButton">
+         <property name="enabled">
+          <bool>false</bool>
+         </property>
+         <property name="text">
+          <string>Rename</string>
+         </property>
+        </widget>
+       </item>
+       <item>
         <spacer>
          <property name="orientation">
           <enum>Qt::Vertical</enum>
diff --git a/src/Gui/DlgMacroExecuteImp.cpp b/src/Gui/DlgMacroExecuteImp.cpp
index d512e90..d9de284 100644
--- a/src/Gui/DlgMacroExecuteImp.cpp
+++ b/src/Gui/DlgMacroExecuteImp.cpp
@@ -53,9 +53,9 @@ namespace Gui {
             MacroItem(QTreeWidget * widget, bool systemwide)
             : QTreeWidgetItem(widget),
             systemWide(systemwide){}
-            
+
             ~MacroItem(){}
-            
+
             bool systemWide;
         };
     }
@@ -65,8 +65,8 @@ namespace Gui {
 /* TRANSLATOR Gui::Dialog::DlgMacroExecuteImp */
 
 /**
- *  Constructs a DlgMacroExecuteImp which is a child of 'parent', with the 
- *  name 'name' and widget flags set to 'f' 
+ *  Constructs a DlgMacroExecuteImp which is a child of 'parent', with the
+ *  name 'name' and widget flags set to 'f'
  *
  *  The dialog will by default be modeless, unless you set 'modal' to
  *  true to construct a modal dialog.
@@ -90,7 +90,7 @@ DlgMacroExecuteImp::DlgMacroExecuteImp( QWidget* parent, Qt::WindowFlags fl )
     fillUpList();
 }
 
-/** 
+/**
  *  Destroys the object and frees any allocated resources
  */
 DlgMacroExecuteImp::~DlgMacroExecuteImp()
@@ -136,11 +136,15 @@ void DlgMacroExecuteImp::on_userMacroListBox_currentItemChanged(QTreeWidgetItem*
         executeButton->setEnabled(true);
         deleteButton->setEnabled(true);
         createButton->setEnabled(true);
+        editButton->setEnabled(true);
+        renameButton->setEnabled(true);
     }
     else {
         executeButton->setEnabled(false);
         deleteButton->setEnabled(false);
         createButton->setEnabled(true);
+        editButton->setEnabled(false);
+        renameButton->setEnabled(false);
     }
 }
 
@@ -152,11 +156,15 @@ void DlgMacroExecuteImp::on_systemMacroListBox_currentItemChanged(QTreeWidgetIte
         executeButton->setEnabled(true);
         deleteButton->setEnabled(false);
         createButton->setEnabled(false);
+        editButton->setEnabled(true); //look but don't touch
+        renameButton->setEnabled(false);
     }
     else {
         executeButton->setEnabled(false);
         deleteButton->setEnabled(false);
         createButton->setEnabled(false);
+        editButton->setEnabled(false);
+        renameButton->setEnabled(false);
     }
 }
 
@@ -170,11 +178,15 @@ void DlgMacroExecuteImp::on_tabMacroWidget_currentChanged(int index)
             executeButton->setEnabled(true);
             deleteButton->setEnabled(true);
             createButton->setEnabled(true);
+            editButton->setEnabled(true);
+            renameButton->setEnabled(true);
         }
         else {
             executeButton->setEnabled(false);
             deleteButton->setEnabled(false);
             createButton->setEnabled(true);
+            editButton->setEnabled(false);
+            renameButton->setEnabled(false);
         }
     }
     else { //index==1 system-wide
@@ -184,11 +196,15 @@ void DlgMacroExecuteImp::on_tabMacroWidget_currentChanged(int index)
             executeButton->setEnabled(true);
             deleteButton->setEnabled(false);
             createButton->setEnabled(false);
+            editButton->setEnabled(true); //but you can't save it
+            renameButton->setEnabled(false);
         }
         else {
             executeButton->setEnabled(false);
             deleteButton->setEnabled(false);
             createButton->setEnabled(false);
+            editButton->setEnabled(false);
+            renameButton->setEnabled(false);
         }
     }
 
@@ -376,4 +392,54 @@ void DlgMacroExecuteImp::on_deleteButton_clicked()
     }
 }
 
+/**
+ * renames the selected macro
+ */
+void DlgMacroExecuteImp::on_renameButton_clicked()
+{
+    QDir dir;
+    QTreeWidgetItem* item = 0;
+
+    int index = tabMacroWidget->currentIndex();
+    if (index == 0) { //user-specific
+        item = userMacroListBox->currentItem();
+        dir.setPath(this->macroPath);
+    }
+
+    if (!item)
+        return;
+
+    QString oldName = item->text(0);
+    QFileInfo oldfi(dir, oldName);
+    QFile oldfile(oldfi.absoluteFilePath());
+    if (!oldfile.open(QFile::ReadWrite)) {
+        QMessageBox::warning(this, tr("System reports read-only file"),
+            tr("Can not rename '%1'.").arg(oldfi.absoluteFilePath()));
+        return;
+    }
+
+    // query new name
+    QString fn = QInputDialog::getText(this, tr("Renaming Macro File"),
+        tr("Enter new name:"), QLineEdit::Normal, oldName, 0);
+    if (!fn.isEmpty()) {
+        QString suffix = QFileInfo(fn).suffix().toLower();
+        if (suffix != QLatin1String("fcmacro") && suffix != QLatin1String("py"))
+            fn += QLatin1String(".FCMacro");
+        QFileInfo fi(dir, fn);
+        // check if new name exists
+        if (fi.exists()) {
+            QMessageBox::warning(this, tr("Existing file"),
+                tr("'%1'\n already exists.").arg(fi.absoluteFilePath()));
+        } else {
+            QFile file(fi.absoluteFilePath());
+            if (!oldfile.rename(fi.absoluteFilePath())) {
+                QMessageBox::warning(this, tr("Rename Failed"),
+                    tr("Failed to rename to '%1'.\nPerhaps a file permission error?").arg(fi.absoluteFilePath()));
+                return;
+            }
+        }
+        fillUpList();
+    }
+}
+
 #include "moc_DlgMacroExecuteImp.cpp"
diff --git a/src/Gui/DlgMacroExecuteImp.h b/src/Gui/DlgMacroExecuteImp.h
index 4ec5b1e..3090fc1 100644
--- a/src/Gui/DlgMacroExecuteImp.h
+++ b/src/Gui/DlgMacroExecuteImp.h
@@ -50,6 +50,7 @@ public Q_SLOTS:
     void on_createButton_clicked();
     void on_deleteButton_clicked();
     void on_editButton_clicked();
+    void on_renameButton_clicked();
 
 protected Q_SLOTS:
     void on_userMacroListBox_currentItemChanged(QTreeWidgetItem*);
-- 
2.7.4

Kunda1

2017-09-07 00:05

administrator   ~0010077

@Sam made a PR for you: FreeCAD Pull Request 973
Thanks for your contribution ;)

wmayer

2017-09-07 08:54

administrator   ~0010079

https://github.com/FreeCAD/FreeCAD/commit/21e1a5fde5de01c4493ae14206e0752bf76bac3b

Issue History

Date Modified Username Field Change
2017-08-30 22:22 Sam New Issue
2017-08-30 22:22 Sam File Added: 0001-Add-a-rename-option-to-the-macro-dialog.patch.txt
2017-08-30 22:22 Sam Tag Attached: #lowhangingfruit
2017-09-07 00:05 Kunda1 Note Added: 0010077
2017-09-07 08:54 wmayer Assigned To => wmayer
2017-09-07 08:54 wmayer Status new => closed
2017-09-07 08:54 wmayer Resolution open => fixed
2017-09-07 08:54 wmayer Fixed in Version => 0.17
2017-09-07 08:54 wmayer Note Added: 0010079
2018-08-26 18:38 Kunda1 Tag Detached: #lowhangingfruit