Debian Codegraph @

src/libs/widgets/KoPageLayoutWidget.cpp

raw
1 /* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2007, 2010 Thomas Zander <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 // clazy:excludeall=qstring-arg
8 #include "KoPageLayoutWidget.h"
9
10 #include <ui_KoPageLayoutWidget.h>
11
12 #include <KoUnit.h>
13
14 #include <QButtonGroup>
15
16 class Q_DECL_HIDDEN KoPageLayoutWidget::Private
17 {
18 public:
19 Ui::KoPageLayoutWidget widget;
20 KoPageLayout pageLayout;
21 KoUnit unit;
22
23 QButtonGroup *orientationGroup;
24 bool marginsEnabled;
25 bool allowSignals;
26 };
27
28
29 KoPageLayoutWidget::KoPageLayoutWidget(QWidget *parent, const KoPageLayout &layout)
30 : QWidget(parent)
31 , d(new Private)
32 {
33 d->widget.setupUi(this);
34
35 d->pageLayout = layout;
36 d->marginsEnabled = true;
37 d->allowSignals = true;
38 d->orientationGroup = new QButtonGroup(this);
39 d->orientationGroup->addButton(d->widget.portrait, KoPageFormat::Portrait);
40 d->orientationGroup->addButton(d->widget.landscape, KoPageFormat::Landscape);
41
42 QButtonGroup *group2 = new QButtonGroup(this);
43 group2->addButton(d->widget.singleSided);
44 group2->addButton(d->widget.facingPages);
45 // the two sets of labels we use might have different lengths; make sure this does not create a 'jumping' ui
46 d->widget.facingPages->setChecked(true);
47 facingPagesChanged();
48 int width = qMax(d->widget.leftLabel->width(), d->widget.rightLabel->width());
49 d->widget.singleSided->setChecked(true);
50 facingPagesChanged();
51 width = qMax(width, qMax(d->widget.leftLabel->width(), d->widget.rightLabel->width()));
52 d->widget.leftLabel->setMinimumSize(QSize(width, 5));
53
54 d->widget.units->addItems(KoUnit::listOfUnitNameForUi(KoUnit::HidePixel));
55 d->widget.sizes->addItems(KoPageFormat::localizedPageFormatNames());
56 setPageSpread(false);
57
58 connect(d->widget.sizes, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &KoPageLayoutWidget::sizeChanged);
59 connect(d->widget.units, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &KoPageLayoutWidget::slotUnitChanged);
60 connect(group2, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::idClicked), this, &KoPageLayoutWidget::facingPagesChanged);
61 connect(d->orientationGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::idClicked), this, &KoPageLayoutWidget::orientationChanged);
62 connect(d->widget.width, &KoUnitDoubleSpinBox::valueChangedPt, this, &KoPageLayoutWidget::optionsChanged);
63 connect(d->widget.height, &KoUnitDoubleSpinBox::valueChangedPt, this, &KoPageLayoutWidget::optionsChanged);
64 connect(d->widget.topMargin, &KoUnitDoubleSpinBox::valueChangedPt, this, &KoPageLayoutWidget::marginsChanged);
65 connect(d->widget.bottomMargin, &KoUnitDoubleSpinBox::valueChangedPt, this, &KoPageLayoutWidget::marginsChanged);
66 connect(d->widget.bindingEdgeMargin, &KoUnitDoubleSpinBox::valueChangedPt, this, &KoPageLayoutWidget::marginsChanged);
67 connect(d->widget.pageEdgeMargin, &KoUnitDoubleSpinBox::valueChangedPt, this, &KoPageLayoutWidget::marginsChanged);
68 connect(d->widget.width, &KoUnitDoubleSpinBox::valueChangedPt, this, &KoPageLayoutWidget::optionsChanged);
69 connect(d->widget.height, &KoUnitDoubleSpinBox::valueChangedPt, this, &KoPageLayoutWidget::optionsChanged);
70
71 setUnit(KoUnit(KoUnit::Millimeter));
72 setPageLayout(layout);
73 if (layout.format == 0) // make sure we always call this during startup, even if the A3 (index=0) was chosen
74 sizeChanged(layout.format);
75
76 showTextDirection(false);
77 /* disable advanced page layout features by default */
78 d->widget.facingPageLabel->setVisible(false);
79 d->widget.facingPages->setVisible(false);
80 d->widget.singleSided->setVisible(false);
81 d->widget.stylesLabel->setVisible(false);
82 d->widget.pageStyle->setVisible(false);
83 }
84
85 KoPageLayoutWidget::~KoPageLayoutWidget()
86 {
87 delete d;
88 }
89
90 KoPageLayout KoPageLayoutWidget::pageLayout() const
91 {
92 return d->pageLayout;
93 }
94
95 void KoPageLayoutWidget::sizeChanged(int row)
96 {
97 if (row < 0) return;
98 if (! d->allowSignals) return;
99 d->allowSignals = false;
100 d->pageLayout.format = static_cast<KoPageFormat::Format> (row);
101 bool custom = d->pageLayout.format == KoPageFormat::CustomSize;
102 d->widget.width->setEnabled(custom);
103 d->widget.height->setEnabled(custom);
104
105 if (!custom) {
106 d->pageLayout.width = MM_TO_POINT(KoPageFormat::width(d->pageLayout.format, d->pageLayout.orientation));
107 d->pageLayout.height = MM_TO_POINT(KoPageFormat::height(d->pageLayout.format, d->pageLayout.orientation));
108 if (d->widget.facingPages->isChecked()) // is pagespread
109 d->pageLayout.width *= 2;
110 }
111
112 d->widget.width->changeValue(d->pageLayout.width);
113 d->widget.height->changeValue(d->pageLayout.height);
114
115 Q_EMIT layoutChanged(d->pageLayout);
116 d->allowSignals = true;
117 }
118
119 void KoPageLayoutWidget::slotUnitChanged(int row)
120 {
121 setUnit(KoUnit::fromListForUi(row, KoUnit::HidePixel));
122 }
123
124 void KoPageLayoutWidget::setUnit(const KoUnit &unit)
125 {
126 if (d->unit == unit)
127 return;
128 d->unit = unit;
129
130 d->widget.width->setUnit(unit);
131 d->widget.height->setUnit(unit);
132 d->widget.topMargin->setUnit(unit);
133 d->widget.bottomMargin->setUnit(unit);
134 d->widget.bindingEdgeMargin->setUnit(unit);
135 d->widget.pageEdgeMargin->setUnit(unit);
136 d->widget.units->setCurrentIndex(unit.indexInListForUi(KoUnit::HidePixel));
137
138 Q_EMIT unitChanged(d->unit);
139 }
140
141 void KoPageLayoutWidget::setPageLayout(const KoPageLayout &layout)
142 {
143 if (! d->allowSignals) return;
144 d->allowSignals = false;
145 d->pageLayout = layout;
146
147 Q_ASSERT(d->orientationGroup->button(layout.orientation));
148 d->orientationGroup->button(layout.orientation)->setChecked(true);
149 if (layout.bindingSide >= 0 && layout.pageEdge >= 0) {
150 d->widget.facingPages->setChecked(true);
151 d->widget.bindingEdgeMargin->changeValue(layout.bindingSide);
152 d->widget.pageEdgeMargin->changeValue(layout.pageEdge);
153 d->pageLayout.leftMargin = -1;
154 d->pageLayout.rightMargin = -1;
155 }
156 else {
157 d->widget.singleSided->setChecked(true);
158 d->widget.bindingEdgeMargin->changeValue(layout.leftMargin);
159 d->widget.pageEdgeMargin->changeValue(layout.rightMargin);
160 d->pageLayout.pageEdge = -1;
161 d->pageLayout.bindingSide = -1;
162 }
163 facingPagesChanged();
164
165 d->widget.topMargin->changeValue(layout.topMargin);
166 d->widget.bottomMargin->changeValue(layout.bottomMargin);
167 d->allowSignals = true;
168 d->widget.sizes->setCurrentIndex(layout.format); // calls sizeChanged()
169 }
170
171 void KoPageLayoutWidget::facingPagesChanged()
172 {
173 if (! d->allowSignals) return;
174 d->allowSignals = false;
175 if (d->widget.singleSided->isChecked()) {
176 d->widget.leftLabel->setText(i18n("Left Edge:"));
177 d->widget.rightLabel->setText(i18n("Right Edge:"));
178 }
179 else {
180 d->widget.leftLabel->setText(i18n("Binding Edge:"));
181 d->widget.rightLabel->setText(i18n("Page Edge:"));
182 }
183 d->allowSignals = true;
184 marginsChanged();
185 sizeChanged(d->widget.sizes->currentIndex());
186 }
187
188 void KoPageLayoutWidget::marginsChanged()
189 {
190 if (! d->allowSignals) return;
191 d->allowSignals = false;
192 d->pageLayout.leftMargin = -1;
193 d->pageLayout.rightMargin = -1;
194 d->pageLayout.bindingSide = -1;
195 d->pageLayout.pageEdge = -1;
196 d->pageLayout.topMargin = d->marginsEnabled?d->widget.topMargin->value():0;
197 d->pageLayout.bottomMargin = d->marginsEnabled?d->widget.bottomMargin->value():0;
198 qreal left = d->marginsEnabled?d->widget.bindingEdgeMargin->value():0;
199 qreal right = d->marginsEnabled?d->widget.pageEdgeMargin->value():0;
200 if (left + right > d->pageLayout.width - 10) {
201 // make sure the actual text area is never smaller than 10 points.
202 qreal diff = d->pageLayout.width - 10 - left - right;
203 left = qMin(d->pageLayout.width - 10, qMax(qreal(0.0), left - diff / qreal(2.0)));
204 right = qMax(qreal(0.0), right - d->pageLayout.width - 10 - left);
205 }
206
207 if (d->widget.singleSided->isChecked()) {
208 d->pageLayout.leftMargin = left;
209 d->pageLayout.rightMargin = right;
210 }
211 else {
212 d->pageLayout.bindingSide = left;
213 d->pageLayout.pageEdge = right;
214 }
215 // debugWidgets << " " << d->pageLayout.left <<"|"<< d->pageLayout.bindingSide << "," <<
216 // d->pageLayout.right << "|"<< d->pageLayout.pageEdge;
217 Q_EMIT layoutChanged(d->pageLayout);
218 d->allowSignals = true;
219 }
220
221 void KoPageLayoutWidget::setTextAreaAvailable(bool available)
222 {
223 d->marginsEnabled = available;
224 d->widget.margins->setEnabled(available);
225 marginsChanged();
226 }
227
228 void KoPageLayoutWidget::optionsChanged()
229 {
230 if (! d->allowSignals) return;
231 if (d->widget.sizes->currentIndex() == KoPageFormat::CustomSize) {
232 d->pageLayout.width = d->widget.width->value();
233 d->pageLayout.height = d->widget.height->value();
234 } else
235 sizeChanged(d->widget.sizes->currentIndex());
236
237 marginsChanged();
238 }
239
240 void KoPageLayoutWidget::orientationChanged()
241 {
242 if (! d->allowSignals) return;
243 d->allowSignals = false;
244 d->pageLayout.orientation = d->widget.landscape->isChecked() ? KoPageFormat::Landscape : KoPageFormat::Portrait;
245
246 qreal x = d->widget.height->value();
247 d->widget.height->changeValue(d->widget.width->value());
248 d->widget.width->changeValue(x);
249
250 d->allowSignals = true;
251 optionsChanged();
252 }
253
254 void KoPageLayoutWidget::showUnitchooser(bool on) {
255 d->widget.units->setVisible(on);
256 d->widget.unitsLabel->setVisible(on);
257 }
258
259 void KoPageLayoutWidget::showPageSpread(bool on)
260 {
261 d->widget.facingPageLabel->setVisible(on);
262 d->widget.singleSided->setVisible(on);
263 d->widget.facingPages->setVisible(on);
264 }
265
266 void KoPageLayoutWidget::setPageSpread(bool pageSpread)
267 {
268 if (pageSpread)
269 d->widget.facingPages->setChecked(true);
270 else
271 d->widget.singleSided->setChecked(true);
272 }
273
274 void KoPageLayoutWidget::setApplyToDocument(bool apply)
275 {
276 if (apply) {
277 d->widget.facingPageLabel->setText(i18n("Facing Pages:"));
278 d->widget.facingPages->setText(i18n("Facing pages"));
279 }
280 else {
281 d->widget.facingPageLabel->setText(i18n("Page Layout:"));
282 d->widget.facingPages->setText(i18n("Page spread"));
283 }
284 }
285
286 void KoPageLayoutWidget::showTextDirection(bool on)
287 {
288 d->widget.directionLabel->setVisible(on);
289 d->widget.textDirection->setVisible(on);
290 }
291 /*
292 void KoPageLayoutWidget::setTextDirection(KoText::Direction direction)
293 {
294 int index = 0;
295 switch(direction) {
296 case KoText::LeftRightTopBottom:
297 index = 1;
298 break;
299 case KoText::RightLeftTopBottom:
300 index = 2;
301 break;
302 case KoText::TopBottomRightLeft: // unused for now.
303 case KoText::InheritDirection:
304 case KoText::AutoDirection:
305 index = 0;
306 case KoText::TopBottomLeftRight:
307 ; // unhandled, because it actually doesn't exist in real-world writing systems.
308 // Boustrophedon would be interesting to implement, though
309 }
310 d->widget.textDirection->setCurrentIndex(index);
311 }
312
313 KoText::Direction KoPageLayoutWidget::textDirection() const
314 {
315 switch(d->widget.textDirection->currentIndex()) {
316 case 1: return KoText::LeftRightTopBottom;
317 case 2: return KoText::RightLeftTopBottom;
318 default:
319 case 0: return KoText::AutoDirection;
320 }
321 }
322 */
323 void KoPageLayoutWidget::showPageStyles(bool on)
324 {
325 d->widget.stylesLabel->setVisible(on);
326 d->widget.pageStyle->setVisible(on);
327 }
328
329 void KoPageLayoutWidget::setPageStyles(const QStringList &styles)
330 {
331 d->widget.pageStyle->clear();
332 d->widget.pageStyle->addItems(styles);
333 }
334
335 QString KoPageLayoutWidget::currentPageStyle() const
336 {
337 return d->widget.pageStyle->currentText();
338 }