Merge pull request #5040 from mwganson/macros

[Macros Dialog] Add 2 new parameters: DuplicateIgnoreExtraNote and Du…
This commit is contained in:
Yorik van Havre
2021-09-20 10:43:00 +02:00
committed by GitHub

View File

@@ -350,8 +350,16 @@ void DlgMacroExecuteImp::on_editButton_clicked()
void DlgMacroExecuteImp::on_createButton_clicked()
{
// query file name
bool replaceSpaces = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")->GetBool("ReplaceSpaces", true);
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")->SetBool("ReplaceSpaces", replaceSpaces); //create parameter
QString fn = QInputDialog::getText(this, tr("Macro file"), tr("Enter a file name, please:"),
QLineEdit::Normal, QString(), nullptr, Qt::MSWindowsFixedSizeDialogHint);
if(replaceSpaces){
fn = fn.replace(QString::fromStdString(" "),QString::fromStdString("_"));
}
if (!fn.isEmpty())
{
QString suffix = QFileInfo(fn).suffix().toLower();
@@ -674,6 +682,9 @@ void DlgMacroExecuteImp::on_renameButton_clicked()
if (!item)
return;
bool replaceSpaces = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")->GetBool("ReplaceSpaces", true);
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")->SetBool("ReplaceSpaces", replaceSpaces); //create parameter
QString oldName = item->text(0);
QFileInfo oldfi(dir, oldName);
QFile oldfile(oldfi.absoluteFilePath());
@@ -681,6 +692,11 @@ void DlgMacroExecuteImp::on_renameButton_clicked()
// query new name
QString fn = QInputDialog::getText(this, tr("Renaming Macro File"),
tr("Enter new name:"), QLineEdit::Normal, oldName, nullptr, Qt::MSWindowsFixedSizeDialogHint);
if(replaceSpaces){
fn = fn.replace(QString::fromStdString(" "),QString::fromStdString("_"));
}
if (!fn.isEmpty() && fn != oldName) {
QString suffix = QFileInfo(fn).suffix().toLower();
if (suffix != QLatin1String("fcmacro") && suffix != QLatin1String("py"))
@@ -718,6 +734,20 @@ void DlgMacroExecuteImp::on_duplicateButton_clicked()
bool from001 = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")->GetBool("DuplicateFrom001", false);
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")->SetBool("DuplicateFrom001", from001); //create parameter
//A user may wish to add a note to end of the filename when duplicating
//example: mymacro@005.fix_bug_in_dialog.FCMacro
//and then when duplicating to have the extra note removed so the suggested new name is:
//mymacro@006.FCMacro instead of mymacro@006.fix_bug_in_dialog.FCMacro since the new duplicate will be given a new note
bool ignoreExtra = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")->GetBool("DuplicateIgnoreExtraNote", false);
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")->SetBool("DuplicateIgnoreExtraNote", ignoreExtra); //create parameter
//when creating a note it will be convenient to convert spaces to underscores if the user desires this behavior
bool replaceSpaces = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")->GetBool("ReplaceSpaces", true);
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")->SetBool("ReplaceSpaces", replaceSpaces); //create parameter
int index = ui->tabMacroWidget->currentIndex();
if (index == 0) { //user-specific
item = ui->userMacroListBox->currentItem();
@@ -732,6 +762,7 @@ void DlgMacroExecuteImp::on_duplicateButton_clicked()
QFileInfo oldfi(dir, oldName);
QFile oldfile(oldfi.absoluteFilePath());
QString completeSuffix = oldfi.completeSuffix(); //everything after the first "."
QString extraNote = completeSuffix.left(completeSuffix.size()-oldfi.suffix().size());
QString baseName = oldfi.baseName(); //everything before first "."
QString neutralSymbol = QString::fromStdString("@");
QString last3 = baseName.right(3);
@@ -752,14 +783,28 @@ void DlgMacroExecuteImp::on_duplicateButton_clicked()
}
//at this point baseName = the base name without any digits, e.g. "MyMacro"
//neutralSymbol = "@"
//last3 is a string representing 3 digits, always "001" at this time
//last3 is a string representing 3 digits, always "001"
//unless from001 = false, in which case we begin with previous numbers
//completeSuffix = FCMacro or py or FCMacro.py or else suffix will become FCMacro below
//if ignoreExtra any extra notes added between @NN. and .FCMacro will be ignored
//when suggesting a new filename
if(ignoreExtra && !extraNote.isEmpty()){
nLast3++;
last3 = QString::number(nLast3);
while (last3.size()<3){
last3.prepend(QString::fromStdString("0")); //pad 0's if needed
}
}
QString oldNameDigitized = baseName+neutralSymbol+last3+QString::fromStdString(".")+completeSuffix;
QFileInfo fi(dir, oldNameDigitized);
// increment until we find available name with smallest digits
// test from "001" through "999", then give up and let user enter name of choice
while (fi.exists()) {
nLast3 = last3.toInt()+1;
if (nLast3 >=1000){ //avoid infinite loop, 999 files will have to be enough
@@ -773,10 +818,17 @@ void DlgMacroExecuteImp::on_duplicateButton_clicked()
fi = QFileInfo(dir,oldNameDigitized);
}
if(ignoreExtra && !extraNote.isEmpty()){
oldNameDigitized = oldNameDigitized.remove(extraNote);
}
// give user a chance to pick a different name from digitized name suggested
QString fn = QInputDialog::getText(this, tr("Duplicate Macro"),
tr("Enter new name:"), QLineEdit::Normal, oldNameDigitized,
nullptr, Qt::MSWindowsFixedSizeDialogHint);
if (replaceSpaces){
fn = fn.replace(QString::fromStdString(" "),QString::fromStdString("_"));
}
if (!fn.isEmpty() && fn != oldName) {
QString suffix = QFileInfo(fn).suffix().toLower();
if (suffix != QLatin1String("fcmacro") && suffix != QLatin1String("py")){