Fix a couple of issues with macro recording

This commit is contained in:
wmayer 2012-12-13 15:02:15 +01:00
parent e6d84af12d
commit 23afef0b78
6 changed files with 71 additions and 54 deletions

View File

@ -727,31 +727,35 @@ void Application::setActiveDocument(Gui::Document* pcDocument)
return; return;
} }
d->activeDocument = pcDocument; d->activeDocument = pcDocument;
std::string name; std::string nameApp, nameGui;
// This adds just a line to the macro file but does not set the active document // This adds just a line to the macro file but does not set the active document
// Macro recording of this is problematic, thus it's written out as comment.
if (pcDocument){ if (pcDocument){
name += "App.setActiveDocument(\""; nameApp += "App.setActiveDocument(\"";
name += pcDocument->getDocument()->getName(); nameApp += pcDocument->getDocument()->getName();
name += "\")\n"; nameApp += "\")\n";
name += "App.ActiveDocument=App.getDocument(\""; nameApp += "App.ActiveDocument=App.getDocument(\"";
name += pcDocument->getDocument()->getName(); nameApp += pcDocument->getDocument()->getName();
name += "\")\n"; nameApp += "\")";
name += "Gui.ActiveDocument=Gui.getDocument(\""; macroManager()->addLine(MacroManager::Cmt,nameApp.c_str());
name += pcDocument->getDocument()->getName(); nameGui += "Gui.ActiveDocument=Gui.getDocument(\"";
name += "\")"; nameGui += pcDocument->getDocument()->getName();
macroManager()->addLine(MacroManager::Gui,name.c_str()); nameGui += "\")";
macroManager()->addLine(MacroManager::Cmt,nameGui.c_str());
} }
else { else {
name += "App.setActiveDocument(\"\")\n"; nameApp += "App.setActiveDocument(\"\")\n";
name += "App.ActiveDocument=None\n"; nameApp += "App.ActiveDocument=None";
name += "Gui.ActiveDocument=None"; macroManager()->addLine(MacroManager::Cmt,nameApp.c_str());
macroManager()->addLine(MacroManager::Gui,name.c_str()); nameGui += "Gui.ActiveDocument=None";
macroManager()->addLine(MacroManager::Cmt,nameGui.c_str());
} }
// Sets the currently active document // Sets the currently active document
try { try {
Base::Interpreter().runString(name.c_str()); Base::Interpreter().runString(nameApp.c_str());
Base::Interpreter().runString(nameGui.c_str());
} }
catch (const Base::Exception& e) { catch (const Base::Exception& e) {
Base::Console().Warning(e.what()); Base::Console().Warning(e.what());
@ -1267,7 +1271,7 @@ void Application::runCommand(bool bForce, const char* sCmd,...)
va_end(namelessVars); va_end(namelessVars);
if (bForce) if (bForce)
d->macroMngr->addLine(MacroManager::Base,format); d->macroMngr->addLine(MacroManager::App,format);
else else
d->macroMngr->addLine(MacroManager::Gui,format); d->macroMngr->addLine(MacroManager::Gui,format);
@ -1288,7 +1292,7 @@ bool Application::runPythonCode(const char* cmd, bool gui, bool pyexc)
if (gui) if (gui)
d->macroMngr->addLine(MacroManager::Gui,cmd); d->macroMngr->addLine(MacroManager::Gui,cmd);
else else
d->macroMngr->addLine(MacroManager::Base,cmd); d->macroMngr->addLine(MacroManager::App,cmd);
try { try {
Base::Interpreter().runString(cmd); Base::Interpreter().runString(cmd);

View File

@ -76,7 +76,7 @@ bool Assistant::startAssistant()
if (proc->state() != QProcess::Running) { if (proc->state() != QProcess::Running) {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QString app; QString app;
app = QDir::convertSeparators(QString::fromUtf8 app = QDir::toNativeSeparators(QString::fromUtf8
(App::GetApplication().GetHomePath()) + QLatin1String("bin/")); (App::GetApplication().GetHomePath()) + QLatin1String("bin/"));
#else #else
QString app = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QDir::separator(); QString app = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QDir::separator();

View File

@ -416,7 +416,7 @@ void Command::doCommand(DoCmd_Type eType,const char* sCmd,...)
if (eType == Gui) if (eType == Gui)
Gui::Application::Instance->macroManager()->addLine(MacroManager::Gui,format); Gui::Application::Instance->macroManager()->addLine(MacroManager::Gui,format);
else else
Gui::Application::Instance->macroManager()->addLine(MacroManager::Base,format); Gui::Application::Instance->macroManager()->addLine(MacroManager::App,format);
try { try {
Base::Interpreter().runString(format); Base::Interpreter().runString(format);
@ -439,7 +439,7 @@ void Command::runCommand(DoCmd_Type eType,const char* sCmd)
if (eType == Gui) if (eType == Gui)
Gui::Application::Instance->macroManager()->addLine(MacroManager::Gui,sCmd); Gui::Application::Instance->macroManager()->addLine(MacroManager::Gui,sCmd);
else else
Gui::Application::Instance->macroManager()->addLine(MacroManager::Base,sCmd); Gui::Application::Instance->macroManager()->addLine(MacroManager::App,sCmd);
Base::Interpreter().runString(sCmd); Base::Interpreter().runString(sCmd);
} }
@ -476,7 +476,7 @@ const std::string Command::strToPython(const char* Str)
void Command::updateActive(void) void Command::updateActive(void)
{ {
WaitCursor wc; WaitCursor wc;
doCommand(Gui,"App.ActiveDocument.recompute()"); doCommand(App,"App.ActiveDocument.recompute()");
} }
bool Command::isActiveObjectValid(void) bool Command::isActiveObjectValid(void)

View File

@ -25,6 +25,8 @@
#ifndef _PreComp_ #ifndef _PreComp_
# include <QMessageBox> # include <QMessageBox>
# include <QDir> # include <QDir>
# include <QFile>
# include <QFileInfo>
#endif #endif
#include "Macro.h" #include "Macro.h"
@ -53,9 +55,7 @@ DlgMacroRecordImp::DlgMacroRecordImp( QWidget* parent, Qt::WFlags fl )
// get the macro home path // get the macro home path
this->macroPath = QString::fromUtf8(getWindowParameter()->GetASCII("MacroPath", this->macroPath = QString::fromUtf8(getWindowParameter()->GetASCII("MacroPath",
App::Application::getUserAppDataDir().c_str()).c_str()); App::Application::getUserAppDataDir().c_str()).c_str());
// check on PATHSEP at the end this->macroPath = QDir::toNativeSeparators(QDir(this->macroPath).path() + QDir::separator());
if (this->macroPath.at(this->macroPath.length()-1) != QLatin1Char(PATHSEP))
this->macroPath += QLatin1Char(PATHSEP);
// set the edit fields // set the edit fields
this->lineEditMacroPath->setText(macroPath); this->lineEditMacroPath->setText(macroPath);
@ -96,10 +96,9 @@ void DlgMacroRecordImp::on_buttonStart_clicked()
// search in the macro path first for an already existing macro // search in the macro path first for an already existing macro
QString fn = this->macroPath + lineEditPath->text(); QString fn = this->macroPath + lineEditPath->text();
if (!fn.endsWith(QLatin1String(".FCMacro")) ) fn += QLatin1String(".FCMacro"); if (!fn.endsWith(QLatin1String(".FCMacro"))) fn += QLatin1String(".FCMacro");
QFileInfo fi(fn); QFileInfo fi(fn);
if ( fi.isFile() && fi.exists() ) if (fi.isFile() && fi.exists()) {
{
if (QMessageBox::question(this, tr("Existing macro"), if (QMessageBox::question(this, tr("Existing macro"),
tr("The macro '%1' already exists. Do you want to overwrite?").arg(fn), tr("The macro '%1' already exists. Do you want to overwrite?").arg(fn),
QMessageBox::Yes, QMessageBox::Yes,
@ -109,6 +108,14 @@ void DlgMacroRecordImp::on_buttonStart_clicked()
return; return;
} }
QFile file(fn);
if (!file.open(QFile::WriteOnly)) {
QMessageBox::information(getMainWindow(), tr("Macro recorder"),
tr("You have no write permission for the directory. Please, choose another one."));
return;
}
file.close();
// open the macro recording // open the macro recording
this->macroManager->open(MacroManager::File, fn.toUtf8().constData()); this->macroManager->open(MacroManager::File, fn.toUtf8().constData());
accept(); accept();
@ -143,7 +150,7 @@ void DlgMacroRecordImp::on_pushButtonChooseDir_clicked()
{ {
QString newDir = QFileDialog::getExistingDirectory(0,tr("Choose macro directory"),macroPath); QString newDir = QFileDialog::getExistingDirectory(0,tr("Choose macro directory"),macroPath);
if (!newDir.isEmpty()) { if (!newDir.isEmpty()) {
macroPath = QDir::convertSeparators(newDir + QDir::separator()); macroPath = QDir::toNativeSeparators(newDir + QDir::separator());
this->lineEditMacroPath->setText(macroPath); this->lineEditMacroPath->setText(macroPath);
getWindowParameter()->SetASCII("MacroPath",macroPath.toUtf8()); getWindowParameter()->SetASCII("MacroPath",macroPath.toUtf8());
} }

View File

@ -99,22 +99,22 @@ void MacroManager::commit(void)
{ {
// sort import lines and avoid duplicates // sort import lines and avoid duplicates
QTextStream str(&file); QTextStream str(&file);
QStringList lines = this->macroInProgress.split(QLatin1Char('\n')); QStringList import;
QStringList import; import << QString::fromAscii("import FreeCAD\n"); import << QString::fromAscii("import FreeCAD");
QStringList body; QStringList body;
QStringList::Iterator it; QStringList::Iterator it;
for ( it = lines.begin(); it != lines.end(); ++it ) for (it = this->macroInProgress.begin(); it != this->macroInProgress.end(); ++it )
{ {
if ((*it).startsWith(QLatin1String("import ")) || if ((*it).startsWith(QLatin1String("import ")) ||
(*it).startsWith(QLatin1String("#import "))) (*it).startsWith(QLatin1String("#import ")))
{ {
if (import.indexOf(*it + QLatin1Char('\n')) == -1) if (import.indexOf(*it) == -1)
import.push_back(*it + QLatin1Char('\n')); import.push_back(*it);
} }
else else
{ {
body.push_back(*it + QLatin1Char('\n')); body.push_back(*it);
} }
} }
@ -128,11 +128,11 @@ void MacroManager::commit(void)
// write the data to the text file // write the data to the text file
str << header; str << header;
for ( it = import.begin(); it != import.end(); ++it ) for (it = import.begin(); it != import.end(); ++it)
str << (*it); str << (*it) << QLatin1Char('\n');
str << QLatin1Char('\n'); str << QLatin1Char('\n');
for ( it = body.begin(); it != body.end(); ++it ) for (it = body.begin(); it != body.end(); ++it)
str << (*it); str << (*it) << QLatin1Char('\n');
str << footer; str << footer;
Base::Console().Log("Commit macro: %s\n",(const char*)this->macroName.toUtf8()); Base::Console().Log("Commit macro: %s\n",(const char*)this->macroName.toUtf8());
@ -159,18 +159,24 @@ void MacroManager::cancel(void)
void MacroManager::addLine(LineType Type, const char* sLine) void MacroManager::addLine(LineType Type, const char* sLine)
{ {
if (this->openMacro) if (this->openMacro) {
{ bool comment = false;
if(Type == Gui) if (Type == Gui) {
{
if (this->recordGui && this->guiAsComment) if (this->recordGui && this->guiAsComment)
this->macroInProgress += QLatin1Char('#'); comment = true;
else if (!this->recordGui) else if (!this->recordGui)
return; // ignore Gui commands return; // ignore Gui commands
} }
else if (Type == Cmt) {
comment = true;
}
this->macroInProgress += QString::fromAscii(sLine); QStringList lines = QString::fromAscii(sLine).split(QLatin1String("\n"));
this->macroInProgress += QLatin1Char('\n'); if (comment) {
for (QStringList::iterator it = lines.begin(); it != lines.end(); ++it)
it->prepend(QLatin1String("#"));
}
this->macroInProgress.append(lines);
} }
if (this->scriptToPyConsole) { if (this->scriptToPyConsole) {
@ -187,9 +193,7 @@ void MacroManager::setModule(const char* sModule)
{ {
if (this->openMacro && sModule && *sModule != '\0') if (this->openMacro && sModule && *sModule != '\0')
{ {
this->macroInProgress += QString::fromAscii("import "); this->macroInProgress.append(QString::fromAscii("import %1").arg(QString::fromAscii(sModule)));
this->macroInProgress += QString::fromAscii(sModule);
this->macroInProgress += QLatin1Char('\n');
} }
} }

View File

@ -26,6 +26,7 @@
// Std. configurations // Std. configurations
#include <QString> #include <QString>
#include <QStringList>
#include <Base/Observer.h> #include <Base/Observer.h>
#include <Base/Parameter.h> #include <Base/Parameter.h>
@ -51,14 +52,15 @@ public:
/** Macro type enumeration */ /** Macro type enumeration */
enum MacroType { enum MacroType {
File, /**< The macro will be saved in a file */ File, /**< The macro will be saved in a file */
App, /**< The macro belongs to the Application and will be saved in the UserParameter */ User, /**< The macro belongs to the Application and will be saved in the UserParameter */
Doc /**< Teh macro belongs to the Document and will be saved and restored with the Document */ Doc /**< The macro belongs to the Document and will be saved and restored with the Document */
}; };
/** Line type enumeration */ /** Line type enumeration */
enum LineType { enum LineType {
Base, /**< The line effects only the document and Application (FreeCAD) */ App, /**< The line effects only the document and Application (FreeCAD) */
Gui, /**< The line effects the Gui (FreeCADGui) */ Gui, /**< The line effects the Gui (FreeCADGui) */
Cmt /**< The line is handled as a comment */
}; };
/** Opens a new Macro recording session /** Opens a new Macro recording session
@ -92,7 +94,7 @@ public:
void OnChange(Base::Subject<const char*> &rCaller, const char * sReason); void OnChange(Base::Subject<const char*> &rCaller, const char * sReason);
protected: protected:
QString macroInProgress; /**< Container for the macro */ QStringList macroInProgress; /**< Container for the macro */
QString macroName; /**< name of the macro */ QString macroName; /**< name of the macro */
bool openMacro; bool openMacro;
bool recordGui; bool recordGui;