Radium Engine  1.5.20
Loading...
Searching...
No Matches
SelectionManager.cpp
1#include <Gui/SelectionManager/SelectionManager.hpp>
2
3#include <Core/Utils/Log.hpp>
4#include <Engine/RadiumEngine.hpp>
5
7
8namespace Ra {
9namespace Gui {
10
11using namespace Core::Utils; // log
12
13SelectionManager::SelectionManager( ItemModel* model, QObject* parent ) :
14 QItemSelectionModel( model, parent ) {
15 connect( this, &SelectionManager::selectionChanged, this, &SelectionManager::printSelection );
16 connect( model, &ItemModel::modelRebuilt, this, &SelectionManager::onModelRebuilt );
17}
18
19bool SelectionManager::isSelected( const ItemEntry& ent ) const {
20 QModelIndex idx = itemModel()->findEntryIndex( ent );
21 if ( idx.isValid() ) {
22 const auto& pos = std::find( selectedIndexes().begin(), selectedIndexes().end(), idx );
23 return ( pos != selectedIndexes().end() );
24 }
25 return false;
26}
27
28std::vector<ItemEntry> SelectionManager::selectedEntries() const {
30 result.reserve( uint( selectedIndexes().size() ) );
31 for ( const auto& idx : selectedIndexes() ) {
32 result.push_back( itemModel()->getEntry( idx ) );
33 CORE_ASSERT( result.back().isValid(), "Invalid entry in selection" );
34 }
35 return result;
36}
37
38const ItemEntry& SelectionManager::currentItem() const {
39 // getEntry returns an invalid entry when given an invalid index.
40 return itemModel()->getEntry( currentIndex() );
41}
42
43void SelectionManager::select( const ItemEntry& ent, QItemSelectionModel::SelectionFlags command ) {
44 QModelIndex idx = itemModel()->findEntryIndex( ent );
45 if ( idx.isValid() && ent.isSelectable() ) { QItemSelectionModel::select( idx, command ); }
46}
47
48void SelectionManager::setCurrentEntry( const ItemEntry& ent,
49 QItemSelectionModel::SelectionFlags command ) {
50 QModelIndex idx = itemModel()->findEntryIndex( ent );
51 if ( idx.isValid() ) {
52 QItemSelectionModel::setCurrentIndex( idx, command );
53 emit currentChanged( idx, idx );
54 }
55}
56
57void SelectionManager::onModelRebuilt() {
58 clear();
59}
60
61void SelectionManager::printSelection() const {
62 LOG( logDEBUG ) << "Selected entries : ";
63 for ( const auto& ent : selectedEntries() ) {
64 LOG( logDEBUG ) << getEntryName( Ra::Engine::RadiumEngine::getInstance(), ent );
65 }
66 LOG( logDEBUG ) << "Current : "
67 << getEntryName( Ra::Engine::RadiumEngine::getInstance(), currentItem() );
68}
69} // namespace Gui
70} // 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:16
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
T push_back(T... args)
T reserve(T... args)
bool isSelectable() const
Returns true if the item can be selected.
Definition ItemEntry.cpp:52