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

« back to all changes in this revision

Viewing changes to lib/print/printoptionspage.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>
34
34
#include <signalblocker.h>
35
35
#include <ui_printoptionspage.h>
36
36
 
37
 
namespace Gwenview {
38
 
 
39
 
 
40
 
static inline double unitToInches(PrintOptionsPage::Unit unit) {
41
 
        if (unit == PrintOptionsPage::Inches) {
42
 
                return 1.;
43
 
        } else if (unit == PrintOptionsPage::Centimeters) {
44
 
                return 1/2.54;
45
 
        } else { // Millimeters
46
 
                return 1/25.4;
47
 
        }
 
37
namespace Gwenview
 
38
{
 
39
 
 
40
static inline double unitToInches(PrintOptionsPage::Unit unit)
 
41
{
 
42
    if (unit == PrintOptionsPage::Inches) {
 
43
        return 1.;
 
44
    } else if (unit == PrintOptionsPage::Centimeters) {
 
45
        return 1 / 2.54;
 
46
    } else { // Millimeters
 
47
        return 1 / 25.4;
 
48
    }
48
49
}
49
50
 
50
 
 
51
51
struct PrintOptionsPagePrivate : public Ui_PrintOptionsPage {
52
 
        QSize mImageSize;
53
 
        QButtonGroup mScaleGroup;
54
 
        QButtonGroup mPositionGroup;
55
 
        KConfigDialogManager* mConfigDialogManager;
56
 
 
57
 
        void initPositionFrame() {
58
 
                mPositionFrame->setStyleSheet(
59
 
                        "QFrame {"
60
 
                        "       background-color: palette(mid);"
61
 
                        "       border: 1px solid palette(dark);"
62
 
                        "}"
63
 
                        "QToolButton {"
64
 
                        "       border: none;"
65
 
                        "       background: palette(base);"
66
 
                        "}"
67
 
                        "QToolButton:hover {"
68
 
                        "       background: palette(alternate-base);"
69
 
                        "       border: 1px solid palette(highlight);"
70
 
                        "}"
71
 
                        "QToolButton:checked {"
72
 
                        "       background-color: palette(highlight);"
73
 
                        "}"
74
 
                        );
75
 
 
76
 
                QGridLayout* layout = new QGridLayout(mPositionFrame);
77
 
                layout->setMargin(0);
78
 
                layout->setSpacing(1);
79
 
                for (int row = 0; row < 3; ++row) {
80
 
                        for (int col = 0; col < 3; ++col) {
81
 
                                QToolButton* button = new QToolButton(mPositionFrame);
82
 
                                button->setFixedSize(40, 40);
83
 
                                button->setCheckable(true);
84
 
                                layout->addWidget(button, row, col);
85
 
 
86
 
                                Qt::Alignment alignment;
87
 
                                if (row == 0) {
88
 
                                        alignment = Qt::AlignTop;
89
 
                                } else if (row == 1) {
90
 
                                        alignment = Qt::AlignVCenter;
91
 
                                } else {
92
 
                                        alignment = Qt::AlignBottom;
93
 
                                }
94
 
                                if (col == 0) {
95
 
                                        alignment |= Qt::AlignLeft;
96
 
                                } else if (col == 1) {
97
 
                                        alignment |= Qt::AlignHCenter;
98
 
                                } else {
99
 
                                        alignment |= Qt::AlignRight;
100
 
                                }
101
 
 
102
 
                                mPositionGroup.addButton(button, int(alignment));
103
 
                        }
104
 
                }
105
 
        }
 
52
    QSize mImageSize;
 
53
    QButtonGroup mScaleGroup;
 
54
    QButtonGroup mPositionGroup;
 
55
    KConfigDialogManager* mConfigDialogManager;
 
56
 
 
57
    void initPositionFrame()
 
58
    {
 
59
        mPositionFrame->setStyleSheet(
 
60
            "QFrame {"
 
61
            "   background-color: palette(mid);"
 
62
            "   border: 1px solid palette(dark);"
 
63
            "}"
 
64
            "QToolButton {"
 
65
            "   border: none;"
 
66
            "   background: palette(base);"
 
67
            "}"
 
68
            "QToolButton:hover {"
 
69
            "   background: palette(alternate-base);"
 
70
            "   border: 1px solid palette(highlight);"
 
71
            "}"
 
72
            "QToolButton:checked {"
 
73
            "   background-color: palette(highlight);"
 
74
            "}"
 
75
        );
 
76
 
 
77
        QGridLayout* layout = new QGridLayout(mPositionFrame);
 
78
        layout->setMargin(0);
 
79
        layout->setSpacing(1);
 
80
        for (int row = 0; row < 3; ++row) {
 
81
            for (int col = 0; col < 3; ++col) {
 
82
                QToolButton* button = new QToolButton(mPositionFrame);
 
83
                button->setFixedSize(40, 40);
 
84
                button->setCheckable(true);
 
85
                layout->addWidget(button, row, col);
 
86
 
 
87
                Qt::Alignment alignment;
 
88
                if (row == 0) {
 
89
                    alignment = Qt::AlignTop;
 
90
                } else if (row == 1) {
 
91
                    alignment = Qt::AlignVCenter;
 
92
                } else {
 
93
                    alignment = Qt::AlignBottom;
 
94
                }
 
95
                if (col == 0) {
 
96
                    alignment |= Qt::AlignLeft;
 
97
                } else if (col == 1) {
 
98
                    alignment |= Qt::AlignHCenter;
 
99
                } else {
 
100
                    alignment |= Qt::AlignRight;
 
101
                }
 
102
 
 
103
                mPositionGroup.addButton(button, int(alignment));
 
104
            }
 
105
        }
 
106
    }
106
107
};
107
108
 
108
 
 
109
109
PrintOptionsPage::PrintOptionsPage(const QSize& imageSize)
110
110
: QWidget()
111
 
, d(new PrintOptionsPagePrivate) {
112
 
        d->setupUi(this);
113
 
        d->mImageSize = imageSize;
114
 
        d->mConfigDialogManager = new KConfigDialogManager(this, GwenviewConfig::self());
115
 
 
116
 
        d->initPositionFrame();
117
 
 
118
 
        d->mScaleGroup.addButton(d->mNoScale, NoScale);
119
 
        d->mScaleGroup.addButton(d->mScaleToPage, ScaleToPage);
120
 
        d->mScaleGroup.addButton(d->mScaleTo, ScaleToCustomSize);
121
 
 
122
 
        connect(d->kcfg_PrintWidth, SIGNAL(valueChanged(double)),
123
 
                SLOT(adjustHeightToRatio()) );
124
 
 
125
 
        connect(d->kcfg_PrintHeight, SIGNAL(valueChanged(double)),
126
 
                SLOT(adjustWidthToRatio()) );
127
 
 
128
 
        connect(d->kcfg_PrintKeepRatio, SIGNAL(toggled(bool)),
129
 
                SLOT(adjustHeightToRatio()) );
130
 
}
131
 
 
132
 
 
133
 
PrintOptionsPage::~PrintOptionsPage() {
134
 
        delete d;
135
 
}
136
 
 
137
 
 
138
 
Qt::Alignment PrintOptionsPage::alignment() const {
139
 
        int id = d->mPositionGroup.checkedId();
140
 
        kWarning() << "alignment=" << id;
141
 
        return Qt::Alignment(id);
142
 
}
143
 
 
144
 
 
145
 
PrintOptionsPage::ScaleMode PrintOptionsPage::scaleMode() const {
146
 
        return PrintOptionsPage::ScaleMode( d->mScaleGroup.checkedId() );
147
 
}
148
 
 
149
 
 
150
 
bool PrintOptionsPage::enlargeSmallerImages() const {
151
 
        return d->kcfg_PrintEnlargeSmallerImages->isChecked();
152
 
}
153
 
 
154
 
 
155
 
PrintOptionsPage::Unit PrintOptionsPage::scaleUnit() const {
156
 
        return PrintOptionsPage::Unit(d->kcfg_PrintUnit->currentIndex());
157
 
}
158
 
 
159
 
 
160
 
double PrintOptionsPage::scaleWidth() const {
161
 
        return d->kcfg_PrintWidth->value() * unitToInches(scaleUnit());
162
 
}
163
 
 
164
 
 
165
 
double PrintOptionsPage::scaleHeight() const {
166
 
        return d->kcfg_PrintHeight->value() * unitToInches(scaleUnit());
167
 
}
168
 
 
169
 
 
170
 
void PrintOptionsPage::adjustWidthToRatio() {
171
 
        if (!d->kcfg_PrintKeepRatio->isChecked()) {
172
 
                return;
173
 
        }
174
 
        double width = d->mImageSize.width() * d->kcfg_PrintHeight->value() / d->mImageSize.height();
175
 
 
176
 
        SignalBlocker blocker(d->kcfg_PrintWidth);
177
 
        d->kcfg_PrintWidth->setValue(width ? width : 1.);
178
 
}
179
 
 
180
 
 
181
 
void PrintOptionsPage::adjustHeightToRatio() {
182
 
        if (!d->kcfg_PrintKeepRatio->isChecked()) {
183
 
                return;
184
 
        }
185
 
        double height = d->mImageSize.height() * d->kcfg_PrintWidth->value() / d->mImageSize.width();
186
 
 
187
 
        SignalBlocker blocker(d->kcfg_PrintHeight);
188
 
        d->kcfg_PrintHeight->setValue(height ? height : 1.);
189
 
}
190
 
 
191
 
 
192
 
void PrintOptionsPage::loadConfig() {
193
 
        QAbstractButton* button;
194
 
 
195
 
        button = d->mPositionGroup.button(GwenviewConfig::printPosition());
196
 
        if (button) {
197
 
                button->setChecked(true);
198
 
        } else {
199
 
                kWarning() << "Unknown button for position group";
200
 
        }
201
 
 
202
 
        button = d->mScaleGroup.button(GwenviewConfig::printScaleMode());
203
 
        if (button) {
204
 
                button->setChecked(true);
205
 
        } else {
206
 
                kWarning() << "Unknown button for scale group";
207
 
        }
208
 
 
209
 
        d->mConfigDialogManager->updateWidgets();
210
 
 
211
 
        if (d->kcfg_PrintKeepRatio->isChecked()) {
212
 
                adjustHeightToRatio();
213
 
        }
214
 
}
215
 
 
216
 
 
217
 
void PrintOptionsPage::saveConfig() {
218
 
        int position = d->mPositionGroup.checkedId();
219
 
        GwenviewConfig::setPrintPosition(position);
220
 
 
221
 
        ScaleMode scaleMode = ScaleMode( d->mScaleGroup.checkedId() );
222
 
        GwenviewConfig::setPrintScaleMode(scaleMode);
223
 
 
224
 
        d->mConfigDialogManager->updateSettings();
225
 
 
226
 
        GwenviewConfig::self()->writeConfig();
227
 
}
228
 
 
 
111
, d(new PrintOptionsPagePrivate)
 
112
{
 
113
    d->setupUi(this);
 
114
    d->mImageSize = imageSize;
 
115
    d->mConfigDialogManager = new KConfigDialogManager(this, GwenviewConfig::self());
 
116
 
 
117
    d->initPositionFrame();
 
118
 
 
119
    d->mScaleGroup.addButton(d->mNoScale, NoScale);
 
120
    d->mScaleGroup.addButton(d->mScaleToPage, ScaleToPage);
 
121
    d->mScaleGroup.addButton(d->mScaleTo, ScaleToCustomSize);
 
122
 
 
123
    connect(d->kcfg_PrintWidth, SIGNAL(valueChanged(double)),
 
124
            SLOT(adjustHeightToRatio()));
 
125
 
 
126
    connect(d->kcfg_PrintHeight, SIGNAL(valueChanged(double)),
 
127
            SLOT(adjustWidthToRatio()));
 
128
 
 
129
    connect(d->kcfg_PrintKeepRatio, SIGNAL(toggled(bool)),
 
130
            SLOT(adjustHeightToRatio()));
 
131
}
 
132
 
 
133
PrintOptionsPage::~PrintOptionsPage()
 
134
{
 
135
    delete d;
 
136
}
 
137
 
 
138
Qt::Alignment PrintOptionsPage::alignment() const
 
139
{
 
140
    int id = d->mPositionGroup.checkedId();
 
141
    kWarning() << "alignment=" << id;
 
142
    return Qt::Alignment(id);
 
143
}
 
144
 
 
145
PrintOptionsPage::ScaleMode PrintOptionsPage::scaleMode() const
 
146
{
 
147
    return PrintOptionsPage::ScaleMode(d->mScaleGroup.checkedId());
 
148
}
 
149
 
 
150
bool PrintOptionsPage::enlargeSmallerImages() const
 
151
{
 
152
    return d->kcfg_PrintEnlargeSmallerImages->isChecked();
 
153
}
 
154
 
 
155
PrintOptionsPage::Unit PrintOptionsPage::scaleUnit() const
 
156
{
 
157
    return PrintOptionsPage::Unit(d->kcfg_PrintUnit->currentIndex());
 
158
}
 
159
 
 
160
double PrintOptionsPage::scaleWidth() const
 
161
{
 
162
    return d->kcfg_PrintWidth->value() * unitToInches(scaleUnit());
 
163
}
 
164
 
 
165
double PrintOptionsPage::scaleHeight() const
 
166
{
 
167
    return d->kcfg_PrintHeight->value() * unitToInches(scaleUnit());
 
168
}
 
169
 
 
170
void PrintOptionsPage::adjustWidthToRatio()
 
171
{
 
172
    if (!d->kcfg_PrintKeepRatio->isChecked()) {
 
173
        return;
 
174
    }
 
175
    double width = d->mImageSize.width() * d->kcfg_PrintHeight->value() / d->mImageSize.height();
 
176
 
 
177
    SignalBlocker blocker(d->kcfg_PrintWidth);
 
178
    d->kcfg_PrintWidth->setValue(width ? width : 1.);
 
179
}
 
180
 
 
181
void PrintOptionsPage::adjustHeightToRatio()
 
182
{
 
183
    if (!d->kcfg_PrintKeepRatio->isChecked()) {
 
184
        return;
 
185
    }
 
186
    double height = d->mImageSize.height() * d->kcfg_PrintWidth->value() / d->mImageSize.width();
 
187
 
 
188
    SignalBlocker blocker(d->kcfg_PrintHeight);
 
189
    d->kcfg_PrintHeight->setValue(height ? height : 1.);
 
190
}
 
191
 
 
192
void PrintOptionsPage::loadConfig()
 
193
{
 
194
    QAbstractButton* button;
 
195
 
 
196
    button = d->mPositionGroup.button(GwenviewConfig::printPosition());
 
197
    if (button) {
 
198
        button->setChecked(true);
 
199
    } else {
 
200
        kWarning() << "Unknown button for position group";
 
201
    }
 
202
 
 
203
    button = d->mScaleGroup.button(GwenviewConfig::printScaleMode());
 
204
    if (button) {
 
205
        button->setChecked(true);
 
206
    } else {
 
207
        kWarning() << "Unknown button for scale group";
 
208
    }
 
209
 
 
210
    d->mConfigDialogManager->updateWidgets();
 
211
 
 
212
    if (d->kcfg_PrintKeepRatio->isChecked()) {
 
213
        adjustHeightToRatio();
 
214
    }
 
215
}
 
216
 
 
217
void PrintOptionsPage::saveConfig()
 
218
{
 
219
    int position = d->mPositionGroup.checkedId();
 
220
    GwenviewConfig::setPrintPosition(position);
 
221
 
 
222
    ScaleMode scaleMode = ScaleMode(d->mScaleGroup.checkedId());
 
223
    GwenviewConfig::setPrintScaleMode(scaleMode);
 
224
 
 
225
    d->mConfigDialogManager->updateSettings();
 
226
 
 
227
    GwenviewConfig::self()->writeConfig();
 
228
}
229
229
 
230
230
} // namespace