// Register in event loop
Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_CREATED));
Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_UPDATED));
+ Event_Loop::loop()->registerListener(this, Event_Loop::eventByName(EVENT_FEATURE_DELETED));
// Create a top part of data tree model
myModel = new XGUI_TopDataModel(myDocument, this);
void XGUI_DocumentDataModel::processEvent(const Event_Message* theMessage)
{
+ // Created object event *******************
if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_CREATED) {
const ModelAPI_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const ModelAPI_FeatureUpdatedMessage*>(theMessage);
std::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
std::shared_ptr<ModelAPI_Feature> aFeature = aUpdMsg->feature();
- if (aDoc == myDocument) {
- if (aFeature->getGroup().compare(PARTS_GROUP) == 0) {
+ if (aDoc == myDocument) { // If root objects
+ if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Updsate only Parts group
// Add a new part
int aStart = myModel->rowCount(QModelIndex()) + myPartModels.size();
- beginInsertRows(QModelIndex(), aStart, aStart + 1);
XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this);
aModel->setPartId(myPartModels.count());
myPartModels.append(aModel);
- endInsertRows();
- } else {
+ insertRows(QModelIndex(), aStart, aStart);
+ } else { // Update top groups (other except parts
QModelIndex aIndex = myModel->findParent(aFeature);
int aStart = myModel->rowCount(aIndex);
aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
- beginInsertRows(aIndex, aStart-1, aStart);
- endInsertRows();
- if (aStart == 1) // Update parent if this is a first child in order to update node decoration
- emit dataChanged(aIndex, aIndex);
+ insertRows(aIndex, aStart-1, aStart);
}
- } else {
+ } else { // if sub-objects of first level nodes
XGUI_PartModel* aPartModel = 0;
QList<XGUI_PartModel*>::const_iterator aIt;
for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
QModelIndex aIndex = aPartModel->findParent(aFeature);
int aStart = aPartModel->rowCount(aIndex);
aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
- beginInsertRows(aIndex, aStart-1, aStart);
- endInsertRows();
- if (aStart == 1) // Update parent if this is a first child in order to update node decoration
- emit dataChanged(aIndex, aIndex);
+ insertRows(aIndex, aStart-1, aStart);
}
}
- } else {
- // Reset whole tree
+
+ // Deteted object event ***********************
+ } if (QString(theMessage->eventID().eventText()) == EVENT_FEATURE_DELETED) {
+ const ModelAPI_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const ModelAPI_FeatureDeletedMessage*>(theMessage);
+ std::shared_ptr<ModelAPI_Document> aDoc = aUpdMsg->document();
+
+ if (aDoc == myDocument) { // If root objects
+ int aStart = myPartModels.count() - 1;
+ delete myPartModels.last();
+ myPartModels.removeLast();
+ beginRemoveRows(QModelIndex(), aStart, aStart);
+ endRemoveRows();
+ }
+ // Reset whole tree **************************
+ } else {
beginResetModel();
int aNbParts = myDocument->featuresIterator(PARTS_GROUP)->numIterationsLeft();
if (myPartModels.size() != aNbParts) { // resize internal models
{
if (!theIndex.isValid())
return QVariant();
- return toSourceModel(theIndex).data(theRole);
+ return toSourceModelIndex(theIndex).data(theRole);
}
if (!theParent.isValid())
return myModel->rowCount(theParent) + myPartModels.size();
- QModelIndex aParent = toSourceModel(theParent);
+ QModelIndex aParent = toSourceModelIndex(theParent);
return aParent.model()->rowCount(aParent);
}
QModelIndex XGUI_DocumentDataModel::parent(const QModelIndex& theIndex) const
{
- QModelIndex aParent = toSourceModel(theIndex);
+ QModelIndex aParent = toSourceModelIndex(theIndex);
aParent = aParent.model()->parent(aParent);
if (aParent.isValid())
return createIndex(aParent.row(), aParent.column(), (void*)getModelIndex(aParent));
}
-QModelIndex XGUI_DocumentDataModel::toSourceModel(const QModelIndex& theProxy) const
+QModelIndex XGUI_DocumentDataModel::toSourceModelIndex(const QModelIndex& theProxy) const
{
QModelIndex* aIndexPtr = static_cast<QModelIndex*>(theProxy.internalPointer());
return (*aIndexPtr);
FeaturePtr XGUI_DocumentDataModel::feature(const QModelIndex& theIndex) const
{
- QModelIndex aIndex = toSourceModel(theIndex);
+ QModelIndex aIndex = toSourceModelIndex(theIndex);
const XGUI_FeaturesModel* aModel = dynamic_cast<const XGUI_FeaturesModel*>(aIndex.model());
return aModel->feature(aIndex);
+}
+
+void XGUI_DocumentDataModel::insertRows(const QModelIndex& theParent, int theStart, int theEnd)
+{
+ beginInsertRows(theParent, theStart, theEnd);
+ endInsertRows();
+ if (theStart == 1) // Update parent if this is a first child in order to update node decoration
+ emit dataChanged(theParent, theParent);
}
\ No newline at end of file