~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/redeyereduction/redeyereductiontool.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2007 AurĆ©lien GĆ¢teau <agateau@kde.org>
22
22
#include "redeyereductiontool.moc"
23
23
 
24
24
// Qt
25
 
#include <QMouseEvent>
 
25
#include <QGraphicsSceneMouseEvent>
26
26
#include <QPainter>
27
 
#include <QStyle>
28
 
#include <QToolButton>
 
27
#include <QPushButton>
29
28
#include <QRect>
30
29
 
31
30
// KDE
33
32
#include <klocale.h>
34
33
 
35
34
// Local
36
 
#include <lib/hudwidget.h>
 
35
#include <lib/documentview/rasterimageview.h>
37
36
#include "gwenviewconfig.h"
38
 
#include "imageview.h"
39
37
#include "paintutils.h"
40
38
#include "redeyereductionimageoperation.h"
41
 
#include "ui_redeyereductionhud.h"
42
 
#include "widgetfloater.h"
43
 
 
44
 
 
45
 
namespace Gwenview {
46
 
 
47
 
 
48
 
struct RedEyeReductionHud : public QWidget, public Ui_RedEyeReductionHud {
49
 
        RedEyeReductionHud() {
50
 
                setupUi(this);
51
 
                setCursor(Qt::ArrowCursor);
52
 
        }
53
 
};
54
 
 
55
 
 
56
 
struct RedEyeReductionToolPrivate {
57
 
        RedEyeReductionTool* mRedEyeReductionTool;
58
 
        RedEyeReductionTool::Status mStatus;
59
 
        QPointF mCenter;
60
 
        int mDiameter;
61
 
        RedEyeReductionHud* mHud;
62
 
        HudWidget* mHudWidget;
63
 
        WidgetFloater* mFloater;
64
 
 
65
 
 
66
 
        void showNotSetHudWidget() {
67
 
                mHud->deleteLater();
68
 
                mHud = 0;
69
 
                QLabel* label = new QLabel(i18n("Click on the red eye you want to fix."));
70
 
                label->show();
71
 
                label->adjustSize();
72
 
                createHudWidgetForWidget(label);
73
 
        }
74
 
 
75
 
 
76
 
        void showAdjustingHudWidget() {
77
 
                mHud = new RedEyeReductionHud();
78
 
 
79
 
                mHud->diameterSpinBox->setValue(mDiameter);
80
 
                QObject::connect(mHud->applyButton, SIGNAL(clicked()),
81
 
                        mRedEyeReductionTool, SLOT(slotApplyClicked()));
82
 
                QObject::connect(mHud->diameterSpinBox, SIGNAL(valueChanged(int)),
83
 
                        mRedEyeReductionTool, SLOT(setDiameter(int)));
84
 
 
85
 
                createHudWidgetForWidget(mHud);
86
 
        }
87
 
 
88
 
 
89
 
        void createHudWidgetForWidget(QWidget* widget) {
90
 
                mHudWidget->deleteLater();
91
 
                mHudWidget = new HudWidget();
92
 
                mHudWidget->init(widget, HudWidget::OptionCloseButton);
93
 
                mHudWidget->adjustSize();
94
 
                QObject::connect(mHudWidget, SIGNAL(closed()),
95
 
                        mRedEyeReductionTool, SIGNAL(done()) );
96
 
                mFloater->setChildWidget(mHudWidget);
97
 
        }
98
 
 
99
 
 
100
 
        void hideHud() {
101
 
                mHudWidget->hide();
102
 
        }
103
 
 
104
 
 
105
 
        QRectF rectF() const {
106
 
                if (mStatus == RedEyeReductionTool::NotSet) {
107
 
                        return QRectF();
108
 
                }
109
 
                return QRectF(mCenter.x() - mDiameter / 2, mCenter.y() - mDiameter / 2, mDiameter, mDiameter);
110
 
        }
111
 
};
112
 
 
113
 
 
114
 
RedEyeReductionTool::RedEyeReductionTool(ImageView* view)
115
 
: AbstractImageViewTool(view)
116
 
, d(new RedEyeReductionToolPrivate) {
117
 
        d->mRedEyeReductionTool = this;
118
 
        d->mDiameter = GwenviewConfig::redEyeReductionDiameter();
119
 
        d->mStatus = NotSet;
120
 
        d->mHud = 0;
121
 
        d->mHudWidget = 0;
122
 
 
123
 
        d->mFloater = new WidgetFloater(imageView());
124
 
        d->mFloater->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
125
 
        d->mFloater->setVerticalMargin(
126
 
                KDialog::marginHint()
127
 
                + imageView()->style()->pixelMetric(QStyle::PM_ScrollBarExtent)
128
 
                );
129
 
        d->showNotSetHudWidget();
130
 
 
131
 
        view->document()->startLoadingFullImage();
132
 
}
133
 
 
134
 
 
135
 
RedEyeReductionTool::~RedEyeReductionTool() {
136
 
        GwenviewConfig::setRedEyeReductionDiameter(d->mDiameter);
137
 
        delete d;
138
 
}
139
 
 
140
 
 
141
 
void RedEyeReductionTool::paint(QPainter* painter) {
142
 
        if (d->mStatus == NotSet) {
143
 
                return;
144
 
        }
145
 
        QRectF docRectF = d->rectF();
146
 
        imageView()->document()->waitUntilLoaded();
147
 
 
148
 
        QRect docRect = PaintUtils::containingRect(docRectF);
149
 
        QImage img = imageView()->document()->image().copy(docRect);
150
 
        QRectF imgRectF(
151
 
                docRectF.left() - docRect.left(),
152
 
                docRectF.top()  - docRect.top(),
153
 
                docRectF.width(),
154
 
                docRectF.height()
155
 
                );
156
 
        RedEyeReductionImageOperation::apply(&img, imgRectF);
157
 
 
158
 
        const QRectF viewRectF = imageView()->mapToViewportF(docRectF);
159
 
        painter->drawImage(viewRectF, img, imgRectF);
160
 
}
161
 
 
162
 
 
163
 
void RedEyeReductionTool::mousePressEvent(QMouseEvent* event) {
164
 
        if (d->mStatus == NotSet) {
165
 
                d->showAdjustingHudWidget();
166
 
                d->mStatus = Adjusting;
167
 
        }
168
 
        d->mCenter = imageView()->mapToImageF(event->pos());
169
 
        imageView()->viewport()->update();
170
 
}
171
 
 
172
 
 
173
 
void RedEyeReductionTool::mouseMoveEvent(QMouseEvent* event) {
174
 
        if (event->buttons() == Qt::NoButton) {
175
 
                return;
176
 
        }
177
 
        d->mCenter = imageView()->mapToImageF(event->pos());
178
 
        imageView()->viewport()->update();
179
 
}
180
 
 
181
 
 
182
 
void RedEyeReductionTool::toolActivated() {
183
 
        imageView()->viewport()->setCursor(Qt::CrossCursor);
184
 
}
185
 
 
186
 
 
187
 
void RedEyeReductionTool::toolDeactivated() {
188
 
        d->mHudWidget->deleteLater();
189
 
}
190
 
 
191
 
 
192
 
void RedEyeReductionTool::slotApplyClicked() {
193
 
        QRectF docRectF = d->rectF();
194
 
        if (!docRectF.isValid()) {
195
 
                kWarning() << "invalid rect";
196
 
                return;
197
 
        }
198
 
        RedEyeReductionImageOperation* op = new RedEyeReductionImageOperation(docRectF);
199
 
        emit imageOperationRequested(op);
200
 
 
201
 
        d->mStatus = NotSet;
202
 
        d->showNotSetHudWidget();
203
 
}
204
 
 
205
 
 
206
 
void RedEyeReductionTool::setDiameter(int value) {
207
 
        d->mDiameter = value;
208
 
        imageView()->viewport()->update();
209
 
}
210
 
 
 
39
#include "ui_redeyereductionwidget.h"
 
40
 
 
41
namespace Gwenview
 
42
{
 
43
 
 
44
struct RedEyeReductionWidget : public QWidget, public Ui_RedEyeReductionWidget
 
45
{
 
46
    RedEyeReductionWidget()
 
47
    {
 
48
        setupUi(this);
 
49
    }
 
50
 
 
51
    void showNotSetPage()
 
52
    {
 
53
        dialogButtonBox->button(QDialogButtonBox::Ok)->hide();
 
54
        stackedWidget->setCurrentWidget(notSetPage);
 
55
    }
 
56
 
 
57
    void showMainPage()
 
58
    {
 
59
        dialogButtonBox->button(QDialogButtonBox::Ok)->show();
 
60
        stackedWidget->setCurrentWidget(mainPage);
 
61
    }
 
62
};
 
63
 
 
64
struct RedEyeReductionToolPrivate
 
65
{
 
66
    RedEyeReductionTool* q;
 
67
    RedEyeReductionTool::Status mStatus;
 
68
    QPointF mCenter;
 
69
    int mDiameter;
 
70
    RedEyeReductionWidget* mToolWidget;
 
71
 
 
72
    void setupToolWidget()
 
73
    {
 
74
        mToolWidget = new RedEyeReductionWidget;
 
75
        mToolWidget->showNotSetPage();
 
76
        QObject::connect(mToolWidget->diameterSpinBox, SIGNAL(valueChanged(int)),
 
77
                         q, SLOT(setDiameter(int)));
 
78
        QObject::connect(mToolWidget->dialogButtonBox, SIGNAL(accepted()),
 
79
                         q, SLOT(slotApplyClicked()));
 
80
        QObject::connect(mToolWidget->dialogButtonBox, SIGNAL(rejected()),
 
81
                         q, SIGNAL(done()));
 
82
    }
 
83
 
 
84
    QRectF rectF() const {
 
85
        if (mStatus == RedEyeReductionTool::NotSet) {
 
86
            return QRectF();
 
87
        }
 
88
        return QRectF(mCenter.x() - mDiameter / 2, mCenter.y() - mDiameter / 2, mDiameter, mDiameter);
 
89
    }
 
90
};
 
91
 
 
92
RedEyeReductionTool::RedEyeReductionTool(RasterImageView* view)
 
93
: AbstractRasterImageViewTool(view)
 
94
, d(new RedEyeReductionToolPrivate)
 
95
{
 
96
    d->q = this;
 
97
    d->mDiameter = GwenviewConfig::redEyeReductionDiameter();
 
98
    d->mStatus = NotSet;
 
99
    d->setupToolWidget();
 
100
 
 
101
    view->document()->startLoadingFullImage();
 
102
}
 
103
 
 
104
RedEyeReductionTool::~RedEyeReductionTool()
 
105
{
 
106
    GwenviewConfig::setRedEyeReductionDiameter(d->mDiameter);
 
107
    delete d->mToolWidget;
 
108
    delete d;
 
109
}
 
110
 
 
111
void RedEyeReductionTool::paint(QPainter* painter)
 
112
{
 
113
    if (d->mStatus == NotSet) {
 
114
        return;
 
115
    }
 
116
    QRectF docRectF = d->rectF();
 
117
    imageView()->document()->waitUntilLoaded();
 
118
 
 
119
    QRect docRect = PaintUtils::containingRect(docRectF);
 
120
    QImage img = imageView()->document()->image().copy(docRect);
 
121
    QRectF imgRectF(
 
122
        docRectF.left() - docRect.left(),
 
123
        docRectF.top()  - docRect.top(),
 
124
        docRectF.width(),
 
125
        docRectF.height()
 
126
    );
 
127
    RedEyeReductionImageOperation::apply(&img, imgRectF);
 
128
 
 
129
    const QRectF viewRectF = imageView()->mapToView(docRectF);
 
130
    painter->drawImage(viewRectF, img, imgRectF);
 
131
}
 
132
 
 
133
void RedEyeReductionTool::mousePressEvent(QGraphicsSceneMouseEvent* event)
 
134
{
 
135
    event->accept();
 
136
    if (d->mStatus == NotSet) {
 
137
        d->mToolWidget->diameterSpinBox->setValue(d->mDiameter);
 
138
        d->mToolWidget->showMainPage();
 
139
        d->mStatus = Adjusting;
 
140
    }
 
141
    d->mCenter = imageView()->mapToImage(event->pos());
 
142
    imageView()->update();
 
143
}
 
144
 
 
145
void RedEyeReductionTool::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
 
146
{
 
147
    event->accept();
 
148
    if (event->buttons() == Qt::NoButton) {
 
149
        return;
 
150
    }
 
151
    d->mCenter = imageView()->mapToImage(event->pos());
 
152
    imageView()->update();
 
153
}
 
154
 
 
155
void RedEyeReductionTool::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
 
156
{
 
157
    // Just prevent the event from reaching the image view
 
158
    event->accept();
 
159
}
 
160
 
 
161
void RedEyeReductionTool::toolActivated()
 
162
{
 
163
    imageView()->setCursor(Qt::CrossCursor);
 
164
}
 
165
 
 
166
void RedEyeReductionTool::slotApplyClicked()
 
167
{
 
168
    QRectF docRectF = d->rectF();
 
169
    if (!docRectF.isValid()) {
 
170
        kWarning() << "invalid rect";
 
171
        return;
 
172
    }
 
173
    RedEyeReductionImageOperation* op = new RedEyeReductionImageOperation(docRectF);
 
174
    emit imageOperationRequested(op);
 
175
 
 
176
    d->mStatus = NotSet;
 
177
    d->mToolWidget->showNotSetPage();
 
178
}
 
179
 
 
180
void RedEyeReductionTool::setDiameter(int value)
 
181
{
 
182
    d->mDiameter = value;
 
183
    imageView()->update();
 
184
}
 
185
 
 
186
QWidget* RedEyeReductionTool::widget() const
 
187
{
 
188
    return d->mToolWidget;
 
189
}
211
190
 
212
191
} // namespace