void AMSamplePlateItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QString sampleName(index.data(Qt::DisplayRole).toString()); QString elementString(index.data(AM::UserRole).toString()); if(!elementString.isEmpty()) { elementString.prepend("("); elementString.append(")"); } QString createdString(index.data(AM::DescriptionRole).toString()); QString positionsString(index.data(AM::UserRole+1).toString()); if(positionsString.isEmpty()) { positionsString = "[Unknown sample position]"; } QStyleOptionViewItemV4 opt(option); initStyleOption(&opt, index); QStyle* sty = QApplication::style(); // Draw the background: (this will handle selection for us. You can also probe selection directly with opt.state & QStyle::State_Selected) sty->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter); int textStartingPoint = 0; // Do we have a pixmap available? QRect iconRect = opt.rect; iconRect.adjust(6, 0, 0, 0); if(opt.features & QStyleOptionViewItemV4::HasDecoration) { QPixmap p = opt.icon.pixmap(opt.decorationSize); sty->drawItemPixmap(painter, iconRect, Qt::AlignLeft | Qt::AlignVCenter, p); textStartingPoint += 6 + p.width() + 6; } // textRect starts 6px from the edge of the decoration, and is inset 6px on top and bottom and right. It's the full room we have available for text. QRect textRect = opt.rect; textRect.adjust( textStartingPoint, 6, -6, -6); QFont font = opt.font; font.setPointSize(font.pointSize()+4); painter->setFont(font); painter->setPen(opt.palette.text().color()); QRect actualNameTextRect; painter->drawText(textRect, Qt::AlignLeft | Qt::AlignTop, sampleName, &actualNameTextRect); // Move over by the amount that we filled with text QRect elementTextRect(actualNameTextRect.right() + 6, textRect.top(), textRect.width()-actualNameTextRect.width() - 6, actualNameTextRect.height()); font.setPointSize(font.pointSize()-4); font.setItalic(true); font.setBold(true); painter->setFont(font); painter->setPen(Qt::darkGray); QFontMetrics fm(font); QString elidedElements = fm.elidedText(elementString, Qt::ElideRight, elementTextRect.width()); painter->drawText(elementTextRect, Qt::AlignLeft | Qt::AlignBottom, elidedElements); QRect descriptionTextRect(textRect.left(), actualNameTextRect.bottom(), textRect.width(), textRect.height()-actualNameTextRect.height()); font.setPointSize(font.pointSize()-2); font.setItalic(false); font.setBold(false); painter->setFont(font); painter->setPen(Qt::darkGray); painter->drawText(descriptionTextRect, Qt::AlignLeft | Qt::AlignTop, createdString); QFontMetrics fm2(font); QRect actualPositionsTextRect; painter->drawText(textRect, Qt::AlignLeft | Qt::AlignBottom, fm2.elidedText(positionsString, Qt::ElideMiddle, textRect.width()), &actualPositionsTextRect); int dividerLineY = actualPositionsTextRect.top() - 4; painter->drawLine(QPoint(textRect.left(), dividerLineY), QPoint(textRect.right(), dividerLineY)); }
// From: http://stackoverflow.com/questions/3903223/qt4-how-to-blur-qpixmap-image QImage BlurPushButton::blurred(const QImage& image, const QRect& rect, int radius, bool alphaOnly) { int tab[] = { 14, 10, 8, 6, 5, 5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 }; int alpha = (radius < 1) ? 16 : (radius > 17) ? 1 : tab[radius-1]; QImage result = image.convertToFormat(QImage::Format_ARGB32_Premultiplied); int r1 = rect.top(); int r2 = rect.bottom(); int c1 = rect.left(); int c2 = rect.right(); int bpl = result.bytesPerLine(); int rgba[4]; unsigned char* p; int i1 = 0; int i2 = 3; if (alphaOnly) i1 = i2 = (QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 3); for (int col = c1; col <= c2; col++) { p = result.scanLine(r1) + col * 4; for (int i = i1; i <= i2; i++) rgba[i] = p[i] << 4; p += bpl; for (int j = r1; j < r2; j++, p += bpl) for (int i = i1; i <= i2; i++) p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4; } for (int row = r1; row <= r2; row++) { p = result.scanLine(row) + c1 * 4; for (int i = i1; i <= i2; i++) rgba[i] = p[i] << 4; p += 4; for (int j = c1; j < c2; j++, p += 4) for (int i = i1; i <= i2; i++) p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4; } for (int col = c1; col <= c2; col++) { p = result.scanLine(r2) + col * 4; for (int i = i1; i <= i2; i++) rgba[i] = p[i] << 4; p -= bpl; for (int j = r1; j < r2; j++, p -= bpl) for (int i = i1; i <= i2; i++) p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4; } for (int row = r1; row <= r2; row++) { p = result.scanLine(row) + c2 * 4; for (int i = i1; i <= i2; i++) rgba[i] = p[i] << 4; p -= 4; for (int j = c1; j < c2; j++, p -= 4) for (int i = i1; i <= i2; i++) p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4; } return result.copy(rect); }
void HistogramItem::drawBar(QPainter *painter, Qt::Orientation, const QRect& rect) const { painter->save(); const QColor color(painter->pen().color()); #if QT_VERSION >= 0x040000 const QRect r = rect.normalized(); #else const QRect r = rect.normalize(); #endif const int factor = 125; const QColor light(color.light(factor)); const QColor dark(color.dark(factor)); painter->setBrush(color); painter->setPen(Qt::NoPen); QwtPainter::drawRect(painter, r.x() + 1, r.y() + 1, r.width() - 2, r.height() - 2); painter->setBrush(Qt::NoBrush); painter->setPen(QPen(light, 2)); #if QT_VERSION >= 0x040000 QwtPainter::drawLine(painter, r.left() + 1, r.top() + 2, r.right() + 1, r.top() + 2); #else QwtPainter::drawLine(painter, r.left(), r.top() + 2, r.right() + 1, r.top() + 2); #endif painter->setPen(QPen(dark, 2)); #if QT_VERSION >= 0x040000 QwtPainter::drawLine(painter, r.left() + 1, r.bottom(), r.right() + 1, r.bottom()); #else QwtPainter::drawLine(painter, r.left(), r.bottom(), r.right() + 1, r.bottom()); #endif painter->setPen(QPen(light, 1)); #if QT_VERSION >= 0x040000 QwtPainter::drawLine(painter, r.left(), r.top() + 1, r.left(), r.bottom()); QwtPainter::drawLine(painter, r.left() + 1, r.top() + 2, r.left() + 1, r.bottom() - 1); #else QwtPainter::drawLine(painter, r.left(), r.top() + 1, r.left(), r.bottom() + 1); QwtPainter::drawLine(painter, r.left() + 1, r.top() + 2, r.left() + 1, r.bottom()); #endif painter->setPen(QPen(dark, 1)); #if QT_VERSION >= 0x040000 QwtPainter::drawLine(painter, r.right() + 1, r.top() + 1, r.right() + 1, r.bottom()); QwtPainter::drawLine(painter, r.right(), r.top() + 2, r.right(), r.bottom() - 1); #else QwtPainter::drawLine(painter, r.right() + 1, r.top() + 1, r.right() + 1, r.bottom() + 1); QwtPainter::drawLine(painter, r.right(), r.top() + 2, r.right(), r.bottom()); #endif painter->restore(); }
bool GroundPlaneGenerator::getBoardRects(const QString & boardSvg, QGraphicsItem * board, double res, double keepoutSpace, QList<QRect> & rects) { QByteArray boardByteArray; QString tempColor("#000000"); QStringList exceptions; exceptions << "none" << ""; if (!SvgFileSplitter::changeColors(boardSvg, tempColor, exceptions, boardByteArray)) { return false; } QRectF br = board->sceneBoundingRect(); double bWidth = res * br.width() / FSvgRenderer::printerScale(); double bHeight = res * br.height() / FSvgRenderer::printerScale(); QImage image(bWidth, bHeight, QImage::Format_Mono); image.setDotsPerMeterX(res * GraphicsUtils::InchesPerMeter); image.setDotsPerMeterY(res * GraphicsUtils::InchesPerMeter); image.fill(0xffffffff); QSvgRenderer renderer(boardByteArray); QPainter painter; painter.begin(&image); painter.setRenderHint(QPainter::Antialiasing, false); renderer.render(&painter); painter.end(); #ifndef QT_NO_DEBUG image.save("getBoardRects.png"); #endif QColor keepaway(255,255,255); // now add keepout area to the border QImage image2 = image.copy(); painter.begin(&image2); painter.setRenderHint(QPainter::Antialiasing, false); painter.fillRect(0, 0, image2.width(), keepoutSpace, keepaway); painter.fillRect(0, image2.height() - keepoutSpace, image2.width(), keepoutSpace, keepaway); painter.fillRect(0, 0, keepoutSpace, image2.height(), keepaway); painter.fillRect(image2.width() - keepoutSpace, 0, keepoutSpace, image2.height(), keepaway); for (int y = 0; y < image.height(); y++) { for (int x = 0; x < image.width(); x++) { QRgb current = image.pixel(x, y); if (current != 0xffffffff) { continue; } painter.fillRect(x - keepoutSpace, y - keepoutSpace, keepoutSpace + keepoutSpace, keepoutSpace + keepoutSpace, keepaway); } } painter.end(); #ifndef QT_NO_DEBUG image2.save("getBoardRects2.png"); #endif scanLines(image2, bWidth, bHeight, rects); // combine parallel equal-sized rects int ix = 0; while (ix < rects.count()) { QRect r = rects.at(ix++); for (int j = ix; j < rects.count(); j++) { QRect s = rects.at(j); if (s.bottom() == r.bottom()) { // on same row; keep going continue; } if (s.top() > r.bottom() + 1) { // skipped row, can't join break; } if (s.left() == r.left() && s.right() == r.right()) { // join these r.setBottom(s.bottom()); rects.removeAt(j); ix--; rects.replace(ix, r); break; } } } return true; }
QRegion QDecorationWindows::region(const QWidget *widget, const QRect &rect, int type) { Qt::WindowFlags flags = widget->windowFlags(); bool hasTitle = flags & Qt::WindowTitleHint; bool hasSysMenu = flags & Qt::WindowSystemMenuHint; bool hasContextHelp = flags & Qt::WindowContextHelpButtonHint; bool hasMinimize = flags & Qt::WindowMinimizeButtonHint; bool hasMaximize = flags & Qt::WindowMaximizeButtonHint; const QFontMetrics fontMetrics = QApplication::fontMetrics(); int titleHeight = hasTitle ? qMax(20, fontMetrics.height()) : 0; int state = widget->windowState(); bool isMinimized = state & Qt::WindowMinimized; bool isMaximized = state & Qt::WindowMaximized; QRegion region; switch (type) { case Menu: { if (hasSysMenu) { region = QRect(rect.left() + 2, rect.top() - titleHeight, menu_width, titleHeight); } } break; case Title: { QRect r(rect.left() + (hasSysMenu ? menu_width + 4: 0), rect.top() - titleHeight, rect.width() - (hasSysMenu ? menu_width : 0) - close_width - (hasMaximize ? maximize_width : 0) - (hasMinimize ? minimize_width : 0) - (hasContextHelp ? help_width : 0) - 3, titleHeight); if (r.width() > 0) region = r; } break; case Help: { if (hasContextHelp) { QRect r(rect.right() - close_width - (hasMaximize ? maximize_width : 0) - (hasMinimize ? minimize_width : 0) - help_width - 3, rect.top() - titleHeight, help_width, titleHeight); if (r.left() > rect.left() + titleHeight) region = r; } } break; case Minimize: { if (hasMinimize && !isMinimized) { QRect r(rect.right() - close_width - (hasMaximize ? maximize_width : 0) - minimize_width - 3, rect.top() - titleHeight, minimize_width, titleHeight); if (r.left() > rect.left() + titleHeight) region = r; } } break; case Maximize: { if (hasMaximize && !isMaximized) { QRect r(rect.right() - close_width - maximize_width - 3, rect.top() - titleHeight, maximize_width, titleHeight); if (r.left() > rect.left() + titleHeight) region = r; } } break; case Normalize: { if (hasMinimize && isMinimized) { QRect r(rect.right() - close_width - (hasMaximize ? maximize_width : 0) - minimize_width - 3, rect.top() - titleHeight, minimize_width, titleHeight); if (r.left() > rect.left() + titleHeight) region = r; } else if (hasMaximize && isMaximized) { QRect r(rect.right() - close_width - maximize_width - 3, rect.top() - titleHeight, maximize_width, titleHeight); if (r.left() > rect.left() + titleHeight) region = r; } } break; case Close: { QRect r(rect.right() - close_width - 1, rect.top() - titleHeight, close_width, titleHeight); if (r.left() > rect.left() + titleHeight) region = r; } break; default: region = QDecorationDefault::region(widget, rect, type); break; } return region; }