📄 q3header.cpp
字号:
int y2 = height() - 3; if (orient == Qt::Vertical) { int t = x; x = y; y = t; t = x2; x2 = y2; y2 = t; } repaint(x, y, x2-x+1, y2-y+1);}/*! \fn int Q3Header::cellAt(int) const Use sectionAt() instead. Returns the index at which the section is displayed, which contains \a pos in widget coordinates, or -1 if \a pos is outside the header sections.*//* Tries to find a line that is not a neighbor of \c handleIdx.*/int Q3Header::findLine(int c){ int i = 0; if (c > d->lastPos || (reverse() && c < 0)) { return d->count; } else { int section = sectionAt(c); if (section < 0) return handleIdx; i = d->s2i[section]; } int MARKSIZE = style()->pixelMetric(QStyle::PM_HeaderMarkSize); if (i == handleIdx) return i; if (i == handleIdx - 1 && pPos(handleIdx) - c > MARKSIZE/2) return i; if (i == handleIdx + 1 && c - pPos(i) > MARKSIZE/2) return i + 1; if (c - pPos(i) > pSize(i) / 2) return i + 1; else return i;}/*! Returns the handle at position \a p, or -1 if there is no handle at \a p.*/int Q3Header::handleAt(int p){ int section = d->sectionAt(p); if (section >= 0) { int GripMargin = (bool)d->resize[section] ? style()->pixelMetric(QStyle::PM_HeaderGripMargin) : 0; int index = d->s2i[section]; if ((index > 0 && p < d->positions[index] + GripMargin) || (p > d->positions[index] + d->sizes[section] - GripMargin)) { if (index > 0 && p < d->positions[index] + GripMargin) section = d->i2s[--index]; // don't show icon if streaching is enabled it is at the end of the last section if (d->resize.testBit(section) && (d->fullSize == -2 || index != count() - 1)) { return section; } } } return -1;}/*! Use moveSection() instead. Moves the section that is currently displayed at index \a fromIdx to index \a toIdx.*/void Q3Header::moveCell(int fromIdx, int toIdx){ moveSection(mapToSection(fromIdx), toIdx);}/*! Move and signal and repaint. */void Q3Header::handleColumnMove(int fromIdx, int toIdx){ int s = d->i2s[fromIdx]; if (fromIdx < toIdx) toIdx++; //Convert to QRect r = sRect(fromIdx); r |= sRect(toIdx); moveSection(s, toIdx); update(r); emit moved(fromIdx, toIdx); emit indexChange(s, fromIdx, toIdx);}/*! \reimp*/void Q3Header::keyPressEvent(QKeyEvent *e){ int i = d->focusIdx; if (e->key() == Qt::Key_Space) { //don't do it if we're doing something with the mouse if (state == Idle && d->clicks[d->i2s[d->focusIdx] ]) { handleIdx = i; state = Pressed; repaint(sRect(handleIdx)); emit pressed(d->i2s[i]); } } else if (orientation() == Qt::Horizontal && (e->key() == Qt::Key_Right || e->key() == Qt::Key_Left) || orientation() == Qt::Vertical && (e->key() == Qt::Key_Up || e->key() == Qt::Key_Down)) { int dir = e->key() == Qt::Key_Right || e->key() == Qt::Key_Down ? 1 : -1; int s = d->i2s[i]; if (e->state() & Qt::ControlButton && d->resize[s]) { //resize int step = e->state() & Qt::ShiftButton ? dir : 10*dir; int c = d->positions[i] + d->sizes[s] + step; handleColumnResize(i, c, true); } else if (e->state() & (Qt::AltButton|Qt::MetaButton) && d->move) { //move section int i2 = (i + count() + dir) % count(); d->focusIdx = i2; handleColumnMove(i, i2); } else { //focus on different section QRect r = sRect(d->focusIdx); d->focusIdx = (d->focusIdx + count() + dir) % count(); r |= sRect(d->focusIdx); update(r); } } else { e->ignore(); }}/*! \reimp*/void Q3Header::keyReleaseEvent(QKeyEvent *e){ switch (e->key()) { case Qt::Key_Space: //double check that this wasn't started with the mouse if (state == Pressed && handleIdx == d->focusIdx) { repaint(sRect(handleIdx)); int section = d->i2s[d->focusIdx]; emit released(section); emit sectionClicked(handleIdx); emit clicked(section); state = Idle; handleIdx = -1; } break; default: e->ignore(); }}/*! \reimp*/void Q3Header::mousePressEvent(QMouseEvent *e){ if (e->button() != Qt::LeftButton || state != Idle) return; oldHIdxSize = handleIdx; handleIdx = 0; int c = orient == Qt::Horizontal ? e->pos().x() : e->pos().y(); c += offset(); if (reverse()) c = d->lastPos - c; int section = d->sectionAt(c); if (section < 0) return; int GripMargin = (bool)d->resize[section] ? style()->pixelMetric(QStyle::PM_HeaderGripMargin) : 0; int index = d->s2i[section]; if ((index > 0 && c < d->positions[index] + GripMargin) || (c > d->positions[index] + d->sizes[section] - GripMargin)) { if (c < d->positions[index] + GripMargin) handleIdx = index-1; else handleIdx = index; if (d->lastPos <= (orient == Qt::Horizontal ? width() : height()) && d->fullSize != -2 && handleIdx == count() - 1) { handleIdx = -1; return; } oldHIdxSize = d->sizes[d->i2s[handleIdx]]; state = d->resize[d->i2s[handleIdx] ] ? Sliding : Blocked; } else if (index >= 0) { oldHandleIdx = handleIdx = index; moveToIdx = -1; state = d->clicks[d->i2s[handleIdx] ] ? Pressed : Blocked; clickPos = c; repaint(sRect(handleIdx)); if(oldHandleIdx != handleIdx) repaint(sRect(oldHandleIdx)); emit pressed(section); } d->pressDelta = c - (d->positions[handleIdx] + d->sizes[d->i2s[handleIdx]]);}/*! \reimp*/void Q3Header::mouseReleaseEvent(QMouseEvent *e){ if (e->button() != Qt::LeftButton) return; int oldOldHandleIdx = oldHandleIdx; State oldState = state; state = Idle; switch (oldState) { case Pressed: { int section = d->i2s[handleIdx]; emit released(section); if (sRect(handleIdx).contains(e->pos())) { oldHandleIdx = handleIdx; emit sectionClicked(handleIdx); emit clicked(section); } else { handleIdx = oldHandleIdx; } repaint(sRect(handleIdx)); if (oldOldHandleIdx != handleIdx) repaint(sRect(oldOldHandleIdx)); } break; case Sliding: { int c = orient == Qt::Horizontal ? e->pos().x() : e->pos().y(); c += offset(); if (reverse()) c = d->lastPos - c; handleColumnResize(handleIdx, c - d->pressDelta, true); } break; case Moving: {#ifndef QT_NO_CURSOR unsetCursor();#endif int section = d->i2s[handleIdx]; if (handleIdx != moveToIdx && moveToIdx != -1) { moveSection(section, moveToIdx); handleIdx = oldHandleIdx; emit moved(handleIdx, moveToIdx); emit indexChange(section, handleIdx, moveToIdx); emit released(section); repaint(); // a bit overkill, but removes the handle as well } else { if (sRect(handleIdx).contains(e->pos())) { oldHandleIdx = handleIdx; emit released(section); emit sectionClicked(handleIdx); emit clicked(section); } else { handleIdx = oldHandleIdx; } repaint(sRect(handleIdx)); if(oldOldHandleIdx != handleIdx) repaint(sRect(oldOldHandleIdx)); } break; } case Blocked: //nothing break; default: // empty, probably. Idle, at any rate. break; }}/*! \reimp*/void Q3Header::mouseMoveEvent(QMouseEvent *e){ int c = orient == Qt::Horizontal ? e->pos().x() : e->pos().y(); c += offset(); int pos = c; if(reverse()) c = d->lastPos - c; switch(state) { case Idle:#ifndef QT_NO_CURSOR if (handleAt(c) < 0) unsetCursor(); else if (orient == Qt::Horizontal) setCursor(Qt::splitHCursor); else setCursor(Qt::splitVCursor);#endif break; case Blocked: break; case Pressed: if (QABS(c - clickPos) > 4 && d->move) { state = Moving; moveToIdx = -1;#ifndef QT_NO_CURSOR if (orient == Qt::Horizontal) setCursor(Qt::SizeHorCursor); else setCursor(Qt::SizeVerCursor);#endif } break; case Sliding: handleColumnResize(handleIdx, c, false, false); break; case Moving: { int newPos = findLine(pos); if (newPos != moveToIdx) { if (moveToIdx == handleIdx || moveToIdx == handleIdx + 1) repaint(sRect(handleIdx)); else unMarkLine(moveToIdx); moveToIdx = newPos; if (moveToIdx == handleIdx || moveToIdx == handleIdx + 1) paintRect(pPos(handleIdx), pSize(handleIdx)); else markLine(moveToIdx); } break; } default: qWarning("Q3Header::mouseMoveEvent: (%s) unknown state", objectName().toLocal8Bit().data()); break; }}/*! \reimp */void Q3Header::mouseDoubleClickEvent(QMouseEvent *e){ int p = orient == Qt::Horizontal ? e->pos().x() : e->pos().y(); p += offset(); if(reverse()) p = d->lastPos - p; int header = handleAt(p); if (header >= 0) emit sectionHandleDoubleClicked(header);}/* Handles resizing of sections. This means it redraws the relevant parts of the header.*/void Q3Header::handleColumnResize(int index, int c, bool final, bool recalcAll){ int section = d->i2s[index]; int GripMargin = (bool)d->resize[section] ? style()->pixelMetric(QStyle::PM_HeaderGripMargin) : 0; int lim = d->positions[index] + 2*GripMargin; if (c == lim) return; if (c < lim) c = lim; int oldSize = d->sizes[section]; int newSize = c - d->positions[index]; d->sizes[section] = newSize; calculatePositions(!recalcAll, !recalcAll ? section : 0); int pos = d->positions[index]-offset(); if(reverse()) // repaint the whole thing. Could be optimized (lars) repaint(0, 0, width(), height()); else if (orient == Qt::Horizontal) repaint(pos, 0, width() - pos, height()); else repaint(0, pos, width(), height() - pos); int os = 0, ns = 0; if (tracking() && oldSize != newSize) { os = oldSize; ns = newSize; emit sizeChange(section, oldSize, newSize); } else if (!tracking() && final && oldHIdxSize != newSize) { os = oldHIdxSize; ns = newSize; emit sizeChange(section, oldHIdxSize, newSize); } if (os != ns) { if (d->fullSize == -1) { d->fullSize = count() - 1; adjustHeaderSize(); d->fullSize = -1; } else if (d->fullSize >= 0) { int old = d->fullSize; d->fullSize = count() - 1; adjustHeaderSize(); d->fullSize = old; } }}/*! Returns the rectangle covered by the section at index \a index.*/QRect Q3Header::sRect(int index){ int section = mapToSection(index); if (count() > 0 && index >= count()) { int s = d->positions[count() - 1] - offset() + d->sizes[mapToSection(count() - 1)]; if (orient == Qt::Horizontal) return QRect(s, 0, width() - s + 10, height()); else return QRect(0, s, width(), height() - s + 10); } if (section < 0) return rect(); // ### eeeeevil if (reverse()) return QRect( d->lastPos - d->positions[index] - d->sizes[section] -offset(), 0, d->sizes[section], height()); else if (orient == Qt::Horizontal) return QRect( d->positions[index]-offset(), 0, d->sizes[section], height()); else return QRect(0, d->positions[index]-offset(), width(), d->sizes[section]);}/*! Returns the rectangle covered by section \a section.*/QRect Q3Header::sectionRect(int section) const{ int index = mapToIndex(section); if (section < 0) return rect(); // ### eeeeevil if (reverse()) return QRect( d->lastPos - d->positions[index] - d->sizes[section] -offset(), 0, d->sizes[section], height()); else if (orient == Qt::Horizontal) return QRect( d->positions[index]-offset(), 0, d->sizes[section], height()); else return QRect(0, d->positions[index]-offset(), width(), d->sizes[section]);}/*! \overload Sets the icon for section \a section to \a icon and the text to \a s. The section's width is set to \a size if \a size \>= 0; otherwise it is left unchanged. If the section does not exist, nothing happens.*/void Q3Header::setLabel(int section, const QIcon& icon, const QString &s, int size){ if (section < 0 || section >= count()) return; delete d->icons[section]; d->icons[section] = new QIcon(icon); setLabel(section, s, size);}/*! Sets the text of section \a section to \a s. The section's width is set to \a size if \a size \>= 0; otherwise it is left unchanged. Any icon set that has been set for this section remains unchanged. If the section does not exist, nothing happens.*/void Q3Header::setLabel(int section, const QString &s, int size){ if (section < 0 || section >= count()) return; d->labels[section] = s; d->nullStringLabels.setBit(section, s.isNull()); setSectionSizeAndHeight(section, size); if (updatesEnabled()) { updateGeometry(); calculatePositions(); update(); }}bool qt_qheader_label_return_null_strings = false;/*! Returns the text for section \a section. If the section does not exist, returns an empty string.*/QString Q3Header::label(int section) const{ if (section < 0 || section >= count())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -