TaskLoft.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net>     *
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 
00026 #ifndef _PreComp_
00027 # include <QMessageBox>
00028 # include <QTextStream>
00029 #endif
00030 
00031 #include "ui_TaskLoft.h"
00032 #include "TaskLoft.h"
00033 
00034 #include <Gui/Application.h>
00035 #include <Gui/Document.h>
00036 #include <Gui/Selection.h>
00037 #include <Gui/ViewProvider.h>
00038 
00039 #include <Base/Console.h>
00040 #include <Base/Interpreter.h>
00041 #include <App/Application.h>
00042 #include <App/Document.h>
00043 #include <App/DocumentObject.h>
00044 #include <Mod/Part/App/PartFeature.h>
00045 
00046 
00047 using namespace PartGui;
00048 
00049 class LoftWidget::Private
00050 {
00051 public:
00052     Ui_TaskLoft ui;
00053     std::string document;
00054     Private()
00055     {
00056     }
00057     ~Private()
00058     {
00059     }
00060 };
00061 
00062 /* TRANSLATOR PartGui::LoftWidget */
00063 
00064 LoftWidget::LoftWidget(QWidget* parent)
00065   : d(new Private())
00066 {
00067     Gui::Application::Instance->runPythonCode("from FreeCAD import Base");
00068     Gui::Application::Instance->runPythonCode("import Part");
00069 
00070     d->ui.setupUi(this);
00071     connect(d->ui.treeWidgetWire, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
00072             this, SLOT(onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
00073     connect(d->ui.treeWidgetLoft, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
00074             this, SLOT(onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
00075     findShapes();
00076 }
00077 
00078 LoftWidget::~LoftWidget()
00079 {
00080     delete d;
00081 }
00082 
00083 void LoftWidget::findShapes()
00084 {
00085     App::Document* activeDoc = App::GetApplication().getActiveDocument();
00086     Gui::Document* activeGui = Gui::Application::Instance->getDocument(activeDoc);
00087     if (!activeGui) return;
00088     d->document = activeDoc->getName();
00089 
00090     std::vector<Part::Feature*> objs = activeDoc->getObjectsOfType<Part::Feature>();
00091 
00092     for (std::vector<Part::Feature*>::iterator it = objs.begin(); it!=objs.end(); ++it) {
00093         const TopoDS_Shape& shape = (*it)->Shape.getValue();
00094         if (shape.IsNull()) continue;
00095 
00096         if (shape.ShapeType() == TopAbs_WIRE || shape.ShapeType() == TopAbs_VERTEX) {
00097             QString label = QString::fromUtf8((*it)->Label.getValue());
00098             QString name = QString::fromAscii((*it)->getNameInDocument());
00099             
00100             QTreeWidgetItem* child = new QTreeWidgetItem();
00101             child->setText(0, label);
00102             child->setToolTip(0, label);
00103             child->setData(0, Qt::UserRole, name);
00104             Gui::ViewProvider* vp = activeGui->getViewProvider(*it);
00105             if (vp) child->setIcon(0, vp->getIcon());
00106             d->ui.treeWidgetWire->addTopLevelItem(child);
00107         }
00108     }
00109 }
00110 
00111 bool LoftWidget::accept()
00112 {
00113     QString list, solid, ruled;
00114     if (d->ui.checkSolid->isChecked())
00115         solid = QString::fromAscii("True");
00116     else
00117         solid = QString::fromAscii("False");
00118 
00119     if (d->ui.checkRuledSurface->isChecked())
00120         ruled = QString::fromAscii("True");
00121     else
00122         ruled = QString::fromAscii("False");
00123 
00124     QTextStream str(&list);
00125 
00126     int count = d->ui.treeWidgetLoft->topLevelItemCount();
00127     if (count < 2) {
00128         QMessageBox::critical(this, tr("Too few elements"), tr("At least two vertices or wires are required."));
00129         return false;
00130     }
00131     for (int i=0; i<count; i++) {
00132         QTreeWidgetItem* child = d->ui.treeWidgetLoft->topLevelItem(i);
00133         QString name = child->data(0, Qt::UserRole).toString();
00134         str << "App.getDocument('" << d->document.c_str() << "')." << name << ", ";
00135     }
00136 
00137     try {
00138         QString cmd;
00139         cmd = QString::fromAscii(
00140             "App.getDocument('%4').addObject('Part::Loft','Loft')\n"
00141             "App.getDocument('%4').ActiveObject.Sections=[%1]\n"
00142             "App.getDocument('%4').ActiveObject.Solid=%2\n"
00143             "App.getDocument('%4').ActiveObject.Ruled=%3\n"
00144             ).arg(list).arg(solid).arg(ruled).arg(QString::fromAscii(d->document.c_str()));
00145 
00146         Gui::Document* doc = Gui::Application::Instance->getDocument(d->document.c_str());
00147         if (!doc) throw Base::Exception("Document doesn't exist anymore");
00148         doc->openCommand("Loft");
00149         Gui::Application::Instance->runPythonCode((const char*)cmd.toAscii(), false, false);
00150         doc->commitCommand();
00151         doc->getDocument()->recompute();
00152     }
00153     catch (const Base::Exception& e) {
00154         Base::Console().Error("%s\n", e.what());
00155         return false;
00156     }
00157 
00158     return true;
00159 }
00160 
00161 bool LoftWidget::reject()
00162 {
00163     return true;
00164 }
00165 
00166 void LoftWidget::onCurrentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)
00167 {
00168     if (previous) {
00169         Gui::Selection().rmvSelection(d->document.c_str(),
00170             (const char*)previous->data(0,Qt::UserRole).toByteArray());
00171     }
00172     if (current) {
00173         Gui::Selection().addSelection(d->document.c_str(),
00174             (const char*)current->data(0,Qt::UserRole).toByteArray());
00175     }
00176 }
00177 
00178 void LoftWidget::on_addButton_clicked()
00179 {
00180     QTreeWidgetItem* item = d->ui.treeWidgetWire->currentItem();
00181     if (item) {
00182         int index = d->ui.treeWidgetWire->indexOfTopLevelItem(item);
00183         item = d->ui.treeWidgetWire->takeTopLevelItem(index);
00184         d->ui.treeWidgetWire->setCurrentItem(0);
00185         d->ui.treeWidgetLoft->addTopLevelItem(item);
00186         d->ui.treeWidgetLoft->setCurrentItem(item);
00187     }
00188 }
00189 
00190 void LoftWidget::on_removeButton_clicked()
00191 {
00192     QTreeWidgetItem* item = d->ui.treeWidgetLoft->currentItem();
00193     if (item) {
00194         int index = d->ui.treeWidgetLoft->indexOfTopLevelItem(item);
00195         item = d->ui.treeWidgetLoft->takeTopLevelItem(index);
00196         d->ui.treeWidgetLoft->setCurrentItem(0);
00197         d->ui.treeWidgetWire->addTopLevelItem(item);
00198         d->ui.treeWidgetWire->setCurrentItem(item);
00199     }
00200 }
00201 
00202 void LoftWidget::on_upButton_clicked()
00203 {
00204     QTreeWidgetItem* item = d->ui.treeWidgetLoft->currentItem();
00205     if (item && d->ui.treeWidgetLoft->isItemSelected(item)) {
00206         int index = d->ui.treeWidgetLoft->indexOfTopLevelItem(item);
00207         if (index > 0) {
00208             d->ui.treeWidgetLoft->takeTopLevelItem(index);
00209             d->ui.treeWidgetLoft->insertTopLevelItem(index-1, item);
00210             d->ui.treeWidgetLoft->setCurrentItem(item);
00211         }
00212     }
00213 }
00214 
00215 void LoftWidget::on_downButton_clicked()
00216 {
00217     QTreeWidgetItem* item = d->ui.treeWidgetLoft->currentItem();
00218     if (item && d->ui.treeWidgetLoft->isItemSelected(item)) {
00219         int index = d->ui.treeWidgetLoft->indexOfTopLevelItem(item);
00220         if (index < d->ui.treeWidgetLoft->topLevelItemCount()-1) {
00221             d->ui.treeWidgetLoft->takeTopLevelItem(index);
00222             d->ui.treeWidgetLoft->insertTopLevelItem(index+1, item);
00223             d->ui.treeWidgetLoft->setCurrentItem(item);
00224         }
00225     }
00226 }
00227 
00228 void LoftWidget::changeEvent(QEvent *e)
00229 {
00230     QWidget::changeEvent(e);
00231     if (e->type() == QEvent::LanguageChange) {
00232         d->ui.retranslateUi(this);
00233     }
00234 }
00235 
00236 
00237 /* TRANSLATOR PartGui::TaskLoft */
00238 
00239 TaskLoft::TaskLoft()
00240 {
00241     widget = new LoftWidget();
00242     taskbox = new Gui::TaskView::TaskBox(
00243         QPixmap(), widget->windowTitle(), true, 0);
00244     taskbox->groupLayout()->addWidget(widget);
00245     Content.push_back(taskbox);
00246 }
00247 
00248 TaskLoft::~TaskLoft()
00249 {
00250 }
00251 
00252 void TaskLoft::open()
00253 {
00254 }
00255 
00256 void TaskLoft::clicked(int)
00257 {
00258 }
00259 
00260 bool TaskLoft::accept()
00261 {
00262     return widget->accept();
00263 }
00264 
00265 bool TaskLoft::reject()
00266 {
00267     return widget->reject();
00268 }
00269 
00270 #include "moc_TaskLoft.cpp"

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