File indexing completed on 2024-12-22 04:55:33

0001 /*
0002   This file is part of KAddressBook.
0003   SPDX-FileCopyrightText: 2002 Anders Lund <anders.lund@lund.tdcadsl.dk>
0004   SPDX-FileCopyrightText: Tobias Koenig <tokoe@kde.org>
0005 
0006   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0007 */
0008 
0009 #include "stylepage.h"
0010 
0011 #include <QGridLayout>
0012 #include <QGroupBox>
0013 #include <QLabel>
0014 #include <QPixmap>
0015 #include <QVBoxLayout>
0016 
0017 #include <KLocalizedString>
0018 #include <QComboBox>
0019 
0020 using namespace KAddressBookImportExport;
0021 
0022 // helper method to sort contact fields by field label
0023 static bool contactFieldsNameLesser(ContactFields::Field field, ContactFields::Field otherField)
0024 {
0025     return QString::localeAwareCompare(ContactFields::label(field), ContactFields::label(otherField)) < 0;
0026 }
0027 
0028 StylePage::StylePage(QWidget *parent, const QString &name)
0029     : QWidget(parent)
0030 {
0031     setObjectName(name);
0032     initGUI();
0033 
0034     initFieldCombo();
0035 
0036     mSortTypeCombo->addItem(i18nc("@item:inlistbox Ascending sort order", "Ascending"));
0037     mSortTypeCombo->addItem(i18nc("@item:inlistbox Descending sort order", "Descending"));
0038 
0039     connect(mStyleCombo, &QComboBox::activated, this, &StylePage::styleChanged);
0040 }
0041 
0042 StylePage::~StylePage() = default;
0043 
0044 void StylePage::setPreview(const QPixmap &pixmap)
0045 {
0046     if (pixmap.isNull()) {
0047         mPreview->setText(i18nc("@label", "(No preview available.)"));
0048     } else {
0049         mPreview->setPixmap(pixmap);
0050     }
0051 }
0052 
0053 void StylePage::addStyleName(const QString &name)
0054 {
0055     mStyleCombo->addItem(name);
0056 }
0057 
0058 void StylePage::clearStyleNames()
0059 {
0060     mStyleCombo->clear();
0061 }
0062 
0063 void StylePage::setSortField(ContactFields::Field field)
0064 {
0065     mFieldCombo->setCurrentIndex(mFields.indexOf(field));
0066 }
0067 
0068 void StylePage::setSortOrder(Qt::SortOrder sortOrder)
0069 {
0070     if (sortOrder == Qt::AscendingOrder) {
0071         mSortTypeCombo->setCurrentIndex(0);
0072     } else {
0073         mSortTypeCombo->setCurrentIndex(1);
0074     }
0075 }
0076 
0077 ContactFields::Field StylePage::sortField() const
0078 {
0079     if (mFieldCombo->currentIndex() == -1) {
0080         return ContactFields::GivenName;
0081     }
0082 
0083     return mFields[mFieldCombo->currentIndex()];
0084 }
0085 
0086 Qt::SortOrder StylePage::sortOrder() const
0087 {
0088     return mSortTypeCombo->currentIndex() == 0 ? Qt::AscendingOrder : Qt::DescendingOrder;
0089 }
0090 
0091 void StylePage::initFieldCombo()
0092 {
0093     mFieldCombo->clear();
0094 
0095     mFields = ContactFields::allFields();
0096     mFields.remove(0); // remove ContactFields::Undefined
0097 
0098     std::sort(mFields.begin(), mFields.end(), contactFieldsNameLesser);
0099 
0100     ContactFields::Fields::ConstIterator it;
0101     const ContactFields::Fields::ConstIterator end(mFields.constEnd());
0102     for (it = mFields.constBegin(); it != end; ++it) {
0103         mFieldCombo->addItem(ContactFields::label(*it));
0104     }
0105 }
0106 
0107 void StylePage::initGUI()
0108 {
0109     setWindowTitle(i18nc("@title:window", "Choose Printing Style"));
0110 
0111     auto topLayout = new QGridLayout(this);
0112 
0113     auto label = new QLabel(i18nc("@label:textbox",
0114                                   "What should the print look like?\n"
0115                                   "KAddressBook has several printing styles, designed "
0116                                   "for different purposes.\n"
0117                                   "Choose the style that suits your needs below."),
0118                             this);
0119     topLayout->addWidget(label, 0, 0, 1, 2);
0120 
0121     auto group = new QGroupBox(i18nc("@title:group", "Sorting"), this);
0122     auto sortLayout = new QGridLayout();
0123     group->setLayout(sortLayout);
0124     sortLayout->setAlignment(Qt::AlignTop);
0125 
0126     label = new QLabel(i18nc("@label:listbox", "Criterion:"), group);
0127     sortLayout->addWidget(label, 0, 0);
0128 
0129     mFieldCombo = new QComboBox(group);
0130     mFieldCombo->setToolTip(i18nc("@info:tooltip", "Select the primary sort field"));
0131     mFieldCombo->setWhatsThis(i18nc("@info:whatsthis",
0132                                     "From this list you can select the field on which your contacts are sorted "
0133                                     "in the print output.  Use the sorting order option to determine if the "
0134                                     "sort will be in ascending or descending order."));
0135     sortLayout->addWidget(mFieldCombo, 0, 1);
0136 
0137     label = new QLabel(i18nc("@label:listbox", "Order:"), group);
0138     sortLayout->addWidget(label, 1, 0);
0139 
0140     mSortTypeCombo = new QComboBox(group);
0141     mSortTypeCombo->setToolTip(i18nc("@info:tooltip", "Select the sorting order"));
0142     mSortTypeCombo->setWhatsThis(i18nc("@info:whatsthis",
0143                                        "Choose if you want to sort your contacts in ascending or descending order. "
0144                                        "Use the sorting criterion option to specify on which contact field the sorting "
0145                                        "will be performed."));
0146     sortLayout->addWidget(mSortTypeCombo, 1, 1);
0147 
0148     topLayout->addWidget(group, 1, 0);
0149 
0150     group = new QGroupBox(i18nc("@title:group", "Print Style"), this);
0151     auto styleLayout = new QVBoxLayout();
0152     group->setLayout(styleLayout);
0153 
0154     mStyleCombo = new QComboBox(group);
0155     mStyleCombo->setToolTip(i18nc("@info:tooltip", "Select the print style"));
0156     mStyleCombo->setWhatsThis(i18nc("@info:whatsthis", "Choose your desired printing style. See the preview image to help you decide."));
0157 
0158     styleLayout->addWidget(mStyleCombo);
0159 
0160     mPreview = new QLabel(group);
0161     QFont font(mPreview->font());
0162     font.setPointSize(20);
0163     mPreview->setFont(font);
0164     mPreview->setScaledContents(true);
0165     mPreview->setAlignment(Qt::AlignCenter);
0166     mPreview->setWordWrap(true);
0167     styleLayout->addWidget(mPreview);
0168 
0169     topLayout->addWidget(group, 1, 1);
0170     topLayout->setRowStretch(1, 1);
0171 }
0172 
0173 int StylePage::printingStyle() const
0174 {
0175     return mStyleCombo->currentIndex();
0176 }
0177 
0178 void StylePage::setPrintingStyle(int index)
0179 {
0180     mStyleCombo->setCurrentIndex(index);
0181 }
0182 
0183 #include "moc_stylepage.cpp"