QModelIndex SearchEditorTreeView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) { if (cursorAction == QAbstractItemView::MoveNext) { QModelIndex index = currentIndex(); // Only the first column of a group is editable if (!model()->data(index.sibling(index.row(), 0), Qt::UserRole + 1).toBool()) { // Move to next column in row if there is one if (index.column() < (header()->count() - 1)) { return model()->index(index.row(), index.column() + 1, index.parent()); } } // Reset to first column in row so that default moveCursor moves it down one if (indexBelow(index).isValid()) { setCurrentIndex(model()->index(index.row(), 0, index.parent())); } } else if (cursorAction == QAbstractItemView::MovePrevious) { QModelIndex index = currentIndex(); // Only the first column of a group is editable if (!model()->data(index.sibling(index.row(), 0), Qt::UserRole + 1).toBool()) { // Move to previous column in row if there is one if (index.column() > 0) { return model()->index(index.row(), index.column() - 1, index.parent()); } } if (indexAbove(index).isValid()) { // If row above is a group always reset to first column otherwise last column if (model()->data(indexAbove(index).sibling(indexAbove(index).row(), 0), Qt::UserRole + 1).toBool()) { setCurrentIndex(model()->index(index.row(), 0, index.parent())); } else { setCurrentIndex(model()->index(index.row(), header()->count() - 1, index.parent())); } } } return QTreeView::moveCursor(cursorAction, modifiers); }
void hrTreeView::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Down) { QModelIndex index = indexBelow(currentIndex()); emit highlighted(index.isValid() ? index : currentIndex()); } else if (event->key() == Qt::Key_Up) { QModelIndex index = indexAbove(currentIndex()); emit highlighted(index.isValid() ? index : currentIndex()); } QTreeView::keyPressEvent(event); }
/* direction: 1=below only, 2=above only, 3=below then above */ QStandardItem* mail_listview::nearest(const mail_msg* msg, int direction) const { mail_item_model* model = this->model(); QStandardItem* item = model->item_from_id(msg->get_id()); if (item!=NULL) { if (direction==1 || direction==3) { QModelIndex index_below=indexBelow(item->index()); if (index_below.isValid()) { return model->itemFromIndex(index_below); } } if (direction==2 || direction==3) { QModelIndex index_above=indexAbove(item->index()); if (index_above.isValid()) { return model->itemFromIndex(index_above); } } } return NULL; }
void mail_listview::remove_msg(mail_msg* msg, bool select_next) { DBG_PRINTF(8, "mail_listview::remove_msg(mail_id=%d, select_next=%d)", msg->get_id(), select_next); mail_item_model* model = this->model(); QStandardItem* item = model->item_from_id(msg->get_id()); if (!item) return; QStandardItem* nearest = NULL; QSet<QStandardItem*> expanded_set; if (select_next) { QModelIndex index_below = indexBelow(item->index()); if (index_below.isValid()) { nearest = model->itemFromIndex(index_below); DBG_PRINTF(5, "nearest got from index_below"); } else { QModelIndex index_above = indexAbove(item->index()); if (index_above.isValid()) { nearest = model->itemFromIndex(index_above); DBG_PRINTF(5, "nearest got from index_above"); } } if (nearest) { /* record the expansion states of all child items to set them back after the parent's removal */ collect_expansion_states(item, expanded_set); } } model->remove_msg(msg); if (select_next && nearest) { QModelIndex index = nearest->index(); DBG_PRINTF(5,"making current index row=%d col=%d parent is valid=%d", index.row(), index.column(), index.parent().isValid()?1:0); foreach (QStandardItem *child, expanded_set) { setExpanded(child->index(), true); }
void TabTreeView::adjustStyleOption(QStyleOptionViewItem *option) { const QModelIndex index = option->index; option->state.setFlag(QStyle::State_Active, true); option->state.setFlag(QStyle::State_HasFocus, false); option->state.setFlag(QStyle::State_Selected, index.data(TabModel::CurrentTabRole).toBool()); if (!index.isValid()) { option->viewItemPosition = QStyleOptionViewItem::Invalid; } else if (model()->rowCount() == 1) { option->viewItemPosition = QStyleOptionViewItem::OnlyOne; } else { if (!indexAbove(index).isValid()) { option->viewItemPosition = QStyleOptionViewItem::Beginning; } else if (!indexBelow(index).isValid()) { option->viewItemPosition = QStyleOptionViewItem::End; } else { option->viewItemPosition = QStyleOptionViewItem::Middle; } } }