/// Move to next index bool TreeExtensionOld::moveDown() { auto model = impl_->currentIndex_.model(); assert( model != nullptr ); if (impl_->expanded( impl_->currentIndex_ )) { // Move to the first child item when the current item is expanded impl_->currentIndex_ = impl_->currentIndex_.child( 0, 0 ); return handleCurrentIndexChanged(); } else { QModelIndex parent = impl_->currentIndex_.parent(); int nextRow = impl_->currentIndex_.row() + 1; while (parent.isValid()) { if (nextRow < model->rowCount( parent )) { // Update the current index if the next item is available impl_->currentIndex_ = parent.child( nextRow, 0 ); return handleCurrentIndexChanged(); break; } else { // Reached the bottom, keep searching the parent nextRow = parent.row() + 1; if (!parent.parent().isValid()) { break; } parent = parent.parent(); } } parent = parent.isValid() ? parent : impl_->currentIndex_; if (nextRow < model->rowCount( parent )) { QModelIndex sibling = parent.sibling( nextRow, impl_->currentIndex_.column() ); if (sibling.isValid()) { impl_->currentIndex_ = sibling; return handleCurrentIndexChanged(); } } } return false; }
/// Expand the current item if it is expandable or move to the first child bool TreeExtensionOld::moveRight() { auto model = impl_->currentIndex_.model(); assert( model != nullptr ); // Make sure the current item has children if (model->hasChildren( impl_->currentIndex_ ) ) { if (impl_->expanded( impl_->currentIndex_ )) { // Select the first child if the current item is expanded impl_->currentIndex_ = impl_->currentIndex_.child( 0, 0 ); return handleCurrentIndexChanged(); } else { // Expand the current item int expandedRole = -1; this->encodeRole( ExpandedRole::roleId_, expandedRole ); setData( impl_->currentIndex_, QVariant( true ), expandedRole ); } } return false; }
/// Collapse the current item if it is collapsible or move to the parent bool TreeExtensionOld::moveLeft() { auto model = impl_->currentIndex_.model(); assert( model != nullptr ); // Move up to the parent if there are no children or not expanded if (!model->hasChildren( impl_->currentIndex_ ) || !impl_->expanded( impl_->currentIndex_ )) { // Move up to the parent QModelIndex parent = impl_->currentIndex_.parent(); if (parent.isValid()) { // Update the current index if the parent is valid impl_->currentIndex_ = parent; return handleCurrentIndexChanged(); } } else { // Collapse the current item int expandedRole = -1; this->encodeRole( ExpandedRole::roleId_, expandedRole ); setData( impl_->currentIndex_, QVariant( false ), expandedRole ); } return false; }
/// Move to previous index bool TreeExtensionOld::moveUp() { auto model = impl_->currentIndex_.model(); assert( model != nullptr ); int prevRow = impl_->currentIndex_.row() - 1; if (0 <= prevRow) { QModelIndex prevIndex = impl_->currentIndex_.sibling( prevRow, 0 ); int prevIndexsBottomRow = 0; do { // Previous item's bottom row prevIndexsBottomRow = model->rowCount( prevIndex ) - 1; impl_->currentIndex_ = prevIndex; if (model->hasChildren( impl_->currentIndex_ )) { // Keep search in child tree if the bottom item has child tree and expanded prevIndexsBottomRow = model->rowCount( impl_->currentIndex_ ) - 1; prevIndex = impl_->currentIndex_.child( prevIndexsBottomRow, 0 ); } } while (model->hasChildren( impl_->currentIndex_ ) && impl_->expanded( impl_->currentIndex_ )); return handleCurrentIndexChanged(); } else { // We are the first child, move up to the parent QModelIndex parent = impl_->currentIndex_.parent(); if (parent.isValid()) { // Update the current index if the parent is valid impl_->currentIndex_ = parent; return handleCurrentIndexChanged(); } } return false; }
void QtAffiliationEditor::setAffiliations(MUCOccupant::Affiliation affiliation, const std::vector<JID>& jids) { affiliations_[affiliation] = jids; if (affiliationFromIndex(ui_.affiliation->currentIndex()) == affiliation) { handleCurrentIndexChanged(ui_.affiliation->currentIndex()); } }