bool ClipRect::clip(RectF &dstRect, RectF &srcRect) const { if (dstRect.right() <= m_rect.left() || dstRect.left() >= m_rect.right()) return false; if (dstRect.bottom() <= m_rect.top() || dstRect.top() >= m_rect.bottom()) return false; RectF dst = dstRect; RectF src = srcRect; float clipLeft = m_rect.left() - dstRect.left(); if (clipLeft > 0) { dst.x += clipLeft; dst.width -= clipLeft; clipLeft *= srcRect.width/dstRect.width; src.x += clipLeft; src.width -= clipLeft; } float clipRight = dstRect.right() - m_rect.right(); if (clipRight > 0) { dst.width -= clipRight; clipRight *= srcRect.width/dstRect.width; src.width -= clipRight; } float clipTop = m_rect.top() - dstRect.top(); if (clipTop > 0) { dst.y += clipTop; dst.height -= clipTop; clipTop *= srcRect.height/dstRect.height; src.y += clipTop; src.height -= clipTop; } float clipBottom = dstRect.bottom() - m_rect.bottom(); if (clipBottom > 0) { dst.height -= clipBottom; clipBottom *= srcRect.height/dstRect.height; src.height -= clipBottom; } dstRect = dst; srcRect = src; return true; }
// ドラッグアンドドロップ開始 void GridLabel::startDragAndDrop(QMouseEvent *ev) { Q_UNUSED(ev); EditData::ImageData *p = m_pEditData->getImageDataFromNo(m_Index); if (!p) { return; } QImage img = p->Image.copy(m_pEditData->getCatchRect().toRect()); QPixmap pix = QPixmap::fromImage(img); QByteArray itemData; QDataStream stream(&itemData, QIODevice::WriteOnly); RectF rect = m_pEditData->getCatchRect(); // stream << rect << mScale << m_Index ; stream << rect.left() << rect.top() << rect.right() << rect.bottom() << mScale << m_Index; QMimeData *mimeData = new QMimeData; mimeData->setData("editor/selected-image", itemData); QPainter painter; painter.begin(&pix); painter.fillRect(pix.rect(), QColor(127, 127, 127, 127)); painter.end(); QDrag *drag = new QDrag(this); drag->setMimeData(mimeData); drag->setPixmap(pix); drag->setHotSpot(QPoint((rect.right() - rect.left()) / 2, (rect.bottom() - rect.top()) / 2)); // qDebug() << "x:" << ev->pos().x() << " y:" << ev->pos().y() ; m_pEditData->setDraggingImage(true); drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction); m_pEditData->setDraggingImage(false); }
// 描画イベント void GridLabel::paintEvent(QPaintEvent *event) { QPainter painter(this); QPen pen, penCenter; pen.setColor(QColor(64, 64, 64, 255)); pen.setWidth(mScale); penCenter.setColor(QColor(255, 255, 0, 255)); penCenter.setWidth(mScale); EditData::ImageData *p = m_pEditData->getImageDataFromNo(m_Index); if (!p) { return; } if (m_pEditData) { QSize size = p->Image.size() * mScale; size += QSize(mScale, mScale); resize(size); } if (m_bDrawGrid) { painter.setPen(pen); for (int x = 0; x <= size().width(); x += m_GridSize.x() * mScale) { if (x == size().width() / 2) { painter.setPen(penCenter); } painter.drawLine(x, event->rect().top(), x, event->rect().bottom()); if (x == size().width() / 2) { painter.setPen(pen); } } for (int y = 0; y <= size().height(); y += m_GridSize.y() * mScale) { if (y == size().height() / 2) { painter.setPen(penCenter); } painter.drawLine(event->rect().left(), y, event->rect().right(), y); if (y == size().height() / 2) { painter.setPen(pen); } } } // 選択中範囲 if (m_pEditData && m_bCatchable) { if (m_bPressCtrl) { pen.setColor(QColor(0, 255, 0, 255)); } else { pen.setColor(QColor(255, 0, 0, 255)); } painter.setPen(pen); RectF rect = m_pEditData->getCatchRect(); QRect r = QRect(rect.left(), rect.top(), rect.right() - rect.left(), rect.bottom() - rect.top()); painter.drawRect(r); } FrameData data; if (m_pEditData->getNowSelectFrameData(data)) { if (data.nImage != m_Index) { return; } } else { return; } // センター表示 if (m_bDrawCenter) { pen.setColor(QColor(0, 0, 255, 255)); painter.setPen(pen); int x = data.center_x + data.left; int y = data.center_y + data.top - 1; QSize size = p->Image.size(); painter.drawLine(QPointF(0, y), QPointF(size.width(), y)); painter.drawLine(QPointF(x, 0), QPointF(x, size.height())); } }
// 範囲選択中 void GridLabel::mouseMoveEvent(QMouseEvent *ev) { if (!m_pEditData || !m_bCatchable) { return; } if (!bCatching) { return; } RectF r = m_pEditData->getCatchRect(); EditData::ImageData *p = m_pEditData->getImageDataFromNo(m_Index); if (!p) { return; } int img_w = p->Image.width(); int img_h = p->Image.height(); int x = ev->pos().x() / mScale; int y = ev->pos().y() / mScale; if (m_bRectMove) { // 範囲移動中 if (!m_bPressCtrl) { return; } // Ctrlキー押してたら QPoint add = ev->pos() - m_MovePos; r.setLeft(r.left() + add.x()); r.setRight(r.right() + add.x()); r.setTop(r.top() + add.y()); r.setBottom(r.bottom() + add.y()); if (r.left() < 0) { r.setRight(r.right() - r.left()); r.setLeft(0); } if (r.right() > img_w - 1) { r.setLeft(r.left() - (r.right() - (img_w - 1))); r.setRight(img_w - 1); } if (r.top() < 0) { r.setBottom(r.bottom() - r.top()); r.setTop(0); } if (r.bottom() > img_h - 1) { r.setTop(r.top() - (r.bottom() - (img_h - 1))); r.setBottom(img_h - 1); } emit sig_changeSelectLayerUV(r); m_MovePos = ev->pos(); } else { if (x < 0) { x = 0; } if (x >= img_w) { x = img_w; } if (x < r.left()) { x = r.left(); } if (y < 0) { y = 0; } if (y >= img_h) { y = img_h; } if (y < r.top()) { y = r.top(); } r.setRight(x); r.setBottom(y); } m_pEditData->setCatchRect(r); emit sig_changeCatchRect(r); repaint(); }