TaskSketcherConstrains.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 <QRegExp>
00028 # include <QString>
00029 #endif
00030 
00031 #include "ui_TaskSketcherConstrains.h"
00032 #include "TaskSketcherConstrains.h"
00033 #include "EditDatumDialog.h"
00034 #include "ViewProviderSketch.h"
00035 
00036 #include <Mod/Sketcher/App/SketchObject.h>
00037 
00038 #include <Base/Tools.h>
00039 #include <Gui/Application.h>
00040 #include <Gui/Document.h>
00041 #include <Gui/BitmapFactory.h>
00042 #include <Gui/ViewProvider.h>
00043 #include <Gui/BitmapFactory.h>
00044 #include <boost/bind.hpp>
00045 
00046 using namespace SketcherGui;
00047 using namespace Gui::TaskView;
00048 
00049 // helper class to store additional information about the listWidget entry.
00050 class ConstraintItem:public QListWidgetItem
00051 {
00052 public:
00053     ConstraintItem(const QIcon & icon, const QString & text,int ConstNbr,Sketcher::ConstraintType t):QListWidgetItem(icon,text),ConstraintNbr(ConstNbr),Type(t){}
00054     ConstraintItem(const QString & text,int ConstNbr,Sketcher::ConstraintType t):QListWidgetItem(text),ConstraintNbr(ConstNbr),Type(t){}
00055     int ConstraintNbr;
00056     Sketcher::ConstraintType Type;
00057 };
00058 
00059 TaskSketcherConstrains::TaskSketcherConstrains(ViewProviderSketch *sketchView)
00060     : TaskBox(Gui::BitmapFactory().pixmap("document-new"),tr("Constraints"),true, 0)
00061     , sketchView(sketchView)
00062 {
00063     // we need a separate container widget to add all controls to
00064     proxy = new QWidget(this);
00065     ui = new Ui_TaskSketcherConstrains();
00066     ui->setupUi(proxy);
00067     ui->listWidgetConstraints->setSelectionMode(QAbstractItemView::ExtendedSelection);
00068     //QMetaObject::connectSlotsByName(this);
00069 
00070     // connecting the needed signals
00071     QObject::connect(
00072         ui->comboBoxFilter, SIGNAL(currentIndexChanged(int)),
00073         this              , SLOT  (on_comboBoxFilter_currentIndexChanged(int))
00074        );
00075     QObject::connect(
00076         ui->listWidgetConstraints, SIGNAL(itemSelectionChanged()),
00077         this                     , SLOT  (on_listWidgetConstraints_itemSelectionChanged())
00078        );
00079     QObject::connect(
00080         ui->listWidgetConstraints, SIGNAL(itemActivated(QListWidgetItem *)),
00081         this                     , SLOT  (on_listWidgetConstraints_itemActivated(QListWidgetItem *))
00082        );
00083     //QObject::connect(
00084     //    ui->listWidgetConstraints, SIGNAL(entered(const QModelIndex &)),
00085     //    this                     , SLOT  (on_listWidgetConstraints_entered(const QModelIndex &))
00086     //   );
00087 
00088     connectionConstraintsChanged = sketchView->signalConstraintsChanged.connect(boost::bind(&SketcherGui::TaskSketcherConstrains::slotConstraintsChanged, this));
00089 
00090     this->groupLayout()->addWidget(proxy);
00091 
00092     slotConstraintsChanged();
00093 }
00094 
00095 TaskSketcherConstrains::~TaskSketcherConstrains()
00096 {
00097     connectionConstraintsChanged.disconnect();
00098     delete ui;
00099 }
00100 
00101 void TaskSketcherConstrains::onSelectionChanged(const Gui::SelectionChanges& msg)
00102 {
00103     std::string temp;
00104     if (msg.Type == Gui::SelectionChanges::ClrSelection) {
00105         ui->listWidgetConstraints->blockSignals(true);
00106         ui->listWidgetConstraints->clearSelection ();
00107         ui->listWidgetConstraints->blockSignals(false);
00108     }
00109     else if (msg.Type == Gui::SelectionChanges::AddSelection ||
00110              msg.Type == Gui::SelectionChanges::RmvSelection) {
00111         bool select = (msg.Type == Gui::SelectionChanges::AddSelection);
00112         // is it this object??
00113         if (strcmp(msg.pDocName,sketchView->getSketchObject()->getDocument()->getName())==0 &&
00114             strcmp(msg.pObjectName,sketchView->getSketchObject()->getNameInDocument())== 0) {
00115             if (msg.pSubName) {
00116                 QRegExp rx(QString::fromAscii("^Constraint(\\d+)$"));
00117                 QString expr = QString::fromAscii(msg.pSubName);
00118                 int pos = expr.indexOf(rx);
00119                 if (pos > -1) {
00120                     bool ok;
00121                     int index = rx.cap(1).toInt(&ok);
00122                     if (ok) {
00123                         int countItems = ui->listWidgetConstraints->count();
00124                         for (int i=0; i<countItems;i++) {
00125                             ConstraintItem* item = static_cast<ConstraintItem*>
00126                                 (ui->listWidgetConstraints->item(i));
00127                             if (item->ConstraintNbr == index) {
00128                                 ui->listWidgetConstraints->blockSignals(true);
00129                                 item->setSelected(select);
00130                                 ui->listWidgetConstraints->blockSignals(false);
00131                                 break;
00132                             }
00133                         }
00134                     }
00135                 }
00136             }
00137         }
00138     }
00139     else if (msg.Type == Gui::SelectionChanges::SetSelection) {
00140         // do nothing here
00141     }
00142 }
00143 
00144 void TaskSketcherConstrains::on_comboBoxFilter_currentIndexChanged(int)
00145 {
00146     slotConstraintsChanged();
00147 }
00148 
00149 void TaskSketcherConstrains::on_listWidgetConstraints_itemSelectionChanged(void)
00150 {
00151     std::string doc_name = sketchView->getSketchObject()->getDocument()->getName();
00152     std::string obj_name = sketchView->getSketchObject()->getNameInDocument();
00153 
00154     bool block = this->blockConnection(true); // avoid to be notified by itself
00155     Gui::Selection().clearSelection();
00156     QList<QListWidgetItem *> items = ui->listWidgetConstraints->selectedItems();
00157     for (QList<QListWidgetItem *>::iterator it = items.begin(); it != items.end(); ++it) {
00158         std::stringstream ss;
00159         ss << "Constraint" << static_cast<ConstraintItem*>(*it)->ConstraintNbr;
00160         Gui::Selection().addSelection(doc_name.c_str(), obj_name.c_str(), ss.str().c_str());
00161     }
00162     this->blockConnection(block);
00163 }
00164 
00165 void TaskSketcherConstrains::on_listWidgetConstraints_itemActivated(QListWidgetItem *item)
00166 {
00167     ConstraintItem *it = dynamic_cast<ConstraintItem*>(item);
00168 
00169     // if its the right constraint
00170     if (it->Type == Sketcher::Distance ||
00171         it->Type == Sketcher::DistanceX || it->Type == Sketcher::DistanceY ||
00172         it->Type == Sketcher::Radius || it->Type == Sketcher::Angle) {
00173 
00174         EditDatumDialog *editDatumDialog = new EditDatumDialog(this->sketchView, it->ConstraintNbr);
00175         editDatumDialog->exec(false);
00176         delete editDatumDialog;
00177     }
00178 }
00179 
00180 //void TaskSketcherConstrains::on_listWidgetConstraints_entered(const QModelIndex &index)
00181 //{
00182 //    index;
00183 //}
00184 
00185 
00186 void TaskSketcherConstrains::slotConstraintsChanged(void)
00187 {
00188     QIcon hdist( Gui::BitmapFactory().pixmap("Constraint_HorizontalDistance") );
00189     QIcon vdist( Gui::BitmapFactory().pixmap("Constraint_VerticalDistance") );
00190     QIcon horiz( Gui::BitmapFactory().pixmap("Constraint_Horizontal") );
00191     QIcon vert ( Gui::BitmapFactory().pixmap("Constraint_Vertical") );
00192     QIcon lock ( Gui::BitmapFactory().pixmap("Sketcher_ConstrainLock") );
00193     QIcon coinc( Gui::BitmapFactory().pixmap("Constraint_PointOnPoint") );
00194     QIcon para ( Gui::BitmapFactory().pixmap("Constraint_Parallel") );
00195     QIcon perp ( Gui::BitmapFactory().pixmap("Constraint_Perpendicular") );
00196     QIcon tang ( Gui::BitmapFactory().pixmap("Constraint_Tangent") );
00197     QIcon dist ( Gui::BitmapFactory().pixmap("Constraint_Length") );
00198     QIcon radi ( Gui::BitmapFactory().pixmap("Constraint_Radius") );
00199     QIcon angl ( Gui::BitmapFactory().pixmap("Constraint_InternalAngle") );
00200     QIcon equal( Gui::BitmapFactory().pixmap("Constraint_EqualLength") );
00201     QIcon pntoo( Gui::BitmapFactory().pixmap("Constraint_PointOnObject") );
00202     QIcon symm ( Gui::BitmapFactory().pixmap("Constraint_Symmetric") );
00203 
00204     assert(sketchView);
00205     // Build up ListView with the constraints
00206     const std::vector< Sketcher::Constraint * > &vals = sketchView->getSketchObject()->Constraints.getValues();
00207 
00208     ui->listWidgetConstraints->clear();
00209     QString name;
00210 
00211     int Filter = ui->comboBoxFilter->currentIndex();
00212 
00213     int i=1;
00214     for(std::vector< Sketcher::Constraint * >::const_iterator it= vals.begin();it!=vals.end();++it,++i){
00215         if ((*it)->Name == "")
00216             name = QString::fromLatin1("Constraint%1").arg(i);
00217         else
00218             name = QString::fromLatin1((*it)->Name.c_str());
00219 
00220         switch((*it)->Type){
00221             case Sketcher::Horizontal:
00222                 if(Filter<2 || (*it)->Name != "")
00223                     ui->listWidgetConstraints->addItem(new ConstraintItem(horiz,name,i-1,(*it)->Type));
00224                 break;
00225             case Sketcher::Vertical:
00226                 if(Filter<2 || (*it)->Name != "")
00227                     ui->listWidgetConstraints->addItem(new ConstraintItem(vert,name,i-1,(*it)->Type));
00228                 break;
00229             case Sketcher::Coincident:
00230                 if(Filter<1 || (*it)->Name != "")
00231                     ui->listWidgetConstraints->addItem(new ConstraintItem(coinc,name,i-1,(*it)->Type));
00232                 break;
00233             case Sketcher::PointOnObject:
00234                 if(Filter<2 || (*it)->Name != "")
00235                 ui->listWidgetConstraints->addItem(new ConstraintItem(pntoo,name,i-1,(*it)->Type));
00236                 break;
00237             case Sketcher::Parallel:
00238                 if(Filter<2 || (*it)->Name != "")
00239                     ui->listWidgetConstraints->addItem(new ConstraintItem(para,name,i-1,(*it)->Type));
00240                 break;
00241             case Sketcher::Perpendicular:
00242                 if(Filter<2 || (*it)->Name != "")
00243                     ui->listWidgetConstraints->addItem(new ConstraintItem(perp,name,i-1,(*it)->Type));
00244                 break;
00245             case Sketcher::Tangent:
00246                 if(Filter<2 || (*it)->Name != "")
00247                     ui->listWidgetConstraints->addItem(new ConstraintItem(tang,name,i-1,(*it)->Type));
00248                 break;
00249             case Sketcher::Equal:
00250                 if(Filter<2 || (*it)->Name != "")
00251                     ui->listWidgetConstraints->addItem(new ConstraintItem(equal,name,i-1,(*it)->Type));
00252                 break;
00253             case Sketcher::Symmetric:
00254                 if(Filter<2 || (*it)->Name != "")
00255                     ui->listWidgetConstraints->addItem(new ConstraintItem(symm,name,i-1,(*it)->Type));
00256                 break;
00257             case Sketcher::Distance:
00258                 if(Filter<3 || (*it)->Name != ""){
00259                     name = QString::fromLatin1("%1 (%2)").arg(name).arg((*it)->Value);
00260                     ui->listWidgetConstraints->addItem(new ConstraintItem(dist,name,i-1,(*it)->Type));
00261                 }
00262                 break;
00263             case Sketcher::DistanceX:
00264                 if(Filter<3 || (*it)->Name != ""){
00265                     name = QString::fromLatin1("%1 (%2)").arg(name).arg(std::abs((*it)->Value));
00266                     ui->listWidgetConstraints->addItem(new ConstraintItem(hdist,name,i-1,(*it)->Type));
00267                 }
00268                 break;
00269             case Sketcher::DistanceY:
00270                 if(Filter<3 || (*it)->Name != ""){
00271                     name = QString::fromLatin1("%1 (%2)").arg(name).arg(std::abs((*it)->Value));
00272                     ui->listWidgetConstraints->addItem(new ConstraintItem(vdist,name,i-1,(*it)->Type));
00273                 }
00274                 break;
00275             case Sketcher::Radius:
00276                 if(Filter<3 || (*it)->Name != ""){
00277                     name = QString::fromLatin1("%1 (%2)").arg(name).arg((*it)->Value);
00278                     ui->listWidgetConstraints->addItem(new ConstraintItem(radi,name,i-1,(*it)->Type));
00279                 }
00280                 break;
00281             case Sketcher::Angle:
00282                 if(Filter<3 || (*it)->Name != ""){
00283                     name = QString::fromLatin1("%1 (%2)").arg(name).arg(Base::toDegrees<double>(std::abs((*it)->Value)));
00284                     ui->listWidgetConstraints->addItem(new ConstraintItem(angl,name,i-1,(*it)->Type));
00285                 }
00286                 break;
00287             default:
00288                 ui->listWidgetConstraints->addItem(new ConstraintItem(name,i-1,(*it)->Type));
00289                 break;
00290         }
00291 
00292     }
00293 
00294 }
00295 
00296 void TaskSketcherConstrains::changeEvent(QEvent *e)
00297 {
00298     TaskBox::changeEvent(e);
00299     if (e->type() == QEvent::LanguageChange) {
00300         ui->retranslateUi(proxy);
00301     }
00302 }
00303 
00304 
00305 
00306 #include "moc_TaskSketcherConstrains.cpp"

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