Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
SelectionManager.cpp
1 #include <Gui/SelectionManager/SelectionManager.hpp>
2 
3 #include <Core/Utils/Log.hpp>
4 #include <Engine/RadiumEngine.hpp>
5 
7 
8 namespace Ra {
9 namespace Gui {
10 
11 using namespace Core::Utils; // log
12 
13 SelectionManager::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 
19 bool 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 
28 std::vector<ItemEntry> SelectionManager::selectedEntries() const {
29  std::vector<ItemEntry> result;
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 
38 const ItemEntry& SelectionManager::currentItem() const {
39  // getEntry returns an invalid entry when given an invalid index.
40  return itemModel()->getEntry( currentIndex() );
41 }
42 
43 void 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 
48 void 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 
57 void SelectionManager::onModelRebuilt() {
58  clear();
59 }
60 
61 void 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
Definition: Cage.cpp:3
bool isSelectable() const
Returns true if the item can be selected.
Definition: ItemEntry.cpp:52