Example #1
0
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);
    }
}
Example #2
0
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::WindowText));
    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++;
            }
        }
    }
}