File indexing completed on 2024-04-21 03:44:59

0001 /*
0002     SPDX-FileCopyrightText: 2005 Jason Harris and Jasem Mutlaq <kstars@30doradus.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "thumbimage.h"
0008 
0009 #include <QMouseEvent>
0010 #include <QPainter>
0011 #include <QPen>
0012 
0013 QPixmap ThumbImage::croppedImage()
0014 {
0015     return Image->copy(*CropRect);
0016 }
0017 
0018 ThumbImage::ThumbImage(QWidget *parent, const char *name) : QLabel(parent)
0019 {
0020     //FIXME: Deprecated?  Maybe we don't need this, since double-buffering is now built in
0021     //  setBackgroundMode( Qt::WA_NoBackground );
0022 
0023     setObjectName(name);
0024 
0025     CropRect.reset(new QRect());
0026     Anchor.reset(new QPoint());
0027     Image.reset(new QPixmap());
0028 }
0029 
0030 void ThumbImage::paintEvent(QPaintEvent *)
0031 {
0032     QPainter p;
0033     p.begin(this);
0034 
0035     p.drawPixmap(0, 0, *Image);
0036 
0037     p.setPen(QPen(QColor("Grey"), 2));
0038     p.drawRect(*CropRect);
0039 
0040     p.setPen(QPen(QColor("Grey"), 0));
0041     p.drawRect(QRect(CropRect->left(), CropRect->top(), HandleSize, HandleSize));
0042     p.drawRect(QRect(CropRect->right() - HandleSize, CropRect->top(), HandleSize, HandleSize));
0043     p.drawRect(QRect(CropRect->left(), CropRect->bottom() - HandleSize, HandleSize, HandleSize));
0044     p.drawRect(QRect(CropRect->right() - HandleSize, CropRect->bottom() - HandleSize, HandleSize, HandleSize));
0045 
0046     if (CropRect->x() > 0)
0047         p.fillRect(0, 0, CropRect->x(), height(), QBrush(QColor("white"), Qt::Dense3Pattern));
0048     if (CropRect->right() < width())
0049         p.fillRect(CropRect->right(), 0, (width() - CropRect->right()), height(),
0050                    QBrush(QColor("white"), Qt::Dense3Pattern));
0051     if (CropRect->y() > 0)
0052         p.fillRect(0, 0, width(), CropRect->y(), QBrush(QColor("white"), Qt::Dense3Pattern));
0053     if (CropRect->bottom() < height())
0054         p.fillRect(0, CropRect->bottom(), width(), (height() - CropRect->bottom()),
0055                    QBrush(QColor("white"), Qt::Dense3Pattern));
0056 
0057     p.end();
0058 }
0059 
0060 void ThumbImage::mousePressEvent(QMouseEvent *e)
0061 {
0062     if (e->button() == Qt::LeftButton && CropRect->contains(e->pos()))
0063     {
0064         bMouseButtonDown = true;
0065 
0066         //The Anchor tells how far from the CropRect corner we clicked
0067         Anchor->setX(e->x() - CropRect->left());
0068         Anchor->setY(e->y() - CropRect->top());
0069 
0070         if (e->x() <= CropRect->left() + HandleSize && e->y() <= CropRect->top() + HandleSize)
0071         {
0072             bTopLeftGrab = true;
0073         }
0074         if (e->x() <= CropRect->left() + HandleSize && e->y() >= CropRect->bottom() - HandleSize)
0075         {
0076             bBottomLeftGrab = true;
0077             Anchor->setY(e->y() - CropRect->bottom());
0078         }
0079         if (e->x() >= CropRect->right() - HandleSize && e->y() <= CropRect->top() + HandleSize)
0080         {
0081             bTopRightGrab = true;
0082             Anchor->setX(e->x() - CropRect->right());
0083         }
0084         if (e->x() >= CropRect->right() - HandleSize && e->y() >= CropRect->bottom() - HandleSize)
0085         {
0086             bBottomRightGrab = true;
0087             Anchor->setX(e->x() - CropRect->right());
0088             Anchor->setY(e->y() - CropRect->bottom());
0089         }
0090     }
0091 }
0092 
0093 void ThumbImage::mouseReleaseEvent(QMouseEvent *)
0094 {
0095     if (bMouseButtonDown)
0096         bMouseButtonDown = false;
0097     if (bTopLeftGrab)
0098         bTopLeftGrab = false;
0099     if (bTopRightGrab)
0100         bTopRightGrab = false;
0101     if (bBottomLeftGrab)
0102         bBottomLeftGrab = false;
0103     if (bBottomRightGrab)
0104         bBottomRightGrab = false;
0105 }
0106 
0107 void ThumbImage::mouseMoveEvent(QMouseEvent *e)
0108 {
0109     if (bMouseButtonDown)
0110     {
0111         //If a corner was grabbed, we are resizing the box
0112         if (bTopLeftGrab)
0113         {
0114             if (e->x() >= 0 && e->x() <= width())
0115                 CropRect->setLeft(e->x() - Anchor->x());
0116             if (e->y() >= 0 && e->y() <= height())
0117                 CropRect->setTop(e->y() - Anchor->y());
0118             if (CropRect->left() < 0)
0119                 CropRect->setLeft(0);
0120             if (CropRect->top() < 0)
0121                 CropRect->setTop(0);
0122             if (CropRect->width() < 200)
0123                 CropRect->setLeft(CropRect->left() - 200 + CropRect->width());
0124             if (CropRect->height() < 200)
0125                 CropRect->setTop(CropRect->top() - 200 + CropRect->height());
0126         }
0127         else if (bTopRightGrab)
0128         {
0129             if (e->x() >= 0 && e->x() <= width())
0130                 CropRect->setRight(e->x() - Anchor->x());
0131             if (e->y() >= 0 && e->y() <= height())
0132                 CropRect->setTop(e->y() - Anchor->y());
0133             if (CropRect->right() > width())
0134                 CropRect->setRight(width());
0135             if (CropRect->top() < 0)
0136                 CropRect->setTop(0);
0137             if (CropRect->width() < 200)
0138                 CropRect->setRight(CropRect->right() + 200 - CropRect->width());
0139             if (CropRect->height() < 200)
0140                 CropRect->setTop(CropRect->top() - 200 + CropRect->height());
0141         }
0142         else if (bBottomLeftGrab)
0143         {
0144             if (e->x() >= 0 && e->x() <= width())
0145                 CropRect->setLeft(e->x() - Anchor->x());
0146             if (e->y() >= 0 && e->y() <= height())
0147                 CropRect->setBottom(e->y() - Anchor->y());
0148             if (CropRect->left() < 0)
0149                 CropRect->setLeft(0);
0150             if (CropRect->bottom() > height())
0151                 CropRect->setBottom(height());
0152             if (CropRect->width() < 200)
0153                 CropRect->setLeft(CropRect->left() - 200 + CropRect->width());
0154             if (CropRect->height() < 200)
0155                 CropRect->setBottom(CropRect->bottom() + 200 - CropRect->height());
0156         }
0157         else if (bBottomRightGrab)
0158         {
0159             if (e->x() >= 0 && e->x() <= width())
0160                 CropRect->setRight(e->x() - Anchor->x());
0161             if (e->y() >= 0 && e->y() <= height())
0162                 CropRect->setBottom(e->y() - Anchor->y());
0163             if (CropRect->right() > width())
0164                 CropRect->setRight(width());
0165             if (CropRect->bottom() > height())
0166                 CropRect->setBottom(height());
0167             if (CropRect->width() < 200)
0168                 CropRect->setRight(CropRect->right() + 200 - CropRect->width());
0169             if (CropRect->height() < 200)
0170                 CropRect->setBottom(CropRect->bottom() + 200 - CropRect->height());
0171         }
0172         else //no corner grabbed; move croprect
0173         {
0174             CropRect->moveTopLeft(QPoint(e->x() - Anchor->x(), e->y() - Anchor->y()));
0175             if (CropRect->left() < 0)
0176                 CropRect->moveLeft(0);
0177             if (CropRect->right() > width())
0178                 CropRect->moveRight(width());
0179             if (CropRect->top() < 0)
0180                 CropRect->moveTop(0);
0181             if (CropRect->bottom() > height())
0182                 CropRect->moveBottom(height());
0183         }
0184 
0185         emit cropRegionModified();
0186         update();
0187     }
0188 }