Kstars

thumbimage.cpp
1/*
2 SPDX-FileCopyrightText: 2005 Jason Harris and Jasem Mutlaq <kstars@30doradus.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "thumbimage.h"
8
9#include <QMouseEvent>
10#include <QPainter>
11#include <QPen>
12
13QPixmap ThumbImage::croppedImage()
14{
15 return Image->copy(*CropRect);
16}
17
18ThumbImage::ThumbImage(QWidget *parent, const char *name) : QLabel(parent)
19{
20 //FIXME: Deprecated? Maybe we don't need this, since double-buffering is now built in
21 // setBackgroundMode( Qt::WA_NoBackground );
22
23 setObjectName(name);
24
25 CropRect.reset(new QRect());
26 Anchor.reset(new QPoint());
27 Image.reset(new QPixmap());
28}
29
30void ThumbImage::paintEvent(QPaintEvent *)
31{
32 QPainter p;
33 p.begin(this);
34
35 p.drawPixmap(0, 0, *Image);
36
37 p.setPen(QPen(QColor("Grey"), 2));
38 p.drawRect(*CropRect);
39
40 p.setPen(QPen(QColor("Grey"), 0));
41 p.drawRect(QRect(CropRect->left(), CropRect->top(), HandleSize, HandleSize));
42 p.drawRect(QRect(CropRect->right() - HandleSize, CropRect->top(), HandleSize, HandleSize));
43 p.drawRect(QRect(CropRect->left(), CropRect->bottom() - HandleSize, HandleSize, HandleSize));
44 p.drawRect(QRect(CropRect->right() - HandleSize, CropRect->bottom() - HandleSize, HandleSize, HandleSize));
45
46 if (CropRect->x() > 0)
47 p.fillRect(0, 0, CropRect->x(), height(), QBrush(QColor("white"), Qt::Dense3Pattern));
48 if (CropRect->right() < width())
49 p.fillRect(CropRect->right(), 0, (width() - CropRect->right()), height(),
51 if (CropRect->y() > 0)
52 p.fillRect(0, 0, width(), CropRect->y(), QBrush(QColor("white"), Qt::Dense3Pattern));
53 if (CropRect->bottom() < height())
54 p.fillRect(0, CropRect->bottom(), width(), (height() - CropRect->bottom()),
56
57 p.end();
58}
59
60void ThumbImage::mousePressEvent(QMouseEvent *e)
61{
62 if (e->button() == Qt::LeftButton && CropRect->contains(e->pos()))
63 {
64 bMouseButtonDown = true;
65
66 //The Anchor tells how far from the CropRect corner we clicked
67 Anchor->setX(e->x() - CropRect->left());
68 Anchor->setY(e->y() - CropRect->top());
69
70 if (e->x() <= CropRect->left() + HandleSize && e->y() <= CropRect->top() + HandleSize)
71 {
72 bTopLeftGrab = true;
73 }
74 if (e->x() <= CropRect->left() + HandleSize && e->y() >= CropRect->bottom() - HandleSize)
75 {
76 bBottomLeftGrab = true;
77 Anchor->setY(e->y() - CropRect->bottom());
78 }
79 if (e->x() >= CropRect->right() - HandleSize && e->y() <= CropRect->top() + HandleSize)
80 {
81 bTopRightGrab = true;
82 Anchor->setX(e->x() - CropRect->right());
83 }
84 if (e->x() >= CropRect->right() - HandleSize && e->y() >= CropRect->bottom() - HandleSize)
85 {
86 bBottomRightGrab = true;
87 Anchor->setX(e->x() - CropRect->right());
88 Anchor->setY(e->y() - CropRect->bottom());
89 }
90 }
91}
92
93void ThumbImage::mouseReleaseEvent(QMouseEvent *)
94{
95 if (bMouseButtonDown)
96 bMouseButtonDown = false;
97 if (bTopLeftGrab)
98 bTopLeftGrab = false;
99 if (bTopRightGrab)
100 bTopRightGrab = false;
101 if (bBottomLeftGrab)
102 bBottomLeftGrab = false;
103 if (bBottomRightGrab)
104 bBottomRightGrab = false;
105}
106
107void ThumbImage::mouseMoveEvent(QMouseEvent *e)
108{
109 if (bMouseButtonDown)
110 {
111 //If a corner was grabbed, we are resizing the box
112 if (bTopLeftGrab)
113 {
114 if (e->x() >= 0 && e->x() <= width())
115 CropRect->setLeft(e->x() - Anchor->x());
116 if (e->y() >= 0 && e->y() <= height())
117 CropRect->setTop(e->y() - Anchor->y());
118 if (CropRect->left() < 0)
119 CropRect->setLeft(0);
120 if (CropRect->top() < 0)
121 CropRect->setTop(0);
122 if (CropRect->width() < 200)
123 CropRect->setLeft(CropRect->left() - 200 + CropRect->width());
124 if (CropRect->height() < 200)
125 CropRect->setTop(CropRect->top() - 200 + CropRect->height());
126 }
127 else if (bTopRightGrab)
128 {
129 if (e->x() >= 0 && e->x() <= width())
130 CropRect->setRight(e->x() - Anchor->x());
131 if (e->y() >= 0 && e->y() <= height())
132 CropRect->setTop(e->y() - Anchor->y());
133 if (CropRect->right() > width())
134 CropRect->setRight(width());
135 if (CropRect->top() < 0)
136 CropRect->setTop(0);
137 if (CropRect->width() < 200)
138 CropRect->setRight(CropRect->right() + 200 - CropRect->width());
139 if (CropRect->height() < 200)
140 CropRect->setTop(CropRect->top() - 200 + CropRect->height());
141 }
142 else if (bBottomLeftGrab)
143 {
144 if (e->x() >= 0 && e->x() <= width())
145 CropRect->setLeft(e->x() - Anchor->x());
146 if (e->y() >= 0 && e->y() <= height())
147 CropRect->setBottom(e->y() - Anchor->y());
148 if (CropRect->left() < 0)
149 CropRect->setLeft(0);
150 if (CropRect->bottom() > height())
151 CropRect->setBottom(height());
152 if (CropRect->width() < 200)
153 CropRect->setLeft(CropRect->left() - 200 + CropRect->width());
154 if (CropRect->height() < 200)
155 CropRect->setBottom(CropRect->bottom() + 200 - CropRect->height());
156 }
157 else if (bBottomRightGrab)
158 {
159 if (e->x() >= 0 && e->x() <= width())
160 CropRect->setRight(e->x() - Anchor->x());
161 if (e->y() >= 0 && e->y() <= height())
162 CropRect->setBottom(e->y() - Anchor->y());
163 if (CropRect->right() > width())
164 CropRect->setRight(width());
165 if (CropRect->bottom() > height())
166 CropRect->setBottom(height());
167 if (CropRect->width() < 200)
168 CropRect->setRight(CropRect->right() + 200 - CropRect->width());
169 if (CropRect->height() < 200)
170 CropRect->setBottom(CropRect->bottom() + 200 - CropRect->height());
171 }
172 else //no corner grabbed; move croprect
173 {
174 CropRect->moveTopLeft(QPoint(e->x() - Anchor->x(), e->y() - Anchor->y()));
175 if (CropRect->left() < 0)
176 CropRect->moveLeft(0);
177 if (CropRect->right() > width())
178 CropRect->moveRight(width());
179 if (CropRect->top() < 0)
180 CropRect->moveTop(0);
181 if (CropRect->bottom() > height())
182 CropRect->moveBottom(height());
183 }
184
185 emit cropRegionModified();
186 update();
187 }
188}
QPoint pos() const const
int x() const const
int y() const const
bool begin(QPaintDevice *device)
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void drawRect(const QRect &rectangle)
bool end()
void fillRect(const QRect &rectangle, QGradient::Preset preset)
void setPen(Qt::PenStyle style)
QPixmap copy(const QRect &rectangle) const const
Qt::MouseButton button() const const
Dense3Pattern
LeftButton
void update()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.