📄 q3listview.cpp
字号:
return; int oldW = 0; int oldH = 0; if (pixmap(column)) { oldW = pixmap(column)->width(); oldH = pixmap(column)->height(); } Q3ListViewPrivate::ItemColumnInfo * l = (Q3ListViewPrivate::ItemColumnInfo*) columns; if (!l) { l = new Q3ListViewPrivate::ItemColumnInfo; columns = (void*)l; } for(int c = 0; c < column; c++) { if (!l->next) l->next = new Q3ListViewPrivate::ItemColumnInfo; l = l->next; } if ((pm.isNull() && (!l->pm || l->pm->isNull())) || (l->pm && pm.serialNumber() == l->pm->serialNumber())) return; if (pm.isNull()) { delete l->pm; l->pm = 0; } else { if (l->pm) *(l->pm) = pm; else l->pm = new QPixmap(pm); } int newW = 0; int newH = 0; if (pixmap(column)) { newW = pixmap(column)->width(); newH = pixmap(column)->height(); } if (oldW != newW || oldH != newH) { setup(); widthChanged(column); invalidateHeight(); } Q3ListView *lv = listView(); if (lv) { lv->triggerUpdate(); }}/*! Returns the pixmap for \a column, or 0 if there is no pixmap for \a column. \sa setText() setPixmap()*/const QPixmap * Q3ListViewItem::pixmap(int column) const{ Q3ListViewPrivate::ItemColumnInfo * l = (Q3ListViewPrivate::ItemColumnInfo*) columns; while(column && l) { l = l->next; column--; } return (l && l->pm) ? l->pm : 0;}/* This function paints the contents of one column of an item and aligns it as described by \a align. \a p is a QPainter open on the relevant paint device. \a p is translated so (0, 0) is the top-left pixel in the cell and \a width-1, height()-1 is the bottom-right pixel \e in the cell. The other properties of \a p (pen, brush, etc) are undefined. \a pal is the color group to use. \a column is the logical column number within the item that is to be painted; 0 is the column which may contain a tree. This function may use Q3ListView::itemMargin() for readability spacing on the left and right sides of data such as text, and should honor isSelected() and Q3ListView::allColumnsShowFocus(). If you reimplement this function, you should also reimplement width(). The rectangle to be painted is in an undefined state when this function is called, so you \e must draw on all the pixels. The painter \a p has the right font on entry. \sa paintBranches(), Q3ListView::drawContentsOffset()*/static QStyleOptionQ3ListView getStyleOption(const Q3ListView *lv, const Q3ListViewItem *item){ QStyleOptionQ3ListView opt; opt.init(lv); opt.subControls = QStyle::SC_None; opt.activeSubControls = QStyle::SC_None; QWidget *vp = lv->viewport(); opt.viewportPalette = vp->palette(); opt.viewportBGRole = vp->backgroundRole(); opt.itemMargin = lv->itemMargin(); opt.sortColumn = 0; opt.treeStepSize = lv->treeStepSize(); opt.rootIsDecorated = lv->rootIsDecorated(); bool firstItem = true; while (item) { QStyleOptionQ3ListViewItem lvi; lvi.height = item->height(); lvi.totalHeight = item->totalHeight(); lvi.itemY = item->itemPos(); lvi.childCount = item->childCount(); lvi.features = QStyleOptionQ3ListViewItem::None; lvi.state = QStyle::State_None; if (item->isEnabled()) lvi.state |= QStyle::State_Enabled; if (item->isOpen()) lvi.state |= QStyle::State_Open; if (item->isExpandable()) lvi.features |= QStyleOptionQ3ListViewItem::Expandable; if (item->multiLinesEnabled()) lvi.features |= QStyleOptionQ3ListViewItem::MultiLine; if (item->isVisible()) lvi.features |= QStyleOptionQ3ListViewItem::Visible; if (item->parent() && item->parent()->rtti() == 1 && static_cast<Q3CheckListItem *>(item->parent())->type() == Q3CheckListItem::Controller) lvi.features |= QStyleOptionQ3ListViewItem::ParentControl; opt.items.append(lvi); if (!firstItem) { item = item->nextSibling(); } else { firstItem = false; item = item->firstChild(); } } return opt;}/*! \fn void Q3ListViewItem::paintCell(QPainter *painter, const QColorGroup & cg, int column, int width, int align) This virtual function paints the contents of one column of an item and aligns it as described by \a align. The \a painter is a Q3Painter open on the relevant paint device. It is translated so (0, 0) is the top-left pixel in the cell and \a width - 1, height() - 1 is the bottom-right pixel \e in the cell. The other properties of the \a painter (pen, brush, etc) are undefined. \a cg is the color group to use. \a column is the logical column number within the item that is to be painted; 0 is the column which may contain a tree. This function may use Q3ListView::itemMargin() for readability spacing on the left and right sides of data such as text, and should honor \l isSelected() and Q3ListView::allColumnsShowFocus(). If you reimplement this function, you should also reimplement \l width(). The rectangle to be painted is in an undefined state when this function is called, so you \e must draw on all the pixels. The \a painter has the right font on entry. \sa paintBranches(), Q3ListView::drawContentsOffset()*/void Q3ListViewItem::paintCell(QPainter * p, const QColorGroup & cg, int column, int width, int align){ // Change width() if you change this. QPalette pal = cg; if (!p) return; Q3ListView *lv = listView(); if (!lv) return; QFontMetrics fm(p->fontMetrics()); // had, but we _need_ the column info for the ellipsis thingy!!! if (!columns) { for (int i = 0; i < lv->d->column.size(); ++i) { setText(i, text(i)); } } QString t = text(column); if (columns) { Q3ListViewPrivate::ItemColumnInfo *ci = 0; // try until we have a column info.... while (!ci) { ci = (Q3ListViewPrivate::ItemColumnInfo*)columns; for (int i = 0; ci && (i < column); ++i) ci = ci->next; if (!ci) { setText(column, t); ci = 0; } } // if the column width changed and this item was not painted since this change if (ci && (ci->width != width || ci->text != t || ci->dirty)) { ci->text = t; ci->dirty = false; ci->width = width; ci->truncated = false; // if we have to do the ellipsis thingy calc the truncated text int pw = lv->itemMargin()*2 - lv->d->minLeftBearing - lv->d->minRightBearing; pw += pixmap(column) ? pixmap(column)->width() + lv->itemMargin() : 0; if (!mlenabled && fm.width(t) + pw > width) { // take care of arabic shaping in width calculation (lars) ci->truncated = true; ci->tmpText = qEllipsisText(t, fm, width - pw, align); } else if (mlenabled && fm.width(t) + pw > width) { QStringList list = t.split(QChar('\n')); for (QStringList::Iterator it = list.begin(); it != list.end(); ++it) { QString z = *it; if (fm.width(z) + pw > width) { ci->truncated = true; *it = qEllipsisText(z, fm, width - pw, align); } } if (ci->truncated) ci->tmpText = list.join(QString("\n")); } } // if we have to draw the ellipsis thingy, use the truncated text if (ci && ci->truncated) t = ci->tmpText; } int marg = lv->itemMargin(); int r = marg; const QPixmap * icon = pixmap(column); const QPalette::ColorRole crole = lv->backgroundRole(); if (pal.brush(crole) != lv->palette().brush(crole)) p->fillRect(0, 0, width, height(), pal.brush(crole)); else lv->paintEmptyArea(p, QRect(0, 0, width, height())); // (lars) what does this do???#if 0 // RS: #### if (align != Qt::AlignLeft) marg -= lv->d->minRightBearing;#endif if (isSelected() && (column == 0 || lv->allColumnsShowFocus())) { p->fillRect(r - marg, 0, qMax(0, width - r + marg), height(), pal.brush(QPalette::Highlight)); if (enabled || !lv) p->setPen(pal.highlightedText().color()); else if (!enabled && lv) p->setPen(lv->palette().color(QPalette::Disabled, QPalette::HighlightedText)); } else { if (enabled || !lv) p->setPen(pal.text().color()); else if (!enabled && lv) p->setPen(lv->palette().color(QPalette::Disabled, QPalette::Text)); }#if 0 bool reverse = QApplication::reverseLayout();#else bool reverse = false;#endif int iconWidth = 0; if (icon) { iconWidth = icon->width() + lv->itemMargin(); int xo = r; // we default to Qt::AlignVCenter. int yo = (height() - icon->height()) / 2; // I guess we may as well always respect vertical alignment. if (align & Qt::AlignBottom) yo = height() - icon->height(); else if (align & Qt::AlignTop) yo = 0; // respect horizontal alignment when there is no text for an item. if (text(column).isEmpty()) { if (align & Qt::AlignRight) xo = width - 2 * marg - iconWidth; else if (align & Qt::AlignHCenter) xo = (width - iconWidth) / 2; } if (reverse) xo = width - 2 * marg - iconWidth; p->drawPixmap(xo, yo, *icon); } if (!t.isEmpty()) { if (!mlenabled) { if (!(align & Qt::AlignTop || align & Qt::AlignBottom)) align |= Qt::AlignVCenter; } else { if (!(align & Qt::AlignVCenter || align & Qt::AlignBottom)) align |= Qt::AlignTop; } if (!reverse) r += iconWidth; if (!mlenabled) { p->drawText(r, 0, width-marg-r, height(), align, t); } else { p->drawText(r, marg, width-marg-r, height(), align, t); } } if (mlenabled && column == 0 && isOpen() && childCount()) { int textheight = fm.size(align, t).height() + 2 * lv->itemMargin(); textheight = qMax(textheight, QApplication::globalStrut().height()); if (textheight % 2 > 0) textheight++; if (textheight < height()) { int w = lv->treeStepSize() / 2; QStyleOptionQ3ListView opt = getStyleOption(lv, this); opt.rect.setRect(0, textheight, w + 1, height() - textheight + 1); opt.palette = pal; opt.subControls = QStyle::SC_Q3ListViewExpand; opt.activeSubControls = QStyle::SC_All; lv->style()->drawComplexControl(QStyle::CC_Q3ListView, &opt, p, lv); } }}/*! Returns the number of pixels of width required to draw column \a c of list view \a lv, using the metrics \a fm without cropping. The list view containing this item may use this information depending on the Q3ListView::WidthMode settings for the column. The default implementation returns the width of the bounding rectangle of the text of column \a c. \sa listView() widthChanged() Q3ListView::setColumnWidthMode() Q3ListView::itemMargin()*/int Q3ListViewItem::width(const QFontMetrics& fm, const Q3ListView* lv, int c) const{ int w; if (mlenabled) w = fm.size(Qt::AlignVCenter, text(c)).width() + lv->itemMargin() * 2 - lv->d->minLeftBearing - lv->d->minRightBearing; else w = fm.width(text(c)) + lv->itemMargin() * 2 - lv->d->minLeftBearing - lv->d->minRightBearing; const QPixmap * pm = pixmap(c); if (pm) w += pm->width() + lv->itemMargin(); // ### correct margin stuff? return qMax(w, QApplication::globalStrut().width());}/*! Paints a focus indicator on the rectangle \a r using painter \a p and colors \a cg. \a p is already clipped. \sa paintCell() paintBranches() Q3ListView::setAllColumnsShowFocus()*/void Q3ListViewItem::paintFocus(QPainter *p, const QColorGroup &cg, const QRect &r){ QPalette pal = cg; Q3ListView *lv = listView(); if (lv) { QStyleOptionFocusRect opt; opt.init(lv); opt.rect = r; opt.palette = pal; opt.state |= QStyle::State_KeyboardFocusChange; if (isSelected()) { opt.state |= QStyle::State_FocusAtBorder; opt.backgroundColor = pal.highlight().color(); } else { opt.state |= QStyle::State_None; opt.backgroundColor = pal.base().color(); } lv->style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p, lv); }}/*! Paints a set of branches from this item to (some of) its children. Painter \a p is set up with clipping and translation so that you can only draw in the rectangle that needs redrawing; \a cg is the color group to use; the update rectangle is at (0, 0) and has size width \a w by height \a h. The top of the rectangle you own is at \a y (which is never greater than 0 but can be outside the window system's allowed coordinate range). The update rectangle is in an undefined state when this function is called; this function must draw on \e all of the pixels. \sa paintCell(), Q3ListView::drawContentsOffset()*/void Q3ListViewItem::paintBranches(QPainter * p, const QColorGroup & cg, int w, int y, int h){ Q3ListView *lv = listView(); if (lv) lv->paintEmptyArea(p, QRect(0, 0, w, h)); if (!visible || !lv) return; QStyleOptionQ3ListView opt = getStyleOption(lv, this); opt.rect.setRect(0, y, w, h); opt.palette = cg; opt.subControls = QStyle::SC_Q3ListViewBranch | QStyle::SC_Q3ListViewExpand; opt.activeSubControls = QStyle::SC_None; lv->style()->drawComplexControl(QStyle::CC_Q3ListView, &opt, p, lv);}Q3ListViewPrivate::Root::Root(Q3ListView * parent) : Q3ListViewItem(parent){ lv = parent; setHeight(0); setOpen(true);}void Q3ListViewPrivate::Root::setHeight(int){ Q3ListViewItem::setHeight(0);}void Q3ListViewPrivate::Root::invalidateHeight(){ Q3ListViewItem::invalidateHeight();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -