1 #include <Gui/SelectionManager/SelectionManager.hpp>
3 #include <Core/Utils/Log.hpp>
4 #include <Engine/RadiumEngine.hpp>
11 using namespace Core::Utils;
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 );
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() );
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" );
38 const ItemEntry& SelectionManager::currentItem()
const {
40 return itemModel()->getEntry( currentIndex() );
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 ); }
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 );
57 void SelectionManager::onModelRebuilt() {
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 );
66 LOG( logDEBUG ) <<
"Current : "
67 << getEntryName( Ra::Engine::RadiumEngine::getInstance(), currentItem() );
bool isSelectable() const
Returns true if the item can be selected.