File indexing completed on 2024-05-19 15:27:51

0001 /* This file is part of KGraphViewer.
0002    Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr>
0003 
0004    KGraphViewer is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, version 2.
0007 
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    General Public License for more details.
0012 
0013    You should have received a copy of the GNU General Public License
0014    along with this program; if not, write to the Free Software
0015    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0016    02110-1301, USA
0017 */
0018 
0019 /* This file was part of the KDE project
0020    Copyright (C) 2005 Jarosław Staniek <staniek@kde.org>
0021 
0022    This program is free software; you can redistribute it and/or
0023    modify it under the terms of the GNU Library General Public
0024    License as published by the Free Software Foundation; either
0025    version 2 of the License, or (at your option) any later version.
0026  */
0027 
0028 #include "kgraphviewerlib_debug.h"
0029 #include <KgvPageLayoutDia.h>
0030 #include <KgvPageLayoutSize.h>
0031 #include <KgvUnit.h>
0032 #include <KgvUnitWidgets.h>
0033 
0034 #include <QDebug>
0035 #include <QIcon>
0036 #include <QMessageBox>
0037 
0038 #include <qlabel.h>
0039 #include <qlayout.h>
0040 #include <qradiobutton.h>
0041 // Added by qt3to4:
0042 #include "klocalizedstring.h"
0043 #include <QGridLayout>
0044 #include <QHBoxLayout>
0045 #include <QPixmap>
0046 
0047 KgvPageLayoutSize::KgvPageLayoutSize(QWidget *parent, const KgvPageLayout &layout, KgvUnit::Unit unit, const KgvColumns &columns, bool unitChooser, bool enableBorders)
0048     : QWidget(parent)
0049     , m_blockSignals(false)
0050 {
0051     m_layout = layout;
0052     m_unit = unit;
0053 
0054     QGridLayout *grid1 = new QGridLayout(this);
0055     if (unitChooser) {
0056         // ------------- unit _______________
0057         QWidget *unitFrame = new QWidget(this);
0058         grid1->addWidget(unitFrame, 0, 0, Qt::AlignLeft);
0059         QBoxLayout *unitLayout = new QHBoxLayout(unitFrame);
0060 
0061         // label unit
0062         QLabel *lpgUnit = new QLabel(i18n("Unit:"), unitFrame);
0063         unitLayout->addWidget(lpgUnit, 0, Qt::AlignRight | Qt::AlignVCenter);
0064 
0065         // combo unit
0066         QComboBox *cpgUnit = new QComboBox(unitFrame);
0067         lpgUnit->setBuddy(cpgUnit);
0068         cpgUnit->addItems(KgvUnit::listOfUnitName());
0069         cpgUnit->setCurrentIndex(unit);
0070         unitLayout->addWidget(cpgUnit, 0, Qt::AlignLeft | Qt::AlignVCenter);
0071         connect(cpgUnit, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &KgvPageLayoutSize::setUnitInt);
0072     } else {
0073         QString str = KgvUnit::unitDescription(unit);
0074 
0075         QLabel *lpgUnit = new QLabel(i18n("All values are given in %1.", str), this);
0076         grid1->addWidget(lpgUnit, 0, 0, Qt::AlignLeft);
0077     }
0078 
0079     // -------------- page size -----------------
0080     QGroupBox *formatFrame = new QGroupBox(i18n("Page Size"), this);
0081     grid1->addWidget(formatFrame, 1, 0);
0082     QVBoxLayout *vlay = new QVBoxLayout;
0083 
0084     QWidget *formatPageSize = new QWidget(formatFrame);
0085     vlay->addWidget(formatPageSize);
0086     //     formatPageSize->setSpacing( KDialog::spacingHint() );
0087 
0088     // label page size
0089     QLabel *lpgFormat = new QLabel(i18n("&Size:"), formatPageSize);
0090 
0091     // combo size
0092     cpgFormat = new QComboBox(formatPageSize);
0093     cpgFormat->addItems(KgvPageFormat::allFormats());
0094     lpgFormat->setBuddy(cpgFormat);
0095     connect(cpgFormat, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &KgvPageLayoutSize::formatChanged);
0096     QHBoxLayout *lay = new QHBoxLayout;
0097     lay->addWidget(lpgFormat);
0098     lay->addWidget(cpgFormat);
0099     formatPageSize->setLayout(lay);
0100     // spacer
0101     //     formatPageSize->setStretchFactor( new QWidget( formatPageSize ), 10 );
0102 
0103     QHBoxLayout *lay2 = new QHBoxLayout;
0104     QWidget *formatCustomSize = new QWidget(formatFrame);
0105     vlay->addWidget(formatCustomSize);
0106     formatFrame->setLayout(vlay);
0107     //     formatCustomSize->setSpacing( KDialog::spacingHint() );
0108 
0109     // label width
0110     QLabel *lpgWidth = new QLabel(i18n("&Width:"), formatCustomSize);
0111     lay2->addWidget(lpgWidth);
0112     // linedit width
0113     epgWidth = new KgvUnitDoubleSpinBox(formatCustomSize);
0114     lay2->addWidget(epgWidth);
0115     lpgWidth->setBuddy(epgWidth);
0116     if (m_layout.format != PG_CUSTOM)
0117         epgWidth->setEnabled(false);
0118     connect(epgWidth, &KgvUnitDoubleSpinBox::valueChangedPt, this, &KgvPageLayoutSize::widthChanged);
0119 
0120     // label height
0121     QLabel *lpgHeight = new QLabel(i18n("&Height:"), formatCustomSize);
0122     lay2->addWidget(lpgHeight);
0123 
0124     // linedit height
0125     epgHeight = new KgvUnitDoubleSpinBox(formatCustomSize);
0126     lay2->addWidget(epgHeight);
0127     lpgHeight->setBuddy(epgHeight);
0128     if (m_layout.format != PG_CUSTOM)
0129         epgHeight->setEnabled(false);
0130     connect(epgHeight, &KgvUnitDoubleSpinBox::valueChangedPt, this, &KgvPageLayoutSize::heightChanged);
0131     formatCustomSize->setLayout(lay2);
0132 
0133     // --------------- orientation ---------------
0134     QHBoxLayout *lay3 = new QHBoxLayout;
0135     m_orientGroup = new QGroupBox(i18n("Orientation"), this);
0136     //     m_orientGroup->setInsideSpacing( KDialog::spacingHint() );
0137     grid1->addWidget(m_orientGroup, 2, 0);
0138 
0139     QRadioButton *rbPortrait = new QRadioButton(i18n("&Portrait"), m_orientGroup);
0140     lay3->addWidget(rbPortrait);
0141     m_orientButtons.addButton(rbPortrait);
0142 
0143     QRadioButton *rbLandscape = new QRadioButton(i18n("La&ndscape"), m_orientGroup);
0144     lay3->addWidget(rbLandscape);
0145     m_orientGroup->setLayout(lay3);
0146     m_orientButtons.addButton(rbLandscape);
0147 
0148     connect(&m_orientButtons, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), this, &KgvPageLayoutSize::orientationChanged);
0149 
0150     // --------------- page margins ---------------
0151     QGroupBox *marginsFrame = new QGroupBox(i18n("Margins"), this);
0152     //     marginsFrame->setColumnLayout( 0, Qt::Vertical );
0153     //     marginsFrame->setMargin( KDialog::marginHint() );
0154     grid1->addWidget(marginsFrame, 3, 0);
0155 
0156     QWidget *marginsWidget = new QWidget(marginsFrame);
0157     QGridLayout *marginsLayout = new QGridLayout(marginsFrame);
0158 
0159     // left margin
0160     ebrLeft = new KgvUnitDoubleSpinBox(marginsWidget);
0161     marginsLayout->addWidget(ebrLeft, 1, 0);
0162     connect(ebrLeft, &KgvUnitDoubleSpinBox::valueChangedPt, this, &KgvPageLayoutSize::leftChanged);
0163 
0164     // right margin
0165     ebrRight = new KgvUnitDoubleSpinBox(marginsWidget);
0166     marginsLayout->addWidget(ebrRight, 1, 2);
0167     connect(ebrRight, &KgvUnitDoubleSpinBox::valueChangedPt, this, &KgvPageLayoutSize::rightChanged);
0168 
0169     // top margin
0170     ebrTop = new KgvUnitDoubleSpinBox(marginsWidget);
0171     marginsLayout->addWidget(ebrTop, 0, 1, Qt::AlignCenter);
0172     connect(ebrTop, &KgvUnitDoubleSpinBox::valueChangedPt, this, &KgvPageLayoutSize::topChanged);
0173 
0174     // bottom margin
0175     ebrBottom = new KgvUnitDoubleSpinBox(marginsWidget);
0176     marginsLayout->addWidget(ebrBottom, 2, 1, Qt::AlignCenter);
0177     connect(ebrBottom, &KgvUnitDoubleSpinBox::valueChangedPt, this, &KgvPageLayoutSize::bottomChanged);
0178 
0179     marginsFrame->setLayout(marginsLayout);
0180 
0181     // ------------- preview -----------
0182     pgPreview = new KgvPagePreview(this, "Preview", m_layout);
0183     grid1->addWidget(pgPreview, 1, 1, 3, 1);
0184 
0185     // ------------- spacers -----------
0186     QWidget *spacer1 = new QWidget(this);
0187     QWidget *spacer2 = new QWidget(this);
0188     spacer1->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
0189     spacer2->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
0190     grid1->addWidget(spacer1, 4, 0);
0191     grid1->addWidget(spacer2, 4, 1);
0192 
0193     setValues();
0194     updatePreview();
0195     pgPreview->setPageColumns(columns);
0196     setEnableBorders(enableBorders);
0197 }
0198 
0199 void KgvPageLayoutSize::setEnableBorders(bool on)
0200 {
0201     m_haveBorders = on;
0202     ebrLeft->setEnabled(on);
0203     ebrRight->setEnabled(on);
0204     ebrTop->setEnabled(on);
0205     ebrBottom->setEnabled(on);
0206 
0207     // update m_layout
0208     m_layout.ptLeft = on ? ebrLeft->value() : 0;
0209     m_layout.ptRight = on ? ebrRight->value() : 0;
0210     m_layout.ptTop = on ? ebrTop->value() : 0;
0211     m_layout.ptBottom = on ? ebrBottom->value() : 0;
0212 
0213     // use updated m_layout
0214     updatePreview();
0215     emit propertyChange(m_layout);
0216 }
0217 
0218 void KgvPageLayoutSize::updatePreview()
0219 {
0220     pgPreview->setPageLayout(m_layout);
0221 }
0222 
0223 void KgvPageLayoutSize::setValues()
0224 {
0225     // page format
0226     cpgFormat->setCurrentIndex(m_layout.format);
0227     // orientation
0228     //     m_orientGroup->setButton( m_layout.orientation == PG_PORTRAIT ? 0: 1 );
0229 
0230     setUnit(m_unit);
0231     updatePreview();
0232 }
0233 
0234 void KgvPageLayoutSize::setUnit(KgvUnit::Unit unit)
0235 {
0236     m_unit = unit;
0237     m_blockSignals = true; // due to non-atomic changes the propertyChange emits should be blocked
0238 
0239     epgWidth->setUnit(m_unit);
0240     epgWidth->setMinMaxStep(0, KgvUnit::fromUserValue(9999, m_unit), KgvUnit::fromUserValue(0.01, m_unit));
0241     epgWidth->changeValue(m_layout.ptWidth);
0242 
0243     epgHeight->setUnit(m_unit);
0244     epgHeight->setMinMaxStep(0, KgvUnit::fromUserValue(9999, m_unit), KgvUnit::fromUserValue(0.01, m_unit));
0245     epgHeight->changeValue(m_layout.ptHeight);
0246 
0247     double dStep = KgvUnit::fromUserValue(0.2, m_unit);
0248 
0249     ebrLeft->setUnit(m_unit);
0250     ebrLeft->changeValue(m_layout.ptLeft);
0251     ebrLeft->setMinMaxStep(0, m_layout.ptWidth, dStep);
0252 
0253     ebrRight->setUnit(m_unit);
0254     ebrRight->changeValue(m_layout.ptRight);
0255     ebrRight->setMinMaxStep(0, m_layout.ptWidth, dStep);
0256 
0257     ebrTop->setUnit(m_unit);
0258     ebrTop->changeValue(m_layout.ptTop);
0259     ebrTop->setMinMaxStep(0, m_layout.ptHeight, dStep);
0260 
0261     ebrBottom->setUnit(m_unit);
0262     ebrBottom->changeValue(m_layout.ptBottom);
0263     ebrBottom->setMinMaxStep(0, m_layout.ptHeight, dStep);
0264 
0265     m_blockSignals = false;
0266 }
0267 
0268 void KgvPageLayoutSize::setUnitInt(int unit)
0269 {
0270     setUnit((KgvUnit::Unit)unit);
0271 }
0272 
0273 void KgvPageLayoutSize::formatChanged(int format)
0274 {
0275     if ((KgvFormat)format == m_layout.format)
0276         return;
0277     m_layout.format = (KgvFormat)format;
0278     bool enable = (KgvFormat)format == PG_CUSTOM;
0279     epgWidth->setEnabled(enable);
0280     epgHeight->setEnabled(enable);
0281 
0282     if (m_layout.format != PG_CUSTOM) {
0283         m_layout.ptWidth = MM_TO_POINT(KgvPageFormat::width(m_layout.format, m_layout.orientation));
0284         m_layout.ptHeight = MM_TO_POINT(KgvPageFormat::height(m_layout.format, m_layout.orientation));
0285     }
0286 
0287     epgWidth->changeValue(m_layout.ptWidth);
0288     epgHeight->changeValue(m_layout.ptHeight);
0289 
0290     updatePreview();
0291     emit propertyChange(m_layout);
0292 }
0293 
0294 void KgvPageLayoutSize::orientationChanged(int which)
0295 {
0296     qCDebug(KGRAPHVIEWERLIB_LOG) << "KgvPageLayoutSize::orientationChanged";
0297     m_layout.orientation = which == 0 ? PG_PORTRAIT : PG_LANDSCAPE;
0298 
0299     // swap dimension
0300     double val = epgWidth->value();
0301     epgWidth->changeValue(epgHeight->value());
0302     epgHeight->changeValue(val);
0303     // and adjust margins
0304     m_blockSignals = true;
0305     val = ebrTop->value();
0306     if (m_layout.orientation == PG_PORTRAIT) { // clockwise
0307         ebrTop->changeValue(ebrRight->value());
0308         ebrRight->changeValue(ebrBottom->value());
0309         ebrBottom->changeValue(ebrLeft->value());
0310         ebrLeft->changeValue(val);
0311     } else { // counter clockwise
0312         ebrTop->changeValue(ebrLeft->value());
0313         ebrLeft->changeValue(ebrBottom->value());
0314         ebrBottom->changeValue(ebrRight->value());
0315         ebrRight->changeValue(val);
0316     }
0317     m_blockSignals = false;
0318 
0319     setEnableBorders(m_haveBorders); // will update preview+emit
0320 }
0321 
0322 void KgvPageLayoutSize::widthChanged(double width)
0323 {
0324     if (m_blockSignals)
0325         return;
0326     m_layout.ptWidth = width;
0327     updatePreview();
0328     emit propertyChange(m_layout);
0329 }
0330 void KgvPageLayoutSize::heightChanged(double height)
0331 {
0332     if (m_blockSignals)
0333         return;
0334     m_layout.ptHeight = height;
0335     updatePreview();
0336     emit propertyChange(m_layout);
0337 }
0338 void KgvPageLayoutSize::leftChanged(double left)
0339 {
0340     if (m_blockSignals)
0341         return;
0342     m_layout.ptLeft = left;
0343     updatePreview();
0344     emit propertyChange(m_layout);
0345 }
0346 void KgvPageLayoutSize::rightChanged(double right)
0347 {
0348     if (m_blockSignals)
0349         return;
0350     m_layout.ptRight = right;
0351     updatePreview();
0352     emit propertyChange(m_layout);
0353 }
0354 void KgvPageLayoutSize::topChanged(double top)
0355 {
0356     if (m_blockSignals)
0357         return;
0358     m_layout.ptTop = top;
0359     updatePreview();
0360     emit propertyChange(m_layout);
0361 }
0362 void KgvPageLayoutSize::bottomChanged(double bottom)
0363 {
0364     if (m_blockSignals)
0365         return;
0366     m_layout.ptBottom = bottom;
0367     updatePreview();
0368     emit propertyChange(m_layout);
0369 }
0370 
0371 bool KgvPageLayoutSize::queryClose()
0372 {
0373     if (m_layout.ptLeft + m_layout.ptRight > m_layout.ptWidth) {
0374         QMessageBox::critical(this, i18n("Page Layout Problem"), i18n("The page width is smaller than the left and right margins."));
0375         return false;
0376     }
0377     if (m_layout.ptTop + m_layout.ptBottom > m_layout.ptHeight) {
0378         QMessageBox::critical(this, i18n("Page Layout Problem"), i18n("The page height is smaller than the top and bottom margins."));
0379         return false;
0380     }
0381     return true;
0382 }
0383 
0384 void KgvPageLayoutSize::setColumns(KgvColumns &columns)
0385 {
0386     pgPreview->setPageColumns(columns);
0387 }
0388 
0389 #include "moc_KgvPageLayoutSize.cpp"