~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Gui/Macro.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
Import upstream version 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) 2004 J�rgen Riegel <juergen.riegel@web.de>              *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
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.      *
 
10
 *                                                                         *
 
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.                  *
 
15
 *                                                                         *
 
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                                *
 
20
 *                                                                         *
 
21
 ***************************************************************************/
 
22
 
 
23
 
 
24
#include "PreCompiled.h"
 
25
 
 
26
#ifndef _PreComp_
 
27
# include <assert.h>
 
28
# include <stdio.h>
 
29
#endif
 
30
 
 
31
/// Here the FreeCAD includes sorted by Base,App,Gui......
 
32
#include "Macro.h"
 
33
 
 
34
#include <Base/Interpreter.h>
 
35
#include <Base/Console.h>
 
36
#include <Base/Exception.h>
 
37
#include <App/Application.h>
 
38
 
 
39
#include "DockWindowManager.h"
 
40
#include "MainWindow.h"
 
41
#include "PythonConsole.h"
 
42
 
 
43
using namespace Gui;
 
44
 
 
45
 
 
46
MacroManager::MacroManager()
 
47
  : openMacro(false), recordGui(true), guiAsComment(true),scriptToPyConsole(false),pyConsole(0)
 
48
{
 
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();
 
53
}
 
54
 
 
55
MacroManager::~MacroManager()
 
56
{
 
57
    this->params->Detach(this);
 
58
}
 
59
 
 
60
void MacroManager::OnChange(Base::Subject<const char*> &rCaller, const char * sReason)
 
61
{
 
62
    this->recordGui         = this->params->GetBool("RecordGui", true);
 
63
    this->guiAsComment      = this->params->GetBool("GuiAsComment", true);
 
64
    this->scriptToPyConsole = this->params->GetBool("ScriptToPyConsole", false);
 
65
}
 
66
 
 
67
void MacroManager::open(MacroType eType,const char *sName)
 
68
{
 
69
    // check 
 
70
    assert(!this->openMacro);
 
71
    assert(eType == File);
 
72
 
 
73
    // Convert from Utf-8
 
74
    this->macroName = QString::fromUtf8(sName);
 
75
    if (!this->macroName.endsWith(QLatin1String(".FCMacro")))
 
76
        this->macroName += QLatin1String(".FCMacro");
 
77
 
 
78
    this->macroInProgress.clear();
 
79
    this->openMacro = true;
 
80
 
 
81
    Base::Console().Log("CmdM: Open macro: %s\n", sName);
 
82
}
 
83
 
 
84
void MacroManager::commit(void)
 
85
{
 
86
    QFile file(this->macroName);
 
87
    if (file.open(QFile::WriteOnly)) 
 
88
    {
 
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");
 
93
        QStringList body;
 
94
 
 
95
        QStringList::Iterator it;
 
96
        for ( it = lines.begin(); it != lines.end(); ++it )
 
97
        {
 
98
            if ((*it).startsWith(QLatin1String("import ")) ||
 
99
                (*it).startsWith(QLatin1String("#import ")))
 
100
            {
 
101
                if (import.indexOf(*it + QLatin1Char('\n')) == -1)
 
102
                    import.push_back(*it + QLatin1Char('\n'));
 
103
            }
 
104
            else
 
105
            {
 
106
                body.push_back(*it + QLatin1Char('\n'));
 
107
            }
 
108
        }
 
109
 
 
110
        QString header = QString::fromAscii("# Macro Begin: ");
 
111
        header += this->macroName;
 
112
        header += QString::fromAscii(" +++++++++++++++++++++++++++++++++++++++++++++++++\n");
 
113
 
 
114
        QString footer = QString::fromAscii("# Macro End: ");
 
115
        footer += this->macroName;
 
116
        footer += QString::fromAscii(" +++++++++++++++++++++++++++++++++++++++++++++++++\n");
 
117
 
 
118
        // write the data to the text file
 
119
        str << header;
 
120
        for ( it = import.begin(); it != import.end(); ++it )
 
121
            str << (*it);
 
122
        str << QLatin1Char('\n');
 
123
        for ( it = body.begin(); it != body.end(); ++it )
 
124
            str << (*it);
 
125
        str << footer;
 
126
    }
 
127
 
 
128
    Base::Console().Log("CmdM: Commit macro: %s\n",(const char*)this->macroName.toUtf8());
 
129
 
 
130
    this->macroInProgress.clear();
 
131
    this->macroName.clear();
 
132
    this->openMacro = false;
 
133
}
 
134
 
 
135
void MacroManager::cancel(void)
 
136
{
 
137
    Base::Console().Log("CmdM: Cancel macro: %s\n",(const char*)this->macroName.toUtf8());
 
138
 
 
139
    this->macroInProgress.clear();
 
140
    this->macroName.clear();
 
141
    this->openMacro = false;
 
142
}
 
143
 
 
144
void MacroManager::addLine(LineType Type, const char* sLine)
 
145
{
 
146
    if (this->openMacro)
 
147
    {
 
148
        if(Type == Gui)
 
149
        {
 
150
            if (this->recordGui && this->guiAsComment)
 
151
                this->macroInProgress += QLatin1Char('#');
 
152
            else if (!this->recordGui)
 
153
                return; // ignore Gui commands
 
154
        }
 
155
 
 
156
        this->macroInProgress += QString::fromAscii(sLine);
 
157
        this->macroInProgress += QLatin1Char('\n');
 
158
    }
 
159
 
 
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?
 
165
        if (this->pyConsole)
 
166
            this->pyConsole->printStatement(QString::fromUtf8(sLine));
 
167
    }
 
168
}
 
169
 
 
170
void MacroManager::setModule(const char* sModule)
 
171
{
 
172
    if (this->openMacro && sModule && *sModule != '\0')
 
173
    {
 
174
        this->macroInProgress += QString::fromAscii("import ");
 
175
        this->macroInProgress += QString::fromAscii(sModule);
 
176
        this->macroInProgress += QLatin1Char('\n');
 
177
    }
 
178
}
 
179
 
 
180
void MacroManager::run(MacroType eType,const char *sName)
 
181
{
 
182
    try
 
183
    {
 
184
        //The given path name is expected to be Utf-8
 
185
        Base::Interpreter().runFile(sName);
 
186
    }
 
187
    catch (const Base::Exception& e)
 
188
    {
 
189
        qWarning("%s",e.what());
 
190
    }
 
191
}