Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/pickrgbutils.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/screenboard.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/menubarcommand.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tools/RGBpicker.h"
Toshihiro Shimizu 890ddd
#include "tools/cursors.h"
Toshihiro Shimizu 890ddd
#include "tools/cursormanager.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qmouseevent></qmouseevent>
Toshihiro Shimizu 890ddd
#include <qpaintevent></qpaintevent>
Toshihiro Shimizu 890ddd
#include <qpainter></qpainter>
Toshihiro Shimizu 890ddd
#include <qcoreapplication></qcoreapplication>
Toshihiro Shimizu 890ddd
#include <qcursor></qcursor>
Toshihiro Shimizu 890ddd
#include <qtimer></qtimer>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tools/screenpicker.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************
Toshihiro Shimizu 890ddd
//    Screen Picker implementation
Toshihiro Shimizu 890ddd
//***********************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ScreenPicker::ScreenPicker(QWidget *parent)
Toshihiro Shimizu 890ddd
	: m_mousePressed(false), m_mouseGrabbed(false)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool ScreenPicker::acceptScreenEvents(const QRect &screenRect) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return screenRect.contains(QCursor::pos()) || //Accept mouse events
Toshihiro Shimizu 890ddd
		   screenRect.intersects(m_geometry);	 //Accept paint events
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ScreenPicker::event(QWidget *widget, QEvent *e)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	switch (e->type()) {
Toshihiro Shimizu 890ddd
	case QEvent::MouseButtonPress:
Toshihiro Shimizu 890ddd
		mousePressEvent(widget, static_cast<qmouseevent *="">(e));</qmouseevent>
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	case QEvent::MouseMove:
Toshihiro Shimizu 890ddd
		mouseMoveEvent(widget, static_cast<qmouseevent *="">(e));</qmouseevent>
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	case QEvent::MouseButtonRelease:
Toshihiro Shimizu 890ddd
		mouseReleaseEvent(widget, static_cast<qmouseevent *="">(e));</qmouseevent>
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ScreenPicker::mousePressEvent(QWidget *widget, QMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_mousePressed = true;
Toshihiro Shimizu 890ddd
	m_start = widget->mapToGlobal(me->pos());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_geometry = QRect(m_start, QSize(1, 1));
Toshihiro Shimizu 890ddd
	DVGui::ScreenBoard::instance()->update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ScreenPicker::mouseMoveEvent(QWidget *widget, QMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_mouseGrabbed);
Toshihiro Shimizu 890ddd
	if (!m_mousePressed)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QPoint pos(widget->mapToGlobal(me->pos()));
Toshihiro Shimizu 890ddd
	m_geometry = QRect(QRect(m_start, QSize(1, 1)) | QRect(pos, QSize(1, 1)));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	DVGui::ScreenBoard::instance()->update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ScreenPicker::mouseReleaseEvent(QWidget *widget, QMouseEvent *me)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_mouseGrabbed);
Toshihiro Shimizu 890ddd
	if (!m_mousePressed)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_mouseGrabbed = m_mousePressed = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	DVGui::ScreenBoard *screenBoard = DVGui::ScreenBoard::instance();
Toshihiro Shimizu 890ddd
	QList<dvgui::screenboard::drawing *=""> &drawings = screenBoard->drawings();</dvgui::screenboard::drawing>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	drawings.removeAt(drawings.indexOf(this));
Toshihiro Shimizu 890ddd
	screenBoard->releaseMouse();
Toshihiro Shimizu 890ddd
	screenBoard->update();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QPoint pos(widget->mapToGlobal(me->pos()));
Toshihiro Shimizu 890ddd
	m_geometry = QRect(QRect(m_start, QSize(1, 1)) | QRect(pos, QSize(1, 1)));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//TimerEvents execution is delayed until all other events have been processed.
Toshihiro Shimizu 890ddd
	//In particular, we want to pick after the screen refreshes
Toshihiro Shimizu 890ddd
	QTimer::singleShot(0, this, SLOT(pick()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ScreenPicker::pick()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//Someway, on MACOSX 10.7 (Lion) there may be screen refresh events pending at this point.
Toshihiro Shimizu 890ddd
	//Process them before picking.
Toshihiro Shimizu 890ddd
	QCoreApplication::processEvents();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QColor color(pickScreenRGB(m_geometry));
Toshihiro Shimizu 890ddd
	RGBPicker::setCurrentColorWithUndo(TPixel32(color.red(), color.green(), color.blue()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_geometry = QRect();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ScreenPicker::paintEvent(QWidget *widget, QPaintEvent *pe)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_mousePressed)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QPainter painter(widget);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QRect relativeGeom(
Toshihiro Shimizu 890ddd
		widget->mapFromGlobal(m_geometry.topLeft()),
Toshihiro Shimizu 890ddd
		widget->mapFromGlobal(m_geometry.bottomRight()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	painter.setPen(QColor(0, 0, 255, 128));
Toshihiro Shimizu 890ddd
	painter.setBrush(QColor(0, 0, 255, 64));
Toshihiro Shimizu 890ddd
	painter.drawRect(relativeGeom);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ScreenPicker::startGrab()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_mouseGrabbed) {
Toshihiro Shimizu 890ddd
		m_mouseGrabbed = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		DVGui::ScreenBoard *screenBoard = DVGui::ScreenBoard::instance();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		screenBoard->drawings().push_back(this);
Toshihiro Shimizu 890ddd
		screenBoard->grabMouse(getToolCursor(ToolCursor::PickerCursor));
Toshihiro Shimizu 890ddd
		screenBoard->update();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************
Toshihiro Shimizu 890ddd
//    Pick Screen Command instantiation
Toshihiro Shimizu 890ddd
//***********************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class PickScreenCommandHandler : public MenuItemHandler
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	PickScreenCommandHandler(CommandId cmdId) : MenuItemHandler(cmdId) {}
Toshihiro Shimizu 890ddd
	void execute()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		static ScreenPicker *picker = new ScreenPicker;
Toshihiro Shimizu 890ddd
		picker->startGrab();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
} pickScreenCHInstance("A_ToolOption_PickScreen");