From db0ad3a49c2ecaf07ce19d80ff2b6d17d145763c Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 30 Dec 2011 14:30:52 +0000 Subject: [PATCH] + add new custom widget git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5364 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d --- src/Gui/InputVector.cpp | 171 +++++++++++++++++++++ src/Gui/InputVector.h | 38 +++++ src/Tools/plugins/widget/customwidgets.cpp | 63 ++++++++ src/Tools/plugins/widget/customwidgets.h | 29 ++++ src/Tools/plugins/widget/plugin.cpp | 42 +++++ 5 files changed, 343 insertions(+) diff --git a/src/Gui/InputVector.cpp b/src/Gui/InputVector.cpp index 4d1d2a6e8..25757fdaa 100644 --- a/src/Gui/InputVector.cpp +++ b/src/Gui/InputVector.cpp @@ -28,6 +28,177 @@ using namespace Gui; +LocationWidget::LocationWidget (QWidget * parent) + : QWidget(parent) +{ + box = new QGridLayout(); + + xValue = new QDoubleSpinBox(this); + xValue->setMinimum(-2.14748e+09); + xValue->setMaximum(2.14748e+09); + xLabel = new QLabel(this); + box->addWidget(xLabel, 0, 0, 1, 1); + box->addWidget(xValue, 0, 1, 1, 1); + + yValue = new QDoubleSpinBox(this); + yValue->setMinimum(-2.14748e+09); + yValue->setMaximum(2.14748e+09); + yLabel = new QLabel(this); + box->addWidget(yLabel, 1, 0, 1, 1); + box->addWidget(yValue, 1, 1, 1, 1); + + zValue = new QDoubleSpinBox(this); + zValue->setMinimum(-2.14748e+09); + zValue->setMaximum(2.14748e+09); + zLabel = new QLabel(this); + box->addWidget(zLabel, 2, 0, 1, 1); + box->addWidget(zValue, 2, 1, 1, 1); + + dLabel = new QLabel(this); + dValue = new QComboBox(this); + dValue->setCurrentIndex(-1); + box->addWidget(dLabel, 3, 0, 1, 1); + box->addWidget(dValue, 3, 1, 1, 1); + + QGridLayout* gridLayout = new QGridLayout(this); + gridLayout->addLayout(box, 0, 0, 1, 2); + + retranslateUi(); +} + +LocationWidget::~LocationWidget() +{ +} + +QSize LocationWidget::sizeHint() const +{ + return QSize(150,190); +} + +void LocationWidget::changeEvent(QEvent* e) +{ + if (e->type() == QEvent::LanguageChange) { + this->retranslateUi(); + } + QWidget::changeEvent(e); +} + +void LocationWidget::retranslateUi() +{ + xLabel->setText(QApplication::translate("Gui::LocationWidget", "X:")); + yLabel->setText(QApplication::translate("Gui::LocationWidget", "Y:")); + zLabel->setText(QApplication::translate("Gui::LocationWidget", "Z:")); + dLabel->setText(QApplication::translate("Gui::LocationWidget", "Direction:")); + + if (dValue->count() == 0) { + dValue->insertItems(0, QStringList() + << QApplication::translate("Gui::LocationDialog", "X") + << QApplication::translate("Gui::LocationDialog", "Y") + << QApplication::translate("Gui::LocationDialog", "Z") + << QApplication::translate("Gui::LocationDialog", "User defined...") + ); + + dValue->setCurrentIndex(2); + + // Vector3f declared to use with QVariant see Gui/propertyeditor/PropertyItem.h + dValue->setItemData(0, QVariant::fromValue(Base::Vector3f(1,0,0))); + dValue->setItemData(1, QVariant::fromValue(Base::Vector3f(0,1,0))); + dValue->setItemData(2, QVariant::fromValue(Base::Vector3f(0,0,1))); + } + else { + dValue->setItemText(0, QApplication::translate("Gui::LocationDialog", "X")); + dValue->setItemText(1, QApplication::translate("Gui::LocationDialog", "Y")); + dValue->setItemText(2, QApplication::translate("Gui::LocationDialog", "Z")); + dValue->setItemText(dValue->count()-1, + QApplication::translate("Gui::LocationDialog", "User defined...")); + } +} + +Base::Vector3f LocationWidget::getPosition() const +{ + return Base::Vector3f((float)this->xValue->value(), + (float)this->yValue->value(), + (float)this->zValue->value()); +} + +void LocationWidget::setDirection(const Base::Vector3f& dir) +{ + if (dir.Length() < FLT_EPSILON) { + return; + } + + // check if the user-defined direction is already there + for (int i=0; icount()-1; i++) { + QVariant data = dValue->itemData (i); + if (data.canConvert()) { + const Base::Vector3f val = data.value(); + if (val == dir) { + dValue->setCurrentIndex(i); + return; + } + } + } + + // add a new item before the very last item + QString display = QString::fromAscii("(%1,%2,%3)") + .arg(dir.x) + .arg(dir.y) + .arg(dir.z); + dValue->insertItem(dValue->count()-1, display, + QVariant::fromValue(dir)); + dValue->setCurrentIndex(dValue->count()-2); +} + +Base::Vector3f LocationWidget::getDirection() const +{ + QVariant data = dValue->itemData (this->dValue->currentIndex()); + if (data.canConvert()) { + return data.value(); + } + else { + return Base::Vector3f(0,0,1); + } +} + +Base::Vector3f LocationWidget::getUserDirection(bool* ok) const +{ + Gui::Dialog::Ui_InputVector iv; + QDialog dlg(const_cast(this)); + iv.setupUi(&dlg); + Base::Vector3f dir; + if (dlg.exec()) { + dir.x = (float)iv.vectorX->value(); + dir.y = (float)iv.vectorY->value(); + dir.z = (float)iv.vectorZ->value(); + if (ok) *ok = true; + } + else { + if (ok) *ok = false; + } + + return dir; +} + +void LocationWidget::on_direction_activated(int index) +{ + // last item is selected to define direction by user + if (index+1 == dValue->count()) { + bool ok; + Base::Vector3f dir = this->getUserDirection(&ok); + if (ok) { + if (dir.Length() < FLT_EPSILON) { + QMessageBox::critical(this, LocationDialog::tr("Wrong direction"), + LocationDialog::tr("Direction must not be the null vector")); + return; + } + + setDirection(dir); + } + } +} + +// ---------------------------------------------------------------------------- + LocationDialog::LocationDialog(QWidget* parent, Qt::WFlags fl) : QDialog(parent, fl) { diff --git a/src/Gui/InputVector.h b/src/Gui/InputVector.h index 4508e1986..852ad2394 100644 --- a/src/Gui/InputVector.h +++ b/src/Gui/InputVector.h @@ -30,8 +30,46 @@ #include +class QGridLayout; +class QLabel; +class QDoubleSpinBox; +class QComboBox; + namespace Gui { +class LocationWidget : public QWidget +{ + Q_OBJECT + +public: + LocationWidget (QWidget * parent = 0); + virtual ~LocationWidget(); + QSize sizeHint() const; + + Base::Vector3f getPosition() const; + void setDirection(const Base::Vector3f& dir); + Base::Vector3f getDirection() const; + Base::Vector3f getUserDirection(bool* ok=0) const; + +private Q_SLOTS: + void on_direction_activated(int); + +private: + void changeEvent(QEvent*); + void retranslateUi(); + +private: + QGridLayout *box; + QLabel *xLabel; + QLabel *yLabel; + QLabel *zLabel; + QLabel *dLabel; + QDoubleSpinBox *xValue; + QDoubleSpinBox *yValue; + QDoubleSpinBox *zValue; + QComboBox *dValue; +}; + /** This is the base dialog class that defines the interface for * specifying a direction vector by the user. * @author Werner Mayer diff --git a/src/Tools/plugins/widget/customwidgets.cpp b/src/Tools/plugins/widget/customwidgets.cpp index 321fec30e..d46bd0541 100644 --- a/src/Tools/plugins/widget/customwidgets.cpp +++ b/src/Tools/plugins/widget/customwidgets.cpp @@ -71,6 +71,69 @@ void UrlLabel::setUrl(const QString& u) setToolTip(this->_url); } +LocationWidget::LocationWidget (QWidget * parent) + : QWidget(parent) +{ + box = new QGridLayout(); + + xValue = new QDoubleSpinBox(this); + xValue->setMinimum(-2.14748e+09); + xValue->setMaximum(2.14748e+09); + xLabel = new QLabel(this); + box->addWidget(xLabel, 0, 0, 1, 1); + box->addWidget(xValue, 0, 1, 1, 1); + + yValue = new QDoubleSpinBox(this); + yValue->setMinimum(-2.14748e+09); + yValue->setMaximum(2.14748e+09); + yLabel = new QLabel(this); + box->addWidget(yLabel, 1, 0, 1, 1); + box->addWidget(yValue, 1, 1, 1, 1); + + zValue = new QDoubleSpinBox(this); + zValue->setMinimum(-2.14748e+09); + zValue->setMaximum(2.14748e+09); + zLabel = new QLabel(this); + box->addWidget(zLabel, 2, 0, 1, 1); + box->addWidget(zValue, 2, 1, 1, 1); + + dLabel = new QLabel(this); + dValue = new QComboBox(this); + dValue->setCurrentIndex(-1); + box->addWidget(dLabel, 3, 0, 1, 1); + box->addWidget(dValue, 3, 1, 1, 1); + + QGridLayout* gridLayout = new QGridLayout(this); + gridLayout->addLayout(box, 0, 0, 1, 2); + + retranslateUi(); +} + +LocationWidget::~LocationWidget() +{ +} + +QSize LocationWidget::sizeHint() const +{ + return QSize(150,190); +} + +void LocationWidget::changeEvent(QEvent* e) +{ + if (e->type() == QEvent::LanguageChange) { + this->retranslateUi(); + } + QWidget::changeEvent(e); +} + +void LocationWidget::retranslateUi() +{ + xLabel->setText(QApplication::translate("Gui::LocationWidget", "X:")); + yLabel->setText(QApplication::translate("Gui::LocationWidget", "Y:")); + zLabel->setText(QApplication::translate("Gui::LocationWidget", "Z:")); + dLabel->setText(QApplication::translate("Gui::LocationWidget", "Direction:")); +} + FileChooser::FileChooser( QWidget *parent ) : QWidget( parent ), md( File ), _filter( QString::null ) { diff --git a/src/Tools/plugins/widget/customwidgets.h b/src/Tools/plugins/widget/customwidgets.h index cd22ccadd..2543a119e 100644 --- a/src/Tools/plugins/widget/customwidgets.h +++ b/src/Tools/plugins/widget/customwidgets.h @@ -35,6 +35,8 @@ #include #include #include +#include +#include namespace Gui { @@ -62,6 +64,33 @@ private: QString _url; }; +class LocationWidget : public QWidget +{ + Q_OBJECT + +public: + LocationWidget (QWidget * parent = 0); + virtual ~LocationWidget(); + QSize sizeHint() const; + +public Q_SLOTS: + +private: + void changeEvent(QEvent*); + void retranslateUi(); + +private: + QGridLayout *box; + QLabel *xLabel; + QLabel *yLabel; + QLabel *zLabel; + QLabel *dLabel; + QDoubleSpinBox *xValue; + QDoubleSpinBox *yValue; + QDoubleSpinBox *zValue; + QComboBox *dValue; +}; + /** * There is a bug in QtDesigner of Qt version 4.0, 4.1 and 4.2. If a class declaration * is inside a namespace and it uses the Q_ENUMS macro then QtDesigner doesn't handle diff --git a/src/Tools/plugins/widget/plugin.cpp b/src/Tools/plugins/widget/plugin.cpp index f23c057e6..d0a2aca92 100644 --- a/src/Tools/plugins/widget/plugin.cpp +++ b/src/Tools/plugins/widget/plugin.cpp @@ -108,6 +108,47 @@ public: } }; +class LocationWidgetPlugin : public QDesignerCustomWidgetInterface +{ + Q_INTERFACES(QDesignerCustomWidgetInterface) +public: + LocationWidgetPlugin() + { + } + QWidget *createWidget(QWidget *parent) + { + return new Gui::LocationWidget(parent); + } + QString group() const + { + return QLatin1String("Display Widgets"); + } + QIcon icon() const + { + return QIcon( QPixmap( urllabel_pixmap ) ); + } + QString includeFile() const + { + return QLatin1String("Gui/InputVector.h"); + } + QString toolTip() const + { + return QLatin1String("Location"); + } + QString whatsThis() const + { + return QLatin1String("A widget to define a location."); + } + bool isContainer() const + { + return false; + } + QString name() const + { + return QLatin1String("Gui::LocationWidget"); + } +}; + static const char *filechooser_pixmap[] = { "22 22 8 1", " c Gray100", @@ -1017,6 +1058,7 @@ QList CustomWidgetPlugin::customWidgets () con { QList cw; cw.append(new UrlLabelPlugin); + cw.append(new LocationWidgetPlugin); cw.append(new FileChooserPlugin); cw.append(new AccelLineEditPlugin); cw.append(new CommandIconViewPlugin);