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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// vim: set tabstop=4 shiftwidth=4 expandtab:
/*
Gwenview: an image viewer
Copyright 2007 Aurélien Gâteau <agateau@kde.org>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

*/
// Self
#include "printoptionspage.moc"

// Qt
#include <QButtonGroup>
#include <QGridLayout>
#include <QToolButton>

// KDE
#include <kconfigdialogmanager.h>

// Local
#include <gwenviewconfig.h>
#include <signalblocker.h>
#include <ui_printoptionspage.h>

namespace Gwenview
{

static inline double unitToInches(PrintOptionsPage::Unit unit)
{
    if (unit == PrintOptionsPage::Inches) {
        return 1.;
    } else if (unit == PrintOptionsPage::Centimeters) {
        return 1 / 2.54;
    } else { // Millimeters
        return 1 / 25.4;
    }
}

struct PrintOptionsPagePrivate : public Ui_PrintOptionsPage {
    QSize mImageSize;
    QButtonGroup mScaleGroup;
    QButtonGroup mPositionGroup;
    KConfigDialogManager* mConfigDialogManager;

    void initPositionFrame()
    {
        mPositionFrame->setStyleSheet(
            "QFrame {"
            "	background-color: palette(mid);"
            "	border: 1px solid palette(dark);"
            "}"
            "QToolButton {"
            "	border: none;"
            "	background: palette(base);"
            "}"
            "QToolButton:hover {"
            "	background: palette(alternate-base);"
            "	border: 1px solid palette(highlight);"
            "}"
            "QToolButton:checked {"
            "	background-color: palette(highlight);"
            "}"
        );

        QGridLayout* layout = new QGridLayout(mPositionFrame);
        layout->setMargin(0);
        layout->setSpacing(1);
        for (int row = 0; row < 3; ++row) {
            for (int col = 0; col < 3; ++col) {
                QToolButton* button = new QToolButton(mPositionFrame);
                button->setFixedSize(40, 40);
                button->setCheckable(true);
                layout->addWidget(button, row, col);

                Qt::Alignment alignment;
                if (row == 0) {
                    alignment = Qt::AlignTop;
                } else if (row == 1) {
                    alignment = Qt::AlignVCenter;
                } else {
                    alignment = Qt::AlignBottom;
                }
                if (col == 0) {
                    alignment |= Qt::AlignLeft;
                } else if (col == 1) {
                    alignment |= Qt::AlignHCenter;
                } else {
                    alignment |= Qt::AlignRight;
                }

                mPositionGroup.addButton(button, int(alignment));
            }
        }
    }
};

PrintOptionsPage::PrintOptionsPage(const QSize& imageSize)
: QWidget()
, d(new PrintOptionsPagePrivate)
{
    d->setupUi(this);
    d->mImageSize = imageSize;
    d->mConfigDialogManager = new KConfigDialogManager(this, GwenviewConfig::self());

    d->initPositionFrame();

    d->mScaleGroup.addButton(d->mNoScale, NoScale);
    d->mScaleGroup.addButton(d->mScaleToPage, ScaleToPage);
    d->mScaleGroup.addButton(d->mScaleTo, ScaleToCustomSize);

    connect(d->kcfg_PrintWidth, SIGNAL(valueChanged(double)),
            SLOT(adjustHeightToRatio()));

    connect(d->kcfg_PrintHeight, SIGNAL(valueChanged(double)),
            SLOT(adjustWidthToRatio()));

    connect(d->kcfg_PrintKeepRatio, SIGNAL(toggled(bool)),
            SLOT(adjustHeightToRatio()));
}

PrintOptionsPage::~PrintOptionsPage()
{
    delete d;
}

Qt::Alignment PrintOptionsPage::alignment() const
{
    int id = d->mPositionGroup.checkedId();
    kWarning() << "alignment=" << id;
    return Qt::Alignment(id);
}

PrintOptionsPage::ScaleMode PrintOptionsPage::scaleMode() const
{
    return PrintOptionsPage::ScaleMode(d->mScaleGroup.checkedId());
}

bool PrintOptionsPage::enlargeSmallerImages() const
{
    return d->kcfg_PrintEnlargeSmallerImages->isChecked();
}

PrintOptionsPage::Unit PrintOptionsPage::scaleUnit() const
{
    return PrintOptionsPage::Unit(d->kcfg_PrintUnit->currentIndex());
}

double PrintOptionsPage::scaleWidth() const
{
    return d->kcfg_PrintWidth->value() * unitToInches(scaleUnit());
}

double PrintOptionsPage::scaleHeight() const
{
    return d->kcfg_PrintHeight->value() * unitToInches(scaleUnit());
}

void PrintOptionsPage::adjustWidthToRatio()
{
    if (!d->kcfg_PrintKeepRatio->isChecked()) {
        return;
    }
    double width = d->mImageSize.width() * d->kcfg_PrintHeight->value() / d->mImageSize.height();

    SignalBlocker blocker(d->kcfg_PrintWidth);
    d->kcfg_PrintWidth->setValue(width ? width : 1.);
}

void PrintOptionsPage::adjustHeightToRatio()
{
    if (!d->kcfg_PrintKeepRatio->isChecked()) {
        return;
    }
    double height = d->mImageSize.height() * d->kcfg_PrintWidth->value() / d->mImageSize.width();

    SignalBlocker blocker(d->kcfg_PrintHeight);
    d->kcfg_PrintHeight->setValue(height ? height : 1.);
}

void PrintOptionsPage::loadConfig()
{
    QAbstractButton* button;

    button = d->mPositionGroup.button(GwenviewConfig::printPosition());
    if (button) {
        button->setChecked(true);
    } else {
        kWarning() << "Unknown button for position group";
    }

    button = d->mScaleGroup.button(GwenviewConfig::printScaleMode());
    if (button) {
        button->setChecked(true);
    } else {
        kWarning() << "Unknown button for scale group";
    }

    d->mConfigDialogManager->updateWidgets();

    if (d->kcfg_PrintKeepRatio->isChecked()) {
        adjustHeightToRatio();
    }
}

void PrintOptionsPage::saveConfig()
{
    int position = d->mPositionGroup.checkedId();
    GwenviewConfig::setPrintPosition(position);

    ScaleMode scaleMode = ScaleMode(d->mScaleGroup.checkedId());
    GwenviewConfig::setPrintScaleMode(scaleMode);

    d->mConfigDialogManager->updateSettings();

    GwenviewConfig::self()->writeConfig();
}

} // namespace