What's the point of checking the event type in mouseReleaseEvent()?
Drop event is a drop event and not mouse release event. Here is an extract from my QwwTwoColorIndicator:
void QwwTwoColorIndicator
::mouseMoveEvent(QMouseEvent *ev
) { Q_D(QwwTwoColorIndicator);
if (d->fgP || d->bgP) {
if (dragEnabled
() && (ev
->pos
() - d
->pressPos
).
manhattanLength() >
= QApplication::startDragDistance()) { QColor col
= d
->fgP ? fgColor
() : bgColor
();
ColorDrag *drag = new ColorDrag(this, col, col.name());
drag->exec();
d->fgP = d->bgP = false;
update();
}
} else if (testAttribute(Qt::WA_Hover)) update();
}
void QwwTwoColorIndicator::mouseMoveEvent(QMouseEvent *ev) {
Q_D(QwwTwoColorIndicator);
if (d->fgP || d->bgP) {
if (dragEnabled() && (ev->pos() - d->pressPos).manhattanLength() >= QApplication::startDragDistance()) {
QColor col = d->fgP ? fgColor() : bgColor();
ColorDrag *drag = new ColorDrag(this, col, col.name());
drag->exec();
d->fgP = d->bgP = false;
update();
}
} else if (testAttribute(Qt::WA_Hover)) update();
QWidget::mouseMoveEvent(ev);
}
To copy to clipboard, switch view to plain text mode
void QwwTwoColorIndicator
::mouseReleaseEvent(QMouseEvent *ev
) { Q_D(QwwTwoColorIndicator);
if (ev->button() == Qt::LeftButton) {
if (d->fgP && d->foregroundRect().contains(ev->pos())) {
emit fgClicked();
if (d->active) {
#if QT_VERSION >= 0x040500
#else
#endif
if (c.isValid()) {
setFgColor(c);
}
}
} else if (d->bgP && d->backgroundRect().contains(ev->pos())) {
emit bgClicked();
if (d->active) {
#if QT_VERSION >= 0x040500
#else
#endif
if (c.isValid()) {
setBgColor(c);
}
}
}
d->fgP = d->bgP = false;
update();
}
}
void QwwTwoColorIndicator::mouseReleaseEvent(QMouseEvent *ev) {
Q_D(QwwTwoColorIndicator);
if (ev->button() == Qt::LeftButton) {
if (d->fgP && d->foregroundRect().contains(ev->pos())) {
emit fgClicked();
if (d->active) {
#if QT_VERSION >= 0x040500
QColor c = QColorDialog::getColor(d->fg, this, tr("Choose foreground color"), QColorDialog::ShowAlphaChannel);
#else
QColor c = QColorDialog::getColor(d->fg, this);
#endif
if (c.isValid()) {
setFgColor(c);
}
}
} else if (d->bgP && d->backgroundRect().contains(ev->pos())) {
emit bgClicked();
if (d->active) {
#if QT_VERSION >= 0x040500
QColor c = QColorDialog::getColor(d->bg, this, tr("Choose background color"), QColorDialog::ShowAlphaChannel);
#else
QColor c = QColorDialog::getColor(d->bg, this);
#endif
if (c.isValid()) {
setBgColor(c);
}
}
}
d->fgP = d->bgP = false;
update();
}
}
To copy to clipboard, switch view to plain text mode
void QwwTwoColorIndicator
::dropEvent(QDropEvent *ev
) { Q_D(QwwTwoColorIndicator);
if (ev->mimeData()->hasColor())
col = qvariant_cast<QColor>(ev->mimeData()->colorData());
else
col.setNamedColor(ev->mimeData()->text());
if (d->foregroundRect().contains(ev->pos())) {
setFgColor(col);
} else if (d->backgroundRect().contains(ev->pos())) {
setBgColor(col);
}
ev->setDropAction(Qt::CopyAction);
}
void QwwTwoColorIndicator::dropEvent(QDropEvent *ev) {
Q_D(QwwTwoColorIndicator);
QColor col;
if (ev->mimeData()->hasColor())
col = qvariant_cast<QColor>(ev->mimeData()->colorData());
else
col.setNamedColor(ev->mimeData()->text());
if (d->foregroundRect().contains(ev->pos())) {
setFgColor(col);
} else if (d->backgroundRect().contains(ev->pos())) {
setBgColor(col);
}
ev->setDropAction(Qt::CopyAction);
}
To copy to clipboard, switch view to plain text mode
There is also dragEnterEvent and dragMoveEvent implemented.
Bookmarks