File: KoPageLayoutWidget.cpp

package info (click to toggle)
calligra 1:3.1.0+dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 308,444 kB
  • sloc: cpp: 644,218; xml: 27,515; python: 6,058; perl: 2,724; yacc: 1,817; ansic: 1,310; sh: 1,247; lex: 1,107; ruby: 1,018; makefile: 34
file content (350 lines) | stat: -rw-r--r-- 12,172 bytes parent folder | download
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* This file is part of the KDE project
 * Copyright (C) 2007, 2010 Thomas Zander <zander@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "KoPageLayoutWidget.h"

#include <ui_KoPageLayoutWidget.h>

#include <KoUnit.h>

#include <QButtonGroup>

class Q_DECL_HIDDEN KoPageLayoutWidget::Private
{
public:
    Ui::KoPageLayoutWidget widget;
    KoPageLayout pageLayout;
    KoUnit unit;

    QButtonGroup *orientationGroup;
    bool marginsEnabled;
    bool allowSignals;
};


KoPageLayoutWidget::KoPageLayoutWidget(QWidget *parent, const KoPageLayout &layout)
    : QWidget(parent)
    , d(new Private)
{
    d->widget.setupUi(this);

    d->pageLayout = layout;
    d->marginsEnabled = true;
    d->allowSignals = true;
    d->orientationGroup = new QButtonGroup(this);
    d->orientationGroup->addButton(d->widget.portrait, KoPageFormat::Portrait);
    d->orientationGroup->addButton(d->widget.landscape, KoPageFormat::Landscape);

    QButtonGroup *group2 = new QButtonGroup(this);
    group2->addButton(d->widget.singleSided);
    group2->addButton(d->widget.facingPages);
    // the two sets of labels we use might have different lengths; make sure this does not create a 'jumping' ui
    d->widget.facingPages->setChecked(true);
    facingPagesChanged();
    int width = qMax(d->widget.leftLabel->width(), d->widget.rightLabel->width());
    d->widget.singleSided->setChecked(true);
    facingPagesChanged();
    width = qMax(width, qMax(d->widget.leftLabel->width(), d->widget.rightLabel->width()));
    d->widget.leftLabel->setMinimumSize(QSize(width, 5));

    d->widget.units->addItems(KoUnit::listOfUnitNameForUi(KoUnit::HidePixel));
    d->widget.sizes->addItems(KoPageFormat::localizedPageFormatNames());
    setPageSpread(false);

    connect(d->widget.sizes, SIGNAL(currentIndexChanged(int)), this, SLOT(sizeChanged(int)));
    connect(d->widget.units, SIGNAL(currentIndexChanged(int)), this, SLOT(unitChanged(int)));
    connect(group2, SIGNAL(buttonClicked(int)), this, SLOT(facingPagesChanged()));
    connect(d->orientationGroup, SIGNAL(buttonClicked(int)), this, SLOT(orientationChanged()));
    connect(d->widget.width, SIGNAL(valueChangedPt(qreal)), this, SLOT(optionsChanged()));
    connect(d->widget.height, SIGNAL(valueChangedPt(qreal)), this, SLOT(optionsChanged()));
    connect(d->widget.topMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(marginsChanged()));
    connect(d->widget.bottomMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(marginsChanged()));
    connect(d->widget.bindingEdgeMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(marginsChanged()));
    connect(d->widget.pageEdgeMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(marginsChanged()));
    connect(d->widget.width, SIGNAL(valueChangedPt(qreal)), this, SLOT(optionsChanged()));
    connect(d->widget.height, SIGNAL(valueChangedPt(qreal)), this, SLOT(optionsChanged()));

    setUnit(KoUnit(KoUnit::Millimeter));
    setPageLayout(layout);
    if (layout.format == 0) // make sure we always call this during startup, even if the A3 (index=0) was chosen
        sizeChanged(layout.format);

    showTextDirection(false);
    /* disable advanced page layout features by default */
    d->widget.facingPageLabel->setVisible(false);
    d->widget.facingPages->setVisible(false);
    d->widget.singleSided->setVisible(false);
    d->widget.stylesLabel->setVisible(false);
    d->widget.pageStyle->setVisible(false);
}

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

KoPageLayout KoPageLayoutWidget::pageLayout() const
{
    return d->pageLayout;
}

