/** * Sets a new current item based on key events in the edit box. * This slot is connected to the keyPressed() signal of the edit widget. * @param pKey The key evant passed by the edit box */ void SearchList::slotKeyPressed(QKeyEvent* pKey) { QListViewItem* pItem, * pNewItem; int nPageSize, nPos; // Select the current item, or the first one if there is no current item pItem = m_pList->currentItem(); // Set a new current item based on the pressed key switch (pKey->key()) { case Qt::Key_Up: if (pItem) { for (pNewItem = pItem->itemAbove(); pNewItem && !SEARCH_MATCH(pNewItem); pNewItem = pNewItem->itemAbove()); if (pNewItem) pItem = pNewItem; } break; case Qt::Key_Down: if (pItem) { for (pNewItem = pItem->itemBelow(); pNewItem && !SEARCH_MATCH(pNewItem); pNewItem = pNewItem->itemBelow()); if (pNewItem) pItem = pNewItem; } break; case Qt::Key_PageUp: nPageSize = m_pList->visibleHeight() / pItem->height(); for (nPos = 0; pItem && pItem->itemAbove() && (nPos < nPageSize); nPos++) pItem = pItem->itemAbove(); break; case Qt::Key_PageDown: nPageSize = m_pList->visibleHeight() / pItem->height(); for (nPos = 0; pItem && pItem->itemBelow() && (nPos < nPageSize); nPos++) pItem = pItem->itemBelow(); break; default: pKey->ignore(); return; } // Select the first item if no other item was selected if (pItem == NULL) pItem = m_pList->firstChild(); // Select the new item if (pItem) { m_pList->setSelected(pItem, true); m_pList->ensureItemVisible(pItem); } }
/** * Sets a new current item based on key events in the edit box. * This slot is connected to the keyPressed() signal of the edit widget. * @param pKey The key evant passed by the edit box */ void SearchList::slotKeyPressed(QKeyEvent* pKey) { QTreeWidgetItem *pItem, *pNewItem; QModelIndex index; int nPageSize; int key = pKey->key(); // Select the current item, or the first one if there is no current item pItem = m_pList->currentItem(); if ((key == Qt::Key_PageUp) || (key == Qt::Key_PageDown)){ nPageSize = m_pList->contentsRect().height() / m_pList->sizeHintForRow(0); index = m_pList->currentIndex(); } // Set a new current item based on the pressed key switch (key) { case Qt::Key_Up: if (pItem) { for (pNewItem = m_pList->itemAbove(pItem); pNewItem && !SEARCH_MATCH(pNewItem); pNewItem = m_pList->itemAbove(pNewItem)); if (pNewItem) pItem = pNewItem; } break; case Qt::Key_Down: if (pItem) { for (pNewItem = m_pList->itemBelow(pItem); pNewItem && !SEARCH_MATCH(pNewItem); pNewItem = m_pList->itemBelow(pNewItem)); if (pNewItem) pItem = pNewItem; } break; case Qt::Key_PageUp: pNewItem = m_pList->topLevelItem(index.row() - (nPageSize - 1)); if ( pNewItem != NULL ){ m_pList->scrollToItem(pNewItem, QAbstractItemView::EnsureVisible); pItem = pNewItem; } else { m_pList->scrollToTop(); pItem = m_pList->topLevelItem(0); } break; case Qt::Key_PageDown: pNewItem = m_pList->topLevelItem(index.row() + (nPageSize - 1)); if ( pNewItem != NULL ){ m_pList->scrollToItem(pNewItem, QAbstractItemView::EnsureVisible); pItem = pNewItem; } else { m_pList->scrollToBottom(); pItem = m_pList->topLevelItem(m_pList->topLevelItemCount() - 1); } break; default: pKey->ignore(); return; } // Select the first item if no other item was selected if (pItem == NULL) pItem = m_pList->topLevelItem(0); // Select the new item if (pItem) { pItem->setSelected(true); pItem->setHidden(false); m_pList->setCurrentItem(pItem); } }