📄 q3listbox.cpp
字号:
while (i) { Q3ListBoxItem * n = i->n; i->n = i->p = 0; delete i; i = n; } d->count = 0; d->numRows = 1; d->numColumns = 1; d->currentRow = 0; d->currentColumn = 0; d->mousePressRow = -1; d->mousePressColumn = -1; d->mouseMoveRow = -1; d->mouseMoveColumn = -1; clearSelection(); blockSignals(blocked); triggerUpdate(true); d->last = 0; d->clearing = false;}/*! Returns the text at position \a index, or an empty string if there is no text at that position. \sa pixmap()*/QString Q3ListBox::text(int index) const{ Q3ListBoxItem * i = item(index); if (i) return i->text(); return QString();}/*! Returns a pointer to the pixmap at position \a index, or 0 if there is no pixmap there. \sa text()*/const QPixmap *Q3ListBox::pixmap(int index) const{ Q3ListBoxItem * i = item(index); if (i) return i->pixmap(); return 0;}/*! \overload Replaces the item at position \a index with a new list box text item with text \a text. The operation is ignored if \a index is out of range. \sa insertItem(), removeItem()*/void Q3ListBox::changeItem(const QString &text, int index){ if(index >= 0 && index < (int)count()) changeItem(new Q3ListBoxText(text), index);}/*! \overload Replaces the item at position \a index with a new list box pixmap item with pixmap \a pixmap. The operation is ignored if \a index is out of range. \sa insertItem(), removeItem()*/void Q3ListBox::changeItem(const QPixmap &pixmap, int index){ if(index >= 0 && index < (int)count()) changeItem(new Q3ListBoxPixmap(pixmap), index);}/*! \overload Replaces the item at position \a index with a new list box pixmap item with pixmap \a pixmap and text \a text. The operation is ignored if \a index is out of range. \sa insertItem(), removeItem()*/void Q3ListBox::changeItem(const QPixmap &pixmap, const QString &text, int index){ if(index >= 0 && index < (int)count()) changeItem(new Q3ListBoxPixmap(pixmap, text), index);}/*! Replaces the item at position \a index with \a lbi. If \a index is negative or too large, changeItem() does nothing. The item that has been changed will become selected. \sa insertItem(), removeItem()*/void Q3ListBox::changeItem(const Q3ListBoxItem *lbi, int index){ if (!lbi || index < 0 || index >= (int)count()) return; removeItem(index); insertItem(lbi, index); setCurrentItem(index);}/*! \property Q3ListBox::numItemsVisible \brief the number of visible items. Both partially and entirely visible items are counted.*/int Q3ListBox::numItemsVisible() const{ doLayout(); int columns = 0; int x = contentsX(); int i=0; while (i < (int)d->columnPos.size()-1 && d->columnPos[i] < x) i++; if (i < (int)d->columnPos.size()-1 && d->columnPos[i] > x) columns++; x += visibleWidth(); while (i < (int)d->columnPos.size()-1 && d->columnPos[i] < x) { i++; columns++; } int y = contentsY(); int rows = 0; while (i < (int)d->rowPos.size()-1 && d->rowPos[i] < y) i++; if (i < (int)d->rowPos.size()-1 && d->rowPos[i] > y) rows++; y += visibleHeight(); while (i < (int)d->rowPos.size()-1 && d->rowPos[i] < y) { i++; rows++; } return rows*columns;}int Q3ListBox::currentItem() const{ if (!d->current || !d->head) return -1; return index(d->current);}/*! \property Q3ListBox::currentText \brief the text of the current item. This is equivalent to text(currentItem()).*//*! \property Q3ListBox::currentItem \brief the current highlighted item When setting this property, the highlighting is moved to the item and the list box scrolled as necessary. If no item is current, currentItem() returns -1.*/void Q3ListBox::setCurrentItem(int index){ setCurrentItem(item(index));}/*! \reimp*/QVariant Q3ListBox::inputMethodQuery(Qt::InputMethodQuery query) const{ if (query == Qt::ImMicroFocus) return d->current ? itemRect(d->current) : QRect(); return QWidget::inputMethodQuery(query);}/*! \overload Sets the current item to the Q3ListBoxItem \a i.*/void Q3ListBox::setCurrentItem(Q3ListBoxItem * i){ if (!i || d->current == i) return; Q3ListBoxItem * o = d->current; d->current = i; int ind = index(i); if (i && selectionMode() == Single) { bool changed = false; if (o && o->s) { changed = true; o->s = false; } if (i && !i->s && d->selectionMode != NoSelection && i->isSelectable()) { i->s = true; changed = true; emit selectionChanged(i);#ifndef QT_NO_ACCESSIBILITY QAccessible::updateAccessibility(viewport(), ind+1, QAccessible::StateChanged);#endif } if (changed) { emit selectionChanged();#ifndef QT_NO_ACCESSIBILITY QAccessible::updateAccessibility(viewport(), 0, QAccessible::Selection);#endif } } d->currentColumn = ind / numRows(); d->currentRow = ind % numRows(); if (o) updateItem(o); if (i) updateItem(i); // scroll after the items are redrawn d->visibleTimer->start(1, true); QString tmp; if (i) tmp = i->text(); emit highlighted(i); if (!tmp.isNull()) emit highlighted(tmp); emit highlighted(ind); emit currentChanged(i);#ifndef QT_NO_ACCESSIBILITY QAccessible::updateAccessibility(viewport(), ind+1, QAccessible::Focus);#endif}/*! Returns a pointer to the item at position \a index, or 0 if \a index is out of bounds. \sa index()*/Q3ListBoxItem *Q3ListBox::item(int index) const{ if (index < 0 || index > d->count -1) return 0; Q3ListBoxItem * i = d->head; if (d->cache && index > 0) { i = d->cache; int idx = d->cacheIndex; while (i && idx < index) { idx++; i = i->n; } while (i && idx > index) { idx--; i = i->p; } } else { int idx = index; while (i && idx > 0) { idx--; i = i->n; } } if (index > 0) { d->cache = i; d->cacheIndex = index; } return i;}/*! Returns the index of \a lbi, or -1 if the item is not in this list box or \a lbi is 0. \sa item()*/int Q3ListBox::index(const Q3ListBoxItem * lbi) const{ if (!lbi) return -1; Q3ListBoxItem * i_n = d->head; int c_n = 0; if (d->cache) { i_n = d->cache; c_n = d->cacheIndex; } Q3ListBoxItem* i_p = i_n; int c_p = c_n; while ((i_n != 0 || i_p != 0) && i_n != lbi && i_p != lbi) { if (i_n) { c_n++; i_n = i_n->n; } if (i_p) { c_p--; i_p = i_p->p; } } if (i_p == lbi) return c_p; if (i_n == lbi) return c_n; return -1;}/*! Returns true if the item at position \a index is at least partly visible; otherwise returns false.*/bool Q3ListBox::itemVisible(int index){ Q3ListBoxItem * i = item(index); return i ? itemVisible(i) : false;}/*! \overload Returns true if \a item is at least partly visible; otherwise returns false.*/bool Q3ListBox::itemVisible(const Q3ListBoxItem * item){ if (d->layoutDirty) doLayout(); int i = index(item); int col = i / numRows(); int row = i % numRows(); return (d->columnPos[col] < contentsX()+visibleWidth() && d->rowPos[row] < contentsY()+visibleHeight() && d->columnPos[col+1] > contentsX() && d->rowPos[row+1] > contentsY());}/*! \reimp */void Q3ListBox::mousePressEvent(QMouseEvent *e){ mousePressEventEx(e);}void Q3ListBox::mousePressEventEx(QMouseEvent *e){ d->mouseInternalPress = true; Q3ListBoxItem * i = itemAt(e->pos()); if (!i && !d->current && d->head) { d->current = d->head; updateItem(d->head); } if (!i && (d->selectionMode != Single || e->button() == Qt::RightButton) && !(e->state() & Qt::ControlButton)) clearSelection(); d->select = d->selectionMode == Multi ? (i ? !i->isSelected() : false) : true; d->pressedSelected = i && i->s; if (i) d->selectAnchor = i; if (i) { switch(selectionMode()) { default: case Single: if (!i->s || i != d->current) { if (i->isSelectable()) setSelected(i, true); else setCurrentItem(i); } break; case Extended: if (i) { bool shouldBlock = false; if (!(e->state() & Qt::ShiftButton) && !(e->state() & Qt::ControlButton)) { if (!i->isSelected()) { bool b = signalsBlocked(); blockSignals(true); clearSelection(); blockSignals(b); } setSelected(i, true); d->dragging = true; // always assume dragging shouldBlock = true; } else if (e->state() & Qt::ShiftButton) { d->pressedSelected = false; Q3ListBoxItem *oldCurrent = item(currentItem()); bool down = index(oldCurrent) < index(i); Q3ListBoxItem *lit = down ? oldCurrent : i; bool select = d->select; bool blocked = signalsBlocked(); blockSignals(true); for (;; lit = lit->n) { if (!lit) { triggerUpdate(false); break; } if (down && lit == i) { setSelected(i, select); triggerUpdate(false); break; } if (!down && lit == oldCurrent) { setSelected(oldCurrent, select); triggerUpdate(false); break; } setSelected(lit, select); } blockSignals(blocked); emit selectionChanged(); } else if (e->state() & Qt::ControlButton) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -