/*
	QAD - Qt ADvanced

	Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program 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 Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef COLORBUTTON_H
#define COLORBUTTON_H

#include <QPushButton>
#include <QFrame>
#include <QColorDialog>
#include <QMouseEvent>
#include <QAction>
#include <QMenu>
#include <QClipboard>
#include <QApplication>
#include "qad_export.h"


class QAD_EXPORT ColorButton: public QPushButton
{
	Q_OBJECT
	Q_PROPERTY(QColor color READ color WRITE setColor)
	Q_PROPERTY(bool useNativeDialog READ useNativeDialog WRITE setUseNativeDialog)
	Q_PROPERTY(bool useAlphaChannel READ useAlphaChannel WRITE setUseAlphaChannel)
	Q_PROPERTY(bool frameOnly READ frameOnly WRITE setFrameOnly)

public:
	explicit ColorButton(QWidget * parent = 0);
	~ColorButton();

	QColor color() const {return pal.color(label->backgroundRole());}
	bool useNativeDialog() const {return !options.testFlag(QColorDialog::DontUseNativeDialog);}
	bool useAlphaChannel() const {return options.testFlag(QColorDialog::ShowAlphaChannel);}
	bool frameOnly() const {return frame;}

public slots:
	void setColor(const QColor & col);
	void setUseNativeDialog(bool yes) {if (yes) options &= ~QColorDialog::DontUseNativeDialog; else options |= QColorDialog::DontUseNativeDialog;}
	void setUseAlphaChannel(bool yes) {if (yes) options |= QColorDialog::ShowAlphaChannel; else options &= ~QColorDialog::ShowAlphaChannel;}
	void setFrameOnly(bool yes) {frame = yes; setFlat(frame); resizeEvent(0);}

private:
	void mousePressEvent(QMouseEvent * e);
	void mouseMoveEvent(QMouseEvent * e);
	void resizeEvent(QResizeEvent * );
	void dragEnterEvent(QDragEnterEvent * e);
	void dropEvent(QDropEvent * e);
	void changeEvent(QEvent *e);

	QFrame * label;
	QWidget * back;
	QAction * a_copy, * a_paste, * a_mix;
	QPalette pal;
	QPoint pp;
	QMenu menu;
	QColorDialog::ColorDialogOptions options;
	bool frame;

private slots:
	void clicked();
	void copy() {QApplication::clipboard()->setText(color().name());}
	void paste() {QColor c(QApplication::clipboard()->text()); if (c.isValid()) setColor(c);}
	void mix();

signals:
	void colorChanged(QColor);

};

#endif // COLORBUTTON_H
