QRect AMScanThumbnailGridView::visualRect(const QModelIndex &index) const { QRect rowContentRect; if(index.parent().isValid()) { // is a thumbnail rowContentRect = geometryManager_->contentGeometryAt(index.parent().row(), horizontalOffset(), verticalOffset()); } else { rowContentRect = geometryManager_->contentGeometryAt(index.row(), horizontalOffset(), verticalOffset()); } if(!index.parent().isValid()) { // Is Scan switch(index.column()) { case 0: return rowContentRect; case 1: return geometryManager_->scanNameRectangle(rowContentRect); case 3: return geometryManager_->scanDateRectangle(rowContentRect); case 4: return geometryManager_->scanTechniqueRectangle(rowContentRect); default: return QRect(); } } else { // Is Thumbnail switch(index.column()) { case 1: return geometryManager_->thumbnailImageRectangle(rowContentRect); case 2: return geometryManager_->thumbnailTitleRectangle(rowContentRect); default: return QRect(); } } }
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(); }
QPoint CodeCompleterListView::infoFramePos() const { const QRect &r = rectForIndex(currentIndex()); QPoint p((parentWidget()->mapToGlobal( parentWidget()->rect().topRight())).x() + 3, mapToGlobal(r.topRight()).y() - verticalOffset() ); return p; }
QRect AMScanThumbnailGridView::backgroundRect(const QModelIndex &index) const { QRect rowContentRect; rowContentRect = geometryManager_->contentGeometryAt(index.row(), horizontalOffset(), verticalOffset()); return rowContentRect; }
QModelIndex GroupedIconView::indexAt(const QPoint& p) const { const QPoint viewport_p = p + QPoint(horizontalOffset(), verticalOffset()); const int count = visual_rects_.count(); for (int i=0 ; i<count ; ++i) { if (visual_rects_[i].contains(viewport_p)) { return model()->index(i, 0); } } return QModelIndex(); }
QPoint CodeCompleterListView::infoFramePos() const { const QRect &r = rectForIndex(currentIndex()); int xoffset = this->frameWidth()+3; int yoffset = this->frameWidth()-verticalOffset(); QScrollBar *vsb = this->verticalScrollBar(); if (vsb && vsb->isVisible()) xoffset += this->horizontalScrollBar()->sizeHint().height(); QPoint pt = this->mapToGlobal(r.topRight()); pt.rx() += xoffset; pt.ry() += yoffset; return pt; }
QModelIndex AMScanThumbnailGridView::indexAt(const QPoint &point) const { if(!model()) return QModelIndex(); QPoint adjustedPoint(point.x() + horizontalOffset(), point.y() + verticalOffset()); int integerIndexAt = geometryManager_->contentIndexAt(adjustedPoint); if(integerIndexAt == -1) return QModelIndex(); return model()->index(integerIndexAt, 1, QModelIndex()); }
void EditDeleteComboBoxView::mousePressEvent( QMouseEvent *event ) { DEBUG_BLOCK QModelIndex index = indexAt( event->pos() ); QPoint mousePressPos = event->pos(); mousePressPos.rx() += horizontalOffset(); mousePressPos.ry() += verticalOffset(); if ( EditDeleteDelegate::hitsEdit( mousePressPos, rectForIndex( index ) ) ) emit( editItem( index.data().toString() ) ); else if ( EditDeleteDelegate::hitsDelete( mousePressPos, rectForIndex( index ) ) ) emit( deleteItem( index.data().toString() ) ); QListView::mousePressEvent( event ); }
void ListView_Impl::paintDropTarget(QPainter *painter, const QModelIndex& index, int where) { QStyleOptionViewItem option = viewOptions(); QRect rect = this->visualRect(index); QWidget *viewport = this->viewport(); QColor highlight = palette().color(QPalette::HighlightedText); QColor color = option.state & QStyle::State_Selected ? highlight : palette().color(QPalette::Highlight); QPen pen(color); painter->save(); if (!index.isValid()) where = SL_EVENT_DRAG_ON_VIEWPORT; switch (where) { case SL_EVENT_DRAG_BELOW_ITEM: case SL_EVENT_DRAG_ABOVE_ITEM: { if (viewMode() == IconMode) { QSize size = gridSize(); if (size.isEmpty()) size = rect.size(); int x, y, height = size.height(); int cellWidth = size.width() + spacing(); int cellHeight = height + spacing(); x = rect.left() + horizontalOffset(); if (where == SL_EVENT_DRAG_BELOW_ITEM) x += cellWidth; x = ((x / cellWidth) * cellWidth) - horizontalOffset(); y = (((rect.top() + verticalOffset()) / cellHeight) * cellHeight) - verticalOffset(); height = qMax(5, height - 5); painter->setRenderHint(QPainter::Antialiasing); pen.setWidth(3); pen.setColor(highlight); painter->setPen(pen); painter->drawEllipse(QPointF(x, y + height), 3, 3); painter->drawLine(x, y + 5, x, y + height - 3); pen.setWidth(2); pen.setColor(color); painter->setPen(pen); painter->drawEllipse(QPointF(x, y + height), 3, 3); painter->drawLine(x, y + 5, x, y + height - 3); } else { int x, y, width; if (where == SL_EVENT_DRAG_BELOW_ITEM) y = rect.bottom() + 1; else y = rect.top(); x = rect.left(); width = viewport->width() - rect.left() - 10; painter->setRenderHint(QPainter::Antialiasing); pen.setWidth(3); pen.setColor(highlight); painter->setPen(pen); painter->drawEllipse(QPointF(x + width, y), 3, 3); painter->drawLine(x, y, x + width - 3, y); pen.setWidth(2); pen.setColor(color); painter->setPen(pen); painter->drawEllipse(QPointF(x + width, y), 3, 3); painter->drawLine(x, y, x + width - 3, y); } } break; case SL_EVENT_DRAG_ON_ITEM: { option.rect = rect; rect.adjust(1, 1, -1, -1); painter->setRenderHint(QPainter::Antialiasing); int radius = qMin(8, rect.height() / 2); pen.setWidth(3); pen.setColor(highlight); painter->setPen(pen); painter->drawRoundedRect(rect, radius, radius); pen.setWidth(2); if (viewMode() == IconMode) { color = palette().color(QPalette::Inactive, QPalette::Highlight); pen.setColor(color); painter->setPen(pen); painter->setBrush(QBrush(color)); painter->drawRoundedRect(rect, radius, radius); QItemSelectionModel *selection = selectionModel(); if ((selection) && (selection->isSelected(index))) option.state |= QStyle::State_Selected; if (!(model()->flags(index) & Qt::ItemIsEnabled)) option.state &= ~QStyle::State_Enabled; if (option.state & QStyle::State_Enabled) option.palette.setCurrentColorGroup(QPalette::Normal); else option.palette.setCurrentColorGroup(QPalette::Disabled); itemDelegate(index)->paint(painter, option, index); } else { pen.setColor(color); painter->setPen(pen); painter->drawRoundedRect(rect, radius, radius); } } break; case SL_EVENT_DRAG_ON_VIEWPORT: { rect = viewport->rect(); rect.adjust(0, 0, -1, -1); painter->setRenderHint(QPainter::Antialiasing, false); pen.setWidth(5); pen.setColor(highlight); painter->setPen(pen); painter->drawRect(rect); pen.setWidth(3); pen.setColor(color); painter->setPen(pen); painter->drawRect(rect); } break; } painter->restore(); }
void AMScanThumbnailGridView::scrollTo(const QModelIndex &index, QAbstractItemView::ScrollHint hint) { // Note: currently ignores the scroll hint. Places the corresponding scan as close to the top of // the view as is possible Q_UNUSED(hint); if(!index.isValid()) return; // If the passed index refers to a thumbnail (i.e. its parent is valid) then we need to // scrollTo the index of the parent scan if(index.parent().isValid()) { scrollTo(index.parent(), hint); return; } // Obtian the geometry of the index int itemCellIndex = index.row(); QRect indexCellGeometry = geometryManager_->cellGeometryAt(itemCellIndex, horizontalOffset(), verticalOffset()); verticalScrollBar()->setValue(indexCellGeometry.y()); horizontalScrollBar()->setValue(indexCellGeometry.x()); }
inline int tst_verticalOffset() const { return verticalOffset(); }
void ClipboardBrowser::preload(int minY, int maxY) { ClipboardBrowser::Lock lock(this); QModelIndex ind; int i = 0; int y = spacing(); const int s = 2 * spacing(); int offset = verticalOffset(); // Find first index to preload. forever { ind = index(i); if ( !ind.isValid() ) return; if ( !isIndexHidden(ind) ) { const int h = d.sizeHint(ind).height(); y += h; // bottom of item if (y >= offset) break; y += s; // top of next item } d.setRowVisible(i, false); ++i; } // Absolute to relative. y -= offset; bool update = false; // Preload items backwards. forever { const int lastIndex = i; for ( ind = index(--i); ind.isValid() && isIndexHidden(ind); ind = index(--i) ) {} if ( !ind.isValid() ) { i = lastIndex; ind = index(i); break; } const QRect oldRect(visualRect(ind)); // Fetch item. d.cache(ind); const int h = d.sizeHint(ind).height(); // Re-layout rows afterwards if size has changed. const int dy = h - oldRect.height(); if (dy != 0) { maxY += dy; update = true; } // Done? y -= h; // top of item if (y + h < minY) break; y -= s; // bottom of previous item } y = visualRect(ind).y(); bool lastToPreload = false; // Render visible items, re-layout rows and correct scroll offset. forever { if (m_lastFiltered != -1 && m_lastFiltered < i) break; const QRect oldRect(update ? QRect() : visualRect(ind)); // Fetch item. d.cache(ind); const int h = d.sizeHint(ind).height(); // Re-layout rows afterwards if row position or size has changed. if (!update) update = (y != oldRect.y() || h != oldRect.height()); // Correct widget position. d.updateRowPosition(i, y); d.setRowVisible(i, true); // Next. ind = index(++i); // Done? y += h + s; // top of item if (y > maxY) { if (lastToPreload) break; lastToPreload = true; // One more item to preload. } // Skip hidden. for ( ; ind.isValid() && isIndexHidden(ind); ind = index(++i) ) {} if ( !ind.isValid() ) break; } // Hide the rest. for ( ; i < m.rowCount(); ++i ) d.setRowVisible(i, false); if (update) scheduleDelayedItemsLayout(); }
void GroupedIconView::paintEvent(QPaintEvent* e) { // This code was adapted from QListView::paintEvent(), changed to use the // visualRect() of items, and to draw headers. QStyleOptionViewItemV4 option(viewOptions()); if (isWrapping()) option.features = QStyleOptionViewItemV2::WrapText; option.locale = locale(); option.locale.setNumberOptions(QLocale::OmitGroupSeparator); option.widget = this; QPainter painter(viewport()); const QRect viewport_rect(e->rect().translated(horizontalOffset(), verticalOffset())); QVector<QModelIndex> toBeRendered = IntersectingItems(viewport_rect); const QModelIndex current = currentIndex(); const QAbstractItemModel *itemModel = model(); const QItemSelectionModel *selections = selectionModel(); const bool focus = (hasFocus() || viewport()->hasFocus()) && current.isValid(); const QStyle::State state = option.state; const QAbstractItemView::State viewState = this->state(); const bool enabled = (state & QStyle::State_Enabled) != 0; int maxSize = (flow() == TopToBottom) ? viewport()->size().width() - 2 * spacing() : viewport()->size().height() - 2 * spacing(); QVector<QModelIndex>::const_iterator end = toBeRendered.constEnd(); for (QVector<QModelIndex>::const_iterator it = toBeRendered.constBegin(); it != end; ++it) { if (!it->isValid()) { continue; } option.rect = visualRect(*it); if (flow() == TopToBottom) option.rect.setWidth(qMin(maxSize, option.rect.width())); else option.rect.setHeight(qMin(maxSize, option.rect.height())); option.state = state; if (selections && selections->isSelected(*it)) option.state |= QStyle::State_Selected; if (enabled) { QPalette::ColorGroup cg; if ((itemModel->flags(*it) & Qt::ItemIsEnabled) == 0) { option.state &= ~QStyle::State_Enabled; cg = QPalette::Disabled; } else { cg = QPalette::Normal; } option.palette.setCurrentColorGroup(cg); } if (focus && current == *it) { option.state |= QStyle::State_HasFocus; if (viewState == EditingState) option.state |= QStyle::State_Editing; } itemDelegate()->paint(&painter, option, *it); } // Draw headers foreach (const Header& header, headers_) { const QRect header_rect = QRect( header_indent_, header.y, viewport()->width() - header_indent_ * 2, header_height()); // Is this header contained in the area we're drawing? if (!header_rect.intersects(viewport_rect)) { continue; } // Draw the header DrawHeader(model()->index(header.first_row, 0), header_rect.translated(-horizontalOffset(), -verticalOffset()), &painter); } }
QRect GroupedIconView::visualRect(const QModelIndex& index) const { if (index.row() < 0 || index.row() >= visual_rects_.count()) return QRect(); return visual_rects_[index.row()].translated(-horizontalOffset(), -verticalOffset()); }