Loading [MathJax]/jax/output/HTML-CSS/config.js
Radium Engine  1.7.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SelectionManager.cpp
1#include <Core/CoreMacros.hpp>
2#include <Core/Utils/Log.hpp>
3#include <Engine/RadiumEngine.hpp>
4#include <Engine/Scene/ItemEntry.hpp>
5#include <Gui/SelectionManager/SelectionManager.hpp>
6#include <Gui/TreeModel/EntityTreeModel.hpp>
7#include <algorithm>
8#include <memory>
9#include <ostream>
10#include <string>
11
12class QObject;
13
15
16namespace Ra {
17namespace Gui {
18
19using namespace Core::Utils; // log
20
21SelectionManager::SelectionManager( ItemModel* model, QObject* parent ) :
22 QItemSelectionModel( model, parent ) {
23 connect( this, &SelectionManager::selectionChanged, this, &SelectionManager::printSelection );
24 connect( model, &ItemModel::modelRebuilt, this, &SelectionManager::onModelRebuilt );
25}
26
27bool SelectionManager::isSelected( const ItemEntry& ent ) const {
28 QModelIndex idx = itemModel()->findEntryIndex( ent );
29 if ( idx.isValid() ) {
30 const auto& pos = std::find( selectedIndexes().begin(), selectedIndexes().end(), idx );
31 return ( pos != selectedIndexes().end() );
32 }
33 return false;
34}
35
36std::vector<ItemEntry> SelectionManager::selectedEntries() const {
38 result.reserve( uint( selectedIndexes().size() ) );
39 for ( const auto& idx : selectedIndexes() ) {
40 result.push_back( itemModel()->getEntry( idx ) );
41 CORE_ASSERT( result.back().isValid(), "Invalid entry in selection" );
42 }
43 return result;
44}
45
46const ItemEntry& SelectionManager::currentItem() const {
47 // getEntry returns an invalid entry when given an invalid index.
48 return itemModel()->getEntry( currentIndex() );
49}
50
51void SelectionManager::select( const ItemEntry& ent, QItemSelectionModel::SelectionFlags command ) {
52 QModelIndex idx = itemModel()->findEntryIndex( ent );
53 if ( idx.isValid() && ent.isSelectable() ) { QItemSelectionModel::select( idx, command ); }
54}
55
56void SelectionManager::setCurrentEntry( const ItemEntry& ent,
57 QItemSelectionModel::SelectionFlags command ) {
58 QModelIndex idx = itemModel()->findEntryIndex( ent );
59 if ( idx.isValid() ) {
60 QItemSelectionModel::setCurrentIndex( idx, command );
61 emit currentChanged( idx, idx );
62 }
63}
64
65void SelectionManager::onModelRebuilt() {
66 clear();
67}
68
69void SelectionManager::printSelection() const {
70 LOG( logDEBUG ) << "Selected entries : ";
71 for ( const auto& ent : selectedEntries() ) {
72 LOG( logDEBUG ) << getEntryName( Ra::Engine::RadiumEngine::getInstance(), ent );
73 }
74 LOG( logDEBUG ) << "Current : "
75 << getEntryName( Ra::Engine::RadiumEngine::getInstance(), currentItem() );
76}
77} // namespace Gui
78} // namespace Ra
T back(T... args)
T find(T... args)
std::string getEntryName(const Engine::RadiumEngine *engine, const ItemEntry &ent)
Returns the name associated to the given item.
Definition ItemEntry.cpp:17
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4
T push_back(T... args)
T reserve(T... args)
bool isSelectable() const
Returns true if the item can be selected.
Definition ItemEntry.cpp:53