Add a rename option to the macro dialog

Forum thread: https://forum.freecadweb.org/viewtopic.php?f=8&t=24005
Fixes #3173
This commit is contained in:
apelly
2017-08-31 01:03:42 +12:00
committed by wmayer
parent d0c5865f32
commit 21e1a5fde5
3 changed files with 85 additions and 5 deletions

View File

@@ -246,11 +246,24 @@
</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">

View File

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

View File

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