3 Copyright (C) 2009 RafaĆ Rzepecki <divided.mind@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "cropwidget.h"
22 #include <QStackedLayout>
23 #include <QMouseEvent>
25 #include <QPaintEvent>
26 #include <QResizeEvent>
28 CropWidget::CropWidget(QWidget
*parent
): QFrame(parent
), _snapped(false)
32 void CropWidget::snap(bool snapped
)
36 const QList
<QWidget
*> widgets
= findChildren
<QWidget
*>();
38 snapshot
= QPixmap::grabWidget(this, contentsRect());
39 foreach(QWidget
*w
, widgets
)
42 foreach(QWidget
*w
, widgets
)
44 QPainter
painter(this);
45 painter
.eraseRect(contentsRect());
51 void CropWidget::paintEvent(QPaintEvent
*event
)
53 QFrame::paintEvent(event
);
57 QPainter
painter(this);
58 painter
.setRenderHint(QPainter::SmoothPixmapTransform
);
59 painter
.drawPixmap(contentsRect(), snapshot
);
61 if (cropRect
.isValid()) {
62 QImage
img(contentsRect().size(), QImage::Format_ARGB32
);
64 ppm
.fillRect(rect(), QColor(255, 255, 255, 200)); ppm
.setCompositionMode(QPainter::CompositionMode_Clear
);
65 ppm
.eraseRect(cropRect
);
66 painter
.drawImage(contentsRect(), img
);
72 void CropWidget::crop()
74 if ((!_snapped
) || (!cropRect
.isValid()))
77 int x
= cropRect
.x() * snapshot
.width() / width();
78 int y
= cropRect
.y() * snapshot
.height() / height();
79 int w
= cropRect
.width() * snapshot
.width() / width();
80 int h
= cropRect
.height() * snapshot
.height() / height();
82 snapshot
= snapshot
.copy(QRect(x
, y
, w
, h
));
87 void CropWidget::mousePressEvent(QMouseEvent
*event
)
89 if ((!_snapped
) || (event
->button() != Qt::LeftButton
))
90 return QWidget::mousePressEvent(event
);
92 const QPoint pos
= toContents(event
->pos());
98 void CropWidget::mouseMoveEvent(QMouseEvent
*event
)
100 if ((!_snapped
) || (cropStart
.isNull()) || (!(event
->buttons() & Qt::LeftButton
)))
101 return QWidget::mouseMoveEvent(event
);
103 const QPoint pos
= toContents(event
->pos());
105 cropRect
= QRect(cropStart
, pos
).normalized();
110 void CropWidget::mouseReleaseEvent(QMouseEvent
*event
)
112 if ((!_snapped
) || (cropStart
.isNull()) || (event
->button() != Qt::LeftButton
))
113 return QWidget::mouseReleaseEvent(event
);
115 const QPoint pos
= toContents(event
->pos());
117 cropRect
= QRect(cropStart
, pos
).normalized();
119 cropStart
= QPoint();
123 QSize
CropWidget::sizeHint() const
126 return QSize(snapshot
.size().width() + 2 * frameWidth(), snapshot
.size().height() + 2 * frameWidth());
128 return QFrame::sizeHint();
131 QSizePolicy
CropWidget::sizePolicy() const
134 return QSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
136 return QFrame::sizePolicy();
139 QPoint
CropWidget::toContents(QPoint point
) const
141 QPoint p
= point
- contentsRect().topLeft();
144 if (p
.x() >= contentsRect().size().width())
145 p
.setX(contentsRect().size().width() - 1);
148 if (p
.y() >= contentsRect().size().height())
149 p
.setY(contentsRect().size().height() - 1);