TaskFaceColors.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 <TopExp_Explorer.hxx>
00028 # include <TopTools_IndexedMapOfShape.hxx>
00029 # include <QSet>
00030 #endif
00031 
00032 #include "ui_TaskFaceColors.h"
00033 #include "TaskFaceColors.h"
00034 #include "ViewProviderExt.h"
00035 
00036 #include <Gui/Application.h>
00037 #include <Gui/Document.h>
00038 #include <Gui/Selection.h>
00039 
00040 #include <App/Document.h>
00041 #include <App/DocumentObject.h>
00042 #include <Mod/Part/App/PartFeature.h>
00043 
00044 
00045 using namespace PartGui;
00046 
00047 namespace PartGui {
00048     class FaceSelection : public Gui::SelectionFilterGate
00049     {
00050         const App::DocumentObject* object;
00051     public:
00052         FaceSelection(const App::DocumentObject* obj)
00053             : Gui::SelectionFilterGate((Gui::SelectionFilter*)0), object(obj)
00054         {
00055         }
00056         bool allow(App::Document*pDoc, App::DocumentObject*pObj, const char*sSubName)
00057         {
00058             if (pObj != this->object)
00059                 return false;
00060             if (!sSubName || sSubName[0] == '\0')
00061                 return false;
00062             std::string element(sSubName);
00063             return element.substr(0,4) == "Face";
00064         }
00065     };
00066 }
00067 
00068 class FaceColors::Private
00069 {
00070 public:
00071     Ui_TaskFaceColors* ui;
00072     ViewProviderPartExt* vp;
00073     App::DocumentObject* obj;
00074     std::vector<App::Color> current,perface;
00075     QSet<int> index;
00076 
00077     Private(ViewProviderPartExt* vp) : ui(new Ui_TaskFaceColors()), vp(vp)
00078     {
00079         obj = vp->getObject();
00080 
00081         // build up map edge->face
00082         TopTools_IndexedMapOfShape mapOfShape;
00083         TopExp_Explorer xp(static_cast<Part::Feature*>(obj)->Shape.getValue(), TopAbs_FACE);
00084         while (xp.More()) {
00085             mapOfShape.Add(xp.Current());
00086             xp.Next();
00087         }
00088 
00089         current = vp->DiffuseColor.getValues();
00090         if (current.empty())
00091             current.push_back(vp->ShapeColor.getValue());
00092         perface = current;
00093         perface.resize(mapOfShape.Extent(), perface.front());
00094     }
00095     ~Private()
00096     {
00097         delete ui;
00098     }
00099 };
00100 
00101 /* TRANSLATOR PartGui::TaskFaceColors */
00102 
00103 FaceColors::FaceColors(ViewProviderPartExt* vp, QWidget* parent)
00104   : d(new Private(vp))
00105 {
00106     d->ui->setupUi(this);
00107     d->ui->groupBox->setTitle(QString::fromUtf8(vp->getObject()->Label.getValue()));
00108     d->ui->colorButton->setDisabled(true);
00109 
00110     FaceSelection* gate = new FaceSelection(d->vp->getObject());
00111     Gui::Selection().addSelectionGate(gate);
00112 }
00113 
00114 FaceColors::~FaceColors()
00115 {
00116     Gui::Selection().rmvSelectionGate();
00117     delete d;
00118 }
00119 
00120 void FaceColors::on_defaultButton_clicked()
00121 {
00122     std::fill(d->perface.begin(), d->perface.end(), d->vp->ShapeColor.getValue());
00123     d->vp->DiffuseColor.setValues(d->perface);
00124 }
00125 
00126 void FaceColors::on_colorButton_changed()
00127 {
00128     if (!d->index.isEmpty()) {
00129         QColor c = d->ui->colorButton->color();
00130         for (QSet<int>::iterator it = d->index.begin(); it != d->index.end(); ++it) {
00131             d->perface[*it].set(c.redF(), c.greenF(), c.blueF());
00132         }
00133         d->vp->DiffuseColor.setValues(d->perface);
00134     }
00135 }
00136 
00137 void FaceColors::onSelectionChanged(const Gui::SelectionChanges& msg)
00138 {
00139     // no object selected in the combobox or no sub-element was selected
00140     if (!msg.pSubName)
00141         return;
00142     bool selection_changed = false;
00143     if (msg.Type == Gui::SelectionChanges::AddSelection) {
00144         // when adding a sub-element to the selection check
00145         // whether this is the currently handled object
00146         App::Document* doc = d->obj->getDocument();
00147         std::string docname = doc->getName();
00148         std::string objname = d->obj->getNameInDocument();
00149         if (docname==msg.pDocName && objname==msg.pObjectName) {
00150             int index = std::atoi(msg.pSubName+4)-1;
00151             d->index.insert(index);
00152             const App::Color& c = d->perface[index];
00153             QColor color;
00154             color.setRgbF(c.r,c.g,c.b);
00155             d->ui->colorButton->setColor(color);
00156             selection_changed = true;
00157         }
00158     }
00159     else if (msg.Type == Gui::SelectionChanges::RmvSelection) {
00160         App::Document* doc = d->obj->getDocument();
00161         std::string docname = doc->getName();
00162         std::string objname = d->obj->getNameInDocument();
00163         if (docname==msg.pDocName && objname==msg.pObjectName) {
00164             int index = std::atoi(msg.pSubName+4)-1;
00165             d->index.remove(index);
00166             selection_changed = true;
00167         }
00168     }
00169     else if (msg.Type == Gui::SelectionChanges::ClrSelection) {
00170         d->index.clear();
00171         selection_changed = true;
00172     }
00173 
00174     if (selection_changed) {
00175         QString faces = QString::fromAscii("[");
00176         int size = d->index.size();
00177         for (QSet<int>::iterator it = d->index.begin(); it != d->index.end(); ++it) {
00178             faces += QString::number(*it + 1);
00179             if (--size > 0)
00180                 faces += QString::fromAscii(",");
00181         }
00182         faces += QString::fromAscii("]");
00183         d->ui->labelElement->setText(faces);
00184         d->ui->colorButton->setDisabled(d->index.isEmpty());
00185     }
00186 }
00187 
00188 bool FaceColors::accept()
00189 {
00190     Gui::Document* doc = Gui::Application::Instance->getDocument(d->vp->getObject()->getDocument());
00191     doc->resetEdit();
00192     return true;
00193 }
00194 
00195 bool FaceColors::reject()
00196 {
00197     Gui::Document* doc = Gui::Application::Instance->getDocument(d->vp->getObject()->getDocument());
00198     doc->resetEdit();
00199     d->vp->DiffuseColor.setValues(d->current);
00200     return true;
00201 }
00202 
00203 void FaceColors::changeEvent(QEvent *e)
00204 {
00205     QWidget::changeEvent(e);
00206     if (e->type() == QEvent::LanguageChange) {
00207         d->ui->retranslateUi(this);
00208     }
00209 }
00210 
00211 
00212 /* TRANSLATOR PartGui::TaskFaceColors */
00213 
00214 TaskFaceColors::TaskFaceColors(ViewProviderPartExt* vp)
00215 {
00216     widget = new FaceColors(vp);
00217     taskbox = new Gui::TaskView::TaskBox(
00218         QPixmap(), widget->windowTitle(), true, 0);
00219     taskbox->groupLayout()->addWidget(widget);
00220     Content.push_back(taskbox);
00221 }
00222 
00223 TaskFaceColors::~TaskFaceColors()
00224 {
00225 }
00226 
00227 void TaskFaceColors::open()
00228 {
00229 }
00230 
00231 void TaskFaceColors::clicked(int)
00232 {
00233 }
00234 
00235 bool TaskFaceColors::accept()
00236 {
00237     return widget->accept();
00238 }
00239 
00240 bool TaskFaceColors::reject()
00241 {
00242     return widget->reject();
00243 }
00244 
00245 #include "moc_TaskFaceColors.cpp"

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