TaskAppearance.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2009 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 
00026 #ifndef _PreComp_
00027 # include <algorithm>
00028 # include <boost/bind.hpp>
00029 #endif
00030 
00031 #include "ui_TaskAppearance.h"
00032 #include "TaskAppearance.h"
00033 #include <Gui/Application.h>
00034 #include <Gui/Document.h>
00035 #include <Gui/BitmapFactory.h>
00036 #include <Gui/ViewProvider.h>
00037 #include <Gui/WaitCursor.h>
00038 #include <Base/Console.h>
00039 
00040 using namespace Gui::TaskView;
00041 
00042 /* TRANSLATOR Gui::TaskView::TaskAppearance */
00043 
00044 TaskAppearance::TaskAppearance(QWidget *parent)
00045     : TaskBox(Gui::BitmapFactory().pixmap("document-new"),tr("Appearance"),true, parent)
00046 {
00047     // we need a separate container widget to add all controls to
00048     proxy = new QWidget(this);
00049     ui = new Ui_TaskAppearance();
00050     ui->setupUi(proxy);
00051     ui->textLabel1_3->hide();
00052     ui->changePlot->hide();
00053     QMetaObject::connectSlotsByName(this);
00054 
00055     this->groupLayout()->addWidget(proxy);
00056     Gui::Selection().Attach(this);
00057 
00058     this->connectChangedObject =
00059     Gui::Application::Instance->signalChangedObject.connect(boost::bind
00060         (&TaskAppearance::slotChangedObject, this, _1, _2));
00061 }
00062 
00063 TaskAppearance::~TaskAppearance()
00064 {
00065     delete ui;
00066     this->connectChangedObject.disconnect();
00067     Gui::Selection().Detach(this);
00068 }
00069 
00070 void TaskAppearance::changeEvent(QEvent *e)
00071 {
00072     TaskBox::changeEvent(e);
00073     if (e->type() == QEvent::LanguageChange) {
00074         ui->retranslateUi(proxy);
00075     }
00076 }
00077 
00079 void TaskAppearance::OnChange(Gui::SelectionSingleton::SubjectType &rCaller,
00080                               Gui::SelectionSingleton::MessageType Reason)
00081 {
00082     if (Reason.Type == SelectionChanges::AddSelection ||
00083         Reason.Type == SelectionChanges::RmvSelection ||
00084         Reason.Type == SelectionChanges::SetSelection ||
00085         Reason.Type == SelectionChanges::ClrSelection) {
00086         std::vector<Gui::ViewProvider*> views = getSelection();
00087         setDisplayModes(views);
00088         setPointSize(views);
00089         setLineWidth(views);
00090         setTransparency(views);
00091     }
00092 }
00094 
00095 void TaskAppearance::slotChangedObject(const Gui::ViewProvider& obj,
00096                                        const App::Property& prop)
00097 {
00098     // This method gets called if a property of any view provider is changed.
00099     // We pick out all the properties for which we need to update this dialog.
00100     std::vector<Gui::ViewProvider*> Provider = getSelection();
00101     std::vector<Gui::ViewProvider*>::const_iterator vp = std::find_if
00102         (Provider.begin(), Provider.end(), 
00103         std::bind2nd(std::equal_to<Gui::ViewProvider*>(),
00104         const_cast<Gui::ViewProvider*>(&obj)));
00105     if (vp != Provider.end()) {
00106         std::string prop_name = obj.getName(&prop);
00107         if (prop.getTypeId().isDerivedFrom(App::PropertyInteger::getClassTypeId())) {
00108             long value = static_cast<const App::PropertyInteger&>(prop).getValue();
00109             if (prop_name == "Transparency") {
00110                 bool blocked = ui->spinTransparency->blockSignals(true);
00111                 ui->spinTransparency->setValue(value);
00112                 ui->spinTransparency->blockSignals(blocked);
00113                 blocked = ui->horizontalSlider->blockSignals(true);
00114                 ui->horizontalSlider->setValue(value);
00115                 ui->horizontalSlider->blockSignals(blocked);
00116             }
00117         }
00118         else if (prop.getTypeId().isDerivedFrom(App::PropertyFloat::getClassTypeId())) {
00119             float value = static_cast<const App::PropertyFloat&>(prop).getValue();
00120             if (prop_name == "PointSize") {
00121                 bool blocked = ui->spinPointSize->blockSignals(true);
00122                 ui->spinPointSize->setValue((int)value);
00123                 ui->spinPointSize->blockSignals(blocked);
00124             }
00125             else if (prop_name == "LineWidth") {
00126                 bool blocked = ui->spinLineWidth->blockSignals(true);
00127                 ui->spinLineWidth->setValue((int)value);
00128                 ui->spinLineWidth->blockSignals(blocked);
00129             }
00130         }
00131     }
00132 }
00133 
00137 void TaskAppearance::on_changeMode_activated(const QString& s)
00138 {
00139     Gui::WaitCursor wc;
00140     std::vector<Gui::ViewProvider*> Provider = getSelection();
00141     for (std::vector<Gui::ViewProvider*>::iterator It= Provider.begin();It!=Provider.end();It++) {
00142         App::Property* prop = (*It)->getPropertyByName("DisplayMode");
00143         if (prop && prop->getTypeId() == App::PropertyEnumeration::getClassTypeId()) {
00144             App::PropertyEnumeration* Display = (App::PropertyEnumeration*)prop;
00145             Display->setValue((const char*)s.toAscii());
00146         }
00147     }
00148 }
00149 
00150 void TaskAppearance::on_changePlot_activated(const QString&s)
00151 {
00152     Base::Console().Log("Plot = %s\n",(const char*)s.toAscii());
00153 }
00154 
00158 void TaskAppearance::on_spinTransparency_valueChanged(int transparency)
00159 {
00160     std::vector<Gui::ViewProvider*> Provider = getSelection();
00161     for (std::vector<Gui::ViewProvider*>::iterator It= Provider.begin();It!=Provider.end();It++) {
00162         App::Property* prop = (*It)->getPropertyByName("Transparency");
00163         if (prop && prop->getTypeId().isDerivedFrom(App::PropertyInteger::getClassTypeId())) {
00164             App::PropertyInteger* Transparency = (App::PropertyInteger*)prop;
00165             Transparency->setValue(transparency);
00166         }
00167     }
00168 }
00169 
00173 void TaskAppearance::on_spinPointSize_valueChanged(int pointsize)
00174 {
00175     std::vector<Gui::ViewProvider*> Provider = getSelection();
00176     for (std::vector<Gui::ViewProvider*>::iterator It= Provider.begin();It!=Provider.end();It++) {
00177         App::Property* prop = (*It)->getPropertyByName("PointSize");
00178         if (prop && prop->getTypeId().isDerivedFrom(App::PropertyFloat::getClassTypeId())) {
00179             App::PropertyFloat* PointSize = (App::PropertyFloat*)prop;
00180             PointSize->setValue((float)pointsize);
00181         }
00182     }
00183 }
00184 
00188 void TaskAppearance::on_spinLineWidth_valueChanged(int linewidth)
00189 {
00190     std::vector<Gui::ViewProvider*> Provider = getSelection();
00191     for (std::vector<Gui::ViewProvider*>::iterator It= Provider.begin();It!=Provider.end();It++) {
00192         App::Property* prop = (*It)->getPropertyByName("LineWidth");
00193         if (prop && prop->getTypeId().isDerivedFrom(App::PropertyFloat::getClassTypeId())) {
00194             App::PropertyFloat* LineWidth = (App::PropertyFloat*)prop;
00195             LineWidth->setValue((float)linewidth);
00196         }
00197     }
00198 }
00199 
00200 void TaskAppearance::setDisplayModes(const std::vector<Gui::ViewProvider*>& views)
00201 {
00202     QStringList commonModes, modes;
00203     for (std::vector<Gui::ViewProvider*>::const_iterator it = views.begin(); it != views.end(); ++it) {
00204         App::Property* prop = (*it)->getPropertyByName("DisplayMode");
00205         if (prop && prop->getTypeId() == App::PropertyEnumeration::getClassTypeId()) {
00206             App::PropertyEnumeration* display = static_cast<App::PropertyEnumeration*>(prop);
00207             if (!display->getEnums()) return;
00208             const std::vector<std::string>& value = display->getEnumVector();
00209             if (it == views.begin()) {
00210                 for (std::vector<std::string>::const_iterator jt = value.begin(); jt != value.end(); ++jt)
00211                     commonModes << QLatin1String(jt->c_str());
00212             }
00213             else {
00214                 for (std::vector<std::string>::const_iterator jt = value.begin(); jt != value.end(); ++jt) {
00215                     if (commonModes.contains(QLatin1String(jt->c_str())))
00216                         modes << QLatin1String(jt->c_str());
00217                 }
00218 
00219                 commonModes = modes;
00220                 modes.clear();
00221             }
00222         }
00223     }
00224 
00225     ui->changeMode->clear();
00226     ui->changeMode->addItems(commonModes);
00227     ui->changeMode->setDisabled(commonModes.isEmpty());
00228 
00229     // find the display mode to activate
00230     for (std::vector<Gui::ViewProvider*>::const_iterator it = views.begin(); it != views.end(); ++it) {
00231         App::Property* prop = (*it)->getPropertyByName("DisplayMode");
00232         if (prop && prop->getTypeId() == App::PropertyEnumeration::getClassTypeId()) {
00233             App::PropertyEnumeration* display = static_cast<App::PropertyEnumeration*>(prop);
00234             QString activeMode = QString::fromAscii(display->getValueAsString());
00235             int index = ui->changeMode->findText(activeMode);
00236             if (index != -1) {
00237                 ui->changeMode->setCurrentIndex(index);
00238                 break;
00239             }
00240         }
00241     }
00242 }
00243 
00244 void TaskAppearance::setPointSize(const std::vector<Gui::ViewProvider*>& views)
00245 {
00246     bool pointSize = false;
00247     for (std::vector<Gui::ViewProvider*>::const_iterator it = views.begin(); it != views.end(); ++it) {
00248         App::Property* prop = (*it)->getPropertyByName("PointSize");
00249         if (prop && prop->getTypeId().isDerivedFrom(App::PropertyFloat::getClassTypeId())) {
00250             bool blocked = ui->spinPointSize->blockSignals(true);
00251             ui->spinPointSize->setValue((int)static_cast<App::PropertyFloat*>(prop)->getValue());
00252             ui->spinPointSize->blockSignals(blocked);
00253             pointSize = true;
00254             break;
00255         }
00256     }
00257 
00258     ui->spinPointSize->setEnabled(pointSize);
00259 }
00260 
00261 void TaskAppearance::setLineWidth(const std::vector<Gui::ViewProvider*>& views)
00262 {
00263     bool lineWidth = false;
00264     for (std::vector<Gui::ViewProvider*>::const_iterator it = views.begin(); it != views.end(); ++it) {
00265         App::Property* prop = (*it)->getPropertyByName("LineWidth");
00266         if (prop && prop->getTypeId().isDerivedFrom(App::PropertyFloat::getClassTypeId())) {
00267             bool blocked = ui->spinLineWidth->blockSignals(true);
00268             ui->spinLineWidth->setValue((int)static_cast<App::PropertyFloat*>(prop)->getValue());
00269             ui->spinLineWidth->blockSignals(blocked);
00270             lineWidth = true;
00271             break;
00272         }
00273     }
00274 
00275     ui->spinLineWidth->setEnabled(lineWidth);
00276 }
00277 
00278 void TaskAppearance::setTransparency(const std::vector<Gui::ViewProvider*>& views)
00279 {
00280     bool transparency = false;
00281     for (std::vector<Gui::ViewProvider*>::const_iterator it = views.begin(); it != views.end(); ++it) {
00282         App::Property* prop = (*it)->getPropertyByName("Transparency");
00283         if (prop && prop->getTypeId().isDerivedFrom(App::PropertyInteger::getClassTypeId())) {
00284             bool blocked = ui->spinTransparency->blockSignals(true);
00285             ui->spinTransparency->setValue(static_cast<App::PropertyInteger*>(prop)->getValue());
00286             ui->spinTransparency->blockSignals(blocked);
00287             transparency = true;
00288             break;
00289         }
00290     }
00291 
00292     ui->spinTransparency->setEnabled(transparency);
00293     ui->horizontalSlider->setEnabled(transparency);
00294 }
00295 
00296 std::vector<Gui::ViewProvider*> TaskAppearance::getSelection() const
00297 {
00298     std::vector<Gui::ViewProvider*> views;
00299 
00300     // get the complete selection
00301     std::vector<SelectionSingleton::SelObj> sel = Selection().getCompleteSelection();
00302     for (std::vector<SelectionSingleton::SelObj>::iterator it = sel.begin(); it != sel.end(); ++it) {
00303         Gui::ViewProvider* view = Application::Instance->getDocument(it->pDoc)->getViewProvider(it->pObject);
00304         if (view) views.push_back(view);
00305     }
00306 
00307     return views;
00308 }
00309 
00310 
00311 #include "moc_TaskAppearance.cpp"

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