1
/***************************************************************************
2
* Copyright (c) 2004 J�rgen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
31
/// Here the FreeCAD includes sorted by Base,App,Gui......
34
#include <Base/Interpreter.h>
35
#include <Base/Console.h>
36
#include <Base/Exception.h>
37
#include <App/Application.h>
39
#include "DockWindowManager.h"
40
#include "MainWindow.h"
41
#include "PythonConsole.h"
46
MacroManager::MacroManager()
47
: openMacro(false), recordGui(true), guiAsComment(true),scriptToPyConsole(false),pyConsole(0)
49
// Attach to the Parametergroup regarding macros
50
this->params = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro");
51
this->params->Attach(this);
52
this->params->NotifyAll();
55
MacroManager::~MacroManager()
57
this->params->Detach(this);
60
void MacroManager::OnChange(Base::Subject<const char*> &rCaller, const char * sReason)
62
this->recordGui = this->params->GetBool("RecordGui", true);
63
this->guiAsComment = this->params->GetBool("GuiAsComment", true);
64
this->scriptToPyConsole = this->params->GetBool("ScriptToPyConsole", false);
67
void MacroManager::open(MacroType eType,const char *sName)
70
assert(!this->openMacro);
71
assert(eType == File);
74
this->macroName = QString::fromUtf8(sName);
75
if (!this->macroName.endsWith(QLatin1String(".FCMacro")))
76
this->macroName += QLatin1String(".FCMacro");
78
this->macroInProgress.clear();
79
this->openMacro = true;
81
Base::Console().Log("CmdM: Open macro: %s\n", sName);
84
void MacroManager::commit(void)
86
QFile file(this->macroName);
87
if (file.open(QFile::WriteOnly))
89
// sort import lines and avoid duplicates
90
QTextStream str(&file);
91
QStringList lines = this->macroInProgress.split(QLatin1Char('\n'));
92
QStringList import; import << QString::fromAscii("import FreeCAD\n");
95
QStringList::Iterator it;
96
for ( it = lines.begin(); it != lines.end(); ++it )
98
if ((*it).startsWith(QLatin1String("import ")) ||
99
(*it).startsWith(QLatin1String("#import ")))
101
if (import.indexOf(*it + QLatin1Char('\n')) == -1)
102
import.push_back(*it + QLatin1Char('\n'));
106
body.push_back(*it + QLatin1Char('\n'));
110
QString header = QString::fromAscii("# Macro Begin: ");
111
header += this->macroName;
112
header += QString::fromAscii(" +++++++++++++++++++++++++++++++++++++++++++++++++\n");
114
QString footer = QString::fromAscii("# Macro End: ");
115
footer += this->macroName;
116
footer += QString::fromAscii(" +++++++++++++++++++++++++++++++++++++++++++++++++\n");
118
// write the data to the text file
120
for ( it = import.begin(); it != import.end(); ++it )
122
str << QLatin1Char('\n');
123
for ( it = body.begin(); it != body.end(); ++it )
128
Base::Console().Log("CmdM: Commit macro: %s\n",(const char*)this->macroName.toUtf8());
130
this->macroInProgress.clear();
131
this->macroName.clear();
132
this->openMacro = false;
135
void MacroManager::cancel(void)
137
Base::Console().Log("CmdM: Cancel macro: %s\n",(const char*)this->macroName.toUtf8());
139
this->macroInProgress.clear();
140
this->macroName.clear();
141
this->openMacro = false;
144
void MacroManager::addLine(LineType Type, const char* sLine)
150
if (this->recordGui && this->guiAsComment)
151
this->macroInProgress += QLatin1Char('#');
152
else if (!this->recordGui)
153
return; // ignore Gui commands
156
this->macroInProgress += QString::fromAscii(sLine);
157
this->macroInProgress += QLatin1Char('\n');
160
if (this->scriptToPyConsole) {
161
// search for the Python console
162
if (!this->pyConsole)
163
this->pyConsole = Gui::getMainWindow()->findChild<Gui::PythonConsole*>();
164
// Python console found?
166
this->pyConsole->printStatement(QString::fromUtf8(sLine));
170
void MacroManager::setModule(const char* sModule)
172
if (this->openMacro && sModule && *sModule != '\0')
174
this->macroInProgress += QString::fromAscii("import ");
175
this->macroInProgress += QString::fromAscii(sModule);
176
this->macroInProgress += QLatin1Char('\n');
180
void MacroManager::run(MacroType eType,const char *sName)
184
//The given path name is expected to be Utf-8
185
Base::Interpreter().runFile(sName);
187
catch (const Base::Exception& e)
189
qWarning("%s",e.what());