InputVector.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 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 #ifndef GUI_INPUTVECTOR_H
00024 #define GUI_INPUTVECTOR_H
00025 
00026 #include <cfloat>
00027 #include <QDialog>
00028 #include <QMessageBox>
00029 #include <QApplication> 
00030 
00031 #include <Gui/propertyeditor/PropertyItem.h>
00032 
00033 namespace Gui {
00034 
00039 class GuiExport LocationDialog : public QDialog
00040 {
00041     Q_OBJECT
00042 
00043 protected:
00044     LocationDialog(QWidget* parent = 0, Qt::WFlags fl = 0);
00045     virtual ~LocationDialog();
00046 
00047 protected:
00048     virtual void changeEvent(QEvent *e) = 0;
00049 
00050 private Q_SLOTS:
00051     void on_direction_activated(int);
00052 
00053 public:
00054     virtual Base::Vector3f getDirection() const = 0;
00055     Base::Vector3f getUserDirection(bool* ok=0) const;
00056 
00057 private:
00058     virtual void directionActivated(int) = 0;
00059 };
00060 
00061 /* TRANSLATOR Gui::LocationDialog */
00062 
00073 template <class Ui>
00074 class LocationInterface : public LocationDialog, public Ui
00075 {
00076 public:
00077     LocationInterface(QWidget* parent = 0, Qt::WFlags fl = 0)  : LocationDialog(parent, fl)
00078     {
00079         this->setupUi(this);
00080         this->retranslate();
00081     }
00082     virtual ~LocationInterface(){}
00083 
00084     void retranslate()
00085     {
00086         Ui::retranslateUi(this);
00087 
00088         if (this->direction->count() == 0) {
00089             this->direction->insertItems(0, QStringList()
00090              << QApplication::translate("Gui::LocationDialog", "X", 0, QApplication::UnicodeUTF8)
00091              << QApplication::translate("Gui::LocationDialog", "Y", 0, QApplication::UnicodeUTF8)
00092              << QApplication::translate("Gui::LocationDialog", "Z", 0, QApplication::UnicodeUTF8)
00093              << QApplication::translate("Gui::LocationDialog", "User defined...", 0, 
00094                 QApplication::UnicodeUTF8)
00095             );
00096 
00097             this->direction->setCurrentIndex(2);
00098 
00099             // Vector3f declared to use with QVariant see Gui/propertyeditor/PropertyItem.h
00100             this->direction->setItemData(0, QVariant::fromValue<Base::Vector3f>(Base::Vector3f(1,0,0)));
00101             this->direction->setItemData(1, QVariant::fromValue<Base::Vector3f>(Base::Vector3f(0,1,0)));
00102             this->direction->setItemData(2, QVariant::fromValue<Base::Vector3f>(Base::Vector3f(0,0,1)));
00103         }
00104         else {
00105             this->direction->setItemText(0, QApplication::translate("Gui::LocationDialog", "X", 0,
00106                 QApplication::UnicodeUTF8));
00107             this->direction->setItemText(1, QApplication::translate("Gui::LocationDialog", "Y", 0,
00108                 QApplication::UnicodeUTF8));
00109             this->direction->setItemText(2, QApplication::translate("Gui::LocationDialog", "Z", 0,
00110                 QApplication::UnicodeUTF8));
00111             this->direction->setItemText(this->direction->count()-1,
00112                 QApplication::translate("Gui::LocationDialog", "User defined...", 0,
00113                 QApplication::UnicodeUTF8));
00114         }
00115     }
00116 
00117     Base::Vector3f getPosition() const
00118     {
00119         return Base::Vector3f((float)this->xPos->value(),
00120                               (float)this->yPos->value(),
00121                               (float)this->zPos->value());
00122     }
00123 
00124     Base::Vector3f getDirection() const
00125     {
00126         QVariant data = this->direction->itemData (this->direction->currentIndex());
00127         if (data.canConvert<Base::Vector3f>()) {
00128             return data.value<Base::Vector3f>();
00129         }
00130         else {
00131             return Base::Vector3f(0,0,1);
00132         }
00133     }
00134 
00135 protected:
00136     void changeEvent(QEvent *e)
00137     {
00138         if (e->type() == QEvent::LanguageChange) {
00139             this->retranslate();
00140         }
00141         else {
00142             QDialog::changeEvent(e);
00143         }
00144     }
00145 
00146 private:
00147     void directionActivated(int index)
00148     {
00149         // last item is selected to define direction by user
00150         if (index+1 == this->direction->count()) {
00151             bool ok;
00152             Base::Vector3f dir = this->getUserDirection(&ok);
00153             if (ok) {
00154                 if (dir.Length() < FLT_EPSILON) {
00155                     QMessageBox::critical(this, LocationDialog::tr("Wrong direction"),
00156                         LocationDialog::tr("Direction must not be the null vector"));
00157                     return;
00158                 }
00159 
00160                 // check if the user-defined direction is already there
00161                 for (int i=0; i<this->direction->count()-1; i++) {
00162                     QVariant data = this->direction->itemData (i);
00163                     if (data.canConvert<Base::Vector3f>()) {
00164                         const Base::Vector3f val = data.value<Base::Vector3f>();
00165                         if (val == dir) {
00166                             this->direction->setCurrentIndex(i);
00167                             return;
00168                         }
00169                     }
00170                 }
00171 
00172                 // add a new item before the very last item
00173                 QString display = QString::fromAscii("(%1,%2,%3)")
00174                     .arg(dir.x)
00175                     .arg(dir.y)
00176                     .arg(dir.z);
00177                 this->direction->insertItem(this->direction->count()-1, display,
00178                     QVariant::fromValue<Base::Vector3f>(dir));
00179                 this->direction->setCurrentIndex(this->direction->count()-2);
00180             }
00181         }
00182     }
00183 };
00184 
00193 template <class Ui>
00194 class LocationInterfaceComp : public Ui
00195 {
00196 public:
00197     LocationInterfaceComp(QDialog *dlg)
00198     {
00199         this->setupUi(dlg);
00200         this->retranslate(dlg);
00201     }
00202     ~LocationInterfaceComp()
00203     {
00204     }
00205 
00206     void retranslate(QDialog *dlg)
00207     {
00208         Ui::retranslateUi(dlg);
00209 
00210         if (this->direction->count() == 0) {
00211             this->direction->insertItems(0, QStringList()
00212              << QApplication::translate("Gui::LocationDialog", "X", 0, QApplication::UnicodeUTF8)
00213              << QApplication::translate("Gui::LocationDialog", "Y", 0, QApplication::UnicodeUTF8)
00214              << QApplication::translate("Gui::LocationDialog", "Z", 0, QApplication::UnicodeUTF8)
00215              << QApplication::translate("Gui::LocationDialog", "User defined...", 0, 
00216                 QApplication::UnicodeUTF8)
00217             );
00218 
00219             this->direction->setCurrentIndex(2);
00220 
00221             // Vector3f declared to use with QVariant see Gui/propertyeditor/PropertyItem.h
00222             this->direction->setItemData(0, QVariant::fromValue<Base::Vector3f>(Base::Vector3f(1,0,0)));
00223             this->direction->setItemData(1, QVariant::fromValue<Base::Vector3f>(Base::Vector3f(0,1,0)));
00224             this->direction->setItemData(2, QVariant::fromValue<Base::Vector3f>(Base::Vector3f(0,0,1)));
00225         }
00226         else {
00227             this->direction->setItemText(0, QApplication::translate("Gui::LocationDialog", "X", 0,
00228                 QApplication::UnicodeUTF8));
00229             this->direction->setItemText(1, QApplication::translate("Gui::LocationDialog", "Y", 0,
00230                 QApplication::UnicodeUTF8));
00231             this->direction->setItemText(2, QApplication::translate("Gui::LocationDialog", "Z", 0,
00232                 QApplication::UnicodeUTF8));
00233             this->direction->setItemText(this->direction->count()-1,
00234                 QApplication::translate("Gui::LocationDialog", "User defined...", 0,
00235                 QApplication::UnicodeUTF8));
00236         }
00237     }
00238 
00239     Base::Vector3f getPosition() const
00240     {
00241         return Base::Vector3f((float)this->xPos->value(),
00242                               (float)this->yPos->value(),
00243                               (float)this->zPos->value());
00244     }
00245 
00246     Base::Vector3f getDirection() const
00247     {
00248         QVariant data = this->direction->itemData (this->direction->currentIndex());
00249         if (data.canConvert<Base::Vector3f>()) {
00250             return data.value<Base::Vector3f>();
00251         }
00252         else {
00253             return Base::Vector3f(0,0,1);
00254         }
00255     }
00256 
00257 public:
00258     void setDirection(const Base::Vector3f& dir)
00259     {
00260         if (dir.Length() < FLT_EPSILON) {
00261             return;
00262         }
00263 
00264         // check if the user-defined direction is already there
00265         for (int i=0; i<this->direction->count()-1; i++) {
00266             QVariant data = this->direction->itemData (i);
00267             if (data.canConvert<Base::Vector3f>()) {
00268                 const Base::Vector3f val = data.value<Base::Vector3f>();
00269                 if (val == dir) {
00270                     this->direction->setCurrentIndex(i);
00271                     return;
00272                 }
00273             }
00274         }
00275 
00276         // add a new item before the very last item
00277         QString display = QString::fromAscii("(%1,%2,%3)")
00278             .arg(dir.x)
00279             .arg(dir.y)
00280             .arg(dir.z);
00281         this->direction->insertItem(this->direction->count()-1, display,
00282             QVariant::fromValue<Base::Vector3f>(dir));
00283         this->direction->setCurrentIndex(this->direction->count()-2);
00284     }
00285     bool directionActivated(LocationDialog* dlg, int index)
00286     {
00287         // last item is selected to define direction by user
00288         if (index+1 == this->direction->count()) {
00289             bool ok;
00290             Base::Vector3f dir = dlg->getUserDirection(&ok);
00291             if (ok) {
00292                 if (dir.Length() < FLT_EPSILON) {
00293                     QMessageBox::critical(dlg, LocationDialog::tr("Wrong direction"),
00294                         LocationDialog::tr("Direction must not be the null vector"));
00295                     return false;
00296                 }
00297                 setDirection(dir);
00298             }
00299         }
00300         return true;
00301     }
00302 };
00303 
00312 template <class Ui>
00313 class LocationDialogComp : public LocationDialog
00314 {
00315 public:
00316     LocationDialogComp(QWidget* parent = 0, Qt::WFlags fl = 0)
00317       : LocationDialog(parent, fl), ui(this)
00318     {
00319     }
00320     virtual ~LocationDialogComp()
00321     {
00322         // no need to delete child widgets, Qt does it all for us
00323     }
00324 
00325     Base::Vector3f getDirection() const
00326     {
00327         return ui.getDirection();
00328     }
00329 
00330 protected:
00331     void changeEvent(QEvent *e)
00332     {
00333         if (e->type() == QEvent::LanguageChange) {
00334             ui.retranslate(this);
00335         }
00336         else {
00337             QDialog::changeEvent(e);
00338         }
00339     }
00340 
00341 private:
00342     void directionActivated(int index)
00343     {
00344         ui.directionActivated(this,index);
00345     }
00346 
00347 protected:
00348     LocationInterfaceComp<Ui> ui;
00349 };
00350 
00351 } // namespace Gui
00352 
00353 #endif // GUI_INPUTVECTOR_H

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