From b00c51f919a1de6a50403a7824a9d71d26c69993 Mon Sep 17 00:00:00 2001 From: nds Date: Thu, 12 Nov 2015 13:19:01 +0300 Subject: [PATCH] Table template --- src/ModuleBase/CMakeLists.txt | 4 + src/ModuleBase/ModuleBase_TableModel.cpp | 110 ++++++++++++++++++++++ src/ModuleBase/ModuleBase_TableModel.h | 61 ++++++++++++ src/ModuleBase/ModuleBase_WidgetTable.cpp | 78 +++++++++++++++ src/ModuleBase/ModuleBase_WidgetTable.h | 58 ++++++++++++ src/PartSet/PartSet_Module.cpp | 6 +- src/SketchPlugin/SketchPlugin_Point.cpp | 3 + src/SketchPlugin/SketchPlugin_Point.h | 7 ++ src/SketchPlugin/plugin-Sketch.xml | 1 + 9 files changed, 327 insertions(+), 1 deletion(-) create mode 100755 src/ModuleBase/ModuleBase_TableModel.cpp create mode 100755 src/ModuleBase/ModuleBase_TableModel.h create mode 100755 src/ModuleBase/ModuleBase_WidgetTable.cpp create mode 100755 src/ModuleBase/ModuleBase_WidgetTable.h diff --git a/src/ModuleBase/CMakeLists.txt b/src/ModuleBase/CMakeLists.txt index 0969deb10..2467dcd98 100644 --- a/src/ModuleBase/CMakeLists.txt +++ b/src/ModuleBase/CMakeLists.txt @@ -33,6 +33,7 @@ SET(PROJECT_HEADERS ModuleBase_Preferences.h ModuleBase_ResultPrs.h ModuleBase_SelectionValidator.h + ModuleBase_TableModel.h ModuleBase_ToolBox.h ModuleBase_Tools.h ModuleBase_ViewerFilters.h @@ -52,6 +53,7 @@ SET(PROJECT_HEADERS ModuleBase_WidgetShapeSelector.h ModuleBase_WidgetSwitch.h ModuleBase_WidgetToolbox.h + ModuleBase_WidgetTable.h ModuleBase_WidgetValidated.h ModuleBase_IconFactory.h ModuleBase_WidgetErrorLabel.h @@ -84,6 +86,7 @@ SET(PROJECT_SOURCES ModuleBase_ParamSpinBox.cpp ModuleBase_Preferences.cpp ModuleBase_ResultPrs.cpp + ModuleBase_TableModel.cpp ModuleBase_ToolBox.cpp ModuleBase_Tools.cpp ModuleBase_ViewerFilters.cpp @@ -103,6 +106,7 @@ SET(PROJECT_SOURCES ModuleBase_WidgetShapeSelector.cpp ModuleBase_WidgetSwitch.cpp ModuleBase_WidgetToolbox.cpp + ModuleBase_WidgetTable.cpp ModuleBase_WidgetValidated.cpp ModuleBase_IconFactory.cpp ModuleBase_WidgetErrorLabel.cpp diff --git a/src/ModuleBase/ModuleBase_TableModel.cpp b/src/ModuleBase/ModuleBase_TableModel.cpp new file mode 100755 index 000000000..9f66fd407 --- /dev/null +++ b/src/ModuleBase/ModuleBase_TableModel.cpp @@ -0,0 +1,110 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: ModuleBase_TableModel.cxx +// Author: Natalia ERMOLAEVA +// +#include "ModuleBase_TableModel.h" + +ModuleBase_TableModel::ModuleBase_TableModel(QObject* theParent) +: QAbstractTableModel(theParent) +{ +} + +void ModuleBase_TableModel::addRow(const QStringList& theValue) +{ + if (theValue.size() != columnCount()) + return; + + int aRowId = rowCount(); + for (int i = 0, aCols = columnCount(); i < aCols; i++) + myValues.append(Value(aRowId, i, theValue[i].toDouble())); + + emit layoutChanged(); +} + +void ModuleBase_TableModel::addRow() +{ + return; + int aRowId = rowCount(); + for (int i = 0, aCols = columnCount(); i < aCols; i++) + myValues.append(Value(aRowId, i, 0.0)); + + emit layoutChanged(); +} + +QVariant ModuleBase_TableModel::headerData(int theSection, Qt::Orientation theOrientation, + int theRole) const +{ + if (theSection < myColumnHeaders.size() && theRole == Qt::DisplayRole) + return myColumnHeaders[theSection]; + + return QAbstractTableModel::headerData(theSection, theOrientation, theRole); +} + +QModelIndex ModuleBase_TableModel::index( int theRow, int theColumn, + const QModelIndex &theParent) const +{ + if (!hasIndex(theRow, theColumn, theParent)) + return QModelIndex(); + + return createIndex(theRow, theColumn); +} + +QVariant ModuleBase_TableModel::data( const QModelIndex& theIndex, int theRole) const +{ + QVariant aValue; + if (theRole == Qt::DisplayRole) { + QList::const_iterator anIt = myValues.begin(), aLast = myValues.end(); + for (; anIt != aLast; anIt++) { + Value aVal = *anIt; + if (aVal.myRow == theIndex.row() && aVal.myColumn == theIndex.column()) { + aValue = aVal.myValue; + break; + } + } + } + else { + aValue = QVariant(); + } + + return aValue; +} + +bool ModuleBase_TableModel::setData( const QModelIndex& theIndex, const QVariant& theValue, + int theRole) +{ + bool isDone = false; + if (theRole == Qt::DisplayRole) { + QList::const_iterator anIt = myValues.begin(), aLast = myValues.end(); + for (; anIt != aLast; anIt++) { + Value aVal = *anIt; + if (aVal.myRow == theIndex.row() && aVal.myColumn == theIndex.column()) { + aVal.myValue = theValue.toDouble(); + break; + } + } + isDone = true; + } + else + isDone = true; + return isDone; +} + +int ModuleBase_TableModel::rowCount( const QModelIndex &theParent) const +{ + return myValues.size()/myColumnHeaders.size(); +} + +int ModuleBase_TableModel::columnCount( const QModelIndex &theParent) const +{ + return myColumnHeaders.size(); +} + +Qt::ItemFlags ModuleBase_TableModel::flags(const QModelIndex& theIndex) const +{ + if (!theIndex.isValid()) + return 0; + Qt::ItemFlags aFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; + + return aFlags; +} diff --git a/src/ModuleBase/ModuleBase_TableModel.h b/src/ModuleBase/ModuleBase_TableModel.h new file mode 100755 index 000000000..a554280a0 --- /dev/null +++ b/src/ModuleBase/ModuleBase_TableModel.h @@ -0,0 +1,61 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: ModuleBase_TableModel.h +// Author: Natalia ERMOLAEVA +// +#ifndef MODULEBASE_TABLE_MODEL_H_ +#define MODULEBASE_TABLE_MODEL_H_ + +#include "ModuleBase.h" + +#include +#include + +/** + * \ingroup GUI + * Enhanced version of the Qt's int spin box. + * It allows to store modified state +*/ +class MODULEBASE_EXPORT ModuleBase_TableModel : public QAbstractTableModel +{ + Q_OBJECT + +public: +struct Value { + Value(int theRowId, int theColumnId, double theValue) + : myRow(theRowId), myColumn(theColumnId), myValue(theValue) {} + int myRow; + int myColumn; + double myValue; + }; + +public: + explicit ModuleBase_TableModel(QObject* theParent = 0); + virtual ~ModuleBase_TableModel() {}; + + void setHeaders(const QStringList& theHeaders) { myColumnHeaders = theHeaders; } + + void addRow(const QStringList& theValue); + void addRow(); + + virtual QVariant headerData( int theSection, Qt::Orientation theOrientation, + int theRole = Qt::DisplayRole ) const; + //-------------------------------------------------------------------------------------- + virtual QModelIndex index( int theRow, int theColumn, + const QModelIndex &theParent = QModelIndex() ) const; + virtual QVariant data( const QModelIndex& theIndex, int theRole = Qt::DisplayRole ) const; + virtual bool setData( const QModelIndex& theIndex, const QVariant& theValue, + int theRole = Qt::EditRole ); + + //-------------------------------------------------------------------------------------- + virtual Qt::ItemFlags flags( const QModelIndex &theIndex ) const; + + virtual int rowCount( const QModelIndex &theParent = QModelIndex() ) const; + virtual int columnCount( const QModelIndex &theParent = QModelIndex() ) const; + +private: + QStringList myColumnHeaders; + QList myValues; +}; + +#endif diff --git a/src/ModuleBase/ModuleBase_WidgetTable.cpp b/src/ModuleBase/ModuleBase_WidgetTable.cpp new file mode 100755 index 000000000..2893d684b --- /dev/null +++ b/src/ModuleBase/ModuleBase_WidgetTable.cpp @@ -0,0 +1,78 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: ModuleBase_WidgetLabel.cpp +// Created: 03 Dec 2014 +// Author: Vitaly SMETANNIKOV + +#include "ModuleBase_WidgetTable.h" + +#include +#include + +#include +#include +#include + +#include +#include +#include + +ModuleBase_WidgetTable::ModuleBase_WidgetTable(QWidget* theParent, + const Config_WidgetAPI* theData, + const std::string& theParentId) + : ModuleBase_ModelWidget(theParent, theData, theParentId) +{ + QString aLabel = "Values"; + + QStringList aHeaders; + aHeaders << "X" << "Y" << "Z"; + + QStringList aValues1, aValues2, aValues3; + + aValues1 << "0" << "0" << "0"; + aValues2 << "0.1" << "0.1" << "0.1"; + aValues3 << "0.2" << "0.2" << "0.2"; + + QGridLayout* aLayout = new QGridLayout(this); + + myTableModel = new ModuleBase_TableModel(theParent); + myTableModel->setHeaders(aHeaders); + myTableModel->addRow(aValues1); + myTableModel->addRow(aValues2); + myTableModel->addRow(aValues3); + + myTableView = new QTableView(this); + myTableView->setModel(myTableModel); + + QPushButton* anAddBtn = new QPushButton("+", this); + QPushButton* aRemBtn = new QPushButton("-", this); + + connect(anAddBtn, SIGNAL(clicked()), this, SLOT(onAddRow())); + + aLayout->addWidget(new QLabel("aLabel"), 0, 0); + aLayout->addWidget(anAddBtn, 0, 1); + aLayout->addWidget(aRemBtn, 0, 2); + + aLayout->addWidget(myTableView, 1, 0, 1, 3); + + //QVBoxLayout* aLayout = new QVBoxLayout(this); + ModuleBase_Tools::zeroMargins(aLayout); + //aLayout->addWidget(myLabel); + setLayout(aLayout); +} + +ModuleBase_WidgetTable::~ModuleBase_WidgetTable() +{ +} + +QList ModuleBase_WidgetTable::getControls() const +{ + QList aControls; + aControls.append(myTableView); + return aControls; +} + +void ModuleBase_WidgetTable::onAddRow() +{ + myTableModel->addRow(); +} diff --git a/src/ModuleBase/ModuleBase_WidgetTable.h b/src/ModuleBase/ModuleBase_WidgetTable.h new file mode 100755 index 000000000..331c19577 --- /dev/null +++ b/src/ModuleBase/ModuleBase_WidgetTable.h @@ -0,0 +1,58 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +/* + * ModuleBase_WidgetTable.h + * + * Created on: Oct 8, 2014 + * Author: sbh + */ + +#ifndef ModuleBase_WidgetTable_H_ +#define ModuleBase_WidgetTable_H_ + +#include +#include + +class ModuleBase_TableModel; +class QTableView; + +/** +* \ingroup GUI +* Implementation of model widget for line edit widget. +* It can be defined with "stringvalue" keyword. +*/ +class MODULEBASE_EXPORT ModuleBase_WidgetTable : public ModuleBase_ModelWidget +{ + Q_OBJECT + public: + /// Constructor + /// \param theParent the parent object + /// \param theData the widget configuration. + /// \param theParentId is Id of a parent of the current attribute + /// \param thePlaceHolder a string of placeholder + ModuleBase_WidgetTable( QWidget* theParent, + const Config_WidgetAPI* theData, + const std::string& theParentId ); + virtual ~ModuleBase_WidgetTable(); + + /// Redefinition of virtual method + virtual QList getControls() const; + +protected slots: + void onAddRow(); + +protected: + /// Saves the internal parameters to the given feature + /// \return True in success + virtual bool storeValueCustom() const { return true;}; + + /// Restore value from attribute data to the widget's control + virtual bool restoreValueCustom() { return true; } + +private: + /// A line edit control + ModuleBase_TableModel* myTableModel; + QTableView* myTableView; +}; + +#endif /* MODULEBASE_WIDGETFILESELECTOR_H_ */ diff --git a/src/PartSet/PartSet_Module.cpp b/src/PartSet/PartSet_Module.cpp index 69d58a916..6a7977835 100755 --- a/src/PartSet/PartSet_Module.cpp +++ b/src/PartSet/PartSet_Module.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -511,7 +512,10 @@ ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& th aPointWgt->setSketch(mySketchMgr->activeSketch()); connect(aPointWgt, SIGNAL(vertexSelected()), sketchReentranceMgr(), SLOT(onVertexSelected())); aWgt = aPointWgt; - } else if (theType == "sketch-2dpoint_flyout_selector") { + } + else if (theType == "tablevalue") { + aWgt = new ModuleBase_WidgetTable(theParent, theWidgetApi, theParentId); + }else if (theType == "sketch-2dpoint_flyout_selector") { PartSet_WidgetPoint2DFlyout* aPointWgt = new PartSet_WidgetPoint2DFlyout(theParent, aWorkshop, theWidgetApi, theParentId); aPointWgt->setSketch(mySketchMgr->activeSketch()); diff --git a/src/SketchPlugin/SketchPlugin_Point.cpp b/src/SketchPlugin/SketchPlugin_Point.cpp index cf718db76..9639cb6bc 100644 --- a/src/SketchPlugin/SketchPlugin_Point.cpp +++ b/src/SketchPlugin/SketchPlugin_Point.cpp @@ -30,6 +30,9 @@ void SketchPlugin_Point::initAttributes() SketchPlugin_SketchEntity::initAttributes(); data()->addAttribute(SketchPlugin_Point::COORD_ID(), GeomDataAPI_Point2D::typeId()); + + data()->addAttribute(SketchPlugin_Point::TABLE_ID(), GeomDataAPI_Point2D::typeId()); + data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId()); ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID()); } diff --git a/src/SketchPlugin/SketchPlugin_Point.h b/src/SketchPlugin/SketchPlugin_Point.h index 59995ff43..88afb276d 100644 --- a/src/SketchPlugin/SketchPlugin_Point.h +++ b/src/SketchPlugin/SketchPlugin_Point.h @@ -31,6 +31,13 @@ class SketchPlugin_Point : public SketchPlugin_SketchEntity static const std::string MY_COORD_ID("PointCoordindates"); return MY_COORD_ID; } + /// Coordinates of the point + inline static const std::string& TABLE_ID() + { + static const std::string MY_TABLE_ID("TableValue"); + return MY_TABLE_ID; + } + /// Returns the kind of a feature SKETCHPLUGIN_EXPORT virtual const std::string& getKind() { diff --git a/src/SketchPlugin/plugin-Sketch.xml b/src/SketchPlugin/plugin-Sketch.xml index a1986c159..d10c08479 100644 --- a/src/SketchPlugin/plugin-Sketch.xml +++ b/src/SketchPlugin/plugin-Sketch.xml @@ -20,6 +20,7 @@ + -- 2.39.2