Warning, file /office/calligra/libs/widgets/KoPageLayoutWidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2007, 2010 Thomas Zander <zander@kde.org> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Library General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Library General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Library General Public License 0015 * along with this library; see the file COPYING.LIB. If not, write to 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "KoPageLayoutWidget.h" 0021 0022 #include <ui_KoPageLayoutWidget.h> 0023 0024 #include <KoUnit.h> 0025 0026 #include <QButtonGroup> 0027 0028 class Q_DECL_HIDDEN KoPageLayoutWidget::Private 0029 { 0030 public: 0031 Ui::KoPageLayoutWidget widget; 0032 KoPageLayout pageLayout; 0033 KoUnit unit; 0034 0035 QButtonGroup *orientationGroup; 0036 bool marginsEnabled; 0037 bool allowSignals; 0038 }; 0039 0040 0041 KoPageLayoutWidget::KoPageLayoutWidget(QWidget *parent, const KoPageLayout &layout) 0042 : QWidget(parent) 0043 , d(new Private) 0044 { 0045 d->widget.setupUi(this); 0046 0047 d->pageLayout = layout; 0048 d->marginsEnabled = true; 0049 d->allowSignals = true; 0050 d->orientationGroup = new QButtonGroup(this); 0051 d->orientationGroup->addButton(d->widget.portrait, KoPageFormat::Portrait); 0052 d->orientationGroup->addButton(d->widget.landscape, KoPageFormat::Landscape); 0053 0054 QButtonGroup *group2 = new QButtonGroup(this); 0055 group2->addButton(d->widget.singleSided); 0056 group2->addButton(d->widget.facingPages); 0057 // the two sets of labels we use might have different lengths; make sure this does not create a 'jumping' ui 0058 d->widget.facingPages->setChecked(true); 0059 facingPagesChanged(); 0060 int width = qMax(d->widget.leftLabel->width(), d->widget.rightLabel->width()); 0061 d->widget.singleSided->setChecked(true); 0062 facingPagesChanged(); 0063 width = qMax(width, qMax(d->widget.leftLabel->width(), d->widget.rightLabel->width())); 0064 d->widget.leftLabel->setMinimumSize(QSize(width, 5)); 0065 0066 d->widget.units->addItems(KoUnit::listOfUnitNameForUi(KoUnit::HidePixel)); 0067 d->widget.sizes->addItems(KoPageFormat::localizedPageFormatNames()); 0068 setPageSpread(false); 0069 0070 connect(d->widget.sizes, SIGNAL(currentIndexChanged(int)), this, SLOT(sizeChanged(int))); 0071 connect(d->widget.units, SIGNAL(currentIndexChanged(int)), this, SLOT(unitChanged(int))); 0072 connect(group2, SIGNAL(buttonClicked(int)), this, SLOT(facingPagesChanged())); 0073 connect(d->orientationGroup, SIGNAL(buttonClicked(int)), this, SLOT(orientationChanged())); 0074 connect(d->widget.width, SIGNAL(valueChangedPt(qreal)), this, SLOT(optionsChanged())); 0075 connect(d->widget.height, SIGNAL(valueChangedPt(qreal)), this, SLOT(optionsChanged())); 0076 connect(d->widget.topMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(marginsChanged())); 0077 connect(d->widget.bottomMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(marginsChanged())); 0078 connect(d->widget.bindingEdgeMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(marginsChanged())); 0079 connect(d->widget.pageEdgeMargin, SIGNAL(valueChangedPt(qreal)), this, SLOT(marginsChanged())); 0080 connect(d->widget.width, SIGNAL(valueChangedPt(qreal)), this, SLOT(optionsChanged())); 0081 connect(d->widget.height, SIGNAL(valueChangedPt(qreal)), this, SLOT(optionsChanged())); 0082 0083 setUnit(KoUnit(KoUnit::Millimeter)); 0084 setPageLayout(layout); 0085 if (layout.format == 0) // make sure we always call this during startup, even if the A3 (index=0) was chosen 0086 sizeChanged(layout.format); 0087 0088 showTextDirection(false); 0089 /* disable advanced page layout features by default */ 0090 d->widget.facingPageLabel->setVisible(false); 0091 d->widget.facingPages->setVisible(false); 0092 d->widget.singleSided->setVisible(false); 0093 d->widget.stylesLabel->setVisible(false); 0094 d->widget.pageStyle->setVisible(false); 0095 } 0096 0097 KoPageLayoutWidget::~KoPageLayoutWidget() 0098 { 0099 delete d; 0100 } 0101 0102 KoPageLayout KoPageLayoutWidget::pageLayout() const 0103 { 0104 return d->pageLayout; 0105 } 0106 0107 void KoPageLayoutWidget::sizeChanged(int row) 0108 { 0109 if (row < 0) return; 0110 if (! d->allowSignals) return; 0111 d->allowSignals = false; 0112 d->pageLayout.format = static_cast<KoPageFormat::Format> (row); 0113 bool custom = d->pageLayout.format == KoPageFormat::CustomSize; 0114 d->widget.width->setEnabled( custom ); 0115 d->widget.height->setEnabled( custom ); 0116 0117 if ( !custom ) { 0118 d->pageLayout.width = MM_TO_POINT( KoPageFormat::width( d->pageLayout.format, d->pageLayout.orientation ) ); 0119 d->pageLayout.height = MM_TO_POINT( KoPageFormat::height( d->pageLayout.format, d->pageLayout.orientation ) ); 0120 if (d->widget.facingPages->isChecked()) // is pagespread 0121 d->pageLayout.width *= 2; 0122 } 0123 0124 d->widget.width->changeValue( d->pageLayout.width ); 0125 d->widget.height->changeValue( d->pageLayout.height ); 0126 0127 emit layoutChanged(d->pageLayout); 0128 d->allowSignals = true; 0129 } 0130 0131 void KoPageLayoutWidget::unitChanged(int row) 0132 { 0133 setUnit(KoUnit::fromListForUi(row, KoUnit::HidePixel)); 0134 } 0135 0136 void KoPageLayoutWidget::setUnit(const KoUnit &unit) 0137 { 0138 if (d->unit == unit) 0139 return; 0140 d->unit = unit; 0141 0142 d->widget.width->setUnit(unit); 0143 d->widget.height->setUnit(unit); 0144 d->widget.topMargin->setUnit(unit); 0145 d->widget.bottomMargin->setUnit(unit); 0146 d->widget.bindingEdgeMargin->setUnit(unit); 0147 d->widget.pageEdgeMargin->setUnit(unit); 0148 d->widget.units->setCurrentIndex(unit.indexInListForUi(KoUnit::HidePixel)); 0149 0150 emit unitChanged(d->unit); 0151 } 0152 0153 void KoPageLayoutWidget::setPageLayout(const KoPageLayout &layout) 0154 { 0155 if (! d->allowSignals) return; 0156 d->allowSignals = false; 0157 d->pageLayout = layout; 0158 0159 Q_ASSERT(d->orientationGroup->button( layout.orientation )); 0160 d->orientationGroup->button( layout.orientation )->setChecked( true ); 0161 if (layout.bindingSide >= 0 && layout.pageEdge >= 0) { 0162 d->widget.facingPages->setChecked(true); 0163 d->widget.bindingEdgeMargin->changeValue(layout.bindingSide); 0164 d->widget.pageEdgeMargin->changeValue(layout.pageEdge); 0165 d->pageLayout.leftMargin = -1; 0166 d->pageLayout.rightMargin = -1; 0167 } 0168 else { 0169 d->widget.singleSided->setChecked(true); 0170 d->widget.bindingEdgeMargin->changeValue(layout.leftMargin); 0171 d->widget.pageEdgeMargin->changeValue(layout.rightMargin); 0172 d->pageLayout.pageEdge = -1; 0173 d->pageLayout.bindingSide = -1; 0174 } 0175 facingPagesChanged(); 0176 0177 d->widget.topMargin->changeValue(layout.topMargin); 0178 d->widget.bottomMargin->changeValue(layout.bottomMargin); 0179 d->allowSignals = true; 0180 d->widget.sizes->setCurrentIndex(layout.format); // calls sizeChanged() 0181 } 0182 0183 void KoPageLayoutWidget::facingPagesChanged() 0184 { 0185 if (! d->allowSignals) return; 0186 d->allowSignals = false; 0187 if (d->widget.singleSided->isChecked()) { 0188 d->widget.leftLabel->setText(i18n("Left Edge:")); 0189 d->widget.rightLabel->setText(i18n("Right Edge:")); 0190 } 0191 else { 0192 d->widget.leftLabel->setText(i18n("Binding Edge:")); 0193 d->widget.rightLabel->setText(i18n("Page Edge:")); 0194 } 0195 d->allowSignals = true; 0196 marginsChanged(); 0197 sizeChanged(d->widget.sizes->currentIndex()); 0198 } 0199 0200 void KoPageLayoutWidget::marginsChanged() 0201 { 0202 if (! d->allowSignals) return; 0203 d->allowSignals = false; 0204 d->pageLayout.leftMargin = -1; 0205 d->pageLayout.rightMargin = -1; 0206 d->pageLayout.bindingSide = -1; 0207 d->pageLayout.pageEdge = -1; 0208 d->pageLayout.topMargin = d->marginsEnabled?d->widget.topMargin->value():0; 0209 d->pageLayout.bottomMargin = d->marginsEnabled?d->widget.bottomMargin->value():0; 0210 qreal left = d->marginsEnabled?d->widget.bindingEdgeMargin->value():0; 0211 qreal right = d->marginsEnabled?d->widget.pageEdgeMargin->value():0; 0212 if (left + right > d->pageLayout.width - 10) { 0213 // make sure the actual text area is never smaller than 10 points. 0214 qreal diff = d->pageLayout.width - 10 - left - right; 0215 left = qMin(d->pageLayout.width - 10, qMax(qreal(0.0), left - diff / qreal(2.0))); 0216 right = qMax(qreal(0.0), right - d->pageLayout.width - 10 - left); 0217 } 0218 0219 if (d->widget.singleSided->isChecked()) { 0220 d->pageLayout.leftMargin = left; 0221 d->pageLayout.rightMargin = right; 0222 } 0223 else { 0224 d->pageLayout.bindingSide = left; 0225 d->pageLayout.pageEdge = right; 0226 } 0227 // debugWidgets << " " << d->pageLayout.left <<"|"<< d->pageLayout.bindingSide << "," << 0228 // d->pageLayout.right << "|"<< d->pageLayout.pageEdge; 0229 emit layoutChanged(d->pageLayout); 0230 d->allowSignals = true; 0231 } 0232 0233 void KoPageLayoutWidget::setTextAreaAvailable(bool available) 0234 { 0235 d->marginsEnabled = available; 0236 d->widget.margins->setEnabled(available); 0237 marginsChanged(); 0238 } 0239 0240 void KoPageLayoutWidget::optionsChanged() 0241 { 0242 if (! d->allowSignals) return; 0243 if (d->widget.sizes->currentIndex() == KoPageFormat::CustomSize) { 0244 d->pageLayout.width = d->widget.width->value(); 0245 d->pageLayout.height = d->widget.height->value(); 0246 } else 0247 sizeChanged(d->widget.sizes->currentIndex()); 0248 0249 marginsChanged(); 0250 } 0251 0252 void KoPageLayoutWidget::orientationChanged() 0253 { 0254 if (! d->allowSignals) return; 0255 d->allowSignals = false; 0256 d->pageLayout.orientation = d->widget.landscape->isChecked() ? KoPageFormat::Landscape : KoPageFormat::Portrait; 0257 0258 qreal x = d->widget.height->value(); 0259 d->widget.height->changeValue( d->widget.width->value() ); 0260 d->widget.width->changeValue( x ); 0261 0262 d->allowSignals = true; 0263 optionsChanged(); 0264 } 0265 0266 void KoPageLayoutWidget::showUnitchooser(bool on) { 0267 d->widget.units->setVisible(on); 0268 d->widget.unitsLabel->setVisible(on); 0269 } 0270 0271 void KoPageLayoutWidget::showPageSpread(bool on) 0272 { 0273 d->widget.facingPageLabel->setVisible(on); 0274 d->widget.singleSided->setVisible(on); 0275 d->widget.facingPages->setVisible(on); 0276 } 0277 0278 void KoPageLayoutWidget::setPageSpread(bool pageSpread) 0279 { 0280 if (pageSpread) 0281 d->widget.facingPages->setChecked(true); 0282 else 0283 d->widget.singleSided->setChecked(true); 0284 } 0285 0286 void KoPageLayoutWidget::setApplyToDocument(bool apply) 0287 { 0288 if (apply) { 0289 d->widget.facingPageLabel->setText(i18n("Facing Pages:")); 0290 d->widget.facingPages->setText(i18n("Facing pages")); 0291 } 0292 else { 0293 d->widget.facingPageLabel->setText(i18n("Page Layout:")); 0294 d->widget.facingPages->setText(i18n("Page spread")); 0295 } 0296 } 0297 0298 void KoPageLayoutWidget::showTextDirection(bool on) 0299 { 0300 d->widget.directionLabel->setVisible(on); 0301 d->widget.textDirection->setVisible(on); 0302 } 0303 0304 void KoPageLayoutWidget::setTextDirection(KoText::Direction direction ) 0305 { 0306 int index = 0; 0307 switch(direction) { 0308 case KoText::LeftRightTopBottom: 0309 index = 1; 0310 break; 0311 case KoText::RightLeftTopBottom: 0312 index = 2; 0313 break; 0314 case KoText::TopBottomRightLeft: // unused for now. 0315 case KoText::InheritDirection: 0316 case KoText::AutoDirection: 0317 index = 0; 0318 case KoText::TopBottomLeftRight: 0319 ; // unhandled, because it actually doesn't exist in real-world writing systems. 0320 // Boustrophedon would be interesting to implement, though 0321 } 0322 d->widget.textDirection->setCurrentIndex(index); 0323 } 0324 0325 KoText::Direction KoPageLayoutWidget::textDirection() const 0326 { 0327 switch(d->widget.textDirection->currentIndex()) { 0328 case 1: return KoText::LeftRightTopBottom; 0329 case 2: return KoText::RightLeftTopBottom; 0330 default: 0331 case 0: return KoText::AutoDirection; 0332 } 0333 } 0334 0335 void KoPageLayoutWidget::showPageStyles(bool on) 0336 { 0337 d->widget.stylesLabel->setVisible(on); 0338 d->widget.pageStyle->setVisible(on); 0339 } 0340 0341 void KoPageLayoutWidget::setPageStyles(const QStringList &styles) 0342 { 0343 d->widget.pageStyle->clear(); 0344 d->widget.pageStyle->addItems(styles); 0345 } 0346 0347 QString KoPageLayoutWidget::currentPageStyle() const 0348 { 0349 return d->widget.pageStyle->currentText(); 0350 }