2017-05-16 15:52:15 +02:00
|
|
|
#include "colorpickerscene.hpp"
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QClipboard>
|
2017-08-05 21:20:24 +02:00
|
|
|
#include <QDebug>
|
2017-05-16 15:52:15 +02:00
|
|
|
#include <QGraphicsEllipseItem>
|
|
|
|
#include <QGraphicsPixmapItem>
|
|
|
|
#include <QGraphicsTextItem>
|
|
|
|
#include <QTimer>
|
2017-07-10 16:27:38 +02:00
|
|
|
#include <settings.hpp>
|
2017-05-16 15:52:15 +02:00
|
|
|
|
2017-07-02 20:51:15 +02:00
|
|
|
ColorPickerScene::ColorPickerScene(QPixmap pixmap, QWidget *parentWidget)
|
2017-05-24 16:51:14 +02:00
|
|
|
: QGraphicsScene(), QGraphicsView(this, parentWidget) {
|
2017-05-16 15:52:15 +02:00
|
|
|
setFrameShape(QFrame::NoFrame); // Time taken to solve: A george99g and 38 minutes.
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2017-08-28 13:01:58 +02:00
|
|
|
setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Dialog);
|
2017-05-16 15:52:15 +02:00
|
|
|
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing);
|
|
|
|
setCursor(QCursor(Qt::CrossCursor));
|
|
|
|
setMouseTracking(true);
|
2017-07-29 17:22:17 +02:00
|
|
|
setWindowTitle(tr("KShare Color Picker"));
|
2017-07-03 12:07:49 +02:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2017-05-16 15:52:15 +02:00
|
|
|
|
2017-07-02 20:51:15 +02:00
|
|
|
pItem = addPixmap(pixmap);
|
2017-05-16 15:52:15 +02:00
|
|
|
pItem->setZValue(-2);
|
|
|
|
ellipse = addEllipse(QRectF(QCursor::pos(), QSize(20, 20)), QPen(Qt::cyan), Qt::NoBrush);
|
|
|
|
QFont font("Monospace");
|
|
|
|
font.setStyleHint(QFont::Monospace);
|
|
|
|
text = addText("#hiyouu", font);
|
|
|
|
textBackground = addRect(text->boundingRect(), Qt::NoPen, QBrush(Qt::black));
|
|
|
|
text->setPos(QCursor::pos() + QPoint(25, 0));
|
|
|
|
textBackground->setPos(text->pos());
|
|
|
|
textBackground->setZValue(-1);
|
|
|
|
color = pItem->pixmap().toImage().pixelColor(QCursor::pos());
|
|
|
|
text->setPlainText(color.name());
|
|
|
|
ellipse->setBrush(color);
|
2017-07-03 15:43:28 +02:00
|
|
|
image = pixmap.toImage();
|
2017-07-03 12:07:49 +02:00
|
|
|
|
2017-08-03 22:00:15 +02:00
|
|
|
if (QApplication::screens().size() == 1)
|
|
|
|
showFullScreen();
|
|
|
|
else
|
|
|
|
show();
|
|
|
|
|
2017-07-10 16:27:38 +02:00
|
|
|
activateWindow();
|
|
|
|
setGeometry(pixmap.rect());
|
|
|
|
QPoint p = screenshotutil::smallestScreenCoordinate()
|
|
|
|
+ QPoint(settings::settings().value("cropx", 0).toInt(), settings::settings().value("cropy", 0).toInt());
|
|
|
|
move(p.x(), p.y());
|
2017-05-16 15:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPickerScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
|
2017-07-03 15:43:28 +02:00
|
|
|
color = image.pixelColor(event->scenePos().toPoint());
|
2017-05-16 15:52:15 +02:00
|
|
|
text->setPlainText(color.name());
|
|
|
|
ellipse->setBrush(color);
|
2017-05-16 16:22:32 +02:00
|
|
|
|
|
|
|
qreal bottom = rect().bottom(); // max y
|
|
|
|
qreal right = rect().right(); // max x
|
|
|
|
qreal width = text->boundingRect().width();
|
|
|
|
qreal height = text->boundingRect().height();
|
|
|
|
|
|
|
|
QPointF origPoint = event->scenePos() + QPoint(25, 0);
|
|
|
|
QPointF scopePoint = event->scenePos();
|
|
|
|
QPointF resPoint = origPoint;
|
|
|
|
if (origPoint.x() + width > right) {
|
|
|
|
scopePoint -= QPoint(20, 0);
|
|
|
|
resPoint -= QPoint(50 + width, 0);
|
|
|
|
}
|
|
|
|
if (origPoint.y() + height > bottom) {
|
|
|
|
scopePoint -= QPoint(0, 20);
|
|
|
|
resPoint -= QPoint(0, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
ellipse->setRect(QRectF(scopePoint, QSize(20, 20)));
|
|
|
|
text->setPos(resPoint);
|
|
|
|
textBackground->setPos(text->pos());
|
2017-05-16 15:52:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPickerScene::keyPressEvent(QKeyEvent *event) {
|
2017-08-28 13:01:58 +02:00
|
|
|
if (event->key() == Qt::Key_Return) {
|
|
|
|
QApplication::clipboard()->setText(color.name());
|
|
|
|
qInfo().noquote() << tr("Copied hex code to clipboard.");
|
|
|
|
}
|
2017-05-16 15:52:15 +02:00
|
|
|
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Escape) close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPickerScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *) {
|
|
|
|
QApplication::clipboard()->setText(color.name());
|
|
|
|
close();
|
2017-08-05 21:20:24 +02:00
|
|
|
qInfo().noquote() << tr("Copied hex code to clipboard.");
|
2017-05-16 15:52:15 +02:00
|
|
|
}
|