void KoPageLayoutWidget::sizeChanged(int row)
{
    if (row < 0) return;
    if (! d->allowSignals) return;
    d->allowSignals = false;
    d->pageLayout.format = static_cast<KoPageFormat::Format> (row);
    bool custom =  d->pageLayout.format == KoPageFormat::CustomSize;
    d->widget.width->setEnabled( custom );
    d->widget.height->setEnabled( custom );

    if ( !custom ) {
        d->pageLayout.width = MM_TO_POINT( KoPageFormat::width( d->pageLayout.format, d->pageLayout.orientation ) );
        d->pageLayout.height = MM_TO_POINT( KoPageFormat::height( d->pageLayout.format, d->pageLayout.orientation ) );
        if (d->widget.facingPages->isChecked()) // is pagespread
            d->pageLayout.width *= 2;
    }

    d->widget.width->changeValue( d->pageLayout.width );
    d->widget.height->changeValue( d->pageLayout.height );

    emit layoutChanged(d->pageLayout);
    d->allowSignals = true;
}

void KoPageLayoutWidget::unitChanged(int row)
{
    setUnit(KoUnit::fromListForUi(row, KoUnit::HidePixel));
}

void KoPageLayoutWidget::setUnit(const KoUnit &unit)
{
    if (d->unit == unit)
        return;
    d->unit = unit;

    d->widget.width->setUnit(unit);
    d->widget.height->setUnit(unit);
    d->widget.topMargin->setUnit(unit);
    d->widget.bottomMargin->setUnit(unit);
    d->widget.bindingEdgeMargin->setUnit(unit);
    d->widget.pageEdgeMargin->setUnit(unit);
    d->widget.units->setCurrentIndex(unit.indexInListForUi(KoUnit::HidePixel));

    emit unitChanged(d->unit);
}

void KoPageLayoutWidget::setPageLayout(const KoPageLayout &layout)
{
    if (! d->allowSignals) return;
    d->allowSignals = false;
    d->pageLayout = layout;

    Q_ASSERT(d->orientationGroup->button( layout.orientation ));
    d->orientationGroup->button( layout.orientation )->setChecked( true );
    if (layout.bindingSide >= 0 && layout.pageEdge >= 0) {
        d->widget.facingPages->setChecked(true);
        d->widget.bindingEdgeMargin->changeValue(layout.bindingSide);
        d->widget.pageEdgeMargin->changeValue(layout.pageEdge);
        d->pageLayout.leftMargin = -1;
        d->pageLayout.rightMargin = -1;
    }
    else {
        d->widget.singleSided->setChecked(true);
        d->widget.bindingEdgeMargin->changeValue(layout.leftMargin);
        d->widget.pageEdgeMargin->changeValue(layout.rightMargin);
        d->pageLayout.pageEdge = -1;
        d->pageLayout.bindingSide = -1;
    }
    facingPagesChanged();

    d->widget.topMargin->changeValue(layout.topMargin);
    d->widget.bottomMargin->changeValue(layout.bottomMargin);
    d->allowSignals = true;
    d->widget.sizes->setCurrentIndex(layout.format); // calls sizeChanged()
}

void KoPageLayoutWidget::facingPagesChanged()
{
    if (! d->allowSignals) return;
    d->allowSignals = false;
    if (d->widget.singleSided->isChecked()) {
        d->widget.leftLabel->setText(i18n("Left Edge:"));
        d->widget.rightLabel->setText(i18n("Right Edge:"));
    }
    else {
        d->widget.leftLabel->setText(i18n("Binding Edge:"));
        d->widget.rightLabel->setText(i18n("Page Edge:"));
    }
    d->allowSignals = true;
    marginsChanged();
    sizeChanged(d->widget.sizes->currentIndex());
}

void KoPageLayoutWidget::marginsChanged()
{
    if (! d->allowSignals) return;
    d->allowSignals = false;
    d->pageLayout.leftMargin = -1;
    d->pageLayout.rightMargin = -1;
    d->pageLayout.bindingSide = -1;
    d->pageLayout.pageEdge = -1;
    d->pageLayout.topMargin = d->marginsEnabled?d->widget.topMargin->value():0;
    d->pageLayout.bottomMargin = d->marginsEnabled?d->widget.bottomMargin->value():0;
    qreal left = d->marginsEnabled?d->widget.bindingEdgeMargin->value():0;
    qreal right = d->marginsEnabled?d->widget.pageEdgeMargin->value():0;
    if (left + right > d->pageLayout.width - 10) {
        // make sure the actual text area is never smaller than 10 points.
        qreal diff = d->pageLayout.width - 10 - left - right;
        left = qMin(d->pageLayout.width - 10, qMax(qreal(0.0), left - diff / qreal(2.0)));
        right = qMax(qreal(0.0), right - d->pageLayout.width - 10 - left);
    }

    if (d->widget.singleSided->isChecked()) {
        d->pageLayout.leftMargin  = left;
        d->pageLayout.rightMargin = right;
    }
    else {
        d->pageLayout.bindingSide = left;
        d->pageLayout.pageEdge = right;
    }
    // debugWidgets << "  " << d->pageLayout.left <<"|"<< d->pageLayout.bindingSide << "," <<
    //    d->pageLayout.right << "|"<< d->pageLayout.pageEdge;
    emit layoutChanged(d->pageLayout);
    d->allowSignals = true;
}

