MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / onCreateButtonClicked

Method onCreateButtonClicked

src/Gui/Dialogs/DlgMacroExecuteImp.cpp:486–550  ·  view source on GitHub ↗

Creates a new macro file. */

Source from the content-addressed store, hash-verified

484
485/** Creates a new macro file. */
486void DlgMacroExecuteImp::onCreateButtonClicked()
487{
488 // query file name
489 bool replaceSpaces = App::GetApplication()
490 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")
491 ->GetBool("ReplaceSpaces", true);
492 App::GetApplication()
493 .GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")
494 ->SetBool("ReplaceSpaces", replaceSpaces); // create parameter
495
496 QString fn = QInputDialog::getText(
497 this,
498 tr("Macro file"),
499 tr("Enter a file name:"),
500 QLineEdit::Normal,
501 QString(),
502 nullptr,
503 Qt::MSWindowsFixedSizeDialogHint
504 );
505
506 if (replaceSpaces) {
507 fn = fn.replace(QStringLiteral(" "), QStringLiteral("_"));
508 }
509
510 if (!fn.isEmpty()) {
511 QString suffix = QFileInfo(fn).suffix().toLower();
512 if (suffix != QLatin1String("fcmacro") && suffix != QLatin1String("py")) {
513 fn += QLatin1String(".FCMacro");
514 }
515 QDir dir(this->macroPath);
516 // create the macroPath if nonexistent
517 if (!dir.exists()) {
518 dir.mkpath(this->macroPath);
519 }
520 QFileInfo fi(dir, fn);
521 if (fi.exists() && fi.isFile()) {
522 QMessageBox::warning(
523 this,
524 tr("Existing file"),
525 tr("'%1'.\nThis file already exists.").arg(fi.fileName())
526 );
527 }
528 else {
529 QFile file(fi.absoluteFilePath());
530 if (!file.open(QFile::WriteOnly)) {
531 QMessageBox::warning(
532 this,
533 tr("Cannot create file"),
534 tr("Creation of file '%1' failed.").arg(fi.absoluteFilePath())
535 );
536 return;
537 }
538 file.close();
539 auto editor = new PythonEditor();
540 editor->setWindowIcon(Gui::BitmapFactory().iconFromTheme("applications-python"));
541 auto edit = new PythonEditorView(editor, getMainWindow());
542 edit->open(fi.absoluteFilePath());
543 getMainWindow()->appendRecentMacro(fi.absoluteFilePath());

Callers

nothing calls this directly

Calls 15

warningFunction · 0.85
closeFunction · 0.85
GetBoolMethod · 0.80
SetBoolMethod · 0.80
isFileMethod · 0.80
iconFromThemeMethod · 0.80
appendRecentMacroMethod · 0.80
getTextFunction · 0.50
QStringClass · 0.50
QFileInfoClass · 0.50
getMainWindowFunction · 0.50

Tested by

no test coverage detected