void ColorPicker::updatePosition() { QPoint pos = QCursor::pos(); QScreen *screen = findScreenAt(pos); if (!screen) { qWarning() << "Could not find a screen containing" << pos; return; } QRect desktopRect = screen->geometry(); QPoint newPos; if (pos.x() + GRAB_SIZE + width() < desktopRect.width()) { newPos.setX(pos.x() + GRAB_SIZE); } else { newPos.setX(pos.x() - GRAB_SIZE - width()); } if (pos.y() + GRAB_SIZE + height() < desktopRect.height()) { newPos.setY(pos.y() + GRAB_SIZE); } else { newPos.setY(pos.y() - GRAB_SIZE - height()); } move(newPos); WId wid = QApplication::desktop()->winId(); mPixmap = screen->grabWindow(wid, pos.x() - GRAB_SIZE / 2, pos.y() - GRAB_SIZE / 2, GRAB_SIZE, GRAB_SIZE); update(); }
QColor QgsColorButton::sampleColor( QPoint point ) const { QScreen *screen = findScreenAt( point ); if ( ! screen ) { return QColor(); } QPixmap snappedPixmap = screen->grabWindow( QApplication::desktop()->winId(), point.x(), point.y(), 1, 1 ); QImage snappedImage = snappedPixmap.toImage(); return snappedImage.pixel( 0, 0 ); }