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

« back to all changes in this revision

Viewing changes to lib/zoomwidget.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 2008 AurĆ©lien GĆ¢teau <agateau@kde.org>
39
39
#include "signalblocker.h"
40
40
#include "statusbartoolbutton.h"
41
41
 
42
 
namespace Gwenview {
 
42
namespace Gwenview
 
43
{
43
44
 
44
45
static const qreal MAGIC_K = 1.04;
45
46
static const qreal MAGIC_OFFSET = 16.;
46
47
static const qreal PRECISION = 100.;
47
 
inline int sliderValueForZoom(qreal zoom) {
48
 
        return int( PRECISION * (log(zoom) / log(MAGIC_K) + MAGIC_OFFSET) );
49
 
}
50
 
 
51
 
 
52
 
inline qreal zoomForSliderValue(int sliderValue) {
53
 
        return pow(MAGIC_K, sliderValue / PRECISION - MAGIC_OFFSET);
54
 
}
55
 
 
56
 
 
57
 
 
 
48
inline int sliderValueForZoom(qreal zoom)
 
49
{
 
50
    return int(PRECISION * (log(zoom) / log(MAGIC_K) + MAGIC_OFFSET));
 
51
}
 
52
 
 
53
inline qreal zoomForSliderValue(int sliderValue)
 
54
{
 
55
    return pow(MAGIC_K, sliderValue / PRECISION - MAGIC_OFFSET);
 
56
}
58
57
 
59
58
struct ZoomWidgetPrivate {
60
 
        ZoomWidget* that;
61
 
 
62
 
        StatusBarToolButton* mZoomToFitButton;
63
 
        StatusBarToolButton* mActualSizeButton;
64
 
        QLabel* mZoomLabel;
65
 
        ZoomSlider* mZoomSlider;
66
 
        QAction* mZoomToFitAction;
67
 
        QAction* mActualSizeAction;
68
 
 
69
 
        bool mZoomUpdatedBySlider;
70
 
 
71
 
        void emitZoomChanged() {
72
 
                // Use QSlider::sliderPosition(), not QSlider::value() because when we are
73
 
                // called from slotZoomSliderActionTriggered(), QSlider::value() has not
74
 
                // been updated yet.
75
 
                qreal zoom = zoomForSliderValue(mZoomSlider->slider()->sliderPosition());
76
 
                mZoomUpdatedBySlider = true;
77
 
                emit that->zoomChanged(zoom);
78
 
                mZoomUpdatedBySlider = false;
79
 
        }
 
59
    ZoomWidget* q;
 
60
 
 
61
    StatusBarToolButton* mZoomToFitButton;
 
62
    StatusBarToolButton* mActualSizeButton;
 
63
    QLabel* mZoomLabel;
 
64
    ZoomSlider* mZoomSlider;
 
65
    QAction* mZoomToFitAction;
 
66
    QAction* mActualSizeAction;
 
67
 
 
68
    bool mZoomUpdatedBySlider;
 
69
 
 
70
    void emitZoomChanged()
 
71
    {
 
72
        // Use QSlider::sliderPosition(), not QSlider::value() because when we are
 
73
        // called from slotZoomSliderActionTriggered(), QSlider::value() has not
 
74
        // been updated yet.
 
75
        qreal zoom = zoomForSliderValue(mZoomSlider->slider()->sliderPosition());
 
76
        mZoomUpdatedBySlider = true;
 
77
        emit q->zoomChanged(zoom);
 
78
        mZoomUpdatedBySlider = false;
 
79
    }
80
80
};
81
81
 
82
 
 
83
82
ZoomWidget::ZoomWidget(QWidget* parent)
84
83
: QFrame(parent)
85
 
, d(new ZoomWidgetPrivate) {
86
 
        d->that = this;
87
 
        d->mZoomUpdatedBySlider = false;
88
 
 
89
 
        setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
90
 
 
91
 
        d->mZoomToFitButton = new StatusBarToolButton;
92
 
        d->mActualSizeButton = new StatusBarToolButton;
93
 
 
94
 
        if (QApplication::isLeftToRight()) {
95
 
                d->mZoomToFitButton->setGroupPosition(StatusBarToolButton::GroupLeft);
96
 
                d->mActualSizeButton->setGroupPosition(StatusBarToolButton::GroupRight);
97
 
        } else {
98
 
                d->mActualSizeButton->setGroupPosition(StatusBarToolButton::GroupLeft);
99
 
                d->mZoomToFitButton->setGroupPosition(StatusBarToolButton::GroupRight);
100
 
        }
101
 
 
102
 
        d->mZoomLabel = new QLabel;
103
 
        d->mZoomLabel->setFixedWidth(d->mZoomLabel->fontMetrics().width(" 1000% "));
104
 
        d->mZoomLabel->setAlignment(Qt::AlignCenter);
105
 
 
106
 
        d->mZoomSlider = new ZoomSlider;
107
 
        d->mZoomSlider->setMinimumWidth(150);
108
 
        d->mZoomSlider->slider()->setSingleStep(int(PRECISION));
109
 
        d->mZoomSlider->slider()->setPageStep(3 * int(PRECISION));
110
 
        connect(d->mZoomSlider->slider(), SIGNAL(actionTriggered(int)), SLOT(slotZoomSliderActionTriggered()) );
111
 
 
112
 
        // Layout
113
 
        QHBoxLayout* layout = new QHBoxLayout(this);
114
 
        layout->setMargin(0);
115
 
        layout->setSpacing(0);
116
 
        layout->addWidget(d->mZoomToFitButton);
117
 
        layout->addWidget(d->mActualSizeButton);
118
 
        layout->addWidget(d->mZoomSlider);
119
 
        layout->addWidget(d->mZoomLabel);
120
 
}
121
 
 
122
 
 
123
 
ZoomWidget::~ZoomWidget() {
124
 
        delete d;
125
 
}
126
 
 
127
 
 
128
 
void ZoomWidget::setActions(QAction* zoomToFitAction, QAction* actualSizeAction, QAction* zoomInAction, QAction* zoomOutAction) {
129
 
        d->mZoomToFitAction = zoomToFitAction;
130
 
        d->mActualSizeAction = actualSizeAction;
131
 
 
132
 
        d->mZoomToFitButton->setDefaultAction(zoomToFitAction);
133
 
        d->mActualSizeButton->setDefaultAction(actualSizeAction);
134
 
 
135
 
        d->mZoomSlider->setZoomInAction(zoomInAction);
136
 
        d->mZoomSlider->setZoomOutAction(zoomOutAction);
137
 
 
138
 
        // Adjust sizes
139
 
        int width = qMax(d->mZoomToFitButton->sizeHint().width(), d->mActualSizeButton->sizeHint().width());
140
 
        d->mZoomToFitButton->setFixedWidth(width);
141
 
        d->mActualSizeButton->setFixedWidth(width);
142
 
}
143
 
 
144
 
 
145
 
void ZoomWidget::slotZoomSliderActionTriggered() {
146
 
        // The slider value changed because of the user (not because of range
147
 
        // changes). In this case disable zoom and apply slider value.
148
 
        d->emitZoomChanged();
149
 
}
150
 
 
151
 
 
152
 
void ZoomWidget::setZoom(qreal zoom) {
153
 
        int intZoom = qRound(zoom * 100);
154
 
        d->mZoomLabel->setText(QString("%1%").arg(intZoom));
155
 
 
156
 
        // Don't change slider value if we come here because the slider change,
157
 
        // avoids choppy sliding scroll.
158
 
        if (!d->mZoomUpdatedBySlider) {
159
 
                QSlider* slider = d->mZoomSlider->slider();
160
 
                SignalBlocker blocker(slider);
161
 
                int value = sliderValueForZoom(zoom);
162
 
 
163
 
                if (value < slider->minimum()) {
164
 
                        // It is possible that we are called *before* setMinimumZoom() as
165
 
                        // been called. In this case, define the minimum ourself.
166
 
                        d->mZoomSlider->setMinimum(value);
167
 
                }
168
 
                d->mZoomSlider->setValue(value);
169
 
        }
170
 
}
171
 
 
172
 
 
173
 
void ZoomWidget::setMinimumZoom(qreal minimumZoom) {
174
 
        d->mZoomSlider->setMinimum(sliderValueForZoom(minimumZoom));
175
 
}
176
 
 
177
 
 
178
 
void ZoomWidget::setMaximumZoom(qreal zoom) {
179
 
        d->mZoomSlider->setMaximum(sliderValueForZoom(zoom));
180
 
}
181
 
 
 
84
, d(new ZoomWidgetPrivate)
 
85
{
 
86
    d->q = this;
 
87
    d->mZoomUpdatedBySlider = false;
 
88
 
 
89
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
 
90
 
 
91
    d->mZoomToFitButton = new StatusBarToolButton;
 
92
    d->mActualSizeButton = new StatusBarToolButton;
 
93
 
 
94
    if (QApplication::isLeftToRight()) {
 
95
        d->mZoomToFitButton->setGroupPosition(StatusBarToolButton::GroupLeft);
 
96
        d->mActualSizeButton->setGroupPosition(StatusBarToolButton::GroupRight);
 
97
    } else {
 
98
        d->mActualSizeButton->setGroupPosition(StatusBarToolButton::GroupLeft);
 
99
        d->mZoomToFitButton->setGroupPosition(StatusBarToolButton::GroupRight);
 
100
    }
 
101
 
 
102
    d->mZoomLabel = new QLabel;
 
103
    d->mZoomLabel->setFixedWidth(d->mZoomLabel->fontMetrics().width(" 1000% "));
 
104
    d->mZoomLabel->setAlignment(Qt::AlignCenter);
 
105
 
 
106
    d->mZoomSlider = new ZoomSlider;
 
107
    d->mZoomSlider->setMinimumWidth(150);
 
108
    d->mZoomSlider->slider()->setSingleStep(int(PRECISION));
 
109
    d->mZoomSlider->slider()->setPageStep(3 * int(PRECISION));
 
110
    connect(d->mZoomSlider->slider(), SIGNAL(actionTriggered(int)), SLOT(slotZoomSliderActionTriggered()));
 
111
 
 
112
    // Layout
 
113
    QHBoxLayout* layout = new QHBoxLayout(this);
 
114
    layout->setMargin(0);
 
115
    layout->setSpacing(0);
 
116
    layout->addWidget(d->mZoomToFitButton);
 
117
    layout->addWidget(d->mActualSizeButton);
 
118
    layout->addWidget(d->mZoomSlider);
 
119
    layout->addWidget(d->mZoomLabel);
 
120
}
 
121
 
 
122
ZoomWidget::~ZoomWidget()
 
123
{
 
124
    delete d;
 
125
}
 
126
 
 
127
void ZoomWidget::setActions(QAction* zoomToFitAction, QAction* actualSizeAction, QAction* zoomInAction, QAction* zoomOutAction)
 
128
{
 
129
    d->mZoomToFitAction = zoomToFitAction;
 
130
    d->mActualSizeAction = actualSizeAction;
 
131
 
 
132
    d->mZoomToFitButton->setDefaultAction(zoomToFitAction);
 
133
    d->mActualSizeButton->setDefaultAction(actualSizeAction);
 
134
 
 
135
    d->mZoomSlider->setZoomInAction(zoomInAction);
 
136
    d->mZoomSlider->setZoomOutAction(zoomOutAction);
 
137
 
 
138
    // Adjust sizes
 
139
    int width = qMax(d->mZoomToFitButton->sizeHint().width(), d->mActualSizeButton->sizeHint().width());
 
140
    d->mZoomToFitButton->setFixedWidth(width);
 
141
    d->mActualSizeButton->setFixedWidth(width);
 
142
}
 
143
 
 
144
void ZoomWidget::slotZoomSliderActionTriggered()
 
145
{
 
146
    // The slider value changed because of the user (not because of range
 
147
    // changes). In this case disable zoom and apply slider value.
 
148
    d->emitZoomChanged();
 
149
}
 
150
 
 
151
void ZoomWidget::setZoom(qreal zoom)
 
152
{
 
153
    int intZoom = qRound(zoom * 100);
 
154
    d->mZoomLabel->setText(QString("%1%").arg(intZoom));
 
155
 
 
156
    // Don't change slider value if we come here because the slider change,
 
157
    // avoids choppy sliding scroll.
 
158
    if (!d->mZoomUpdatedBySlider) {
 
159
        QSlider* slider = d->mZoomSlider->slider();
 
160
        SignalBlocker blocker(slider);
 
161
        int value = sliderValueForZoom(zoom);
 
162
 
 
163
        if (value < slider->minimum()) {
 
164
            // It is possible that we are called *before* setMinimumZoom() as
 
165
            // been called. In this case, define the minimum ourself.
 
166
            d->mZoomSlider->setMinimum(value);
 
167
        }
 
168
        d->mZoomSlider->setValue(value);
 
169
    }
 
170
}
 
171
 
 
172
void ZoomWidget::setMinimumZoom(qreal minimumZoom)
 
173
{
 
174
    d->mZoomSlider->setMinimum(sliderValueForZoom(minimumZoom));
 
175
}
 
176
 
 
177
void ZoomWidget::setMaximumZoom(qreal zoom)
 
178
{
 
179
    d->mZoomSlider->setMaximum(sliderValueForZoom(zoom));
 
180
}
182
181
 
183
182
} // namespace