File indexing completed on 2024-05-05 05:21:42

0001 /*
0002   SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "tableformatdialog.h"
0009 #include "inserttablewidget.h"
0010 
0011 #include <KColorButton>
0012 #include <KLocalizedString>
0013 #include <KSeparator>
0014 #include <QComboBox>
0015 
0016 #include <QCheckBox>
0017 #include <QDialogButtonBox>
0018 #include <QHBoxLayout>
0019 #include <QLabel>
0020 #include <QSpinBox>
0021 #include <QVBoxLayout>
0022 
0023 using namespace KPIMTextEdit;
0024 
0025 class TableFormatDialog::TableFormatDialogPrivate
0026 {
0027 public:
0028     explicit TableFormatDialogPrivate(TableFormatDialog *qq)
0029         : q(qq)
0030     {
0031         q->setWindowTitle(i18nc("@title:window", "Table Format"));
0032         auto mainLayout = new QVBoxLayout(q);
0033 
0034         auto page = new QWidget(q);
0035         auto lay = new QVBoxLayout(page);
0036         lay->setContentsMargins({});
0037         tableWidget = new InsertTableWidget;
0038         lay->addWidget(tableWidget);
0039 
0040         auto sep = new KSeparator;
0041         lay->addWidget(sep);
0042 
0043         auto hbox = new QHBoxLayout;
0044         auto lab = new QLabel(i18n("Spacing:"));
0045         hbox->addWidget(lab);
0046         spacing = new QSpinBox;
0047         spacing->setMinimum(0);
0048         hbox->addWidget(spacing);
0049         lab = new QLabel(i18n("pixels between cells"));
0050         hbox->addWidget(lab);
0051         lay->addLayout(hbox);
0052 
0053         hbox = new QHBoxLayout;
0054         lab = new QLabel(i18n("Padding:"));
0055         hbox->addWidget(lab);
0056         padding = new QSpinBox;
0057         padding->setMinimum(0);
0058         hbox->addWidget(padding);
0059         lab = new QLabel(i18n("pixels between cell border and content"));
0060         hbox->addWidget(lab);
0061         lay->addLayout(hbox);
0062 
0063         sep = new KSeparator;
0064         lay->addWidget(sep);
0065 
0066         alignment = new QComboBox;
0067         alignment->addItem(i18n("Left"), Qt::AlignLeft);
0068         alignment->addItem(i18n("Right"), Qt::AlignRight);
0069         alignment->addItem(i18n("Center"), Qt::AlignHCenter);
0070         alignment->addItem(i18n("Justify"), Qt::AlignJustify);
0071 
0072         hbox = new QHBoxLayout;
0073         lab = new QLabel(i18n("Table Alignment:"));
0074         hbox->addWidget(lab);
0075         hbox->addWidget(alignment);
0076 
0077         lay->addLayout(hbox);
0078 
0079         sep = new KSeparator;
0080         lay->addWidget(sep);
0081 
0082         hbox = new QHBoxLayout;
0083         useBackgroundColor = new QCheckBox(i18n("Background Color:"));
0084 
0085         hbox->addWidget(useBackgroundColor);
0086         backgroundColor = new KColorButton;
0087         backgroundColor->setDefaultColor(Qt::white);
0088         hbox->addWidget(backgroundColor);
0089         lay->addLayout(hbox);
0090 
0091         sep = new KSeparator;
0092         lay->addWidget(sep);
0093         backgroundColor->setEnabled(false);
0094         q->connect(useBackgroundColor, &QCheckBox::toggled, backgroundColor, &KColorButton::setEnabled);
0095         auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, q);
0096         connect(buttonBox, &QDialogButtonBox::accepted, q, &QDialog::accept);
0097         connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
0098 
0099         mainLayout->addWidget(page);
0100         mainLayout->addWidget(buttonBox);
0101     }
0102 
0103     QCheckBox *useBackgroundColor = nullptr;
0104     KColorButton *backgroundColor = nullptr;
0105     QComboBox *alignment = nullptr;
0106     QSpinBox *spacing = nullptr;
0107     QSpinBox *padding = nullptr;
0108     KPIMTextEdit::InsertTableWidget *tableWidget = nullptr;
0109     TableFormatDialog *const q;
0110 };
0111 
0112 TableFormatDialog::TableFormatDialog(QWidget *parent)
0113     : QDialog(parent)
0114     , d(new TableFormatDialogPrivate(this))
0115 {
0116 }
0117 
0118 TableFormatDialog::~TableFormatDialog() = default;
0119 
0120 int TableFormatDialog::columns() const
0121 {
0122     return d->tableWidget->columns();
0123 }
0124 
0125 int TableFormatDialog::rows() const
0126 {
0127     return d->tableWidget->rows();
0128 }
0129 
0130 int TableFormatDialog::border() const
0131 {
0132     return d->tableWidget->border();
0133 }
0134 
0135 void TableFormatDialog::setColumns(int col)
0136 {
0137     d->tableWidget->setColumns(col);
0138 }
0139 
0140 void TableFormatDialog::setRows(int row)
0141 {
0142     d->tableWidget->setRows(row);
0143 }
0144 
0145 void TableFormatDialog::setBorder(int border)
0146 {
0147     d->tableWidget->setBorder(border);
0148 }
0149 
0150 int TableFormatDialog::padding() const
0151 {
0152     return d->padding->value();
0153 }
0154 
0155 void TableFormatDialog::setPadding(int value)
0156 {
0157     d->padding->setValue(value);
0158 }
0159 
0160 int TableFormatDialog::spacing() const
0161 {
0162     return d->spacing->value();
0163 }
0164 
0165 void TableFormatDialog::setSpacing(int value)
0166 {
0167     d->spacing->setValue(value);
0168 }
0169 
0170 void TableFormatDialog::setAlignment(Qt::Alignment alignment)
0171 {
0172     d->alignment->setCurrentIndex(d->alignment->findData(QVariant(alignment)));
0173 }
0174 
0175 Qt::Alignment TableFormatDialog::alignment() const
0176 {
0177     return static_cast<Qt::Alignment>(d->alignment->itemData(d->alignment->currentIndex()).toInt());
0178 }
0179 
0180 QTextLength::Type TableFormatDialog::typeOfLength() const
0181 {
0182     return d->tableWidget->typeOfLength();
0183 }
0184 
0185 int TableFormatDialog::length() const
0186 {
0187     return d->tableWidget->length();
0188 }
0189 
0190 void TableFormatDialog::setLength(int val)
0191 {
0192     d->tableWidget->setLength(val);
0193 }
0194 
0195 void TableFormatDialog::setTypeOfLength(QTextLength::Type type)
0196 {
0197     d->tableWidget->setTypeOfLength(type);
0198 }
0199 
0200 QColor TableFormatDialog::tableBackgroundColor() const
0201 {
0202     return d->backgroundColor->color();
0203 }
0204 
0205 void TableFormatDialog::setTableBackgroundColor(const QColor &col)
0206 {
0207     d->backgroundColor->setColor(col);
0208     d->useBackgroundColor->setChecked(true);
0209 }
0210 
0211 bool TableFormatDialog::useBackgroundColor() const
0212 {
0213     return d->useBackgroundColor->isChecked();
0214 }
0215 
0216 #include "moc_tableformatdialog.cpp"