Commit 23afef0b authored by wmayer's avatar wmayer
Browse files

Fix a couple of issues with macro recording

parent e6d84af1
......@@ -727,31 +727,35 @@ void Application::setActiveDocument(Gui::Document* pcDocument)
return;
}
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
// Macro recording of this is problematic, thus it's written out as comment.
if (pcDocument){
name += "App.setActiveDocument(\"";
name += pcDocument->getDocument()->getName();
name += "\")\n";
name += "App.ActiveDocument=App.getDocument(\"";
name += pcDocument->getDocument()->getName();
name += "\")\n";
name += "Gui.ActiveDocument=Gui.getDocument(\"";
name += pcDocument->getDocument()->getName();
name += "\")";
macroManager()->addLine(MacroManager::Gui,name.c_str());
nameApp += "App.setActiveDocument(\"";
nameApp += pcDocument->getDocument()->getName();
nameApp += "\")\n";
nameApp += "App.ActiveDocument=App.getDocument(\"";
nameApp += pcDocument->getDocument()->getName();
nameApp += "\")";
macroManager()->addLine(MacroManager::Cmt,nameApp.c_str());
nameGui += "Gui.ActiveDocument=Gui.getDocument(\"";
nameGui += pcDocument->getDocument()->getName();
nameGui += "\")";
macroManager()->addLine(MacroManager::Cmt,nameGui.c_str());
}
else {
name += "App.setActiveDocument(\"\")\n";
name += "App.ActiveDocument=None\n";
name += "Gui.ActiveDocument=None";
macroManager()->addLine(MacroManager::Gui,name.c_str());
nameApp += "App.setActiveDocument(\"\")\n";
nameApp += "App.ActiveDocument=None";
macroManager()->addLine(MacroManager::Cmt,nameApp.c_str());
nameGui += "Gui.ActiveDocument=None";
macroManager()->addLine(MacroManager::Cmt,nameGui.c_str());
}
// Sets the currently active document
try {
Base::Interpreter().runString(name.c_str());
Base::Interpreter().runString(nameApp.c_str());
Base::Interpreter().runString(nameGui.c_str());
}
catch (const Base::Exception& e) {
Base::Console().Warning(e.what());
......@@ -1267,7 +1271,7 @@ void Application::runCommand(bool bForce, const char* sCmd,...)
va_end(namelessVars);
if (bForce)
d->macroMngr->addLine(MacroManager::Base,format);
d->macroMngr->addLine(MacroManager::App,format);
else
d->macroMngr->addLine(MacroManager::Gui,format);
......@@ -1288,7 +1292,7 @@ bool Application::runPythonCode(const char* cmd, bool gui, bool pyexc)
if (gui)
d->macroMngr->addLine(MacroManager::Gui,cmd);
else
d->macroMngr->addLine(MacroManager::Base,cmd);
d->macroMngr->addLine(MacroManager::App,cmd);
try {
Base::Interpreter().runString(cmd);
......
......@@ -76,7 +76,7 @@ bool Assistant::startAssistant()
if (proc->state() != QProcess::Running) {
#ifdef Q_OS_WIN
QString app;
app = QDir::convertSeparators(QString::fromUtf8
app = QDir::toNativeSeparators(QString::fromUtf8
(App::GetApplication().GetHomePath()) + QLatin1String("bin/"));
#else
QString app = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QDir::separator();
......
......@@ -416,7 +416,7 @@ void Command::doCommand(DoCmd_Type eType,const char* sCmd,...)
if (eType == Gui)
Gui::Application::Instance->macroManager()->addLine(MacroManager::Gui,format);
else
Gui::Application::Instance->macroManager()->addLine(MacroManager::Base,format);
Gui::Application::Instance->macroManager()->addLine(MacroManager::App,format);
try {
Base::Interpreter().runString(format);
......@@ -439,7 +439,7 @@ void Command::runCommand(DoCmd_Type eType,const char* sCmd)
if (eType == Gui)
Gui::Application::Instance->macroManager()->addLine(MacroManager::Gui,sCmd);
else
Gui::Application::Instance->macroManager()->addLine(MacroManager::Base,sCmd);
Gui::Application::Instance->macroManager()->addLine(MacroManager::App,sCmd);
Base::Interpreter().runString(sCmd);
}
......@@ -476,7 +476,7 @@ const std::string Command::strToPython(const char* Str)
void Command::updateActive(void)
{
WaitCursor wc;
doCommand(Gui,"App.ActiveDocument.recompute()");
doCommand(App,"App.ActiveDocument.recompute()");
}
bool Command::isActiveObjectValid(void)
......
......@@ -25,6 +25,8 @@
#ifndef _PreComp_
# include <QMessageBox>
# include <QDir>
# include <QFile>
# include <QFileInfo>
#endif
#include "Macro.h"
......@@ -53,9 +55,7 @@ DlgMacroRecordImp::DlgMacroRecordImp( QWidget* parent, Qt::WFlags fl )
// get the macro home path
this->macroPath = QString::fromUtf8(getWindowParameter()->GetASCII("MacroPath",
App::Application::getUserAppDataDir().c_str()).c_str());
// check on PATHSEP at the end
if (this->macroPath.at(this->macroPath.length()-1) != QLatin1Char(PATHSEP))
this->macroPath += QLatin1Char(PATHSEP);
this->macroPath = QDir::toNativeSeparators(QDir(this->macroPath).path() + QDir::separator());
// set the edit fields
this->lineEditMacroPath->setText(macroPath);
......@@ -96,10 +96,9 @@ void DlgMacroRecordImp::on_buttonStart_clicked()
// search in the macro path first for an already existing macro
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);
if ( fi.isFile() && fi.exists() )
{
if (fi.isFile() && fi.exists()) {
if (QMessageBox::question(this, tr("Existing macro"),
tr("The macro '%1' already exists. Do you want to overwrite?").arg(fn),
QMessageBox::Yes,
......@@ -109,6 +108,14 @@ void DlgMacroRecordImp::on_buttonStart_clicked()
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
this->macroManager->open(MacroManager::File, fn.toUtf8().constData());
accept();
......@@ -143,7 +150,7 @@ void DlgMacroRecordImp::on_pushButtonChooseDir_clicked()
{
QString newDir = QFileDialog::getExistingDirectory(0,tr("Choose macro directory"),macroPath);
if (!newDir.isEmpty()) {
macroPath = QDir::convertSeparators(newDir + QDir::separator());
macroPath = QDir::toNativeSeparators(newDir + QDir::separator());
this->lineEditMacroPath->setText(macroPath);
getWindowParameter()->SetASCII("MacroPath",macroPath.toUtf8());
}
......
......@@ -99,22 +99,22 @@ void MacroManager::commit(void)
{
// sort import lines and avoid duplicates
QTextStream str(&file);
QStringList lines = this->macroInProgress.split(QLatin1Char('\n'));
QStringList import; import << QString::fromAscii("import FreeCAD\n");
QStringList import;
import << QString::fromAscii("import FreeCAD");
QStringList body;
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 ")) ||
(*it).startsWith(QLatin1String("#import ")))
{
if (import.indexOf(*it + QLatin1Char('\n')) == -1)
import.push_back(*it + QLatin1Char('\n'));
if (import.indexOf(*it) == -1)
import.push_back(*it);
}
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
str << header;
for ( it = import.begin(); it != import.end(); ++it )
str << (*it);
for (it = import.begin(); it != import.end(); ++it)
str << (*it) << QLatin1Char('\n');
str << QLatin1Char('\n');
for ( it = body.begin(); it != body.end(); ++it )
str << (*it);
for (it = body.begin(); it != body.end(); ++it)
str << (*it) << QLatin1Char('\n');
str << footer;
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)
{
if (this->openMacro)
{
if(Type == Gui)
{
if (this->openMacro) {
bool comment = false;
if (Type == Gui) {
if (this->recordGui && this->guiAsComment)
this->macroInProgress += QLatin1Char('#');
comment = true;
else if (!this->recordGui)
return; // ignore Gui commands
}
else if (Type == Cmt) {
comment = true;
}
this->macroInProgress += QString::fromAscii(sLine);
this->macroInProgress += QLatin1Char('\n');
QStringList lines = QString::fromAscii(sLine).split(QLatin1String("\n"));
if (comment) {
for (QStringList::iterator it = lines.begin(); it != lines.end(); ++it)
it->prepend(QLatin1String("#"));
}
this->macroInProgress.append(lines);
}
if (this->scriptToPyConsole) {
......@@ -187,9 +193,7 @@ void MacroManager::setModule(const char* sModule)
{
if (this->openMacro && sModule && *sModule != '\0')
{
this->macroInProgress += QString::fromAscii("import ");
this->macroInProgress += QString::fromAscii(sModule);
this->macroInProgress += QLatin1Char('\n');
this->macroInProgress.append(QString::fromAscii("import %1").arg(QString::fromAscii(sModule)));
}
}
......
......@@ -26,6 +26,7 @@
// Std. configurations
#include <QString>
#include <QStringList>
#include <Base/Observer.h>
#include <Base/Parameter.h>
......@@ -51,14 +52,15 @@ public:
/** Macro type enumeration */
enum MacroType {
File, /**< The macro will be saved in a file */
App, /**< 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 */
User, /**< The macro belongs to the Application and will be saved in the UserParameter */
Doc /**< The macro belongs to the Document and will be saved and restored with the Document */
};
/** Line type enumeration */
enum LineType {
Base, /**< The line effects only the document and Application (FreeCAD) */
Gui, /**< The line effects the Gui (FreeCADGui) */
App, /**< The line effects only the document and Application (FreeCAD) */
Gui, /**< The line effects the Gui (FreeCADGui) */
Cmt /**< The line is handled as a comment */
};
/** Opens a new Macro recording session
......@@ -92,7 +94,7 @@ public:
void OnChange(Base::Subject<const char*> &rCaller, const char * sReason);
protected:
QString macroInProgress; /**< Container for the macro */
QStringList macroInProgress; /**< Container for the macro */
QString macroName; /**< name of the macro */
bool openMacro;
bool recordGui;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment