VisualInspection.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 #include "VisualInspection.h"
00026 #include "ui_VisualInspection.h"
00027 
00028 #include <App/Document.h>
00029 #include <App/DocumentObject.h>
00030 #include <App/Application.h>
00031 #include <Gui/Document.h>
00032 #include <Gui/ViewProvider.h>
00033 #include <Gui/Application.h>
00034 #include <Gui/MainWindow.h>
00035 #include <Gui/PrefWidgets.h>
00036 
00037 using namespace InspectionGui;
00038 
00039 namespace InspectionGui {
00040 class SingleSelectionItem : public QTreeWidgetItem
00041 {
00042 public:
00043     SingleSelectionItem (QTreeWidget* parent)
00044         : QTreeWidgetItem(parent), _compItem(0)
00045     {
00046     }
00047 
00048     SingleSelectionItem (QTreeWidgetItem* parent)
00049         : QTreeWidgetItem (parent), _compItem(0)
00050     {
00051     }
00052 
00053     ~SingleSelectionItem ()
00054     {
00055     }
00056 
00057     SingleSelectionItem* getCompetitiveItem() const
00058     {
00059         return _compItem;
00060     }
00061 
00062     void setCompetitiveItem(SingleSelectionItem* item)
00063     {
00064         _compItem = item;
00065     }
00066 
00067 private:
00068     SingleSelectionItem* _compItem;
00069 };
00070 }
00071 
00072 /* TRANSLATOR InspectionGui::DlgVisualInspectionImp */
00073 
00078 VisualInspection::VisualInspection(QWidget* parent, Qt::WFlags fl)
00079     : QDialog(parent, fl), ui(new Ui_VisualInspection)
00080 {
00081     ui->setupUi(this);
00082     connect(ui->treeWidgetActual, SIGNAL(itemClicked(QTreeWidgetItem*, int)), 
00083             this, SLOT(onActivateItem(QTreeWidgetItem*)));
00084     connect(ui->treeWidgetNominal, SIGNAL(itemClicked(QTreeWidgetItem*, int)), 
00085             this, SLOT(onActivateItem(QTreeWidgetItem*)));
00086 
00087     //FIXME: Not used yet
00088     ui->textLabel2->hide();
00089     ui->prefFloatSpinBox2->hide();
00090 
00091     connect(ui->buttonHelp, SIGNAL(clicked()), Gui::getMainWindow(), SLOT(whatsThis()));
00092 
00093     App::Document* doc = App::GetApplication().getActiveDocument();
00094     // disable Ok button and enable of at least one item in each view is on
00095     ui->buttonOk->setDisabled(true);
00096 
00097     if (!doc) {
00098         ui->treeWidgetActual->setDisabled(true);
00099         ui->treeWidgetNominal->setDisabled(true);
00100         return;
00101     }
00102 
00103     Gui::Document* gui = Gui::Application::Instance->getDocument(doc);
00104 
00105     std::vector<App::DocumentObject*> obj = doc->getObjects();
00106     Base::Type point = Base::Type::fromName("Points::Feature");
00107     Base::Type mesh  = Base::Type::fromName("Mesh::Feature");
00108     Base::Type shape = Base::Type::fromName("Part::Feature");
00109     for (std::vector<App::DocumentObject*>::iterator it = obj.begin(); it != obj.end(); ++it) {
00110         if ((*it)->getTypeId().isDerivedFrom(point) ||
00111             (*it)->getTypeId().isDerivedFrom(mesh)  ||
00112             (*it)->getTypeId().isDerivedFrom(shape)) {
00113             Gui::ViewProvider* view = gui->getViewProvider(*it);
00114             QIcon px = view->getIcon();
00115             SingleSelectionItem* item1 = new SingleSelectionItem(ui->treeWidgetActual);
00116             item1->setText(0, QString::fromUtf8((*it)->Label.getValue()));
00117             item1->setData(0, Qt::UserRole, QString::fromAscii((*it)->getNameInDocument()));
00118             item1->setCheckState(0, Qt::Unchecked);
00119             item1->setIcon(0, px);
00120 
00121             SingleSelectionItem* item2 = new SingleSelectionItem(ui->treeWidgetNominal);
00122             item2->setText(0, QString::fromUtf8((*it)->Label.getValue()));
00123             item2->setData(0, Qt::UserRole, QString::fromAscii((*it)->getNameInDocument()));
00124             item2->setCheckState(0, Qt::Unchecked);
00125             item2->setIcon(0, px);
00126 
00127             item1->setCompetitiveItem(item2);
00128             item2->setCompetitiveItem(item1);
00129         }
00130     }
00131 
00132     loadSettings();
00133 }
00134 
00135 /*
00136  *  Destroys the object and frees any allocated resources
00137  */
00138 VisualInspection::~VisualInspection()
00139 {
00140     // no need to delete child widgets, Qt does it all for us
00141     delete ui;
00142 }
00143 
00144 void VisualInspection::loadSettings()
00145 {
00146     ui->prefFloatSpinBox1->onRestore();
00147     ui->prefFloatSpinBox2->onRestore();
00148 }
00149 
00150 void VisualInspection::saveSettings()
00151 {
00152     ui->prefFloatSpinBox1->onSave();
00153     ui->prefFloatSpinBox2->onSave();
00154 }
00155 
00156 void VisualInspection::onActivateItem(QTreeWidgetItem* item)
00157 {
00158     if (item) {
00159         SingleSelectionItem* sel = (SingleSelectionItem*)item;
00160         SingleSelectionItem* cmp = sel->getCompetitiveItem();
00161         if (cmp && cmp->checkState(0) == Qt::Checked)
00162             cmp->setCheckState(0, Qt::Unchecked);
00163     }
00164 
00165     bool ok=false;
00166     for (QTreeWidgetItemIterator it(ui->treeWidgetActual); *it; ++it) {
00167         SingleSelectionItem* sel = (SingleSelectionItem*)*it;
00168         if (sel->checkState(0) == Qt::Checked) {
00169             ok = true;
00170             break;
00171         }
00172     }
00173 
00174     if (ok) {
00175         ok = false;
00176         for (QTreeWidgetItemIterator it (ui->treeWidgetNominal); *it; ++it) {
00177             SingleSelectionItem* sel = (SingleSelectionItem*)*it;
00178             if (sel->checkState(0) == Qt::Checked) {
00179                 ok = true;
00180                 break;
00181             }
00182         }
00183     }
00184 
00185     ui->buttonOk->setEnabled(ok);
00186 }
00187 
00188 void VisualInspection::accept()
00189 {
00190     onActivateItem(0);
00191     if (ui->buttonOk->isEnabled()) {
00192         QDialog::accept();
00193         saveSettings();
00194 
00195         // collect all nominal geometries
00196         QStringList nominalNames;
00197         for (QTreeWidgetItemIterator it(ui->treeWidgetNominal); *it; it++) {
00198             SingleSelectionItem* sel = (SingleSelectionItem*)*it;
00199             if (sel->checkState(0) == Qt::Checked)
00200                 nominalNames << sel->data(0, Qt::UserRole).toString();
00201         }
00202 
00203         float searchRadius = ui->prefFloatSpinBox1->value();
00204         float thickness = ui->prefFloatSpinBox2->value();
00205 
00206         // open a new command
00207         Gui::Document* doc = Gui::Application::Instance->activeDocument();
00208         doc->openCommand("Visual Inspection");
00209 
00210         // create a group
00211         Gui::Application::Instance->runCommand(
00212             true, "App_activeDocument___InspectionGroup=App.ActiveDocument.addObject(\"Inspection::Group\",\"Inspection\")");
00213     
00214         // for each actual geometry create an inspection feature
00215         for (QTreeWidgetItemIterator it(ui->treeWidgetActual); *it; it++) {
00216             SingleSelectionItem* sel = (SingleSelectionItem*)*it;
00217             if (sel->checkState(0) == Qt::Checked) {
00218                 QString actualName = sel->data(0, Qt::UserRole).toString();
00219                 Gui::Application::Instance->runCommand(
00220                     true, "App_activeDocument___InspectionGroup.newObject(\"Inspection::Feature\",\"%s_Inspect\")", (const char*)actualName.toAscii());
00221                 Gui::Application::Instance->runCommand(
00222                     true, "App.ActiveDocument.ActiveObject.Actual=App.ActiveDocument.%s\n"
00223                           "App_activeDocument___activeObject___Nominals=list()\n"
00224                           "App.ActiveDocument.ActiveObject.SearchRadius=%.3f\n"
00225                           "App.ActiveDocument.ActiveObject.Thickness=%.3f\n", (const char*)actualName.toAscii(), searchRadius, thickness);
00226                 for (QStringList::Iterator it = nominalNames.begin(); it != nominalNames.end(); ++it) {
00227                     Gui::Application::Instance->runCommand(
00228                         true, "App_activeDocument___activeObject___Nominals.append(App.ActiveDocument.%s)\n", (const char*)(*it).toAscii());
00229                 }
00230                 Gui::Application::Instance->runCommand(
00231                     true, "App.ActiveDocument.ActiveObject.Nominals=App_activeDocument___activeObject___Nominals\n"
00232                           "del App_activeDocument___activeObject___Nominals\n");
00233             }
00234         }
00235 
00236         Gui::Application::Instance->runCommand(
00237             true, "del App_activeDocument___InspectionGroup\n");
00238 
00239         doc->commitCommand();
00240         doc->getDocument()->recompute();
00241 
00242         // hide the checked features
00243         for (QTreeWidgetItemIterator it(ui->treeWidgetActual); *it; it++) {
00244             SingleSelectionItem* sel = (SingleSelectionItem*)*it;
00245             if (sel->checkState(0) == Qt::Checked) {
00246                 Gui::Application::Instance->runCommand(
00247                     true, "Gui.ActiveDocument.getObject(\"%s\").Visibility=False"
00248                         , (const char*)sel->data(0, Qt::UserRole).toString().toAscii());
00249             }
00250         }
00251 
00252         for (QTreeWidgetItemIterator it(ui->treeWidgetNominal); *it; it++) {
00253             SingleSelectionItem* sel = (SingleSelectionItem*)*it;
00254             if (sel->checkState(0) == Qt::Checked) {
00255                 Gui::Application::Instance->runCommand(
00256                     true, "Gui.ActiveDocument.getObject(\"%s\").Visibility=False"
00257                         , (const char*)sel->data(0, Qt::UserRole).toString().toAscii());
00258             }
00259         }
00260     }
00261 }
00262 
00263 #include "moc_VisualInspection.cpp"

Generated on Wed Nov 23 19:01:02 2011 for FreeCAD by  doxygen 1.6.1