Skip to content

Commit

Permalink
Add Color Picker button to Palette Editor
Browse files Browse the repository at this point in the history
Request #123
  • Loading branch information
Leonx254 committed Jul 20, 2024
1 parent 043ea23 commit e2c9219
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
24 changes: 24 additions & 0 deletions RetroEDv2/icons/ic_colorpick_48px.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions RetroEDv2/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@
<file>resources/splash.svg</file>
<file>icons/ic_stamp_selection_48px.svg</file>
<file>resources/RSDKv5VarNames.txt</file>
<file>icons/ic_colorpick_48px.svg</file>
</qresource>
</RCC>
78 changes: 78 additions & 0 deletions RetroEDv2/tools/paletteeditor/colourdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ RSDKColorDialog::RSDKColorDialog(PaletteColor color, QWidget *parent)
ui->htmlInput->setModified(false);
setColor(color);
});

connect(ui->colorPick, &QToolButton::pressed, this, &RSDKColorDialog::pickScreenColor);

setColor(m_color.toQColor());
}

Expand All @@ -52,6 +55,72 @@ void RSDKColorDialog::setRGB()
setColor(col);
}

bool RSDKColorDialog::mouseMoveColorEvent(QMouseEvent *e)
{
setColor(grabScreenColor(e->globalPos()));
//PrintLog(QString("Move %1 %2").arg(e->globalPos().x()).arg(e->globalPos().y()));
return true;
}

bool RSDKColorDialog::mouseReleaseColorEvent(QMouseEvent *e)
{
setColor(grabScreenColor(e->globalPos()));
ui->colorPick->setDown(false);

releaseMouse();
releaseKeyboard();
colorPickMode = false;
setMouseTracking(false);
return true;
}

bool RSDKColorDialog::keyPressColorEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Escape) {
setColor(PrevCol.toQColor());
releaseMouse();
releaseKeyboard();
colorPickMode = false;
setMouseTracking(false);
ui->colorPick->setDown(false);
return true;
}
return false;
}

bool RSDKColorDialog::event(QEvent *event)
{
if (colorPickMode){
switch ((int)event->type()) {
default: break;
case QEvent::MouseMove:
return mouseMoveColorEvent(static_cast<QMouseEvent *>(event));
case QEvent::MouseButtonRelease:
return mouseReleaseColorEvent(static_cast<QMouseEvent *>(event));
case QEvent::KeyPress:
return keyPressColorEvent(static_cast<QKeyEvent *>(event));
}
}
return QWidget::event(event);
}

void RSDKColorDialog::pickScreenColor()
{
PrevCol = m_color;

#ifndef QT_NO_CURSOR
grabMouse(Qt::CrossCursor);
#else
grabMouse();
#endif

grabKeyboard();
setMouseTracking(true);
colorPickMode = true;
const QPoint globalPos = QCursor::pos();
setColor(grabScreenColor(globalPos));
}

void RSDKColorDialog::setColor(QColor col)
{

Expand Down Expand Up @@ -123,4 +192,13 @@ PaletteColor RSDKColorDialog::getColor(PaletteColor color, bool *ok, QWidget *pa
return clr;
}

QColor RSDKColorDialog::grabScreenColor(const QPoint &p)
{
const QDesktopWidget *desktop = QApplication::desktop();
const QPixmap pixmap = QGuiApplication::primaryScreen()->grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
QImage i = pixmap.toImage();
return QColor(i.pixel(0, 0));
}


#include "moc_colourdialog.cpp"
10 changes: 10 additions & 0 deletions RetroEDv2/tools/paletteeditor/colourdialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@ class RSDKColorDialog : public QDialog
static PaletteColor getColor(PaletteColor color, bool *ok, QWidget *parent = nullptr);

inline PaletteColor color() { return m_color; }
QColor grabScreenColor(const QPoint &p);

protected:
bool event(QEvent *event);
bool mouseMoveColorEvent(QMouseEvent *e);
bool mouseReleaseColorEvent(QMouseEvent *e);
bool keyPressColorEvent(QKeyEvent *e);

private slots:
void setColor(QColor col);
void setHsv();
void setRGB();
void pickScreenColor();

private:
Ui::RSDKColorDialog *ui;
PaletteColor m_color;
PaletteColor PrevCol;
bool colorPickMode = false;
};


23 changes: 20 additions & 3 deletions RetroEDv2/tools/paletteeditor/colourdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@
<property name="horizontalSpacing">
<number>6</number>
</property>
<item row="7" column="1" colspan="2">
<widget class="color_widgets::ColorLineEdit" name="htmlInput"/>
</item>
<item row="0" column="2">
<widget class="QSpinBox" name="hSpin">
<property name="maximum">
Expand Down Expand Up @@ -383,6 +380,26 @@
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="color_widgets::ColorLineEdit" name="htmlInput"/>
</item>
<item row="7" column="2">
<widget class="QToolButton" name="colorPick">
<property name="toolTip">
<string>Color picker (Press and Hold left click)</string>
</property>
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../resources.qrc">
<normaloff>:/icons/ic_colorpick_48px.svg</normaloff>:/icons/ic_colorpick_48px.svg</iconset>
</property>
<property name="checkable">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
Expand Down

0 comments on commit e2c9219

Please sign in to comment.