void color_button::paintEvent(QPaintEvent* paint_event) { QPushButton::paintEvent(paint_event); QPainter painter(this); QBrush fill_brush(_current_color); fill_brush.setStyle(Qt::SolidPattern); painter.fillRect(this->contentsRect(), fill_brush); }
void BoardDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { // Проверки if (!index.isValid()) return; int row = index.row(); if (row <= 0 || row >= model_->rowCount() - 1) return; int col = index.column(); if (col <= 0 || col >= model_->columnCount() - 1) return; painter->save(); QRectF rect(option.rect); // Отрисовка фона if (skin == 0) { QBrush fill_brush(QColor(220, 179, 92, 255), Qt::SolidPattern); painter->fillRect(rect, fill_brush); } else { QPixmap *pixmap = pixmaps->getBoardPixmap(col - 1, row - 1, rect.width(), rect.height()); painter->drawPixmap(rect, *pixmap, pixmap->rect()); } QBrush brush1(Qt::SolidPattern); int row_min = 2; int row_max = model_->rowCount() - 3; int col_min = 2; int col_max = model_->columnCount() - 3; if (row >= row_min && row <= row_max && col >= col_min && col <= col_max) { // Отрисовка центральных линий qreal x = rect.left() + rect.width() / 2.0; qreal y = rect.top() + rect.height() / 2.0; painter->setPen(Qt::darkGray); painter->drawLine(rect.left(), y - 1, rect.right(), y - 1); painter->drawLine(x - 1, rect.top(), x - 1, rect.bottom()); painter->setPen(Qt::lightGray); painter->drawLine(rect.left(), y, rect.right(), y); painter->drawLine(x, rect.top(), x, rect.bottom()); // Отрисовка разделителя if (row == row_min || col == col_min || row == row_max || col == col_max) { painter->setPen(Qt::black); if (row == row_min) { painter->drawLine(rect.topLeft(), rect.topRight()); } else if (row == row_max) { QPointF p1 = rect.bottomLeft(); p1.setY(p1.y() - 1.0); QPointF p2 = rect.bottomRight(); p2.setY(p2.y() - 1.0); painter->drawLine(p1, p2); } if (col == col_min) { painter->drawLine(rect.topLeft(), rect.bottomLeft()); } else if (col == col_max) { QPointF p1 = rect.topRight(); p1.setX(p1.x() - 1); QPointF p2 = rect.bottomRight(); p2.setX(p2.x() - 1); painter->drawLine(p1, p2); } } if (model_->selectX == col && model_->selectY == row) { brush1.setColor(QColor(0, 255, 0, 64)); painter->fillRect(rect, brush1); } // Отрисовка если курсор мыши над клеткой if (option.state & QStyle::State_MouseOver) { brush1.setColor(QColor(0, 0, 0, 32)); painter->fillRect(rect, brush1); } rect.setWidth(rect.width() - 1); rect.setHeight(rect.height() - 1); // Отрисовка если клетка выбрана if (option.state & QStyle::State_Selected) { QRectF rect2(rect); rect2.setLeft(rect2.left() + 1); rect2.setTop(rect2.top() + 1); rect2.setWidth(rect2.width() - 1); rect2.setHeight(rect2.height() - 1); painter->setPen(Qt::gray); painter->drawRect(rect2); } // Отрисовка элемента const GameElement *el = model_->getGameElement(col, row); if (el) { el->paint(painter, rect); } } else { // Рисуем координаты if ((row == 1 || row == model_->columnCount() - 2) && col >= 2 && col <= model_->columnCount() - 3) { // Буквы QString text = horHeaderString.at(col - 2); painter->drawText(rect, Qt::AlignCenter,text, 0); } else if ((col == 1 || model_->rowCount() - 2) && row >= 2 && row <= model_->rowCount() - 3) { // Цифры QString text = QString::number(row - 1); painter->drawText(rect, Qt::AlignCenter,text, 0); } } painter->restore(); }