From df782571f915278212757d8932e20acc81bca5f9 Mon Sep 17 00:00:00 2001 From: ArsenArsen Date: Tue, 16 May 2017 15:52:15 +0200 Subject: [PATCH] Color picker initial commit --- KShare.pro | 6 ++-- colorpicker/colorpickerscene.cpp | 51 ++++++++++++++++++++++++++++++++ colorpicker/colorpickerscene.hpp | 36 ++++++++++++++++++++++ main.cpp | 2 ++ mainwindow.cpp | 10 ++++++- mainwindow.hpp | 7 ++--- mainwindow.ui | 12 ++++++++ screenshotter.cpp | 5 +++- 8 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 colorpicker/colorpickerscene.cpp create mode 100644 colorpicker/colorpickerscene.hpp diff --git a/KShare.pro b/KShare.pro index e5e7388..8e90da5 100644 --- a/KShare.pro +++ b/KShare.pro @@ -45,7 +45,8 @@ SOURCES += main.cpp\ cropeditor/settings/blurdialog.cpp \ cropeditor/drawing/pathitem.cpp \ cropeditor/drawing/lineitem.cpp \ - cropeditor/drawing/textitem.cpp + cropeditor/drawing/textitem.cpp \ + colorpicker/colorpickerscene.cpp HEADERS += mainwindow.hpp \ cropeditor/cropeditor.hpp \ @@ -70,7 +71,8 @@ HEADERS += mainwindow.hpp \ cropeditor/settings/blurdialog.hpp \ cropeditor/drawing/pathitem.hpp \ cropeditor/drawing/lineitem.hpp \ - cropeditor/drawing/textitem.hpp + cropeditor/drawing/textitem.hpp \ + colorpicker/colorpickerscene.hpp FORMS += mainwindow.ui \ cropeditor/settings/brushpenselection.ui \ diff --git a/colorpicker/colorpickerscene.cpp b/colorpicker/colorpickerscene.cpp new file mode 100644 index 0000000..4e53dfa --- /dev/null +++ b/colorpicker/colorpickerscene.cpp @@ -0,0 +1,51 @@ +#include "colorpickerscene.hpp" +#include +#include +#include +#include +#include +#include + +ColorPickerScene::ColorPickerScene(QPixmap *pixmap, QWidget *parentWidget) +: QGraphicsScene(), QGraphicsView(this, parentWidget) { + setFrameShape(QFrame::NoFrame); // Time taken to solve: A george99g and 38 minutes. + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint); + setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing); + setCursor(QCursor(Qt::CrossCursor)); + setMouseTracking(true); + + pItem = addPixmap(*pixmap); + 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); +} + +void ColorPickerScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + ellipse->setRect(QRectF(event->scenePos(), QSize(20, 20))); + color = pItem->pixmap().toImage().pixelColor(event->scenePos().toPoint()); + text->setPos(QCursor::pos() + QPoint(25, 0)); + text->setPlainText(color.name()); + textBackground->setPos(text->pos()); + ellipse->setBrush(color); +} + +void ColorPickerScene::keyPressEvent(QKeyEvent *event) { + if (event->key() == Qt::Key_Return) QApplication::clipboard()->setText(color.name()); + if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Escape) close(); +} + +void ColorPickerScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *) { + QApplication::clipboard()->setText(color.name()); + close(); +} diff --git a/colorpicker/colorpickerscene.hpp b/colorpicker/colorpickerscene.hpp new file mode 100644 index 0000000..a536fbf --- /dev/null +++ b/colorpicker/colorpickerscene.hpp @@ -0,0 +1,36 @@ +#ifndef COLORPICKERSCENE_HPP +#define COLORPICKERSCENE_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +class ColorPickerScene : public QGraphicsScene, public QGraphicsView { + public: + ColorPickerScene(QPixmap *pixmap, QWidget *parentWidget); + void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; + void keyPressEvent(QKeyEvent *event) override; + void mouseReleaseEvent(QGraphicsSceneMouseEvent *) override; + static void showPicker() { + ColorPickerScene *s = new ColorPickerScene(screenshotutil::fullscreen(), 0); + QTimer::singleShot(0, [s] { + s->showFullScreen(); + QScopedPointer(s); + // Before anyone asks I have 0 clue about how does this not segfault + }); + } + + private: + QColor color; + QGraphicsEllipseItem *ellipse = 0; + QGraphicsPixmapItem *pItem = 0; + QGraphicsTextItem *text; + QGraphicsRectItem *textBackground; +}; + +#endif // COLORPICKERSCENE_HPP diff --git a/main.cpp b/main.cpp index 411791a..5134256 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include bool verbose = false; diff --git a/mainwindow.cpp b/mainwindow.cpp index 041316e..fdc5155 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -41,11 +42,13 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi QAction *shtoggle = new QAction("Show/Hide", this); QAction *fullscreen = new QAction("Take fullscreen shot", this); QAction *area = new QAction("Take area shot", this); - menu->addActions({ quit, shtoggle }); + QAction *picker = new QAction("Show color picker", this); + menu->addActions({ quit, shtoggle, picker }); menu->addSeparator(); menu->addActions({ fullscreen, area }); connect(quit, &QAction::triggered, this, &MainWindow::quit); connect(shtoggle, &QAction::triggered, this, &MainWindow::toggleVisible); + connect(shtoggle, &QAction::triggered, [] { ColorPickerScene::showPicker(); }); connect(tray, &QSystemTrayIcon::messageClicked, this, &MainWindow::toggleVisible); connect(tray, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) { if (reason == QSystemTrayIcon::DoubleClick) toggleVisible(); @@ -83,6 +86,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi addHotkeyItem("Fullscreen image", "fullscreen", new std::function([] { screenshotter::fullscreen(); })); addHotkeyItem("Area image", "area", new std::function([] { screenshotter::area(); })); + addHotkeyItem("Color picker", "picker", new std::function([] { ColorPickerScene::showPicker(); })); ui->quickMode->setChecked(settings::settings().value("quickMode", false).toBool()); ui->hideToTray->setChecked(settings::settings().value("hideOnClose", true).toBool()); @@ -181,3 +185,7 @@ void MainWindow::on_quickMode_clicked(bool checked) { void MainWindow::on_hideToTray_clicked(bool checked) { settings::settings().setValue("hideOnClose", checked); } + +void MainWindow::on_actionColor_Picker_triggered() { + ColorPickerScene::showPicker(); +} diff --git a/mainwindow.hpp b/mainwindow.hpp index cf36673..e729ea5 100644 --- a/mainwindow.hpp +++ b/mainwindow.hpp @@ -25,17 +25,14 @@ class MainWindow : public QMainWindow { void on_actionArea_triggered(); void on_uploaderList_clicked(const QModelIndex &); void on_nameScheme_textEdited(const QString &arg1); - void on_delay_valueChanged(double arg1); - void on_hotkeys_doubleClicked(const QModelIndex &index); - void on_settingsButton_clicked(); - void on_quickMode_clicked(bool checked); - void on_hideToTray_clicked(bool checked); + void on_actionColor_Picker_triggered(); + public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); diff --git a/mainwindow.ui b/mainwindow.ui index 205b709..4fcc7b6 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -136,8 +136,15 @@ + + + &Utilities + + + + @@ -155,6 +162,11 @@ &Area + + + Color Picker + + diff --git a/screenshotter.cpp b/screenshotter.cpp index ef0cddc..8cea38b 100644 --- a/screenshotter.cpp +++ b/screenshotter.cpp @@ -8,7 +8,10 @@ void screenshotter::area() { CropEditor *editor = new CropEditor(screenshotutil::fullscreen()); - QObject::connect(editor, &CropEditor::cropped, [&](QPixmap *pixmap) { UploaderSingleton::inst().upload(pixmap); }); + QObject::connect(editor, &CropEditor::cropped, [&](QPixmap *pixmap) { + UploaderSingleton::inst().upload(pixmap); + QScopedPointer(editor); + }); } void screenshotter::fullscreen() {