~ubuntu-branches/ubuntu/trusty/gwenview/trusty

« back to all changes in this revision

Viewing changes to lib/print/printoptionspage.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rohan Garg
  • Date: 2011-07-20 13:46:34 UTC
  • Revision ID: james.westby@ubuntu.com-20110720134634-92930fdjeed4gdc9
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
2
/*
 
3
Gwenview: an image viewer
 
4
Copyright 2007 Aurélien Gâteau <agateau@kde.org>
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 2
 
9
of the License, or (at your option) any later version.
 
10
 
 
11
This program is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with this program; if not, write to the Free Software
 
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
19
 
 
20
*/
 
21
// Self
 
22
#include "printoptionspage.moc"
 
23
 
 
24
// Qt
 
25
#include <QButtonGroup>
 
26
#include <QGridLayout>
 
27
#include <QToolButton>
 
28
 
 
29
// KDE
 
30
#include <kconfigdialogmanager.h>
 
31
 
 
32
// Local
 
33
#include <gwenviewconfig.h>
 
34
#include <signalblocker.h>
 
35
#include <ui_printoptionspage.h>
 
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
        }
 
48
}
 
49
 
 
50
 
 
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
        }
 
106
};
 
107
 
 
108
 
 
109
PrintOptionsPage::PrintOptionsPage(const QSize& imageSize)
 
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
 
 
229
 
 
230
} // namespace