#include "PartSet_DocumentDataModel.h"
#include "PartSet_PartDataModel.h"
+#include "PartSet_TopDataModel.h"
#include "PartSet_Module.h"
//#include "XGUI_Tools.h"
#include <QIcon>
#include <QString>
#include <QBrush>
+#include <QTreeView>
#include <set>
PartSet_DocumentDataModel::PartSet_DocumentDataModel(QObject* theParent)
: ModuleBase_IDocumentDataModel(theParent),
- myActivePartId(-1), myHistoryBackOffset(0)
+ myActivePartModel(0)
{
// Create a top part of data tree model
myModel = new PartSet_TopDataModel(this);
{
DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
+
// Created object event *******************
if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
FeaturePtr aPartFeature = ModelAPI_Feature::feature(aObject);
PartSet_PartDataModel* aModel = new PartSet_PartDataModel(this);
int anId = aRootDoc->index(aPartFeature);
- aModel->setPartId(anId);
- //aModel->setPartId(aRootDoc->index(aObject));
- myPartModels[anId] = aModel;
+ aModel->setPart(aPartFeature);
+ myPartModels.append(aModel);
insertRow(aStart, partFolderNode(0));
}
} else { // Update top groups (other except parts
std::string aGroup = (*aIt);
if (aDoc == aRootDoc) { // If root objects
if (aGroup == ModelAPI_ResultPart::group()) { // Update only Parts group
- ObjectPtr aObj;
- FeaturePtr aFeature;
- int aDelId = -1;
- foreach (int aId, myPartModels.keys()) {
- aObj = aDoc->object(ModelAPI_Feature::group(), aId);
- if (aObj.get()) {
- aFeature = ModelAPI_Feature::feature(aObj);
- if (aFeature.get()) {
- if (aFeature->getKind() == PartSetPlugin_Part::ID())
- continue;
- }
+ PartSet_PartModel* aDelPartModel = 0;
+ foreach (PartSet_PartModel* aPartModel, myPartModels) {
+ if (aPartModel->position() == -1) {
+ aDelPartModel = aPartModel;
+ break;
}
- aDelId = aId;
- break;
}
- if (aDelId != -1) {
+ if (aDelPartModel) {
deactivatePart();
int aStart = myPartModels.size() - 1;
- removeSubModel(aDelId);
+ removeSubModel(aDelPartModel);
removeRow(aStart, partFolderNode(0));
}
+ } if (aGroup == ModelAPI_Feature::group()) { // Update History node
+ int aRow = historyOffset() + aRootDoc->size(ModelAPI_Feature::group());
+ removeRow(aRow);
} else { // Update top groups (other except parts
QModelIndex aIndex = myModel->findGroup(aGroup);
int aStart = myModel->rowCount(aIndex);
}
if (aPartModel) {
QModelIndex aIndex = aPartModel->findGroup(aGroup);
- int aStart = aPartModel->rowCount(aIndex);
- aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
- removeRow(aStart, aIndex);
+ if (aIndex.isValid()) {
+ int aStart = aPartModel->rowCount(aIndex);
+ aIndex = createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex));
+ removeRow(aStart, aIndex);
+ } else {
+ int aRow = aPartModel->rowCount();
+ aIndex = createIndex(aPartModel->position() + historyOffset(), 0, HistoryNode);
+ removeRow(aRow, aIndex);
+ }
}
}
}
beginResetModel();
clearModelIndexes();
- int aNbParts = aRootDoc->size(ModelAPI_ResultPart::group());
- //if (myPartModels.size() != aNbParts) { // resize internal models
- // while (myPartModels.size() > aNbParts) {
- // delete myPartModels.last();
- // myPartModels.removeLast();
- // }
- // while (myPartModels.size() < aNbParts) {
- // myPartModels.append(new PartSet_PartDataModel(this));
- // }
- // for (int i = 0; i < myPartModels.size(); i++)
- // myPartModels.at(i)->setPartId(i);
- //}
+ // Delete extra models
+ ObjectPtr aObj;
+ FeaturePtr aFeature;
+ QList<PartSet_PartModel*> aDelList;
+ foreach (PartSet_PartModel* aPartModel, myPartModels) {
+ if (aPartModel->position() == -1)
+ aDelList.append(aPartModel);
+ }
+ foreach (PartSet_PartModel* aPartModel, aDelList) {
+ removeSubModel(aPartModel);
+ }
+ // Add non existing models
+ int aHistNb = aRootDoc->size(ModelAPI_Feature::group());
+ for (int i = 0; i < aHistNb; i++) {
+ aObj = aRootDoc->object(ModelAPI_Feature::group(), i);
+ aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
+ if (aFeature->getKind() == PartSetPlugin_Part::ID()) {
+ if (!findPartModel(aFeature)) {
+ PartSet_PartDataModel* aModel = new PartSet_PartDataModel(this);
+ aModel->setPart(aFeature);
+ myPartModels.append(aModel);
+ }
+ }
+ }
endResetModel();
}
if (!theIndex.isValid())
return QVariant();
- //if ((theIndex.column() == 1) ) {
- // if ((theIndex.internalId() == HistoryNode) && (!aParent.isValid())) {
- // switch (theRole) {
- // case Qt::DecorationRole:
- // if (theIndex.row() == lastHistoryRow())
- // return QIcon(":pictures/arrow.png");
- // }
- // }
- // return QVariant();
- //}
-
DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
+ QModelIndex aParent = theIndex.parent();
+ if ((theIndex.column() == 1) ) {
+ if ((theIndex.internalId() >= PartsFolder) && (theIndex.internalId() <= PartResult)) {
+ if (ModelAPI_Session::get()->activeDocument() == aRootDoc) {
+ if (!aParent.isValid()) {
+ switch (theRole) {
+ case Qt::DecorationRole:
+ if (theIndex.row() == lastHistoryRow())
+ return QIcon(":pictures/arrow.png");
+ }
+ }
+ }
+ } else {
+ QModelIndex* aIndex = toSourceModelIndex(theIndex);
+ const QAbstractItemModel* aModel = aIndex->model();
+ if (isPartSubModel(aModel)) {
+ return aModel->data(*aIndex, theRole);
+ }
+ }
+ return QVariant();
+ }
+
switch (theIndex.internalId()) {
case PartsFolder:
switch (theRole) {
case Qt::DecorationRole:
return featureIcon(aFeature);
case Qt::ToolTipRole:
- return tr("Feature object");
+ {
+ ResultPtr aResult = aFeature->firstResult();
+ bool isResultAndNotLoaded = false;
+ if( aResult.get() )
+ {
+ ResultPartPtr aResultPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>( aResult );
+ if( aResultPart.get() )
+ isResultAndNotLoaded = !aResultPart->isActivated();
+ }
+ if( isResultAndNotLoaded )
+ return tr( "The part should be activated before the user may edit it" );
+ else
+ return tr( "Feature object" );
+ }
case Qt::ForegroundRole:
if (theIndex.row() > lastHistoryRow())
return QBrush(Qt::lightGray);
}
break;
}
- QModelIndex aParent = theIndex.parent();
if (aParent.internalId() == HistoryNode) {
int aId = aParent.row() - historyOffset();
QModelIndex* aIndex = toSourceModelIndex(theIndex);
- return myPartModels[aId]->data(*aIndex, theRole);
+ return findPartModel(aId)->data(*aIndex, theRole);
}
return toSourceModelIndex(theIndex)->data(theRole);
}
int PartSet_DocumentDataModel::rowCount(const QModelIndex& theParent) const
{
- DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
+ SessionPtr aSession = ModelAPI_Session::get();
+ if (!aSession->hasModuleDocument())
+ return 0;
+ DocumentPtr aRootDoc = aSession->moduleDocument();
if (!theParent.isValid()) {
// Size of external models
int aVal = historyOffset();
}
if (theParent.internalId() == HistoryNode) {
int aId = theParent.row() - historyOffset();
- if (myPartModels.contains(aId))
- return myPartModels[aId]->rowCount(QModelIndex());
+ PartSet_PartModel* aModel = findPartModel(aId);
+ if (aModel)
+ return aModel->rowCount(QModelIndex());
return 0;
}
if (theParent.internalId() == PartResult)
int PartSet_DocumentDataModel::columnCount(const QModelIndex& theParent) const
{
- return 1;
+ return 2;
}
QModelIndex PartSet_DocumentDataModel::index(int theRow, int theColumn,
if (theRow == aOffs) // Create Parts node
aIndex = partFolderNode(theColumn);
else {
- // create history node or a part sub-tree
- int aId = theRow - historyOffset();
+ // create history node
aIndex = createIndex(theRow, theColumn, HistoryNode);
- //if (myPartModels.contains(aId)) {
- // aIndex = myPartModels[aId]->index(theRow, theColumn, QModelIndex());
- //}
}
}
} else {
} else {
if (theParent.internalId() == HistoryNode) {
int aId = theParent.row() - historyOffset();
- aIndex = myPartModels[aId]->index(theRow, theColumn, QModelIndex());
+ aIndex = findPartModel(aId)->index(theRow, theColumn, QModelIndex());
} else {
QModelIndex* aParent = (QModelIndex*) theParent.internalPointer();
aIndex = aParent->model()->index(theRow, theColumn, (*aParent));
return QModelIndex();
if (theIndex.internalId() == PartResult)
- return partFolderNode(theIndex.column());
+ return partFolderNode(0);
QModelIndex* aIndex = toSourceModelIndex(theIndex);
const QAbstractItemModel* aModel = aIndex->model();
return QModelIndex();
QModelIndex aIndex1 = aModel->parent(*aIndex);
- if (isPartSubModel(aModel) && (!aIndex1.isValid())) {
- int aId = myPartModels.key((PartSet_PartModel*) aModel);
+ const PartSet_PartModel* aPartModel = dynamic_cast<const PartSet_PartModel*>(aModel);
+ if (aPartModel && (!aIndex1.isValid())) {
+ int aId = aPartModel->position();
int aRow = aId + historyOffset();
- return createIndex(aRow, theIndex.column(), (qint32) HistoryNode);
+ return createIndex(aRow, 0, (qint32) HistoryNode);
}
if (aIndex1.isValid())
- return createIndex(aIndex1.row(), aIndex1.column(), (void*) getModelIndex(aIndex1));
+ return createIndex(aIndex1.row(), 0, (void*) getModelIndex(aIndex1));
return aIndex1;
}
foreach (PartSet_PartModel* aPart, myPartModels)
delete aPart;
myPartModels.clear();
+ myActivePartModel = 0;
}
ObjectPtr PartSet_DocumentDataModel::object(const QModelIndex& theIndex) const
void PartSet_DocumentDataModel::removeSubModel(int theModelId)
{
- PartSet_PartModel* aModel = myPartModels[theModelId];
+ PartSet_PartModel* aModel = myPartModels.at(theModelId);
+ removeSubModel(aModel);
+}
+
+void PartSet_DocumentDataModel::removeSubModel(PartSet_PartModel* theModel)
+{
QIntList aToRemove;
for (int i = 0; i < myIndexes.size(); i++) {
- if (myIndexes.at(i)->model() == aModel)
+ if (myIndexes.at(i)->model() == theModel)
aToRemove.append(i);
}
int aId;
myIndexes.removeAt(aId);
aToRemove.removeLast();
}
- delete aModel;
- myPartModels.remove(theModelId);
+ if (theModel == myActivePartModel)
+ myActivePartModel = 0;
+ myPartModels.removeAll(theModel);
+ delete theModel;
}
+
bool PartSet_DocumentDataModel::isSubModel(const QAbstractItemModel* theModel) const
{
if (theModel == myModel)
bool PartSet_DocumentDataModel::isPartSubModel(const QAbstractItemModel* theModel) const
{
- return myPartModels.key((PartSet_PartModel*) theModel, -1) != -1;
+ return myPartModels.contains((PartSet_PartModel*) theModel);
}
QModelIndex PartSet_DocumentDataModel::partFolderNode(int theColumn) const
if (theIndex.isValid() && (theIndex.internalId() == PartResult)) {
myActivePartIndex = theIndex;
myModel->setItemsColor(PASSIVE_COLOR);
- if (myActivePartId != -1)
- myPartModels[myActivePartId]->setItemsColor(PASSIVE_COLOR);
+ if (myActivePartModel)
+ myActivePartModel->setItemsColor(PASSIVE_COLOR);
// Find activated part feature by its ID
ResultPartPtr aPartRes = activePart();
FeaturePtr aFeature = ModelAPI_Feature::feature(aPartRes);
- DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
- myActivePartId = aRootDoc->index(aFeature);
- myPartModels[myActivePartId]->setItemsColor(ACTIVE_COLOR);
+ if (aFeature.get()) {
+ myActivePartModel = findPartModel(aFeature);
+ myActivePartModel->setItemsColor(ACTIVE_COLOR);
+ }
}
- //QModelIndex* aIndex = toSourceModelIndex(theIndex);
- //if (!aIndex)
- // return false;
-
- //const QAbstractItemModel* aModel = aIndex->model();
-
- //if (isPartSubModel(aModel)) {
- // // if this is root node (Part item index)
- // if (!aIndex->parent().isValid()) {
- // if (myActivePart)
- // myActivePart->setItemsColor(PASSIVE_COLOR);
-
- // if (myActivePart == aModel) {
- // myActivePart = 0;
- // myActivePartIndex = QModelIndex();
- // } else {
- // myActivePart = (PartSet_PartModel*)aModel;
- // myActivePartIndex = theIndex;
- // }
-
- // if (myActivePart) {
- // myActivePart->setItemsColor(ACTIVE_COLOR);
- // myModel->setItemsColor(PASSIVE_COLOR);
- // } else
- // myModel->setItemsColor(ACTIVE_COLOR);
- // return true;
- // }
- // }
return true;
}
QModelIndex PartSet_DocumentDataModel::activePartTree() const
{
- if (myActivePartId != -1) {
- return createIndex(myActivePartId + historyOffset(), 0, HistoryNode);
+ if (myActivePartModel) {
+ return createIndex(myActivePartModel->position() + historyOffset(), 0, HistoryNode);
}
return QModelIndex();
}
void PartSet_DocumentDataModel::deactivatePart()
{
if (myActivePartIndex.isValid()) {
- if (myActivePartId != -1)
- myPartModels[myActivePartId]->setItemsColor(PASSIVE_COLOR);
- myActivePartId = -1;
+ if (myActivePartModel)
+ myActivePartModel->setItemsColor(PASSIVE_COLOR);
+ myActivePartModel = 0;
myActivePartIndex = QModelIndex();
myModel->setItemsColor(ACTIVE_COLOR);
}
Qt::ItemFlags PartSet_DocumentDataModel::flags(const QModelIndex& theIndex) const
{
- Qt::ItemFlags aFlags = QAbstractItemModel::flags(theIndex); //Qt::ItemIsSelectable;
- if (object(theIndex)) {
- aFlags |= Qt::ItemIsEditable;
+ if ((theIndex.internalId() >= PartsFolder) && (theIndex.internalId() <= PartResult)) {
+ Qt::ItemFlags aFlags = Qt::ItemIsSelectable;
+ if (object(theIndex).get()) {
+ aFlags |= Qt::ItemIsEditable;
+ }
+ // Disable items which are below of last history row
+ // Do not disable second column
+ if (theIndex.internalId() == HistoryNode) {
+ if (theIndex.row() <= lastHistoryRow() || (theIndex.column() == 1))
+ aFlags |= Qt::ItemIsEnabled;
+ } else
+ aFlags |= Qt::ItemIsEnabled;
+ return aFlags;
+ } else {
+ QModelIndex* aIndex = toSourceModelIndex(theIndex);
+ const QAbstractItemModel* aModel = aIndex->model();
+ Qt::ItemFlags aFlags = aModel->flags(*aIndex);
+ if (aModel == myModel) {
+ if (myModel->object(*aIndex).get())
+ aFlags |= Qt::ItemIsEditable;
+ }
+ return aFlags;
}
- // Disable items which are below of last history row
- // Do not disable second column
- //if (theIndex.row() <= lastHistoryRow() || theIndex.column() == 1) {
- // aFlags |= Qt::ItemIsEnabled;
- //}
- return aFlags;
}
QModelIndex PartSet_DocumentDataModel::partIndex(const ResultPartPtr& theObject) const
{
- //int aRow = -1;
- //PartSet_PartModel* aModel = 0;
- //foreach (PartSet_PartModel* aPartModel, myPartModels)
- //{
- // aRow++;
- // if (aPartModel->part() == theObject) {
- // aModel = aPartModel;
- // break;
- // }
- //}
- //if (aModel) {
- // return createIndex(aRow, 0, (void*) getModelIndex(aModel->index(0, 0, QModelIndex())));
- //}
DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
int aNb = aRootDoc->size(ModelAPI_ResultPart::group());
for (int aId = 0; aId < aNb; aId++) {
QModelIndex aIndex = myModel->objectIndex(theObject);
return
aIndex.isValid() ?
- createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
+ createIndex(aIndex.row(), 0, (void*) getModelIndex(aIndex)) :
QModelIndex();
}
} else {
if (aPartModel) {
QModelIndex aIndex = aPartModel->objectIndex(theObject);
return aIndex.isValid() ?
- createIndex(aIndex.row(), aIndex.column(), (void*) getModelIndex(aIndex)) :
+ createIndex(aIndex.row(), 0, (void*) getModelIndex(aIndex)) :
QModelIndex();
}
}
int PartSet_DocumentDataModel::lastHistoryRow() const
{
- return rowCount() - 1 - myHistoryBackOffset;
+ DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
+ FeaturePtr aFeature = aRootDoc->currentFeature(true);
+ if (aFeature.get())
+ return historyOffset() + aRootDoc->index(aFeature);
+ else
+ return historyOffset() - 1;
}
void PartSet_DocumentDataModel::setLastHistoryItem(const QModelIndex& theIndex)
{
+ SessionPtr aMgr = ModelAPI_Session::get();
+ DocumentPtr aRootDoc = aMgr->moduleDocument();
+ std::string aOpName = tr("History change").toStdString();
if (theIndex.internalId() == HistoryNode) {
- myHistoryBackOffset = rowCount() - 1 - theIndex.row();
+ ObjectPtr aObject = object(theIndex);
+ aMgr->startOperation(aOpName);
+ aRootDoc->setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(aObject), true);
+ aMgr->finishOperation();
+ } else {
+ aMgr->startOperation(aOpName);
+ aRootDoc->setCurrentFeature(FeaturePtr(), true);
+ aMgr->finishOperation();
}
}
return anIcon;
}
+void PartSet_DocumentDataModel::onMouseDoubleClick(const QModelIndex& theIndex)
+{
+ if (theIndex.column() != 1)
+ return;
+ if (flags(theIndex) == 0)
+ return;
+ QTreeView* aTreeView = dynamic_cast<QTreeView*>(sender());
+ if ((theIndex.internalId() >= PartsFolder) && (theIndex.internalId() <= PartResult)) {
+ if (myActivePartModel)
+ // It means that the root document is not active
+ return;
+ QModelIndex aNewIndex;
+ if (theIndex.internalId() == HistoryNode)
+ aNewIndex = theIndex;
+ int aOldId = lastHistoryRow();
+ setLastHistoryItem(theIndex);
+ int aStartRow = std::min(aOldId, theIndex.row());
+ int aEndRow = std::max(aOldId, theIndex.row());
+ for (int i = aStartRow; i <= aEndRow; i++) {
+ aTreeView->update(createIndex(i, 0, HistoryNode));
+ aTreeView->update(createIndex(i, 1, HistoryNode));
+ }
+
+ } else {
+ QModelIndex* aIndex = toSourceModelIndex(theIndex);
+ const QAbstractItemModel* aModel = aIndex->model();
+ if (isPartSubModel(aModel)) {
+ PartSet_PartDataModel* aPartModel = (PartSet_PartDataModel*)aModel;
+ QModelIndex aOldItem = aPartModel->lastHistoryItem();
+ aPartModel->setLastHistoryItem(*aIndex);
+ QModelIndex aOldIndex = createIndex(aOldItem.row(), aOldItem.column(), (void*) getModelIndex(aOldItem));
+ int aStartRow = std::min(aOldItem.row(), aIndex->row());
+ int aEndRow = std::max(aOldItem.row(), aIndex->row());
+ for (int i = aStartRow; i <= aEndRow; i++) {
+ QModelIndex aInd1 = aPartModel->index(i, 0);
+ QModelIndex aInd2 = createIndex(i, 0, (void*) getModelIndex(aInd1));
+ aTreeView->update(aInd2);
+ aInd1 = aPartModel->index(i, 1);
+ aInd2 = createIndex(i, 1, (void*) getModelIndex(aInd1));
+ aTreeView->update(aInd2);
+ }
+ }
+ }
+}
+
+
+PartSet_PartModel* PartSet_DocumentDataModel::findPartModel(FeaturePtr thePart) const
+{
+ foreach (PartSet_PartModel* aModel, myPartModels) {
+ if (aModel->part() == thePart)
+ return aModel;
+ }
+ return 0;
+}
+
+PartSet_PartModel* PartSet_DocumentDataModel::findPartModel(int thePosition) const
+{
+ foreach (PartSet_PartModel* aModel, myPartModels) {
+ if (aModel->position() == thePosition)
+ return aModel;
+ }
+ return 0;
+}
\ No newline at end of file