File indexing completed on 2024-02-25 03:46:52

0001 /*
0002     KmPlot - a math. function plotter for the KDE-Desktop
0003 
0004     SPDX-FileCopyrightText: 1998, 1999, 2000, 2002 Klaus-Dieter Möller <kd.moeller@t-online.de>
0005 
0006     This file is part of the KDE Project.
0007     KmPlot is part of the KDE-EDU Project.
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 
0011 */
0012 
0013 #include "kprinterdlg.h"
0014 
0015 // Qt includes
0016 #include <QCheckBox>
0017 #include <QComboBox>
0018 #include <QGridLayout>
0019 #include <QLabel>
0020 
0021 // KDE includes
0022 #include <KLocalizedString>
0023 
0024 // local includes
0025 #include "equationedit.h"
0026 #include "parser.h"
0027 
0028 KPrinterDlg::KPrinterDlg(QWidget *parent)
0029     : QWidget(parent)
0030 {
0031     setWindowTitle(i18nc("@title:window", "KmPlot Options"));
0032 
0033     QGridLayout *layout = new QGridLayout(this);
0034 
0035     m_printHeaderTable = new QCheckBox(i18n("Print header table"), this);
0036     m_transparentBackground = new QCheckBox(i18n("Transparent background"), this);
0037 
0038     m_widthEdit = new EquationEdit(this);
0039     m_heightEdit = new EquationEdit(this);
0040 
0041     m_widthEdit->setText(QStringLiteral("12"));
0042     m_heightEdit->setText(QStringLiteral("12"));
0043 
0044     m_lengthScalingCombo = new QComboBox(this);
0045     m_lengthScalingCombo->addItem(i18n("Pixels (1/72nd in)"));
0046     m_lengthScalingCombo->addItem(i18n("Inches (in)"));
0047     m_lengthScalingCombo->addItem(i18n("Centimeters (cm)"));
0048     m_lengthScalingCombo->addItem(i18n("Millimeters (mm)"));
0049 
0050     m_lengthScalingCombo->setCurrentIndex(2); // default of centimeters
0051 
0052     QLabel *widthLabel = new QLabel(i18n("Width:"), this);
0053     QLabel *heightLabel = new QLabel(i18n("Height:"), this);
0054 
0055     layout->addWidget(m_printHeaderTable, 0, 0, 1, 2);
0056     layout->addWidget(m_transparentBackground, 1, 0, 1, 2);
0057     layout->addWidget(widthLabel, 2, 0, 1, 1);
0058     layout->addWidget(m_widthEdit, 2, 1, 1, 1);
0059     layout->addWidget(heightLabel, 3, 0, 1, 1);
0060     layout->addWidget(m_heightEdit, 3, 1, 1, 1);
0061     layout->addWidget(m_lengthScalingCombo, 4, 1, 1, 1);
0062 
0063     layout->setRowStretch(5, 1);
0064 }
0065 
0066 bool KPrinterDlg::printHeaderTable()
0067 {
0068     return m_printHeaderTable->isChecked();
0069 }
0070 
0071 void KPrinterDlg::setPrintHeaderTable(bool status)
0072 {
0073     m_printHeaderTable->setChecked(status);
0074 }
0075 
0076 bool KPrinterDlg::printBackground()
0077 {
0078     return !m_transparentBackground->isChecked();
0079 }
0080 
0081 void KPrinterDlg::setPrintBackground(bool status)
0082 {
0083     m_transparentBackground->setChecked(!status);
0084 }
0085 
0086 double KPrinterDlg::printWidth()
0087 {
0088     return m_widthEdit->value() * lengthScaling();
0089 }
0090 
0091 void KPrinterDlg::setPrintWidth(double _width)
0092 {
0093     double width = _width / lengthScaling();
0094 
0095     if (width <= 0)
0096         width = 0.12 / lengthScaling();
0097 
0098     m_widthEdit->setText(Parser::number(width));
0099 }
0100 
0101 double KPrinterDlg::printHeight()
0102 {
0103     return m_heightEdit->value() * lengthScaling();
0104 }
0105 
0106 void KPrinterDlg::setPrintHeight(double _height)
0107 {
0108     double height = _height / lengthScaling();
0109 
0110     if (height <= 0)
0111         height = 0.12 / lengthScaling();
0112 
0113     m_heightEdit->setText(Parser::number(height));
0114 }
0115 
0116 bool KPrinterDlg::isValid(QString &msg)
0117 {
0118     bool ok;
0119 
0120     m_widthEdit->value(&ok);
0121     if (!ok) {
0122         msg = i18n("Width is invalid");
0123         return false;
0124     }
0125 
0126     m_heightEdit->value(&ok);
0127     if (!ok) {
0128         msg = i18n("Height is invalid");
0129         return false;
0130     }
0131 
0132     return true;
0133 }
0134 
0135 double KPrinterDlg::scalingToMeter(LengthScaling scaling)
0136 {
0137     switch (scaling) {
0138     case Centimeters:
0139         return 0.01;
0140     case Millimeters:
0141         return 0.001;
0142     case Inches:
0143         return 0.0254;
0144     case Pixels:
0145         return 0.0254 / 72.0;
0146     }
0147 
0148     return 1;
0149 }
0150 
0151 double KPrinterDlg::lengthScaling() const
0152 {
0153     LengthScaling scaling = (LengthScaling)m_lengthScalingCombo->currentIndex();
0154     return scalingToMeter(scaling);
0155 }
0156 
0157 #include "moc_kprinterdlg.cpp"