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)
Shinya Kitaoka 120a6e
    : m_mousePressed(false), m_mouseGrabbed(false) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool ScreenPicker::acceptScreenEvents(const QRect &screenRect) const {
Shinya Kitaoka 120a6e
  return screenRect.contains(QCursor::pos()) ||  // Accept mouse events
Shinya Kitaoka 120a6e
         screenRect.intersects(m_geometry);      // Accept paint events
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScreenPicker::event(QWidget *widget, QEvent *e) {
Shinya Kitaoka 120a6e
  switch (e->type()) {
Shinya Kitaoka 120a6e
  case QEvent::MouseButtonPress:
Shinya Kitaoka 120a6e
    mousePressEvent(widget, static_cast<qmouseevent *="">(e));</qmouseevent>
Shinya Kitaoka 120a6e
    break;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  case QEvent::MouseMove:
Shinya Kitaoka 120a6e
    mouseMoveEvent(widget, static_cast<qmouseevent *="">(e));</qmouseevent>
Shinya Kitaoka 120a6e
    break;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  case QEvent::MouseButtonRelease:
Shinya Kitaoka 120a6e
    mouseReleaseEvent(widget, static_cast<qmouseevent *="">(e));</qmouseevent>
Shinya Kitaoka 120a6e
  };
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScreenPicker::mousePressEvent(QWidget *widget, QMouseEvent *me) {
Shinya Kitaoka 120a6e
  m_mousePressed = true;
Shinya Kitaoka 120a6e
  m_start        = widget->mapToGlobal(me->pos());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_geometry = QRect(m_start, QSize(1, 1));
Shinya Kitaoka 120a6e
  DVGui::ScreenBoard::instance()->update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScreenPicker::mouseMoveEvent(QWidget *widget, QMouseEvent *me) {
Jeremy Bullock d33b09
  // On fast movements, the mouse release can fire before the mouse movement
shun-iwasawa 5b7b9f
  // assert(m_mouseGrabbed); - can cause a crash
Jeremy Bullock d33b09
  if (!m_mousePressed || !m_mouseGrabbed) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QPoint pos(widget->mapToGlobal(me->pos()));
Shinya Kitaoka 120a6e
  m_geometry = QRect(QRect(m_start, QSize(1, 1)) | QRect(pos, QSize(1, 1)));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  DVGui::ScreenBoard::instance()->update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScreenPicker::mouseReleaseEvent(QWidget *widget, QMouseEvent *me) {
Shinya Kitaoka 120a6e
  assert(m_mouseGrabbed);
Shinya Kitaoka 120a6e
  if (!m_mousePressed) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_mouseGrabbed = m_mousePressed = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  DVGui::ScreenBoard *screenBoard = DVGui::ScreenBoard::instance();
Shinya Kitaoka 120a6e
  QList<dvgui::screenboard::drawing *=""> &drawings = screenBoard->drawings();</dvgui::screenboard::drawing>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  drawings.removeAt(drawings.indexOf(this));
Shinya Kitaoka 120a6e
  screenBoard->releaseMouse();
Shinya Kitaoka 120a6e
  screenBoard->update();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QPoint pos(widget->mapToGlobal(me->pos()));
Shinya Kitaoka 120a6e
  m_geometry = QRect(QRect(m_start, QSize(1, 1)) | QRect(pos, QSize(1, 1)));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // TimerEvents execution is delayed until all other events have been
Shinya Kitaoka 120a6e
  // processed.
Shinya Kitaoka 120a6e
  // In particular, we want to pick after the screen refreshes
Shinya Kitaoka 120a6e
  QTimer::singleShot(0, this, SLOT(pick()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScreenPicker::pick() {
Shinya Kitaoka 120a6e
  // Someway, on MACOSX 10.7 (Lion) there may be screen refresh events pending
Shinya Kitaoka 120a6e
  // at this point.
Shinya Kitaoka 120a6e
  // Process them before picking.
Shinya Kitaoka 120a6e
  QCoreApplication::processEvents();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QColor color(pickScreenRGB(m_geometry));
Shinya Kitaoka 120a6e
  RGBPicker::setCurrentColorWithUndo(
Shinya Kitaoka 120a6e
      TPixel32(color.red(), color.green(), color.blue()));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_geometry = QRect();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScreenPicker::paintEvent(QWidget *widget, QPaintEvent *pe) {
Shinya Kitaoka 120a6e
  if (!m_mousePressed) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QPainter painter(widget);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QRect relativeGeom(widget->mapFromGlobal(m_geometry.topLeft()),
Shinya Kitaoka 120a6e
                     widget->mapFromGlobal(m_geometry.bottomRight()));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  painter.setPen(QColor(0, 0, 255, 128));
Shinya Kitaoka 120a6e
  painter.setBrush(QColor(0, 0, 255, 64));
Shinya Kitaoka 120a6e
  painter.drawRect(relativeGeom);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ScreenPicker::startGrab() {
Shinya Kitaoka 120a6e
  if (!m_mouseGrabbed) {
Shinya Kitaoka 120a6e
    m_mouseGrabbed = true;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    DVGui::ScreenBoard *screenBoard = DVGui::ScreenBoard::instance();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    screenBoard->drawings().push_back(this);
Shinya Kitaoka 120a6e
    screenBoard->grabMouse(getToolCursor(ToolCursor::PickerCursor));
Shinya Kitaoka 120a6e
    screenBoard->update();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************
Toshihiro Shimizu 890ddd
//    Pick Screen Command instantiation
Toshihiro Shimizu 890ddd
//***********************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class PickScreenCommandHandler final : public MenuItemHandler {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  PickScreenCommandHandler(CommandId cmdId) : MenuItemHandler(cmdId) {}
Shinya Kitaoka 473e70
  void execute() override {
Shinya Kitaoka 120a6e
    static ScreenPicker *picker = new ScreenPicker;
Shinya Kitaoka 120a6e
    picker->startGrab();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
} pickScreenCHInstance("A_ToolOption_PickScreen");