Skip to content

Commit

Permalink
FEATURE: Add active model support
Browse files Browse the repository at this point in the history
SMTK&CMB now has a concept of active model. Loading, switching and
closing model(s)/data would reset the active model correspondingly.
A singleton class(qtActiveObjects) is used to store active model.
subPhraseGenerator would also keep a copy of it.
Only active model can be selected by rubber band. And only active model
would be expanded in model tree. Attribute panel would only list active
model's entities. Check CMB issue #140 for detail.
  • Loading branch information
Haocheng Liu committed Apr 25, 2017
1 parent c5bb681 commit bf7b6e1
Show file tree
Hide file tree
Showing 22 changed files with 417 additions and 52 deletions.
10 changes: 5 additions & 5 deletions smtk/attribute/ModelEntityItem.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ bool ModelEntityItem::appendValue(const smtk::model::EntityRef& val)
for (std::size_t i = 0; i < n; ++i)
{
if (this->isSet(i) && (this->value(i).entity() == val.entity()))
{
return true;
}
{
return true;
}
if (!this->isSet(i))
{
foundEmpty = true;
emptyIndex = i;
foundEmpty = true;
emptyIndex = i;
}
}
// If not, was there a space available?
Expand Down
2 changes: 2 additions & 0 deletions smtk/extension/qt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ smtk_find_package_qt(qt_targets REQUIRED

# set up sources to build
set(QAttrLibSrcs
qtActiveObjects.cxx
qtUIManager.cxx
qtSelectionManager.cxx
qtAttribute.cxx
Expand Down Expand Up @@ -59,6 +60,7 @@ set(QAttrLibUIs


set(QAttrLibMocHeaders
qtActiveObjects.h
qtUIManager.h
qtSelectionManager.h
qtAttribute.h
Expand Down
32 changes: 32 additions & 0 deletions smtk/extension/qt/qtActiveObjects.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//=========================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//=========================================================================

#include <smtk/extension/qt/qtActiveObjects.h>

qtActiveObjects::qtActiveObjects()
{
this->m_activeModel = smtk::model::Model();
}

qtActiveObjects::~qtActiveObjects()
{
}

qtActiveObjects& qtActiveObjects::instance()
{
static qtActiveObjects theInstance;
return theInstance;
}

void qtActiveObjects::setActiveModel(const smtk::model::Model& inputModel)
{
this->m_activeModel = inputModel;
emit this->activeModelChanged();
}
66 changes: 66 additions & 0 deletions smtk/extension/qt/qtActiveObjects.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//=========================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//=========================================================================
#ifndef qtActiveObjects_h
#define qtActiveObjects_h

#include <QObject>
#include "smtk/PublicPointerDefs.h"
#include "smtk/model/Model.h" //for smtk::model::Model

namespace smtk {
namespace model {
class Model;
}
}

class pqView;
class vtkSMSessionProxyManager;
class vtkSMViewProxy;


/**
qtActiveObjects keeps track of active objects.
This is similar to pqActiveObjects in ParaView, however it tracks objects
relevant to SMTK and CMB.
*/
class qtActiveObjects : public QObject
{
Q_OBJECT

typedef QObject Superclass;

public:
/// Returns reference to the singleton instance.
static qtActiveObjects& instance();

/// Returns the active model.
smtk::model::Model activeModel() const {return this->m_activeModel;}

public slots:
/// Set the active module.
void setActiveModel(const smtk::model::Model& inputModel);

signals:
/// Fire when activeModel changes.
void activeModelChanged();

private slots:

protected:
qtActiveObjects();
virtual ~qtActiveObjects();

smtk::model::Model m_activeModel;

private:
Q_DISABLE_COPY(qtActiveObjects)
};

#endif
44 changes: 32 additions & 12 deletions smtk/extension/qt/qtAssociationWidget.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// PURPOSE. See the above copyright notice for more information.
//=========================================================================

#include "smtk/extension/qt/qtActiveObjects.h"
#include "smtk/extension/qt/qtAssociationWidget.h"

#include "smtk/extension/qt/qtAttribute.h"
Expand Down Expand Up @@ -344,14 +345,14 @@ void qtAssociationWidget::showEntityAssociation(
typedef smtk::model::EntityRefs::const_iterator cit;
for (cit i =modelEnts.begin(); i != modelEnts.end(); ++i)
{
this->addModelAssociationListItem(this->Internals->CurrentList, *i, false);
this->addModelAssociationListItem(this->Internals->CurrentList, *i, false, true);
}
this->Internals->CurrentList->sortItems();
// The returned usedModelEnts should include all of the input modelEnts AND, if attDef is unique,
// entities that have been associated with the attributes with same definition.
std::set<smtk::model::EntityRef> usedModelEnts = this->processAttUniqueness(attDef, modelEnts);

// Now that we have add all the used model entities, we need to move on to all model
// Now that we have added all the used model entities, we need to move on to all model
// entities that are not associated with the attribute, but could be associated.
// We use the "no-exact match required" flag to catch any entity that could possibly match
// the association mask. This gets pruned below.
Expand All @@ -366,18 +367,23 @@ void qtAssociationWidget::showEntityAssociation(
usedModelEnts.begin(), usedModelEnts.end(),
std::inserter(avail, avail.end()));

// Add a subset of the remainder to the list of available entities.
// Add a subset of the remainder which also belongs to current active model
// to the list of available entities.
// We create a temporary group and use Group::meetsMembershipConstraints()
// to test whether the mask allows association.
smtk::model::Model activeModel = qtActiveObjects::instance().activeModel();
smtk::model::Manager::Ptr tmpMgr = smtk::model::Manager::create();
Group tmpGrp = tmpMgr->addGroup();
tmpGrp.setMembershipMask(attDef->associationMask());

for(EntityRefs::iterator i = avail.begin(); i != avail.end(); ++i)
{
if (tmpGrp.meetsMembershipConstraints(*i))
if (tmpGrp.meetsMembershipConstraints(*i) &&
i->owningModel().entity() == activeModel.entity())
{
this->addModelAssociationListItem(this->Internals->AvailableList, *i, false);
}
}
this->Internals->AvailableList->sortItems();
this->Internals->CurrentList->blockSignals(false);
this->Internals->AvailableList->blockSignals(false);
Expand Down Expand Up @@ -550,10 +556,13 @@ void qtAssociationWidget::removeItem(QListWidget* theList,
}

QListWidgetItem* qtAssociationWidget::addModelAssociationListItem(
QListWidget* theList, smtk::model::EntityRef modelItem, bool sort)
QListWidget* theList, smtk::model::EntityRef modelItem, bool sort, bool
appendModelName)
{
std::string name = appendModelName ? (modelItem.name() +
" - " + modelItem.owningModel().name()) : modelItem.name();
QListWidgetItem* item = new QListWidgetItem(
QString::fromStdString(modelItem.name()),
QString::fromStdString(name),
theList,
smtk_USER_DATA_TYPE);
//save the entity as a uuid string
Expand Down Expand Up @@ -648,8 +657,14 @@ void qtAssociationWidget::onRemoveAssigned()
{
this->Internals->CurrentAtt.lock()->disassociateEntity(currentItem);
this->removeItem(theList, item);
selItem = this->addModelAssociationListItem(
this->Internals->AvailableList, currentItem);
// only add entityRef back to availableList when it belongs to
// the current active model
if (currentItem.owningModel().entity() ==
qtActiveObjects::instance().activeModel().entity())
{
selItem = this->addModelAssociationListItem(
this->Internals->AvailableList, currentItem);
}
}
}
else if(!this->Internals->CurrentModelGroup.isNull())
Expand Down Expand Up @@ -695,7 +710,7 @@ void qtAssociationWidget::onAddAvailable()
{
this->removeItem(theList, item);
selItem = this->addModelAssociationListItem(
this->Internals->CurrentList, currentItem);
this->Internals->CurrentList, currentItem, true, true);
}
else // faied to assoicate with new entity
{
Expand Down Expand Up @@ -773,11 +788,16 @@ void qtAssociationWidget::onExchange()
{
this->removeItem(this->Internals->CurrentList, selCurrentItems[i]);
selCurrentItem = this->addModelAssociationListItem(
this->Internals->CurrentList, availableItem);
this->Internals->CurrentList, availableItem, true, true);

this->removeItem(this->Internals->AvailableList, selAvailItems[i]);
selAvailItem = this->addModelAssociationListItem(
this->Internals->AvailableList, currentItem);
// only add it back to available list when it's part of active model
if (currentItem.owningModel().entity() ==
qtActiveObjects::instance().activeModel().entity())
{
selAvailItem = this->addModelAssociationListItem(
this->Internals->AvailableList, currentItem);
}
}
else // faied to exchange, add back association
{
Expand Down
4 changes: 3 additions & 1 deletion smtk/extension/qt/qtAssociationWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ namespace smtk

//returns the Item it has added to the widget
//ownership of the item is handled by the widget so no need to delete
//for now we append model name to currentList
virtual QListWidgetItem* addModelAssociationListItem(
QListWidget* theList, smtk::model::EntityRef modelItem, bool sort=true);
QListWidget* theList, smtk::model::EntityRef modelItem,
bool sort=true, bool appendModelName = false);

//returns the Item it has added to the widget
//ownership of the item is handled by the widget so no need to delete
Expand Down
6 changes: 5 additions & 1 deletion smtk/extension/qt/qtCheckItemComboBox.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "smtk/attribute/ModelEntityItem.h"
#include "smtk/attribute/ModelEntityItemDefinition.h"
#include "smtk/attribute/System.h"
#include "smtk/extension/qt/qtActiveObjects.h"
#include "smtk/extension/qt/qtMeshItem.h"
#include "smtk/extension/qt/qtModelEntityItem.h"
#include "smtk/mesh/Collection.h"
Expand Down Expand Up @@ -151,7 +152,10 @@ void qtModelEntityItemCombo::init()
// if the mask is only groups, get all groups from manager
((onlyGroups && entref.isGroup()) ||
// else, check the membership constraints
(!onlyGroups && tmpGrp.meetsMembershipConstraints(entref))))
(!onlyGroups && tmpGrp.meetsMembershipConstraints(entref))) &&
// only show entities in active model or its type is model
(entref.owningModel().entity() ==
qtActiveObjects::instance().activeModel().entity() || entref.isModel()))
{
QStandardItem* item = new QStandardItem;
std::string entName = entref.name();
Expand Down
6 changes: 6 additions & 0 deletions smtk/extension/qt/qtEntityItemDelegate.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ void QEntityItemDelegate::paint(
QFont titleFont = QApplication::font();
QFont subtitleFont = QApplication::font();
titleFont.setPixelSize(this->m_titleFontSize);
/// add a method to set/get title font
titleFont.setBold(this->titleFontWeight() > 1 ? true : false);
// bold the active model title
if (idx.data(Qt::FontRole).toBool())
{
titleFont.setBold(true);
}
subtitleFont.setPixelSize(this->m_subtitleFontSize);
subtitleFont.setBold(this->subtitleFontWeight() > 1 ? true : false);

Expand Down
15 changes: 14 additions & 1 deletion smtk/extension/qt/qtEntityItemModel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//=========================================================================
#include "smtk/extension/qt/qtEntityItemModel.h"

#include "smtk/extension/qt/qtActiveObjects.h"
#include "smtk/model/Entity.h"
#include "smtk/model/EntityPhrase.h"
#include "smtk/model/FloatData.h"
Expand Down Expand Up @@ -356,7 +357,7 @@ QVariant QEntityItemModel::data(const QModelIndex& idx, int role) const
}
else if (role == EntityColorRole &&
(item->phraseType() == MESH_SUMMARY || item->relatedEntity().isValid()
|| item->phraseType() == ENTITY_LIST )) // if needed, disable assign model color here
|| item->phraseType() == ENTITY_LIST ))
{
QColor color;
FloatList rgba = item->relatedColor();
Expand Down Expand Up @@ -390,6 +391,14 @@ QVariant QEntityItemModel::data(const QModelIndex& idx, int role) const
}
return color;
}
else if (role == Qt::FontRole)
{
// used to bold the active model's title
return (item->relatedEntity().entity() ==
qtActiveObjects::instance().activeModel().entity()) ? QVariant(true)
: QVariant(false);

}
}
}
return QVariant();
Expand Down Expand Up @@ -1454,6 +1463,10 @@ void QEntityItemModel::newSessionOperatorResult(

smtk::model::DescriptivePhrases newSubs =
this->m_root->findDelegate()->subphrases(this->m_root);

// udpate active model in subphraseGenerator
this->m_root->findDelegate()->setActiveModel(qtActiveObjects::instance().activeModel());

std::vector< std::pair<DescriptivePhrasePtr, int> > newDphrs;
int newIdx = 0;
for (smtk::model::DescriptivePhrases::iterator it = newSubs.begin();
Expand Down
2 changes: 2 additions & 0 deletions smtk/extension/qt/qtModelPanel.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//=========================================================================
#include "smtk/extension/qt/qtModelPanel.h"

#include "smtk/extension/qt/qtActiveObjects.h"
#include "smtk/extension/qt/qtEntityItemModel.h"
#include "smtk/extension/qt/qtModelView.h"
#include "smtk/model/Entity.h"
Expand Down Expand Up @@ -112,6 +113,7 @@ void qtModelPanel::resetView(qtModelPanel::enumTreeView enType,
spg->setDirectLimit(-1);
spg->setSkipAttributes(true);
spg->setSkipProperties(true);
spg->setActiveModel(qtActiveObjects::instance().activeModel());

qtModelView* modelview = this->getModelView();
QPointer<smtk::extension::QEntityItemModel> qmodel = modelview->getModel();
Expand Down
Loading

0 comments on commit bf7b6e1

Please sign in to comment.