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 "tablecellformatdialog.h"
0009 
0010 #include <KColorButton>
0011 #include <KLocalizedString>
0012 #include <KSeparator>
0013 #include <QComboBox>
0014 
0015 #include <QCheckBox>
0016 #include <QDialogButtonBox>
0017 #include <QLabel>
0018 #include <QVBoxLayout>
0019 
0020 using namespace KPIMTextEdit;
0021 
0022 class TableCellFormatDialog::TableCellFormatDialogPrivate
0023 {
0024 public:
0025     explicit TableCellFormatDialogPrivate(TableCellFormatDialog *qq)
0026         : q(qq)
0027     {
0028         q->setWindowTitle(i18nc("@title:window", "Cell Format"));
0029         auto mainLayout = new QVBoxLayout;
0030         q->setLayout(mainLayout);
0031 
0032         auto hbox = new QHBoxLayout;
0033         auto lab = new QLabel(i18n("Vertical Alignment:"));
0034         hbox->addWidget(lab);
0035         verticalAlignment = new QComboBox;
0036         verticalAlignment->addItem(i18n("Top"), QTextCharFormat::AlignTop);
0037         verticalAlignment->addItem(i18n("Middle"), QTextCharFormat::AlignMiddle);
0038         verticalAlignment->addItem(i18n("Bottom"), QTextCharFormat::AlignBottom);
0039 
0040         hbox->addWidget(verticalAlignment);
0041         mainLayout->addLayout(hbox);
0042 
0043         auto sep = new KSeparator;
0044         mainLayout->addWidget(sep);
0045 
0046         hbox = new QHBoxLayout;
0047         useBackgroundColor = new QCheckBox(i18n("Background Color:"));
0048         hbox->addWidget(useBackgroundColor);
0049         backgroundColor = new KColorButton;
0050         backgroundColor->setDefaultColor(Qt::white);
0051         hbox->addWidget(backgroundColor);
0052         mainLayout->addLayout(hbox);
0053 
0054         sep = new KSeparator;
0055         mainLayout->addWidget(sep);
0056         backgroundColor->setEnabled(false);
0057         q->connect(useBackgroundColor, &QCheckBox::toggled, backgroundColor, &KColorButton::setEnabled);
0058 
0059         auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, q);
0060         buttonBox->button(QDialogButtonBox::Ok)->setShortcut(Qt::CTRL | Qt::Key_Return);
0061         q->connect(buttonBox, &QDialogButtonBox::accepted, q, &QDialog::accept);
0062         q->connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
0063 
0064         mainLayout->addWidget(buttonBox);
0065     }
0066 
0067     QCheckBox *useBackgroundColor = nullptr;
0068 
0069     KColorButton *backgroundColor = nullptr;
0070     QComboBox *verticalAlignment = nullptr;
0071     TableCellFormatDialog *const q;
0072 };
0073 
0074 TableCellFormatDialog::TableCellFormatDialog(QWidget *parent)
0075     : QDialog(parent)
0076     , d(new TableCellFormatDialogPrivate(this))
0077 {
0078 }
0079 
0080 TableCellFormatDialog::~TableCellFormatDialog() = default;
0081 
0082 QColor TableCellFormatDialog::tableCellBackgroundColor() const
0083 {
0084     return d->backgroundColor->color();
0085 }
0086 
0087 void TableCellFormatDialog::setTableCellBackgroundColor(const QColor &col)
0088 {
0089     d->backgroundColor->setColor(col);
0090     d->useBackgroundColor->setChecked(true);
0091 }
0092 
0093 QTextCharFormat::VerticalAlignment TableCellFormatDialog::verticalAlignment() const
0094 {
0095     return static_cast<QTextCharFormat::VerticalAlignment>(d->verticalAlignment->itemData(d->verticalAlignment->currentIndex()).toInt());
0096 }
0097 
0098 void TableCellFormatDialog::setVerticalAlignment(QTextCharFormat::VerticalAlignment vertical)
0099 {
0100     d->verticalAlignment->setCurrentIndex(d->verticalAlignment->findData(QVariant(vertical)));
0101 }
0102 
0103 bool TableCellFormatDialog::useBackgroundColor() const
0104 {
0105     return d->useBackgroundColor->isChecked();
0106 }
0107 
0108 #include "moc_tablecellformatdialog.cpp"