QBitmap Slider::drawValue() { QFontMetrics fm(this->font()); QString st; st.setNum(value,'f',precision); QPainter painter; int _x,_y; QBitmap res; if (orientation == 0) { res.resize(width(),(height()-20)/2); res.fill(Qt::color0); painter.begin(&res); painter.setPen(Qt::color1); painter.setBrush(Qt::color1); painter.setFont(this->font()); _x = (int)((res.width()-fm.width(st))/2); _y = (int)(res.height()); painter.drawText(_x,_y,st); } else if (orientation == 1) { res.resize((width()-20)/2,height()); res.fill(Qt::color0); painter.begin(&res); painter.setPen(Qt::color1); painter.setBrush(Qt::color1); painter.setFont(this->font()); _x = 0; _y = (int)((res.height()+fm.height())/2); painter.drawText(_x,_y,st); } painter.end(); return res; }
HCURSOR QWindowsCursor::createPixmapCursor(const QPixmap &pixmap, int hotX, int hotY) { HCURSOR cur = 0; QBitmap mask = pixmap.mask(); if (mask.isNull()) { mask = QBitmap(pixmap.size()); mask.fill(Qt::color1); } HBITMAP ic = qt_pixmapToWinHBITMAP(pixmap, /* HBitmapAlpha */ 2); const HBITMAP im = qt_createIconMask(mask); ICONINFO ii; ii.fIcon = 0; ii.xHotspot = hotX; ii.yHotspot = hotY; ii.hbmMask = im; ii.hbmColor = ic; cur = CreateIconIndirect(&ii); DeleteObject(ic); DeleteObject(im); return cur; }
QPixmap BitmapFactoryInst::resize(int w, int h, const QPixmap& p, Qt::BGMode bgmode) const { if (bgmode == Qt::TransparentMode) { if (p.width() == 0 || p.height() == 0) w = 1; QPixmap pix = p; int x = pix.width () > w ? 0 : (w - pix.width ())/2; int y = pix.height() > h ? 0 : (h - pix.height())/2; if (x == 0 && y == 0) return pix; QPixmap pm (w,h); QBitmap mask (w,h); mask.fill(Qt::color0); QBitmap bm = pix.mask(); if (!bm.isNull()) { QPainter painter(&mask); painter.drawPixmap(QPoint(x, y), bm, QRect(0, 0, pix.width(), pix.height())); pm.setMask(mask); } else { pm.setMask(mask); pm = fillRect(x, y, pix.width(), pix.height(), pm, Qt::OpaqueMode); } QPainter pt; pt.begin( &pm ); pt.drawPixmap(x, y, pix); pt.end(); return pm; } else { // Qt::OpaqueMode QPixmap pix = p; if (pix.width() == 0 || pix.height() == 0) return pix; // do not resize a null pixmap QPalette pal = qApp->palette(); QColor dl = pal.color(QPalette::Disabled, QPalette::Light); QColor dt = pal.color(QPalette::Disabled, QPalette::Text); QPixmap pm = pix; pm = QPixmap(w,h); pm.fill(dl); QPainter pt; pt.begin( &pm ); pt.setPen( dl ); pt.drawPixmap(1, 1, pix); pt.setPen( dt ); pt.drawPixmap(0, 0, pix); pt.end(); return pm; } }
void BackgroundWidget::paintEvent( QPaintEvent *e ) { if ( !b_withart ) { /* we just want background autofill */ QWidget::paintEvent( e ); return; } int i_maxwidth, i_maxheight; QPixmap pixmap = QPixmap( pixmapUrl ); QPainter painter(this); QBitmap pMask; float f_alpha = 1.0; i_maxwidth = __MIN( maximumWidth(), width() ) - MARGIN * 2; i_maxheight = __MIN( maximumHeight(), height() ) - MARGIN * 2; painter.setOpacity( property( "opacity" ).toFloat() ); if ( height() > MARGIN * 2 ) { /* Scale down the pixmap if the widget is too small */ if( pixmap.width() > i_maxwidth || pixmap.height() > i_maxheight ) { pixmap = pixmap.scaled( i_maxwidth, i_maxheight, Qt::KeepAspectRatio, Qt::SmoothTransformation ); } else if ( b_expandPixmap && pixmap.width() < width() && pixmap.height() < height() ) { /* Scale up the pixmap to fill widget's size */ f_alpha = ( (float) pixmap.height() / (float) height() ); pixmap = pixmap.scaled( width() - MARGIN * 2, height() - MARGIN * 2, Qt::KeepAspectRatio, ( f_alpha < .2 )? /* Don't waste cpu when not visible */ Qt::SmoothTransformation: Qt::FastTransformation ); /* Non agressive alpha compositing when sizing up */ pMask = QBitmap( pixmap.width(), pixmap.height() ); pMask.fill( QColor::fromRgbF( 1.0, 1.0, 1.0, f_alpha ) ); pixmap.setMask( pMask ); } painter.drawPixmap( MARGIN + ( i_maxwidth - pixmap.width() ) /2, MARGIN + ( i_maxheight - pixmap.height() ) /2, pixmap); } QWidget::paintEvent( e ); }
kpToolWidgetSpraycanSize::kpToolWidgetSpraycanSize (QWidget *parent, const char *name) : kpToolWidgetBase (parent, name) { #if DEBUG_KP_TOOL_WIDGET_SPRAYCAN_SIZE kdDebug () << "kpToolWidgetSpraycanSize::kpToolWidgetSpraycanSize() CALLED!" << endl; #endif for (int i = 0; i < int (sizeof (spraycanSizes) / sizeof (spraycanSizes [0])); i++) { int s = spraycanSizes [i]; QString iconName = QString ("tool_spraycan_%1x%1").arg (s).arg(s); #if DEBUG_KP_TOOL_WIDGET_SPRAYCAN_SIZE kdDebug () << "\ticonName=" << iconName << endl; #endif QPixmap pixmap (s, s); pixmap.fill (Qt::white); QPainter painter (&pixmap); painter.drawPixmap (0, 0, UserIcon (iconName)); painter.end (); QImage image = kpPixmapFX::convertToImage (pixmap); QBitmap mask (pixmap.width (), pixmap.height ()); mask.fill (Qt::color0); painter.begin (&mask); painter.setPen (Qt::color1); for (int y = 0; y < image.height (); y++) { for (int x = 0; x < image.width (); x++) { if ((image.pixel (x, y) & RGB_MASK) == 0/*black*/) painter.drawPoint (x, y); // mark as opaque } } painter.end (); pixmap.setMask (mask); addOption (pixmap, i18n ("%1x%2").arg (s).arg (s)/*tooltip*/); if (i == 1) startNewOptionRow (); } finishConstruction (0, 0); }
void OSDWidget::renderOSDText( const QString &text) { static QBitmap mask; // Set a sensible maximum size, don't cover the whole desktop or cross the screen //Actually unused but it definitly should //QSize max = QApplication::desktop() ->screen( m_screen ) ->size() - QSize( MARGIN*2 + 20, 100 ); if(this->m_srt!=0){ delete m_srt; } this->m_srt=new QSimpleRichText(text,this->font(),this->m_context); // The title cannnot be taller than one line // AlignAuto = align Arabic to the right, etc. int w=this->m_srt->widthUsed(); int h=this->m_srt->height(); osdBuffer.resize(w,h); mask.resize(w,h); // Start painting! QPainter bufferPainter( &osdBuffer ); QPainter maskPainter( &mask ); // Draw backing rectangle //bufferPainter.save(); bufferPainter.setPen( Qt::black ); bufferPainter.setBrush( backgroundColor() ); bufferPainter.drawRoundRect( 0,0,w,h, 1500 /w, 1500/h); //bufferPainter.restore(); this->m_srt->draw(&bufferPainter,0,0,QRect(),this->colorGroup()); // Masking for transparency mask.fill( Qt::black ); maskPainter.setBrush( Qt::white ); maskPainter.drawRoundRect(0,0,w,h, 1500/w,1500/h); setMask( mask ); //do last to reduce noticeable change when showing multiple OSDs in succession reposition(QSize(w,h)); m_currentText = text; m_dirty = false; if(this->m_linking&&this==this->mouseGrabber()){ this->releaseMouse(); this->m_linking=false; } update(); }
HICON QPixmap::toWinHICON() const { QBitmap maskBitmap = mask(); if (maskBitmap.isNull()) { maskBitmap= QBitmap(size()); maskBitmap.fill(Qt::color1); } ICONINFO ii; ii.fIcon = true; ii.hbmMask = qt_createIconMask(maskBitmap); ii.hbmColor = toWinHBITMAP(QPixmap::Alpha); ii.xHotspot = 0; ii.yHotspot = 0; HICON hIcon = CreateIconIndirect(&ii); DeleteObject(ii.hbmColor); DeleteObject(ii.hbmMask); return hIcon; }
kpToolWidgetLineWidth::kpToolWidgetLineWidth (QWidget *parent, const char *name) : kpToolWidgetBase (parent, name) { setInvertSelectedPixmap (); int numLineWidths = sizeof (lineWidths) / sizeof (lineWidths [0]); int w = (width () - 2/*margin*/) * 3 / 4; int h = (height () - 2/*margin*/ - (numLineWidths - 1)/*spacing*/) * 3 / (numLineWidths * 4); for (int i = 0; i < numLineWidths; i++) { QPixmap pixmap ((w <= 0 ? width () : w), (h <= 0 ? height () : h)); pixmap.fill (Qt::white); QBitmap maskBitmap (pixmap.width (), pixmap.height ()); maskBitmap.fill (Qt::color0/*transparent*/); QPainter painter (&pixmap), maskPainter (&maskBitmap); painter.setPen (Qt::black), maskPainter.setPen (Qt::color1/*opaque*/); painter.setBrush (Qt::black), maskPainter.setBrush (Qt::color1/*opaque*/); QRect rect = QRect (0, (pixmap.height () - lineWidths [i]) / 2, pixmap.width (), lineWidths [i]); painter.drawRect (rect), maskPainter.drawRect (rect); painter.end (), maskPainter.end (); pixmap.setMask (maskBitmap); addOption (pixmap, QString::number (lineWidths [i])); startNewOptionRow (); } finishConstruction (0, 0); }
void OSDWidget::renderOSDText( const QString &txt ) { // Escaped text QString text = Konversation::removeIrcMarkup(txt); static QBitmap mask; //This is various spacings and margins, based on the font to look "just right" const uint METRIC = fontMetrics().width( 'x' ); // Set a sensible maximum size, don't cover the whole desktop or cross the screen QSize max = QApplication::desktop()->screen( m_screen )->size() - QSize( MARGIN*2 + METRIC*2, 100 ); QFont titleFont( "Arial", 12, QFont::Bold ); QFontMetrics titleFm( titleFont ); // The title cannnot be taller than one line // AlignAuto = align Arabic to the right, etc. QRect titleRect = titleFm.boundingRect( 0, 0, max.width() - METRIC, titleFm.height(), AlignAuto, m_appName ); // The osd cannot be larger than the screen QRect textRect = fontMetrics().boundingRect( 0, 0, max.width(), max.height(), AlignAuto | WordBreak, text ); if ( textRect.width() < titleRect.width() ) textRect.setWidth( titleRect.width() ); //this should still be within the screen bounds textRect.addCoords( 0, 0, METRIC*2, titleRect.height() + METRIC ); osdBuffer.resize( textRect.size() ); mask.resize( textRect.size() ); // Start painting! QPainter bufferPainter( &osdBuffer ); QPainter maskPainter( &mask ); // Draw backing rectangle const uint xround = (METRIC * 200) / textRect.width(); const uint yround = (METRIC * 200) / textRect.height(); bufferPainter.setPen( Qt::black ); bufferPainter.setBrush( backgroundColor() ); bufferPainter.drawRoundRect( textRect, xround, yround ); bufferPainter.setFont( font() ); const uint w = textRect.width() - 1; const uint h = textRect.height() - 1; // Draw the text shadow if ( m_shadow ) { bufferPainter.setPen( backgroundColor().dark( 175 ) ); bufferPainter.drawText( METRIC + 3, (METRIC/2) + titleFm.height() + 1, w, h, AlignLeft | WordBreak, text ); } // Draw the text bufferPainter.setPen( foregroundColor() ); bufferPainter.drawText( METRIC, (METRIC/2) + titleFm.height() - 1, w, h, AlignLeft | WordBreak, text ); // Draw the title text bufferPainter.setFont( titleFont ); bufferPainter.drawText( METRIC * 2, (METRIC/2), w, h, AlignLeft, m_appName ); // Masking for transparency mask.fill( Qt::black ); maskPainter.setBrush( Qt::white ); maskPainter.drawRoundRect( textRect, xround, yround ); setMask( mask ); //do last to reduce noticeable change when showing multiple OSDs in succession reposition( textRect.size() ); m_currentText = text; m_dirty = false; update(); }
void QBalloonTip::balloon(const QPoint& pos, int msecs, bool showArrow) { QRect scr = QApplication::desktop()->screenGeometry(pos); QSize sh = sizeHint(); const int border = 1; const int ah = 18, ao = 18, aw = 18, rc = 7; bool arrowAtTop = (pos.y() + sh.height() + ah < scr.height()); bool arrowAtLeft = (pos.x() + sh.width() - ao < scr.width()); setContentsMargins(border + 3, border + (arrowAtTop ? ah : 0) + 2, border + 3, border + (arrowAtTop ? 0 : ah) + 2); updateGeometry(); sh = sizeHint(); int ml, mr, mt, mb; QSize sz = sizeHint(); if (!arrowAtTop) { ml = mt = 0; mr = sz.width() - 1; mb = sz.height() - ah - 1; } else { ml = 0; mt = ah; mr = sz.width() - 1; mb = sz.height() - 1; } QPainterPath path; #if defined(QT_NO_XSHAPE) && defined(Q_WS_X11) // XShape is required for setting the mask, so we just // draw an ugly square when its not available path.moveTo(0, 0); path.lineTo(sz.width() - 1, 0); path.lineTo(sz.width() - 1, sz.height() - 1); path.lineTo(0, sz.height() - 1); path.lineTo(0, 0); move(qMax(pos.x() - sz.width(), scr.left()), pos.y()); #else path.moveTo(ml + rc, mt); if (arrowAtTop && arrowAtLeft) { if (showArrow) { path.lineTo(ml + ao, mt); path.lineTo(ml + ao, mt - ah); path.lineTo(ml + ao + aw, mt); } move(qMax(pos.x() - ao, scr.left() + 2), pos.y()); } else if (arrowAtTop && !arrowAtLeft) { if (showArrow) { path.lineTo(mr - ao - aw, mt); path.lineTo(mr - ao, mt - ah); path.lineTo(mr - ao, mt); } move(qMin(pos.x() - sh.width() + ao, scr.right() - sh.width() - 2), pos.y()); } path.lineTo(mr - rc, mt); path.arcTo(QRect(mr - rc*2, mt, rc*2, rc*2), 90, -90); path.lineTo(mr, mb - rc); path.arcTo(QRect(mr - rc*2, mb - rc*2, rc*2, rc*2), 0, -90); if (!arrowAtTop && !arrowAtLeft) { if (showArrow) { path.lineTo(mr - ao, mb); path.lineTo(mr - ao, mb + ah); path.lineTo(mr - ao - aw, mb); } move(qMin(pos.x() - sh.width() + ao, scr.right() - sh.width() - 2), pos.y() - sh.height()); } else if (!arrowAtTop && arrowAtLeft) { if (showArrow) { path.lineTo(ao + aw, mb); path.lineTo(ao, mb + ah); path.lineTo(ao, mb); } move(qMax(pos.x() - ao, scr.x() + 2), pos.y() - sh.height()); } path.lineTo(ml + rc, mb); path.arcTo(QRect(ml, mb - rc*2, rc*2, rc*2), -90, -90); path.lineTo(ml, mt + rc); path.arcTo(QRect(ml, mt, rc*2, rc*2), 180, -90); // Set the mask QBitmap bitmap = QBitmap(sizeHint()); bitmap.fill(Qt::color0); QPainter painter1(&bitmap); painter1.setPen(QPen(Qt::color1, border)); painter1.setBrush(QBrush(Qt::color1)); painter1.drawPath(path); setMask(bitmap); #endif // Draw the border pixmap = QPixmap(sz); QPainter painter2(&pixmap); painter2.setPen(QPen(palette().color(QPalette::Window).darker(160), border)); painter2.setBrush(palette().color(QPalette::Window)); painter2.drawPath(path); if (msecs > 0) timerId = startTimer(msecs); show(); }
static HCURSOR create32BitCursor(const QPixmap &pixmap, int hx, int hy) { HCURSOR cur = 0; #if !defined(Q_WS_WINCE) QBitmap mask = pixmap.mask(); if (mask.isNull()) { mask = QBitmap(pixmap.size()); mask.fill(Qt::color1); } HBITMAP ic = pixmap.toWinHBITMAP(QPixmap::Alpha); HBITMAP im = qt_createIconMask(mask); ICONINFO ii; ii.fIcon = 0; ii.xHotspot = hx; ii.yHotspot = hy; ii.hbmMask = im; ii.hbmColor = ic; cur = CreateIconIndirect(&ii); DeleteObject(ic); DeleteObject(im); #elif defined(GWES_ICONCURS) QImage bbits, mbits; bool invb, invm; bbits = pixmap.toImage().convertToFormat(QImage::Format_Mono); mbits = pixmap.toImage().convertToFormat(QImage::Format_Mono); invb = bbits.colorCount() > 1 && qGray(bbits.color(0)) < qGray(bbits.color(1)); invm = mbits.colorCount() > 1 && qGray(mbits.color(0)) < qGray(mbits.color(1)); int sysW = GetSystemMetrics(SM_CXCURSOR); int sysH = GetSystemMetrics(SM_CYCURSOR); int sysN = qMax(1, sysW / 8); int n = qMax(1, bbits.width() / 8); int h = bbits.height(); uchar* xBits = new uchar[sysH * sysN]; uchar* xMask = new uchar[sysH * sysN]; int x = 0; for (int i = 0; i < sysH; ++i) { if (i >= h) { memset(&xBits[x] , 255, sysN); memset(&xMask[x] , 0, sysN); x += sysN; } else { int fillWidth = n > sysN ? sysN : n; uchar *bits = bbits.scanLine(i); uchar *mask = mbits.scanLine(i); for (int j = 0; j < fillWidth; ++j) { uchar b = bits[j]; uchar m = mask[j]; if (invb) b ^= 0xFF; if (invm) m ^= 0xFF; xBits[x] = ~m; xMask[x] = b ^ m; ++x; } for (int j = fillWidth; j < sysN; ++j ) { xBits[x] = 255; xMask[x] = 0; ++x; } } } cur = CreateCursor(qWinAppInst(), hx, hy, sysW, sysH, xBits, xMask); #else Q_UNUSED(pixmap); Q_UNUSED(hx); Q_UNUSED(hy); #endif return cur; }