1 /*
2     Scan Tailor - Interactive post-processing tool for scanned pages.
3     Copyright (C)  Joseph Artsimovich <joseph.artsimovich@gmail.com>
4 
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 3 of the License, or
8     (at your option) any later version.
9 
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.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "UnremoveButton.h"
20 #include <QMouseEvent>
21 #include <QPainter>
22 
23 namespace page_split {
UnremoveButton(const PositionGetter & position_getter)24 UnremoveButton::UnremoveButton(const PositionGetter& position_getter)
25     : m_positionGetter(position_getter),
26       m_clickCallback(&UnremoveButton::noOp),
27       m_defaultPixmap(":/icons/trashed-big.png"),
28       m_hoveredPixmap(":/icons/untrash-big.png"),
29       m_wasHovered(false) {
30   m_proximityInteraction.setProximityCursor(Qt::PointingHandCursor);
31   m_proximityInteraction.setProximityStatusTip(tr("Restore removed page."));
32 }
33 
onPaint(QPainter & painter,const InteractionState & interaction)34 void UnremoveButton::onPaint(QPainter& painter, const InteractionState& interaction) {
35   const QPixmap& pixmap = interaction.proximityLeader(m_proximityInteraction) ? m_hoveredPixmap : m_defaultPixmap;
36 
37   QRectF rect(pixmap.rect());
38   rect.moveCenter(m_positionGetter());
39 
40   painter.setWorldTransform(QTransform());
41   painter.drawPixmap(rect.topLeft(), pixmap);
42 }
43 
onProximityUpdate(const QPointF & screen_mouse_pos,InteractionState & interaction)44 void UnremoveButton::onProximityUpdate(const QPointF& screen_mouse_pos, InteractionState& interaction) {
45   QRectF rect(m_defaultPixmap.rect());
46   rect.moveCenter(m_positionGetter());
47 
48   const bool hovered = rect.contains(screen_mouse_pos);
49   if (hovered != m_wasHovered) {
50     m_wasHovered = hovered;
51     interaction.setRedrawRequested(true);
52   }
53 
54   interaction.updateProximity(m_proximityInteraction, Proximity::fromSqDist(hovered ? 0.0 : 1e10));
55 }
56 
onMousePressEvent(QMouseEvent * event,InteractionState & interaction)57 void UnremoveButton::onMousePressEvent(QMouseEvent* event, InteractionState& interaction) {
58   if (!interaction.captured() && interaction.proximityLeader(m_proximityInteraction)) {
59     event->accept();
60     m_clickCallback();
61   }
62 }
63 
setClickCallback(const UnremoveButton::ClickCallback & callback)64 void UnremoveButton::setClickCallback(const UnremoveButton::ClickCallback& callback) {
65   m_clickCallback = callback;
66 }
67 
noOp()68 void UnremoveButton::noOp() {}
69 }  // namespace page_split