void WhiteBoardColorChooser::paintEvent(QPaintEvent *e) { QFrame::paintEvent(e); QPainter tPaint; tPaint.begin(this); tPaint.drawPixmap(swapPixmapRect().topLeft(), m_swapPixmap); QRect bgRect = backgroundRect(); QRect bgRectInside = QRect(bgRect.x () + 2, bgRect.y () + 2, bgRect.width () - 4, bgRect.height () - 4); tPaint.fillRect(bgRectInside, m_backgroundColor); qDrawShadePanel(&tPaint, bgRect, palette(), false/*not sunken*/, 2/*lineWidth*/, 0/*never fill*/); QRect fgRect = foregroundRect(); QRect fgRectInside = QRect(fgRect.x () + 2, fgRect.y () + 2, fgRect.width () - 4, fgRect.height () - 4); tPaint.fillRect(fgRectInside, m_foregroundColor); qDrawShadePanel(&tPaint, fgRect, palette (), false/*not sunken*/, 2/*lineWidth*/, 0/*never fill*/); tPaint.end(); }
void DualColorButton::mouseMoveEvent(QMouseEvent *event) { if(dragSource_ != NODRAG && (event->buttons() & Qt::LeftButton) && (event->pos() - dragStart_).manhattanLength() > QApplication::startDragDistance()) { QDrag *drag = new QDrag(this); QMimeData *mimedata = new QMimeData; const QColor color = (dragSource_ == FOREGROUND)?foreground_:background_; mimedata->setColorData(color); drag->setMimeData(mimedata); drag->start(Qt::CopyAction); } // Higlight clickable areas QRectF fgr = foregroundRect(); QRectF bgr = backgroundRect(); if(fgr.contains(event->pos())) hilite_ = 1; else if(bgr.contains(event->pos())) hilite_ = 2; else if(event->pos().x() > fgr.right() && event->pos().y() < bgr.top()) hilite_ = 3; else if(event->pos().x() < bgr.left() && event->pos().y() > fgr.bottom()) hilite_ = 4; else hilite_ = 0; update(); }
void DualColorButton::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); QPen normal = palette().color(QPalette::Mid); QPen hilite = palette().color(QPalette::Light); normal.setCosmetic(true); hilite.setCosmetic(true); painter.setRenderHint(QPainter::Antialiasing); // Draw background box const QRectF bgbox = backgroundRect(); painter.setPen(hilite_==2 ? hilite : normal); painter.setBrush(background_); painter.drawRoundedRect(bgbox, 3, 3); // Draw foreground box const QRectF fgbox = foregroundRect(); painter.setPen(hilite_==1 ? hilite : normal); painter.setBrush(foreground_); painter.drawRoundedRect(fgbox, 3, 3); // Draw reset boxes painter.setPen(hilite_==4 ? hilite : normal); painter.setBrush(Qt::white); painter.drawRect(resetWhiteRect()); painter.setBrush(Qt::black); painter.drawRect(resetBlackRect()); // Draw swap arrow static const QPointF arrows[] = { QPointF(0, 2), QPointF(2, 4), QPointF(2, 3), QPointF(4, 3), QPointF(4, 5), QPointF(3, 5), QPointF(5, 7), QPointF(7, 5), QPointF(6, 5), QPointF(6, 1), QPointF(2, 1), QPointF(2, 0) }; const qreal scale = (1.0 / 8.0) * width() / 3.0; painter.translate(ceil(fgbox.width()) + 0.0, 0); painter.scale(scale, scale); painter.setPen(hilite_==3 ? hilite : normal); painter.setBrush(palette().dark()); painter.drawPolygon(arrows, 12); }
void kpDualColorButton::mouseMoveEvent (QMouseEvent *e) { #if DEBUG_KP_DUAL_COLOR_BUTTON qCDebug(kpLogWidgets) << "kpDualColorButton::mouseMoveEvent() pos=" << e->pos () << " buttons=" << e->buttons () << " dragStartPoint=" << m_dragStartPoint << endl; #endif if (m_dragStartPoint == KP_INVALID_POINT) return; if (!(e->buttons () & Qt::LeftButton)) { m_dragStartPoint = KP_INVALID_POINT; return; } const int delay = QApplication::startDragDistance (); if (e->x () < m_dragStartPoint.x () - delay || e->x () > m_dragStartPoint.x () + delay || e->y () < m_dragStartPoint.y () - delay || e->y () > m_dragStartPoint.y () + delay) { #if DEBUG_KP_DUAL_COLOR_BUTTON qCDebug(kpLogWidgets) << "\tstarting drag as long as it's in a rectangle"; #endif kpColor color; if (foregroundRect ().contains (m_dragStartPoint)) color = foregroundColor (); else if (backgroundRect ().contains (m_dragStartPoint)) color = backgroundColor (); else { // "color" is left as invalid. } #if DEBUG_KP_DUAL_COLOR_BUTTON qCDebug(kpLogWidgets) << "\tcolor.isValid=" << color.isValid () << " rgb=" << (color.isValid () ? (int *) color.toQRgb () : 0) << endl; #endif if (color.isValid ()) { if (!color.isTransparent ()) KColorMimeData::createDrag (color.toQColor (), this)->exec (); } m_dragStartPoint = KP_INVALID_POINT; } }
// protected virtual [base QWidget] void kpDualColorButton::dragMoveEvent (QDragMoveEvent *e) { #if DEBUG_KP_DUAL_COLOR_BUTTON qCDebug(kpLogWidgets) << "kpDualColorButton::dragMoveEvent() canDecode=" << KColorMimeData::canDecode (e->mimeData ()) << endl; #endif e->setAccepted ( (foregroundRect ().contains (e->pos ()) || backgroundRect ().contains (e->pos ())) && KColorMimeData::canDecode (e->mimeData ())); }
void DualColorButton::dropEvent(QDropEvent *event) { const QColor color = qvariant_cast<QColor>(event->mimeData()->colorData()); if(foregroundRect().contains(event->pos())) { foreground_ = color; emit foregroundChanged(color); } else if(backgroundRect().contains(event->pos())) { background_ = color; emit backgroundChanged(color); } update(); }
void DualColorButton::mousePressEvent(QMouseEvent *event) { if(event->button() != Qt::LeftButton) return; dragSource_ = NODRAG; if(backgroundRect().contains(event->pos())) dragSource_ = BACKGROUND; if(foregroundRect().contains(event->pos())) dragSource_ = FOREGROUND; dragStart_ = event->pos(); }
// protected virtual [base QWidget] void kpDualColorButton::dropEvent (QDropEvent *e) { QColor col = KColorMimeData::fromMimeData (e->mimeData ()); #if DEBUG_KP_DUAL_COLOR_BUTTON qCDebug(kpLogWidgets) << "kpDualColorButton::dropEvent() col=" << (int *) col.rgba() << " (with alpha=" << (int *) col.rgba () << ")" << endl; #endif if (col.isValid ()) { if (foregroundRect ().contains (e->pos ())) setForegroundColor (kpColor (col.rgba())); else if (backgroundRect ().contains (e->pos ())) setBackgroundColor (kpColor (col.rgba())); } }
// protected virtual [base QWidget] void kpDualColorButton::mouseDoubleClickEvent (QMouseEvent *e) { int whichColor = -1; if (foregroundRect ().contains (e->pos ())) whichColor = 0; else if (backgroundRect ().contains (e->pos ())) whichColor = 1; if (whichColor == 0 || whichColor == 1) { QColorDialog dialog(this); dialog.setCurrentColor(color(whichColor).toQColor()); dialog.setOptions(QColorDialog::ShowAlphaChannel); if ( dialog.exec() == QDialog::Accepted ) setColor(whichColor, kpColor(dialog.currentColor().rgba())); } }
void DualColorButton::mouseReleaseEvent(QMouseEvent *event) { if(event->button() != Qt::LeftButton) return; QRectF fgr = foregroundRect(); QRectF bgr = backgroundRect(); if(fgr.contains(event->pos())) emit foregroundClicked(foreground_); else if(bgr.contains(event->pos())) emit backgroundClicked(background_); else if(event->pos().x() > fgr.right() && event->pos().y() < bgr.top()) swapColors(); else if(event->pos().x() < bgr.left() && event->pos().y() > fgr.bottom()) { foreground_ = Qt::black; background_ = Qt::white; emit foregroundChanged(foreground_); emit backgroundChanged(background_); update(); } }
void WhiteBoardColorChooser::mouseReleaseEvent(QMouseEvent *e) { // qDebug() << "epos:"<< e->pos(); // qDebug() << "foregroundrect" << foregroundRect(); // qDebug() << "backgroundrect" << backgroundRect(); // qDebug() << "swap" << swapPixmapRect(); ColorLayer whichColor = None; if (foregroundRect().contains(e->pos())) { whichColor = ForegroundColor; qDebug() << "> in foreground"; } else if (backgroundRect().contains(e->pos())) { whichColor = BackgroundColor; qDebug() << "> in background"; } else if (swapPixmapRect().contains(e->pos())) { qDebug() << "> in swap"; QColor oldFore = m_foregroundColor; m_foregroundColor = m_backgroundColor; m_backgroundColor = oldFore; emit colorsSwapped(m_foregroundColor, m_backgroundColor); update(); return; } if (whichColor == ForegroundColor || whichColor == BackgroundColor) { QColor col = color(whichColor); if (QColorDialog::getColor(col, this) == QColorDialog::Accepted) { setColor(whichColor, col); update(); } } }
// protected virtual [base QWidget] void kpDualColorButton::paintEvent (QPaintEvent *e) { #if DEBUG_KP_DUAL_COLOR_BUTTON && 1 qCDebug(kpLogWidgets) << "kpDualColorButton::draw() rect=" << rect () << " contentsRect=" << contentsRect () << endl; #endif // Draw frame first. QFrame::paintEvent (e); QPainter painter (this); // Fill with background. if (isEnabled ()) { kpView::drawTransparentBackground (&painter, contentsRect ().topLeft ()/*checkerboard top-left*/, contentsRect (), true/*preview*/); } else { // Use default widget background. } painter.translate (contentsRect ().x (), contentsRect ().y ()); // Draw "Swap Colours" button (top-right). QPixmap swapPixmap = UserIcon ("colorbutton_swap_16x16"); if (!isEnabled ()) { // Don't let the fill() touch the mask. QBitmap swapBitmapMask = swapPixmap.mask (); swapPixmap.setMask (QBitmap ()); // Grey out the opaque parts of "swapPixmap". swapPixmap.fill (palette ().color (QPalette::Dark)); swapPixmap.setMask (swapBitmapMask); } painter.drawPixmap (swapPixmapRect ().topLeft (), swapPixmap); // Draw background colour patch. QRect bgRect = backgroundRect (); QRect bgRectInside = QRect (bgRect.x () + 2, bgRect.y () + 2, bgRect.width () - 4, bgRect.height () - 4); if (isEnabled ()) { #if DEBUG_KP_DUAL_COLOR_BUTTON && 1 qCDebug(kpLogWidgets) << "\tbackgroundColor=" << (int *) m_color [1].toQRgb () << endl; #endif if (m_color [1].isTransparent ()) // only if fully transparent painter.drawPixmap (bgRectInside, UserIcon ("color_transparent_26x26")); else painter.fillRect (bgRectInside, m_color [1].toQColor ()); } else painter.fillRect (bgRectInside, palette().color (QPalette::Button)); qDrawShadePanel (&painter, bgRect, palette(), false/*not sunken*/, 2/*lineWidth*/, 0/*never fill*/); // Draw foreground colour patch. // Must be drawn after background patch since we're on top. QRect fgRect = foregroundRect (); QRect fgRectInside = QRect (fgRect.x () + 2, fgRect.y () + 2, fgRect.width () - 4, fgRect.height () - 4); if (isEnabled ()) { #if DEBUG_KP_DUAL_COLOR_BUTTON && 1 qCDebug(kpLogWidgets) << "\tforegroundColor=" << (int *) m_color [0].toQRgb () << endl; #endif if (m_color [0].isTransparent ()) // only if fully transparent painter.drawPixmap (fgRectInside, UserIcon ("color_transparent_26x26")); else painter.fillRect (fgRectInside, m_color [0].toQColor ()); } else painter.fillRect (fgRectInside, palette ().color (QPalette::Button)); qDrawShadePanel (&painter, fgRect, palette (), false/*not sunken*/, 2/*lineWidth*/, 0/*never fill*/); }