void KoPageLayoutWidget::setTextAreaAvailable(bool available)
{
    d->marginsEnabled = available;
    d->widget.margins->setEnabled(available);
    marginsChanged();
}

void KoPageLayoutWidget::optionsChanged()
{
    if (! d->allowSignals) return;
    if (d->widget.sizes->currentIndex() == KoPageFormat::CustomSize) {
        d->pageLayout.width = d->widget.width->value();
        d->pageLayout.height = d->widget.height->value();
    } else
        sizeChanged(d->widget.sizes->currentIndex());

    marginsChanged();
}

void KoPageLayoutWidget::orientationChanged()
{
    if (! d->allowSignals) return;
    d->allowSignals = false;
    d->pageLayout.orientation = d->widget.landscape->isChecked() ? KoPageFormat::Landscape : KoPageFormat::Portrait;

    qreal x = d->widget.height->value();
    d->widget.height->changeValue( d->widget.width->value() );
    d->widget.width->changeValue( x );

    d->allowSignals = true;
    optionsChanged();
}

void KoPageLayoutWidget::showUnitchooser(bool on) {
    d->widget.units->setVisible(on);
    d->widget.unitsLabel->setVisible(on);
}

void KoPageLayoutWidget::showPageSpread(bool on)
{
    d->widget.facingPageLabel->setVisible(on);
    d->widget.singleSided->setVisible(on);
    d->widget.facingPages->setVisible(on);
}

void KoPageLayoutWidget::setPageSpread(bool pageSpread)
{
    if (pageSpread)
        d->widget.facingPages->setChecked(true);
    else
        d->widget.singleSided->setChecked(true);
}

void KoPageLayoutWidget::setApplyToDocument(bool apply)
{
    if (apply) {
        d->widget.facingPageLabel->setText(i18n("Facing Pages:"));
        d->widget.facingPages->setText(i18n("Facing pages"));
    }
    else {
        d->widget.facingPageLabel->setText(i18n("Page Layout:"));
        d->widget.facingPages->setText(i18n("Page spread"));
    }
}

void KoPageLayoutWidget::showTextDirection(bool on)
{
    d->widget.directionLabel->setVisible(on);
    d->widget.textDirection->setVisible(on);
}

void KoPageLayoutWidget::setTextDirection(KoText::Direction direction )
{
    int index = 0;
    switch(direction) {
    case KoText::LeftRightTopBottom:
        index = 1;
        break;
    case KoText::RightLeftTopBottom:
        index = 2;
        break;
    case KoText::TopBottomRightLeft: // unused for now.
    case KoText::InheritDirection:
    case KoText::AutoDirection:
        index = 0;
    case KoText::TopBottomLeftRight:
        ; // unhandled, because it actually doesn't exist in real-world writing systems.
          // Boustrophedon would be interesting to implement, though
    }
    d->widget.textDirection->setCurrentIndex(index);
}

KoText::Direction KoPageLayoutWidget::textDirection() const
{
    switch(d->widget.textDirection->currentIndex()) {
    case 1: return KoText::LeftRightTopBottom;
    case 2: return KoText::RightLeftTopBottom;
    default:
    case 0: return KoText::AutoDirection;
    }
}

void KoPageLayoutWidget::showPageStyles(bool on)
{
    d->widget.stylesLabel->setVisible(on);
    d->widget.pageStyle->setVisible(on);
}

void KoPageLayoutWidget::setPageStyles(const QStringList &styles)
{
    d->widget.pageStyle->clear();
    d->widget.pageStyle->addItems(styles);
}

QString KoPageLayoutWidget::currentPageStyle() const
{
    return d->widget.pageStyle->currentText();
}