Commit 389c2bd5 authored by Wolthera van Hövell's avatar Wolthera van Hövell 🛍

Fork KColorButton, make it use internal selector dialog.

For now only in the fill layers.

Ref T2438
parent c71c0b69
......@@ -259,7 +259,7 @@ set(kritaui_LIB_SRCS
widgets/kis_visual_color_selector.cpp
widgets/KoDualColorButton.cpp
widgets/kis_color_input.cpp
widgets/kis_color_button.cpp
input/kis_input_manager.cpp
input/kis_input_manager_p.cpp
input/kis_extended_modifiers_mapper.cpp
......
/* This file is part of the KDE libraries
Copyright (C) 1997 Martin Jones (mjones@kde.org)
Copyright (C) 1999 Cristian Tibirna (ctibirna@kde.org)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kis_color_button.h"
#include <QtCore/QPointer>
#include <QPainter>
#include <qdrawutil.h>
#include <QApplication>
#include <QColorDialog>
#include <QClipboard>
#include <QMimeData>
#include <QDrag>
#include <QStyle>
#include <QMouseEvent>
#include <QStyleOptionButton>
#include <KoColor.h>
#include <dialogs/kis_internal_color_selector.h>
class KisColorButton::KisColorButtonPrivate
{
public:
KisColorButtonPrivate(KisColorButton *q);
void _k_chooseColor();
void _k_colorChosen();
KisColorButton *q;
KoColor m_defaultColor;
bool m_bdefaultColor : 1;
bool m_alphaChannel : 1;
KoColor col;
QPoint mPos;
QPointer<KisInternalColorSelector> dialogPtr;
void initStyleOption(QStyleOptionButton *opt) const;
};
/////////////////////////////////////////////////////////////////////
// Functions duplicated from KColorMimeData
// Should be kept in sync
void _k_populateMimeData(QMimeData *mimeData, const KoColor &color)
{
mimeData->setColorData(color.toQColor());
mimeData->setText(color.toQColor().name());
}
bool _k_canDecode(const QMimeData *mimeData)
{
if (mimeData->hasColor()) {
return true;
}
if (mimeData->hasText()) {
const QString colorName = mimeData->text();
if ((colorName.length() >= 4) && (colorName[0] == QLatin1Char('#'))) {
return true;
}
}
return false;
}
QColor _k_fromMimeData(const QMimeData *mimeData)
{
if (mimeData->hasColor()) {
return mimeData->colorData().value<QColor>();
}
if (_k_canDecode(mimeData)) {
return QColor(mimeData->text());
}
return QColor();
}
QDrag *_k_createDrag(const KoColor &color, QObject *dragsource)
{
QDrag *drag = new QDrag(dragsource);
QMimeData *mime = new QMimeData;
_k_populateMimeData(mime, color);
drag->setMimeData(mime);
QPixmap colorpix(25, 20);
colorpix.fill(color.toQColor());
QPainter p(&colorpix);
p.setPen(Qt::black);
p.drawRect(0, 0, 24, 19);
p.end();
drag->setPixmap(colorpix);
drag->setHotSpot(QPoint(-5, -7));
return drag;
}
/////////////////////////////////////////////////////////////////////
KisColorButton::KisColorButtonPrivate::KisColorButtonPrivate(KisColorButton *q)
: q(q)
{
m_bdefaultColor = false;
m_alphaChannel = false;
q->setAcceptDrops(true);
connect(q, SIGNAL(clicked()), q, SLOT(_k_chooseColor()));
}
KisColorButton::KisColorButton(QWidget *parent)
: QPushButton(parent)
, d(new KisColorButtonPrivate(this))
{
}
KisColorButton::KisColorButton(const KoColor &c, QWidget *parent)
: QPushButton(parent)
, d(new KisColorButtonPrivate(this))
{
d->col = c;
}
KisColorButton::KisColorButton(const KoColor &c, const KoColor &defaultColor, QWidget *parent)
: QPushButton(parent)
, d(new KisColorButtonPrivate(this))
{
d->col = c;
setDefaultColor(defaultColor);
}
KisColorButton::~KisColorButton()
{
delete d;
}
KoColor KisColorButton::color() const
{
return d->col;
}
void KisColorButton::setColor(const KoColor &c)
{
d->col = c;
update();
emit changed(d->col);
}
void KisColorButton::setAlphaChannelEnabled(bool alpha)
{
d->m_alphaChannel = alpha;
}
bool KisColorButton::isAlphaChannelEnabled() const
{
return d->m_alphaChannel;
}
KoColor KisColorButton::defaultColor() const
{
return d->m_defaultColor;
}
void KisColorButton::setDefaultColor(const KoColor &c)
{
d->m_bdefaultColor = true;
d->m_defaultColor = c;
}
void KisColorButton::KisColorButtonPrivate::initStyleOption(QStyleOptionButton *opt) const
{
opt->initFrom(q);
opt->state |= q->isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
opt->features = QStyleOptionButton::None;
if (q->isDefault()) {
opt->features |= QStyleOptionButton::DefaultButton;
}
opt->text.clear();
opt->icon = QIcon();
}
void KisColorButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QStyle *style = QWidget::style();
//First, we need to draw the bevel.
QStyleOptionButton butOpt;
d->initStyleOption(&butOpt);
style->drawControl(QStyle::CE_PushButtonBevel, &butOpt, &painter, this);
//OK, now we can muck around with drawing out pretty little color box
//First, sort out where it goes
QRect labelRect = style->subElementRect(QStyle::SE_PushButtonContents,
&butOpt, this);
int shift = style->pixelMetric(QStyle::PM_ButtonMargin, &butOpt, this) / 2;
labelRect.adjust(shift, shift, -shift, -shift);
int x, y, w, h;
labelRect.getRect(&x, &y, &w, &h);
if (isChecked() || isDown()) {
x += style->pixelMetric(QStyle::PM_ButtonShiftHorizontal, &butOpt, this);
y += style->pixelMetric(QStyle::PM_ButtonShiftVertical, &butOpt, this);
}
QColor fillCol = isEnabled() ? d->col.toQColor() : palette().color(backgroundRole());
qDrawShadePanel(&painter, x, y, w, h, palette(), true, 1, NULL);
if (fillCol.isValid()) {
const QRect rect(x + 1, y + 1, w - 2, h - 2);
if (fillCol.alpha() < 255) {
QPixmap chessboardPattern(16, 16);
QPainter patternPainter(&chessboardPattern);
patternPainter.fillRect(0, 0, 8, 8, Qt::black);
patternPainter.fillRect(8, 8, 8, 8, Qt::black);
patternPainter.fillRect(0, 8, 8, 8, Qt::white);
patternPainter.fillRect(8, 0, 8, 8, Qt::white);
patternPainter.end();
painter.fillRect(rect, QBrush(chessboardPattern));
}
painter.fillRect(rect, fillCol);
}
if (hasFocus()) {
QRect focusRect = style->subElementRect(QStyle::SE_PushButtonFocusRect, &butOpt, this);
QStyleOptionFocusRect focusOpt;
focusOpt.init(this);
focusOpt.rect = focusRect;
focusOpt.backgroundColor = palette().background().color();
style->drawPrimitive(QStyle::PE_FrameFocusRect, &focusOpt, &painter, this);
}
}
QSize KisColorButton::sizeHint() const
{
QStyleOptionButton opt;
d->initStyleOption(&opt);
return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(40, 15), this).
expandedTo(QApplication::globalStrut());
}
QSize KisColorButton::minimumSizeHint() const
{
QStyleOptionButton opt;
d->initStyleOption(&opt);
return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(3, 3), this).
expandedTo(QApplication::globalStrut());
}
void KisColorButton::dragEnterEvent(QDragEnterEvent *event)
{
event->setAccepted(_k_canDecode(event->mimeData()) && isEnabled());
}
void KisColorButton::dropEvent(QDropEvent *event)
{
QColor c = _k_fromMimeData(event->mimeData());
if (c.isValid()) {
KoColor col;
col.fromQColor(c);
setColor(col);
}
}
void KisColorButton::keyPressEvent(QKeyEvent *e)
{
int key = e->key() | e->modifiers();
if (QKeySequence::keyBindings(QKeySequence::Copy).contains(key)) {
QMimeData *mime = new QMimeData;
_k_populateMimeData(mime, color());
QApplication::clipboard()->setMimeData(mime, QClipboard::Clipboard);
} else if (QKeySequence::keyBindings(QKeySequence::Paste).contains(key)) {
QColor color = _k_fromMimeData(QApplication::clipboard()->mimeData(QClipboard::Clipboard));
KoColor col;
col.fromQColor(color);
setColor(col);
} else {
QPushButton::keyPressEvent(e);
}
}
void KisColorButton::mousePressEvent(QMouseEvent *e)
{
d->mPos = e->pos();
QPushButton::mousePressEvent(e);
}
void KisColorButton::mouseMoveEvent(QMouseEvent *e)
{
if ((e->buttons() & Qt::LeftButton) &&
(e->pos() - d->mPos).manhattanLength() > QApplication::startDragDistance()) {
_k_createDrag(color(), this)->start();
setDown(false);
}
}
void KisColorButton::KisColorButtonPrivate::_k_chooseColor()
{
KisInternalColorSelector *dialog = dialogPtr.data();
if (dialog) {
dialog->show();
dialog->raise();
dialog->activateWindow();
return;
}
KisInternalColorSelector::Config cfg;
dialog = new KisInternalColorSelector(q,
q->color(),
cfg,
"Blabla");
//dialog->setOption(QColorDialog::ShowAlphaChannel, m_alphaChannel);
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog, SIGNAL(accepted()), q, SLOT(_k_colorChosen()));
dialogPtr = dialog;
dialog->show();
}
void KisColorButton::KisColorButtonPrivate::_k_colorChosen()
{
KisInternalColorSelector *dialog = dialogPtr.data();
if (!dialog) {
return;
}
q->setColor(dialog->getCurrentColor());
}
#include "moc_kis_color_button.cpp"
/*
* Copyright (C) Wolthera van Hovell tot Westerflier <griffinvalley@gmail.com>, (C) 2016
* This file is forked from the KF5 KColorButton
Copyright (C) 1997 Martin Jones (mjones@kde.org)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef KisColorButton_H
#define KisColorButton_H
#include <kritaui_export.h>
#include <KoColor.h>
#include <QPushButton>
class KisColorButtonPrivate;
/**
* @short A pushbutton to display or allow user selection of a color.
*
* This widget can be used to display or allow user selection of a color.
*
* @see QColorDialog
*
* \image html KisColorButton.png "KDE Color Button"
*/
class KRITAUI_EXPORT KisColorButton : public QPushButton
{
Q_OBJECT
Q_PROPERTY(KoColor color READ color WRITE setColor NOTIFY changed USER true)
Q_PROPERTY(KoColor defaultColor READ defaultColor WRITE setDefaultColor)
Q_PROPERTY(bool alphaChannelEnabled READ isAlphaChannelEnabled WRITE setAlphaChannelEnabled)
public:
/**
* Creates a color button.
*/
explicit KisColorButton(QWidget *parent = 0);
/**
* Creates a color button with an initial color @p c.
*/
explicit KisColorButton(const KoColor &c, QWidget *parent = 0);
/**
* Creates a color button with an initial color @p c and default color @p defaultColor.
*/
KisColorButton(const KoColor &c, const KoColor &defaultColor, QWidget *parent = 0);
virtual ~KisColorButton();
/**
* Returns the currently chosen color.
*/
KoColor color() const;
/**
* Sets the current color to @p c.
*/
void setColor(const KoColor &c);
/**
* When set to true, allow the user to change the alpha component
* of the color. The default value is false.
* @since 4.5
*/
void setAlphaChannelEnabled(bool alpha);
/**
* Returns true if the user is allowed to change the alpha component.
* @since 4.5
*/
bool isAlphaChannelEnabled() const;
/**
* Returns the default color or an invalid color
* if no default color is set.
*/
KoColor defaultColor() const;
/**
* Sets the default color to @p c.
*/
void setDefaultColor(const KoColor &c);
QSize sizeHint() const Q_DECL_OVERRIDE;
QSize minimumSizeHint() const Q_DECL_OVERRIDE;
Q_SIGNALS:
/**
* Emitted when the color of the widget
* is changed, either with setColor() or via user selection.
*/
void changed(const KoColor &newColor);
protected:
void paintEvent(QPaintEvent *pe) Q_DECL_OVERRIDE;
void dragEnterEvent(QDragEnterEvent *) Q_DECL_OVERRIDE;
void dropEvent(QDropEvent *) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
void mouseMoveEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
void keyPressEvent(QKeyEvent *e) Q_DECL_OVERRIDE;
private:
class KisColorButtonPrivate;
KisColorButtonPrivate *const d;
Q_PRIVATE_SLOT(d, void _k_chooseColor())
Q_PRIVATE_SLOT(d, void _k_colorChosen())
};
#endif
......@@ -100,4 +100,4 @@ void KisColorGenerator::generate(KisProcessingInformation dstInfo,
}
}
#include "colorgenerator.moc"
\ No newline at end of file
#include "colorgenerator.moc"
......@@ -27,11 +27,12 @@
#include "ui_wdgcoloroptions.h"
KisWdgColor::KisWdgColor(QWidget* parent)
KisWdgColor::KisWdgColor(QWidget* parent, const KoColorSpace *cs)
: KisConfigWidget(parent)
{
m_widget = new Ui_WdgColorOptions();
m_widget->setupUi(this);
m_cs = cs;
}
KisWdgColor::~KisWdgColor()
......@@ -43,14 +44,16 @@ KisWdgColor::~KisWdgColor()
void KisWdgColor::setConfiguration(const KisPropertiesConfiguration* config)
{
QVariant value;
widget()->bnColor->setColor(config->getColor("color").toQColor());
KoColor c =config->getColor("color");
c.convertTo(m_cs);
widget()->bnColor->setColor(c);
}
KisPropertiesConfiguration* KisWdgColor::configuration() const
{
KisFilterConfiguration* config = new KisFilterConfiguration("color", 1);
KoColor c;
c.fromQColor(this->widget()->bnColor->color());
c.fromKoColor(this->widget()->bnColor->color());
QVariant v;
v.setValue(c);
config->setProperty("color", v);
......
......@@ -22,6 +22,8 @@
#define KIS_WDG_COLOR_H
#include <kis_config_widget.h>
#include <KoColorSpace.h>
#include <KoColorSpaceRegistry.h>
class Ui_WdgColorOptions;
......@@ -29,7 +31,7 @@ class KisWdgColor : public KisConfigWidget
{
Q_OBJECT
public:
KisWdgColor(QWidget* parent = 0);
KisWdgColor(QWidget* parent = 0, const KoColorSpace *cs = KoColorSpaceRegistry::instance()->rgb8());
~KisWdgColor();
public:
inline const Ui_WdgColorOptions* widget() const {
......@@ -39,6 +41,7 @@ public:
virtual KisPropertiesConfiguration* configuration() const;
private:
Ui_WdgColorOptions* m_widget;
const KoColorSpace *m_cs;
};
#endif
......
......@@ -25,7 +25,7 @@
</widget>
</item>
<item row="0" column="2">
<widget class="KColorButton" name="bnColor"/>
<widget class="KisColorButton" name="bnColor"/>
</item>
<item row="0" column="3">
<spacer>
......@@ -63,9 +63,9 @@
</widget>
<customwidgets>
<customwidget>
<class>KColorButton</class>
<class>KisColorButton</class>
<extends>QPushButton</extends>
<header>kcolorbutton.h</header>
<header>kis_color_button.h</header>
</customwidget>
</customwidgets>
<resources/>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment