DlgMacroExecuteImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de>              *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <QInputDialog>
00027 # include <QHeaderView>
00028 # include <QMessageBox>
00029 #endif
00030 
00031 #include "DlgMacroExecuteImp.h"
00032 #include "Application.h"
00033 #include "BitmapFactory.h"
00034 #include "MainWindow.h"
00035 #include "FileDialog.h"
00036 #include "Macro.h"
00037 #include "Document.h"
00038 #include "EditorView.h"
00039 #include "PythonEditor.h"
00040 
00041 #include <App/Application.h>
00042 #include <App/Document.h>
00043 
00044 using namespace Gui;
00045 using namespace Gui::Dialog;
00046 
00047 /* TRANSLATOR Gui::Dialog::DlgMacroExecuteImp */
00048 
00056 DlgMacroExecuteImp::DlgMacroExecuteImp( QWidget* parent, Qt::WFlags fl )
00057     : QDialog( parent, fl ), WindowParameter( "Macro" )
00058 {
00059     this->setupUi(this);
00060     // retrieve the macro path from parameter or use the user data as default
00061     std::string path = getWindowParameter()->GetASCII("MacroPath",
00062         App::Application::getUserAppDataDir().c_str());
00063     this->macroPath = QString::fromUtf8(path.c_str());
00064     fileChooser->setFileName(this->macroPath);
00065 
00066     // Fill the List box
00067     QStringList labels; labels << tr("Macros");
00068     macroListBox->setHeaderLabels(labels);
00069     macroListBox->header()->hide();
00070     fillUpList();
00071 }
00072 
00076 DlgMacroExecuteImp::~DlgMacroExecuteImp()
00077 {
00078     // no need to delete child widgets, Qt does it all for us
00079 }
00080 
00084 void DlgMacroExecuteImp::fillUpList(void)
00085 {
00086     // lists all files in macro path
00087     QDir dir(this->macroPath, QLatin1String("*.FCMacro *.py"));
00088   
00089     // fill up with the directory
00090     macroListBox->clear();
00091     for (unsigned int i=0; i<dir.count(); i++ ) {
00092         QTreeWidgetItem* item = new QTreeWidgetItem(macroListBox);
00093         item->setText(0, dir[i]);
00094     }
00095 }
00096 
00100 void DlgMacroExecuteImp::on_macroListBox_currentItemChanged(QTreeWidgetItem* item)
00101 {
00102     if (item) {
00103         LineEditMacroName->setText(item->text(0));
00104         executeButton->setEnabled(true);
00105         editButton->setEnabled(true);
00106         deleteButton->setEnabled(true);
00107     }
00108 }
00109 
00113 void DlgMacroExecuteImp::accept()
00114 {
00115     QTreeWidgetItem* item = macroListBox->currentItem();
00116     if (!item) return;
00117     
00118     QDialog::accept();
00119     QDir dir(this->macroPath);
00120     QFileInfo fi(dir, item->text(0));
00121     Application::Instance->macroManager()->run(Gui::MacroManager::File, fi.filePath().toUtf8());
00122     // after macro run recalculate the document
00123     if (Application::Instance->activeDocument())
00124         Application::Instance->activeDocument()->getDocument()->recompute();
00125 }
00126 
00130 void DlgMacroExecuteImp::on_fileChooser_fileNameChanged(const QString& fn)
00131 {
00132     if (!fn.isEmpty())
00133     {
00134         // save the path in the parameters
00135         this->macroPath = fn;
00136         getWindowParameter()->SetASCII("MacroPath",fn.toUtf8());
00137         // fill the list box
00138         fillUpList();
00139     }
00140 }
00141 
00145 void DlgMacroExecuteImp::on_editButton_clicked()
00146 {
00147     QTreeWidgetItem* item = macroListBox->currentItem();
00148     if (!item) return;
00149 
00150     QDir dir(this->macroPath);
00151     QString file = QString::fromAscii("%1/%2").arg(dir.absolutePath()).arg(item->text(0));
00152 
00153     Application::Instance->open(file.toUtf8(), "FreeCADGui");
00154     close();
00155 }
00156 
00158 void DlgMacroExecuteImp::on_createButton_clicked()
00159 {
00160     // query file name
00161     QString fn = QInputDialog::getText(this, tr("Macro file"), tr("Enter a file name, please:"),
00162         QLineEdit::Normal, QString::null, 0);
00163     if (!fn.isEmpty())
00164     {
00165         QString suffix = QFileInfo(fn).suffix().toLower();
00166         if (suffix != QLatin1String("fcmacro") && suffix != QLatin1String("py"))
00167             fn += QLatin1String(".FCMacro");
00168         QDir dir(this->macroPath);
00169         QFileInfo fi(dir, fn);
00170         if (fi.exists() && fi.isFile())
00171         {
00172             QMessageBox::warning(this, tr("Existing file"),
00173                 tr("'%1'.\nThis file already exists.").arg(fi.fileName()));
00174         }
00175         else
00176         {
00177             QFile file(fi.absoluteFilePath());
00178             if (!file.open(QFile::WriteOnly)) {
00179                 QMessageBox::warning(this, tr("Cannot create file"),
00180                     tr("Creation of file '%1' failed.").arg(fi.absoluteFilePath()));
00181                 return;
00182             }
00183             file.close();
00184             PythonEditor* editor = new PythonEditor();
00185             editor->setWindowIcon(Gui::BitmapFactory().pixmap("python_small"));
00186             PythonEditorView* edit = new PythonEditorView(editor, getMainWindow());
00187             edit->open(fi.absoluteFilePath());
00188             edit->setWindowTitle(fn);
00189             edit->resize(400, 300);
00190             getMainWindow()->addWindow(edit);
00191             close();
00192         }
00193     }
00194 }
00195 
00197 void DlgMacroExecuteImp::on_deleteButton_clicked()
00198 {
00199     QTreeWidgetItem* item = macroListBox->currentItem();
00200     if (!item) return;
00201 
00202     QString fn = item->text(0);
00203     int ret = QMessageBox::question(this, tr("Delete macro"),
00204         tr("Do you really want to delete the macro '%1'?").arg( fn ),
00205         QMessageBox::Yes, QMessageBox::No|QMessageBox::Default|QMessageBox::Escape);
00206     if (ret == QMessageBox::Yes)
00207     {
00208         QDir dir(this->macroPath);
00209         dir.remove(fn);
00210         int index = macroListBox->indexOfTopLevelItem(item);
00211         macroListBox->takeTopLevelItem(index);
00212         delete item;
00213     }
00214 }
00215 
00216 #include "moc_DlgMacroExecuteImp.cpp"

Generated on Wed Nov 23 19:00:06 2011 for FreeCAD by  doxygen 1.6.1