📄 pieview.cpp
字号:
return current;}void PieView::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::Foreground)); QPen textPen(option.palette.color(QPalette::Text)); QPen highlightedPen(option.palette.color(QPalette::HighlightedText)); QPainter painter(viewport()); painter.setRenderHint(QPainter::Antialiasing); painter.fillRect(event->rect(), background); painter.setPen(foreground); // Viewport rectangles QRect pieRect = QRect(margin, margin, pieSize, pieSize); QPoint keyPoint = QPoint(totalSize - horizontalScrollBar()->value(), margin - verticalScrollBar()->value()); if (validItems > 0) { painter.save(); painter.translate(pieRect.x() - horizontalScrollBar()->value(), pieRect.y() - verticalScrollBar()->value()); painter.drawEllipse(0, 0, pieSize, pieSize); double startAngle = 0.0; int row; for (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 angle = 360*value/totalValue; QModelIndex colorIndex = model()->index(row, 0, rootIndex()); QColor color = QColor(model()->data(colorIndex, Qt::DecorationRole).toString()); if (currentIndex() == index) painter.setBrush(QBrush(color, Qt::Dense4Pattern)); else if (selections->isSelected(index)) painter.setBrush(QBrush(color, Qt::Dense3Pattern)); else painter.setBrush(QBrush(color)); painter.drawPie(0, 0, pieSize, pieSize, int(startAngle*16), int(angle*16)); startAngle += angle; } } painter.restore(); int keyNumber = 0; for (row = 0; row < model()->rowCount(rootIndex()); ++row) { QModelIndex index = model()->index(row, 1, rootIndex()); double value = model()->data(index).toDouble(); if (value > 0.0) { QModelIndex labelIndex = model()->index(row, 0, rootIndex()); QStyleOptionViewItem option = viewOptions(); option.rect = visualRect(labelIndex); if (selections->isSelected(labelIndex)) option.state |= QStyle::State_Selected; if (currentIndex() == labelIndex) option.state |= QStyle::State_HasFocus; itemDelegate()->paint(&painter, option, labelIndex); keyNumber++; } } }}void PieView::resizeEvent(QResizeEvent * /* event */){ updateGeometries();}int PieView::rows(const QModelIndex &index) const{ return model()->rowCount(model()->parent(index));}void PieView::rowsInserted(const QModelIndex &parent, int start, int end){ for (int row = start; row <= end; ++row) { QModelIndex index = model()->index(row, 1, rootIndex()); double value = model()->data(index).toDouble(); if (value > 0.0) { totalValue += value; validItems++; } } QAbstractItemView::rowsInserted(parent, start, end);}void PieView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end){ for (int row = start; row <= end; ++row) { QModelIndex index = model()->index(row, 1, rootIndex()); double value = model()->data(index).toDouble(); if (value > 0.0) { totalValue -= value; validItems--; } } QAbstractItemView::rowsAboutToBeRemoved(parent, start, end);}void PieView::scrollContentsBy(int dx, int dy){ viewport()->scroll(dx, dy);}void PieView::scrollTo(const QModelIndex &index, ScrollHint){ QRect area = viewport()->rect(); QRect rect = visualRect(index); if (rect.left() < area.left()) horizontalScrollBar()->setValue( horizontalScrollBar()->value() + rect.left() - area.left()); else if (rect.right() > area.right()) horizontalScrollBar()->setValue( horizontalScrollBar()->value() + qMin( rect.right() - area.right(), rect.left() - area.left())); if (rect.top() < area.top()) verticalScrollBar()->setValue( verticalScrollBar()->value() + rect.top() - area.top()); else if (rect.bottom() > area.bottom()) verticalScrollBar()->setValue( verticalScrollBar()->value() + qMin( rect.bottom() - area.bottom(), rect.top() - area.top())); update();}/* Find the indices corresponding to the extent of the selection.*/void PieView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command){ // Use content widget coordinates because we will use the itemRegion() // function to check for intersections. QRect contentsRect = rect.translated(horizontalScrollBar()->value(), verticalScrollBar()->value()); int rows = model()->rowCount(rootIndex()); int columns = model()->columnCount(rootIndex()); QModelIndexList indexes; for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { QModelIndex index = model()->index(row, column, rootIndex()); QRegion region = itemRegion(index); if (!region.intersect(contentsRect).isEmpty()) indexes.append(index); } } if (indexes.size() > 0) { int firstRow = indexes[0].row(); int lastRow = indexes[0].row(); int firstColumn = indexes[0].column(); int lastColumn = indexes[0].column(); for (int i = 1; i < indexes.size(); ++i) { firstRow = qMin(firstRow, indexes[i].row()); lastRow = qMax(lastRow, indexes[i].row()); firstColumn = qMin(firstColumn, indexes[i].column()); lastColumn = qMax(lastColumn, indexes[i].column()); } QItemSelection selection( model()->index(firstRow, firstColumn, rootIndex()), model()->index(lastRow, lastColumn, rootIndex())); selectionModel()->select(selection, command); } else { QModelIndex noIndex; QItemSelection selection(noIndex, noIndex); selectionModel()->select(selection, command); } selectionRect = rect; update();}void PieView::updateGeometries(){ horizontalScrollBar()->setPageStep(viewport()->width()); horizontalScrollBar()->setRange(0, qMax(0, 2*totalSize - viewport()->width())); verticalScrollBar()->setPageStep(viewport()->height()); verticalScrollBar()->setRange(0, qMax(0, totalSize - viewport()->height()));}int PieView::verticalOffset() const{ return verticalScrollBar()->value();}/* Returns the position of the item in viewport coordinates.*/QRect PieView::visualRect(const QModelIndex &index) const{ QRect rect = itemRect(index); if (rect.isValid()) return QRect(rect.left() - horizontalScrollBar()->value(), rect.top() - verticalScrollBar()->value(), rect.width(), rect.height()); else return rect;}/* Returns a region corresponding to the selection in viewport coordinates.*/QRegion PieView::visualRegionForSelection(const QItemSelection &selection) const{ int ranges = selection.count(); if (ranges == 0) return QRect(); // Note that we use the top and bottom functions of the selection range // since the data is stored in rows. int firstRow = selection.at(0).top(); int lastRow = selection.at(0).top(); for (int i = 0; i < ranges; ++i) { firstRow = qMin(firstRow, selection.at(i).top()); lastRow = qMax(lastRow, selection.at(i).bottom()); } QModelIndex firstItem = model()->index(qMin(firstRow, lastRow), 0, rootIndex()); QModelIndex lastItem = model()->index(qMax(firstRow, lastRow), 0, rootIndex()); QRect firstRect = visualRect(firstItem); QRect lastRect = visualRect(lastItem); return firstRect.unite(lastRect);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -