void UrlDialogTreeWidget::paintEvent(QPaintEvent * event) { QPainter * p = new QPainter(viewport()); QStyleOptionViewItem option = viewOptions(); QRect rect = event->rect(); #ifdef COMPILE_PSEUDO_TRANSPARENCY if(KVI_OPTION_BOOL(KviOption_boolUseCompositingForTransparency) && g_pApp->supportsCompositing()) { p->save(); p->setCompositionMode(QPainter::CompositionMode_Source); QColor col = KVI_OPTION_COLOR(KviOption_colorGlobalTransparencyFade); col.setAlphaF((float)((float)KVI_OPTION_UINT(KviOption_uintGlobalTransparencyChildFadeFactor) / (float)100)); p->fillRect(rect, col); p->restore(); } else if(g_pShadedChildGlobalDesktopBackground) { QPoint pnt = ((KviWindow *)parent())->isDocked() ? viewport()->mapTo(g_pMainWindow, rect.topLeft()) : viewport()->mapTo((KviWindow *)parent(), rect.topLeft()); p->drawTiledPixmap(rect, *(g_pShadedChildGlobalDesktopBackground), pnt); } else { #endif //FIXME this is not the treewindowlist p->fillRect(rect, KVI_OPTION_COLOR(KviOption_colorTreeWindowListBackground)); #ifdef COMPILE_PSEUDO_TRANSPARENCY } #endif delete p; //call paint on all children QTreeWidget::paintEvent(event); }
bool MyTableView::viewportEvent(QEvent * event) { if ( event->type() == QEvent::ToolTip ) { qDebug() << "3"; QHelpEvent *hev = static_cast<QHelpEvent *>(event); int col = columnAt(hev->x()); int row = rowAt(hev->y()); qDebug() << "col = " << col << ", row = " << row; QModelIndex idx = model()->index(row, col, QModelIndex()); QString text = idx.data().toString(); QFont font = viewOptions().font; QFontMetrics fm(font); int requiredWidth = fm.width(text); qDebug() << "require width = " << requiredWidth; qDebug() << "col width = " << columnWidth(col); if ( columnWidth(col) > requiredWidth ) { // don't show the tooltip if all text is visible return true; } } return QTableView::viewportEvent(event); }
void QmlConsoleView::mousePressEvent(QMouseEvent *event) { QPoint pos = event->pos(); QModelIndex index = indexAt(pos); if (index.isValid()) { ConsoleItem::ItemType type = (ConsoleItem::ItemType)index.data( QmlConsoleItemModel::TypeRole).toInt(); bool handled = false; if (type == ConsoleItem::UndefinedType) { bool showTypeIcon = index.parent() == QModelIndex(); ConsoleItemPositions positions(visualRect(index), viewOptions().font, showTypeIcon, true); if (positions.expandCollapseIcon().contains(pos)) { if (isExpanded(index)) setExpanded(index, false); else setExpanded(index, true); handled = true; } } if (!handled) Utils::TreeView::mousePressEvent(event); } else { selectionModel()->setCurrentIndex(model()->index(model()->rowCount() - 1, 0), QItemSelectionModel::ClearAndSelect); } }
QWidget* fastEdit(const QModelIndex &i) // Consider this as QAbstractItemView::edit( ) { QWidget *v = itemDelegate()->createEditor(viewport(), viewOptions(), i); if (v) itemDelegate()->setEditorData(v, i); return v; }
void TreeView::startDrag(Qt::DropActions supportedActions) { QModelIndex index = selectedIndexes().value(0); if (!index.isValid()) return; QMimeData *data = model()->mimeData(QModelIndexList() << index); if (!data) return; QRect rect; QPixmap pixmap; QPoint point; { QAbstractItemDelegate *delegate = itemDelegate(index); QStyleOptionViewItemV4 option = viewOptions(); option.locale = this->locale(); option.locale.setNumberOptions(QLocale::OmitGroupSeparator); option.widget = this; option.state |= QStyle::State_Selected; option.rect = visualRect(index); point = option.rect.topLeft(); option.rect.moveTo(0, 0); option.rect.setSize(delegate->sizeHint(option, index)); rect = option.rect; pixmap = QPixmap(rect.size()); pixmap.fill(Qt::transparent); QPainter painter(&pixmap); delegate->paint(&painter, option, index); } QDrag *drag = new QDrag(this); drag->setPixmap(pixmap); drag->setMimeData(data); point = QCursor::pos() - viewport()->mapToGlobal(point); drag->setHotSpot(point); // drag->setHotSpot(QCursor::pos() - rect.topLeft()); Qt::DropAction setDefaultDropAction = QAbstractItemView::defaultDropAction(); Qt::DropAction defaultDropAction = Qt::IgnoreAction; if (setDefaultDropAction != Qt::IgnoreAction && (supportedActions & setDefaultDropAction)) defaultDropAction = setDefaultDropAction; else if (supportedActions & Qt::CopyAction && dragDropMode() != QAbstractItemView::InternalMove) defaultDropAction = Qt::CopyAction; if (drag->exec(supportedActions, defaultDropAction) == Qt::IgnoreAction && index.data(ItemTypeRole).toInt() == ContactType) { if (QWidget *widget = QApplication::topLevelAt(QCursor::pos())) { if (widget->window() == this->window()) return; } Event ev("contact-list-drop", QCursor::pos() - point, index.data(BuddyRole)); ev.send(); } // debug() << "DropAction" << drag->exec(supportedActions, defaultDropAction); // if (drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction) // d->clearOrRemove(); // {} }
void ImageView::paintEvent(QPaintEvent *event) { //QItemSelectionModel *selections = selectionModel(); QStyleOptionViewItem option = viewOptions(); //QStyle::State state = option.state; //QBrush background = option.palette.base(); QPen foreground(option.palette.color(QPalette::WindowText)); //QBrush highlight(option.palette.color(QPalette::Highlight)); //p.fillRect(event->rect(),background); if (!model()) return; QPainter p(viewport()); int n = model()->rowCount(); p.translate(-horizontalOffset(),-verticalOffset()); for (int i=0;i<n;i++) { QModelIndex index = model()->index(i,0); QRect rect = itemRect(index); p.setPen(foreground); //p.drawRect(rect); QVariant decoration = model()->data(index,Qt::DecorationRole); if (decoration.type() == QVariant::Pixmap) { QPixmap pixmap = qvariant_cast<QPixmap>(decoration); if (!pixmap.isNull()) { QSize pixmapSize = pixmap.size(); QSize itemSize = rect.size(); pixmapSize.scale(itemSize,Qt::KeepAspectRatio); QSize rest = (itemSize - pixmapSize)/2; QRect pixmapRect(QPoint(rest.width(),rest.height()),pixmapSize); pixmapRect.translate(rect.topLeft()); p.drawPixmap(pixmapRect,pixmap); } } if (index == currentIndex()) { QColor color(option.palette.color(QPalette::Highlight)); color.setAlpha(100); p.fillRect(rect,color); } //qDebug() << rect << foreground.color() << background.color(); //p.drawText(rect,Qt::AlignCenter,QString::number(i)); } event->accept(); }
QModelIndex PieView::indexAt(const QPoint &point) const { if (validItems == 0) return QModelIndex(); int wx = point.x() + horizontalScrollBar()->value(); int wy = point.y() + verticalScrollBar()->value(); if (wx < totalSize) { double cx = wx - totalSize/2; double cy = totalSize/2 - wy; double d = pow(pow(cx, 2) + pow(cy, 2), 0.5); if (d == 0 || d > pieSize/2) return QModelIndex(); double angle = (180 / M_PI) * acos(cx/d); if (cy < 0) angle = 360 - angle; double startAngle = 0.0; for (int row = 0; row < model()->rowCount(rootIndex()); ++row) { QModelIndex index = model()->index(row, 1, rootIndex()); double value = model()->data(index).toDouble(); if (value > 0.0) { double sliceAngle = 360*value/totalValue; if (angle >= startAngle && angle < (startAngle + sliceAngle)) return model()->index(row, 1, rootIndex()); startAngle += sliceAngle; } } } else { double itemHeight = QFontMetrics(viewOptions().font).height(); int listItem = int((wy - margin) / itemHeight); int validRow = 0; for (int row = 0; row < model()->rowCount(rootIndex()); ++row) { QModelIndex index = model()->index(row, 1, rootIndex()); if (model()->data(index).toDouble() > 0.0) { if (listItem == validRow) return model()->index(row, 0, rootIndex()); validRow++; } } } return QModelIndex(); }
bool AlbumSelectionTreeView::viewportEvent(QEvent* event) { // let the base class handle the event if it is not a tool tip request if (event->type() != QEvent::ToolTip) { return AlbumTreeView::viewportEvent(event); } // only show tool tips if requested if (!d->enableToolTips) { return false; } // check that we got a correct event QHelpEvent* const helpEvent = dynamic_cast<QHelpEvent*> (event); if (!helpEvent) { qCDebug(DIGIKAM_GENERAL_LOG) << "Unable to determine the correct type of the event. " << "This should not happen."; return false; } // find the item this tool tip belongs to QModelIndex index = indexAt(helpEvent->pos()); if (!index.isValid()) { return true; } PAlbum* const album = albumForIndex(index); if (!album || album->isRoot() || album->isAlbumRoot()) { // there was no album so we really don't want to show a tooltip. return true; } QRect itemRect = visualRect(index); if (!itemRect.contains(helpEvent->pos())) { return true; } QStyleOptionViewItem option = viewOptions(); option.rect = itemRect; // visualRect can be larger than viewport, intersect with viewport rect option.rect &= viewport()->rect(); option.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None); d->toolTip->show(option, index); return true; }
/** * QT 4.7.1 拖拽的函数默认调用过程: * * Model.supportedDropActions() * * View.startDrag() * Model.mimeData() // 获取拖拽数据 * Model.supportedDragActions() * Model.supportedDropActions() * QDrag.exec() // 阻塞,直到拖拽结束 * * // 在拖拽过程中,如果拖拽进入某个节点 * View.dragEnterEvent() // 默认实现会拒绝拖拽,需要重写并调用 QDragEnterEvent.acceptProposedAction() * Model.mimeTypes() * View.dragMoveEvent() * Model.mimeTypes() * View.dragLeaveEvent() * * // 拖拽结束后 * View.dropEvent() * Model.dropMimeData() // QT用来接收数据 * * // exec()执行完后,如果是 MoveAction,则调用 * Model.removeSelectedRows() // QT用来删除数据 * * 上述默认过程对跨 View 拖放支持不好, 下面的startDrag()函数摘自默认实现,并 * 将部分代码替换掉以免调用removeSelectedRows()函数 */ void DirectoryTree::startDrag(Qt::DropActions) { const QModelIndexList indexes = selectedIndexes(); if (indexes.count() == 0) return; // setup pixmap QRect rect = visualRect(indexes.at(0)); QList<QRect> rects; for (size_t i = 0, s = indexes.count(); i < s; ++i) { rects.append(visualRect(indexes.at(i))); rect |= rects.at(i); } rect = rect.intersected(viewport()->rect()); QPixmap pixmap(rect.size()); pixmap.fill(palette().base().color()); QPainter painter(&pixmap); QStyleOptionViewItem option = viewOptions(); option.state |= QStyle::State_Selected; for (size_t j = 0, s = indexes.count(); j < s; ++j) { option.rect = QRect(rects.at(j).topLeft() - rect.topLeft(), rects.at(j).size()); itemDelegate()->paint(&painter, option, indexes.at(j)); } painter.end(); // create drag object QDrag *drag = new QDrag(this); drag->setPixmap(pixmap); drag->setMimeData(model()->mimeData(indexes)); drag->setHotSpot(viewport()->mapFromGlobal(QCursor::pos()) - rect.topLeft()); /** * 在拖动之前,先把 parent 和 row 保存下来, * 在 Model.dropMimeData() 中统一进行底层数据和视图层数据的修改, * 避免索引混乱 */ DirectoryTreeModel *m = (DirectoryTreeModel*) model(); QModelIndex parent = indexes.at(0).parent(); int row = indexes.at(0).row(); m->prepare_draging_drop(parent, row); /* * 这里的原来的实现 removeSelectedRows() 是我们不需要的, 且 QDrag->start() 总是允许 Qt::CopyAction * 而我们需要的是,内部用 Qt::MoveAction,外部用 Qt::CopyAction且内部不支持 Qt::CopyAction且内部不支持 * Qt::CopyAction,否则导致错误 * if (drag->start(supportedActions) == Qt::MoveAction) removeSelectedRows(); */ drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::MoveAction); }
bool KoResourceItemView::viewportEvent(QEvent *event) { if (event->type() == QEvent::ToolTip && model()) { QHelpEvent *he = static_cast<QHelpEvent *>(event); QStyleOptionViewItem option = viewOptions(); QModelIndex index = model()->buddy(indexAt(he->pos())); if (index.isValid()) { option.rect = visualRect(index); m_tip.showTip(this, he->pos(), option, index); return true; } } return QTableView::viewportEvent(event); }
void QgsLocatorResultsView::recalculateSize() { // try to show about 20 rows int rowSize = 20 * itemDelegate()->sizeHint( viewOptions(), model()->index( 0, 0 ) ).height(); // try to take up a sensible portion of window width (about half) int width = std::max( 300, window()->size().width() / 2 ); QSize newSize( width, rowSize + frameWidth() * 2 ); // resize the floating widget this is contained within parentWidget()->resize( newSize ); QTreeView::resize( newSize ); header()->resizeSection( 0, width / 2 ); header()->resizeSection( 1, 0 ); }
void ItemList::startDrag(Qt::DropActions supportedActions) { const QModelIndexList indexes = selectedIndexes(); if (indexes.count() == 0) return; // setup pixmap QRect rect = visualRect(indexes.at(0)); QList<QRect> rects; for (size_t i = 0, s = indexes.count(); i < s; ++i) { rects.append(visualRect(indexes.at(i))); rect |= rects.at(i); } rect = rect.intersected(viewport()->rect()); QPixmap pixmap(rect.size()); pixmap.fill(palette().base().color()); QPainter painter(&pixmap); QStyleOptionViewItem option = viewOptions(); option.state |= QStyle::State_Selected; for (size_t j = 0, s = indexes.count(); j < s; ++j) { option.rect = QRect(rects.at(j).topLeft() - rect.topLeft(), rects.at(j).size()); itemDelegate()->paint(&painter, option, indexes.at(j)); } painter.end(); // create drag object QDrag *drag = new QDrag(this); drag->setPixmap(pixmap); drag->setMimeData(model()->mimeData(indexes)); drag->setHotSpot(viewport()->mapFromGlobal(QCursor::pos()) - rect.topLeft()); /* * 这里的原来的实现被注释掉 if (drag->start(supportedActions) == Qt::MoveAction) removeSelectedRows(); */ ItemListModel *m = dynamic_cast<ItemListModel*>(model()); assert(NULL != m); /// QAbstractItemModel::supportedDragActions()居然不是虚函数,我C了!!! Qt::DropAction rs = drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::MoveAction); if (rs == Qt::MoveAction) { assert(indexes.count() != 0); m->_remove_after_drag(indexes.at(0).row()); } }
void KDPropertyView::drawBranches(QPainter* painter, const QRect& rect, const QModelIndex& index) const { QStyleOptionViewItem opt = viewOptions(); QStyleOptionViewItem option = opt; const QAbstractItemModel * const m = model(); if ( index.column() == 0 && m && m->data( index, KDPropertyModel::ChangedRole ) == true ) option.font.setBold(true); /* if ( m && m->data(index,KDPropertyModel::SeparatorRole) == true ) { painter->fillRect(rect, option.palette.dark()); } else if (selectionModel()->isSelected(index)) { painter->fillRect(rect, option.palette.brush(QPalette::Highlight)); } */ if( m && m->parent(index) == QModelIndex() ) { painter->fillRect(rect, option.palette.color(QPalette::AlternateBase)); } else { QRect topr = QRect( rect.topLeft(), QPoint( rect.left()+indentation(), rect.bottom() ) ); painter->fillRect( topr, option.palette.color(QPalette::AlternateBase)); //painter->drawLine( rect.x()+indentation(), rect.top(), rect.x()+indentation(), rect.bottom()); painter->fillRect( QRect( topr.topRight(), rect.bottomRight() ), option.palette.base()); painter->setPen( option.palette.color(QPalette::AlternateBase) ); painter->drawLine( topr.right(), rect.bottom(), rect.right(), rect.bottom()); } if (m && m->hasChildren(index)) { static const int size = 9; opt.state |= QStyle::State_Children; opt.rect.setRect(rect.width() - (indentation() + size) / 2, rect.y() + (rect.height() - size) / 2, size, size); if (isExpanded(index)) opt.state |= QStyle::State_Open; QColor bg; QVariant bgv = m->data( index, Qt::BackgroundColorRole); if( qVariantCanConvert<QColor>(bgv) ) { bg = qVariantValue<QColor>(bgv); } else { bg = option.palette.color( QPalette::Background ); } painter->fillRect(opt.rect, bg); style()->drawPrimitive(QStyle::PE_IndicatorBranch, &opt, painter, this); } }
int MovableTableWidget::sizeHintForColumn(int column) const { ensurePolished(); QStyleOptionViewItem option = viewOptions(); int hint = 0; QModelIndex index; for (int row = 0; row <= rowCount(); ++row) { index = indexFromItem(item(row, column)); hint = qMax(hint, itemDelegate(index)->sizeHint(option, index).width()); } return showGrid() ? hint + 1 : hint; }
// returns the selected item whose checkbox lies under 'pos' // returns 0 if not selected, no item at pos, or does not fit inside checkbox bool QMultiSelectWidget::isCheckBoxUnderPosition(const QPoint &pos) { QListWidgetItem* item = itemAt(pos); if (item) { // with the help of styles, check if checkbox rect contains 'pos' QStyleOptionButton opt; opt.QStyleOption::operator=(viewOptions()); opt.rect = visualItemRect(item); QRect r = style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt); // assure that the item is also selected if (r.contains(pos)) { return true; } } return false; }
QListWidgetItem* PluginListWidget::selectedCheckStateItem(const QPoint& pos){ QListWidgetItem* item = itemAt(pos); if (item) { // with the help of styles, check if checkbox rect contains 'pos' QStyleOptionButton opt; opt.QStyleOption::operator=(viewOptions()); opt.rect = visualItemRect(item); QRect r = style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt); // assure that the item is also selected if (selectedItems().contains(item) && r.contains(pos)) { return item; } } return 0; }
int QCustomTableWidget::sizeHintForColumn(int column) const { QStyleOptionViewItem option(viewOptions()); int hint = 0; QAbstractItemDelegate *delegate = itemDelegate(); QAbstractItemModel *itemModel = model(); for(int row = 0; row < rowCount(); row++) { hint = qMax(hint, delegate->sizeHint(option, itemModel->index(row,column)).width()); } if (showGrid()) { hint++; } return hint; }
QSize CodeCompleterListView::calculateSize() const { static const int maxVisibleItems = 10; // Determine size by calculating the space of the visible items const int visibleItems = qMin(model()->rowCount(), maxVisibleItems); const int firstVisibleRow = verticalScrollBar()->value(); const QStyleOptionViewItem &option = viewOptions(); QSize shint; for (int i = 0; i < visibleItems; ++i) { QSize tmp = itemDelegate()->sizeHint(option, model()->index(i + firstVisibleRow, 0)); if (shint.width() < tmp.width()) shint = tmp; } shint.rheight() *= visibleItems; return shint; }
bool TracksView::viewportEvent(QEvent *event) { if (event->type() == QEvent::ToolTip) { QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event); QModelIndex index = indexAt(helpEvent->pos()); if (index.isValid()) { QSize sizeHint = itemDelegate(index)->sizeHint(viewOptions(), index); QRect rItem(0, 0, sizeHint.width(), sizeHint.height()); QRect rVisual = visualRect(index); if (rItem.width() <= rVisual.width()) { QToolTip::hideText(); return false; } } } return QTableView::viewportEvent(event); }
int CustomTreeView::sizeHintForColumn(int col) const { if (!model() || col > 0) return -1; ensurePolished(); int hint = 0; QModelIndex index = model()->index(0,col); while (index.isValid()) { if (viewport()->rect().contains(visualRect(index))) { hint = qMax(hint, visualRect(index).x() + itemDelegate(index)->sizeHint(viewOptions(), index).width()); } index = indexBelow(index); } return qMin(hint, viewport()->rect().width()); }
//----------------------------------------------------------------------------- void vktraceviewer_QTimelineView::drawTimelineItem(QPainter* painter, const QModelIndex &index) { QRectF rect = viewportRect(index); // don't draw if the rect is outside the viewport if (!rect.isValid() || rect.bottom() < 0 || rect.y() > viewport()->height() || rect.x() > viewport()->width() || rect.right() < 0) { return; } QStyleOptionViewItem option = viewOptions(); option.rect = rect.toRect(); if (selectionModel()->isSelected(index)) option.state |= QStyle::State_Selected; if (currentIndex() == index) option.state |= QStyle::State_HasFocus; // check mask to determine if this item should be drawn, or if something has already covered it's pixels vktrace_trace_packet_header* pHeader = (vktrace_trace_packet_header*)index.internalPointer(); QVector<int>& mask = m_threadMask[pHeader->thread_id]; bool drawItem = false; int x = option.rect.x(); int right = qMin( qMax(x, option.rect.right()), viewport()->width()-1); for (int pixel = qMax(0, x); pixel <= right; pixel++) { if (mask[pixel] == 0) { drawItem = true; mask[pixel] = 1; } } // draw item if it should be visible if (drawItem) { itemDelegate()->paint(painter, option, index); } }
void TiledListView::paintEvent(QPaintEvent*) { QPainter painter(viewport()); painter.setRenderHints(QPainter::Antialiasing| QPainter::TextAntialiasing); for (int row = 0; row < model()->rowCount(rootIndex()); ++row) { QModelIndex index = model()->index(row, 0, rootIndex()); QRectF rect = viewportRectForRow(row); if (!rect.isValid() || rect.bottom() < 0 || rect.y() > viewport()->height()) continue; QStyleOptionViewItem option = viewOptions(); option.rect = rect.toRect(); if (selectionModel()->isSelected(index)) option.state |= QStyle::State_Selected; if (currentIndex() == index) option.state |= QStyle::State_HasFocus; itemDelegate()->paint(&painter, option, index); paintOutline(&painter, rect); } }
QRect PieView::itemRect(const QModelIndex &index) const { if (!index.isValid()) return QRect(); // Check whether the index's row is in the list of rows represented // by slices. QModelIndex valueIndex; if (index.column() != 1) valueIndex = model()->index(index.row(), 1, rootIndex()); else valueIndex = index; if (model()->data(valueIndex).toDouble() > 0.0) { int listItem = 0; for (int row = index.row()-1; row >= 0; --row) { if (model()->data(model()->index(row, 1, rootIndex())).toDouble() > 0.0) listItem++; } double itemHeight; switch (index.column()) { case 0: itemHeight = QFontMetrics(viewOptions().font).height(); return QRect(totalSize, int(margin + listItem*itemHeight), totalSize - margin, int(itemHeight)); case 1: return viewport()->rect(); } } return QRect(); }
int NavigableTableView::sizeHintForRow(int row) const { if (!model()) return -1; ensurePolished(); int left = qMax(0, horizontalHeader()->visualIndexAt(0)); int right = horizontalHeader()->visualIndexAt(viewport()->width()); if (right < 0) right = model()->columnCount(); int hint = 0; for (int column = left; column <= right; ++column) { if (horizontalHeader()->isSectionHidden(column)) continue; QModelIndex index = model()->index(row, column); hint = qMax(hint, itemDelegate(index)->sizeHint(viewOptions(), index).height()); } return hint; }
bool TreeWidget::viewportEvent(QEvent* event) { if (event->type() == QEvent::ToolTip) { QHelpEvent* he = static_cast<QHelpEvent*>(event); TreeItem* item = static_cast<TreeItem*>(itemAt(he->pos())); if (item && !item->parentItem() && !item->toolTip(0).isEmpty()) { QStyleOptionViewItem opt = viewOptions(); opt.icon = item->icon(0); opt.rect = visualItemRect(item); opt.features |= QStyleOptionViewItem::HasDecoration; QRect rect = style()->subElementRect(QStyle::SE_ItemViewItemDecoration, &opt, this); if (rect.contains(he->pos())) { #if QT_VERSION >= 0x050200 QToolTip::showText(he->globalPos(), item->toolTip(0), this, rect, 1250); #else QToolTip::showText(he->globalPos(), item->toolTip(0), this, rect); #endif } } return true; } return QTreeWidget::viewportEvent(event); }
void LayerTreeWidget::mousePressEvent(QMouseEvent *event) { if (event->button()== Qt::RightButton) return; else { QTreeWidgetItem* item = itemAt(event->pos()); if (rectCheckbox.isEmpty() || rectCheckbox.width() <= 0) { QStyleOptionViewItem option = viewOptions(); if (item) rectCheckbox = m_itemDelegate->GetCheckBoxRect(indexFromItem(item), option); } if (item && item->childCount() == 0 && event->x() < rectCheckbox.right() && event->x() > rectCheckbox.left()) { m_bCheckBoxClicked = true; return; } QTreeWidget::mousePressEvent(event); } }
int NavigableTableView::sizeHintForColumn(int col) const { if (!model()) return -1; ensurePolished(); int top = qMax(0, verticalHeader()->visualIndexAt(0)); int bottom = verticalHeader()->visualIndexAt(viewport()->height()); int hint = 0; if (bottom == -1 || (bottom+10) >= model()->rowCount()) { bottom = model()->rowCount() - 1; } else { bottom += 10; } for (int row = top; row <= bottom; ++row) { QModelIndex index = model()->index(row, col); hint = qMax(hint, itemDelegate(index)->sizeHint(viewOptions(), index).width()); } return hint; }
void KNMusicAlbumView::paintEvent(QPaintEvent *event) { Q_UNUSED(event) //Initial the painter. QPainter painter(viewport()); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true); //Get the option view item. QStyleOptionViewItem option=viewOptions(); //Check if we need to paint the background. if(autoFillBackground()) { painter.fillRect(rect(), option.palette.base()); } //Update the parameters of the view first. updateParameters(); //Check the model first. if(m_proxyModel==nullptr) { return; } //Get the row count. int albumCount=m_proxyModel->rowCount(); //Calculate the line count. m_lineCount=(albumCount+m_maxColumnCount-1)/m_maxColumnCount; //Check the album count first. if(albumCount==0) { return; } int currentColumn=0, currentLeft=m_spacing, currentLine=verticalScrollBar()->value()/m_itemSpacingHeight, //Skip the before. currentTop=m_spacing+currentLine*m_itemSpacingHeight, currentRow=currentLine*m_maxColumnCount, heightSurplus=height()+m_itemSpacingHeight; //Change the origin of coordinate. painter.translate(0, -verticalScrollBar()->value()); //Draw all the albums. while(currentRow < albumCount && heightSurplus > 0) { //Get the source index of the item. QModelIndex proxyIndex=m_proxyModel->index(currentRow, 0); //If the source index is not the current index, then draw the album. if(m_proxyModel->mapToSource(proxyIndex)!=m_selectedIndex) { paintAlbum(painter, QPoint(currentLeft, currentTop), proxyIndex); } //Add current row and column. currentRow++; currentColumn++; //Check if we need to move to next row. if(currentColumn>=m_maxColumnCount) { //Add one line. currentLine++; //Reset the column. currentColumn=0; //Change the position. currentLeft=m_spacing; currentTop+=m_itemSpacingHeight; heightSurplus-=m_itemSpacingHeight; } else { //Move to next column position. currentLeft+=m_itemSpacingWidth; } } //Update the scroll bar value. updateGeometries(); }
void ColorGridView::paintEvent(QPaintEvent *event) { // To make it clear, we do not use delegate at all // QItemSelectionModel *selections = selectionModel(); QStyleOptionViewItem option = viewOptions(); QStyle::State state = option.state; QBrush background = option.palette.base(); QPen foreground(option.palette.color(QPalette::WindowText)); QPen textPen(option.palette.color(QPalette::Text)); QPen highlightedPen(option.palette.color(QPalette::HighlightedText)); QPainter painter(viewport()); // close anti aliasing to make it more clear // painter.setRenderHint(QPainter::Antialiasing); painter.fillRect(event->rect(), background); painter.setPen(Qt::white); QBrush brush; brush.setTextureImage(backgroundImg); painter.setBrush(brush); painter.translate(margin - horizontalScrollBar()->value(), margin - verticalScrollBar()->value()); for(int i = 0; i<gridCount; ++i){ for(int j=0; j<gridCount; ++j){ painter.drawImage(i*(gridMargin+gridWidth),j*(gridMargin+gridWidth), backgroundImg); } } if( !model() )return; for (int row = 0; row < model()->rowCount(rootIndex()); ++row) { for(int column = 0; column < model()->columnCount(rootIndex()); ++column) { QModelIndex index = model()->index(row, column, rootIndex()); QColor color = model()->data(index).value<QColor>(); int j = index.row(); int i = index.column(); if(color.isValid()){ //draw white background to prevent disturbings of background if transparent color painter.setBrush(Qt::white); painter.drawRect(i*(gridMargin+gridWidth),j*(gridMargin+gridWidth), gridWidth,gridWidth); painter.setBrush(color); painter.drawRect(i*(gridMargin+gridWidth),j*(gridMargin+gridWidth), gridWidth,gridWidth); } } } QModelIndexList list = selectedIndexes(); if( !list.isEmpty() ){ QModelIndex index = list.at(0); // Qt::ItemFlags flags = index.flags(); // if(flags.testFlag(QItemSelectionModel::Select)){ painter.setPen(textPen); // } // if(flags.testFlag(QItemSelectionModel::Toggle)){ // painter.setPen(highlightedPen); // } int row = index.row(); int column = index.column(); // QPointF start(column*(gridMargin+gridWidth),row*(gridMargin+gridWidth)); // QRadialGradient gradient(start+QPointF(gridWidth/2,gridWidth/2),0.1,start+QPointF(gridWidth,gridWidth)); // gradient.setColorAt(0.5,Qt::transparent); // gradient.setColorAt(1.0,Qt::black); // foreground = QPen(Qt::black); // QBrush brush(gradient); // brush.setStyle(Qt::RadialGradientPattern); painter.setBrush(Qt::NoBrush); // painter.setBrush(brush); painter.drawRect(column*(gridMargin+gridWidth),row*(gridMargin+gridWidth), gridWidth,gridWidth); } }
void KNMusicAlbumView::paintEvent(QPaintEvent *event) { Q_UNUSED(event) //Initial the painter. QPainter painter(viewport()); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true); //Check if we need to paint the background. if(autoFillBackground()) { painter.fillRect(rect(), viewOptions().palette.base()); } //Check the model first. if(m_proxyModel==nullptr) { return; } //Get the row count. int albumCount=m_proxyModel->rowCount(); //Check the album count first. if(albumCount==0) { return; } //Calculate the line count. m_lineCount=(albumCount+m_maxColumnCount-1)/m_maxColumnCount; //Generate the parameters. int currentColumn=0, currentLeft=m_spacing, currentLine=verticalScrollBar()->value()/m_itemSpacingHeight, currentTop=m_spacing+currentLine*m_itemSpacingHeight, currentRow=currentLine*m_maxColumnCount, heightSurplus=height()+m_itemSpacingHeight; /* * currentColumn * | * +-------------------------------------------------+ * | +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ | * | | | | | | | | | | | | | | | | | | * | +---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+ | * | | * | +---+ +---+ xxxxx-currentTop | * | | | | | xxxxx | - currentLine * | +---+ +---+ xxxxx | * | | | | -+ * | currentLeft-+ | | | * | +--QModelIndex(25252, 0) | | * | | | | * | currentRow | +-heightSurplus * | | | * | | | * | | | * +-------------------------------------------------+ -+ */ //Change the origin of coordinate. painter.translate(0, -verticalScrollBar()->value()); //Draw all the albums, until there's no album left, no height surplus. while(currentRow < albumCount && heightSurplus > 0) { //Get the source index of the item. QModelIndex &&proxyIndex=m_proxyModel->index(currentRow, 0); //If the source index is not the current index, then draw the album. if(m_proxyModel->mapToSource(proxyIndex)!=m_selectedIndex) { //Draw the unselect album. paintAlbum(painter, currentLeft, currentTop, proxyIndex); } //Add current row to the next album model row. ++currentRow; //Add current column to the next position. ++currentColumn; //Check if we need to move to next row. if(currentColumn>=m_maxColumnCount) { //Add current line. ++currentLine; //Reset the column. currentColumn=0; //Change the position. currentLeft=m_spacing; currentTop+=m_itemSpacingHeight; heightSurplus-=m_itemSpacingHeight; } else { //Move to next column position. currentLeft+=m_itemSpacingWidth; } } //Update the scroll bar value. updateGeometries(); }