File indexing completed on 2024-12-22 04:55:33
0001 /* 0002 This file is part of KAddressBook. 0003 SPDX-FileCopyrightText: 2011 Mario Scheel <zweistein12@gmx.de> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 0006 */ 0007 0008 #include "compactstyle.h" 0009 #include "importexport/contactfields.h" 0010 #include "printprogress.h" 0011 0012 #include "printingwizard.h" 0013 #include "ui_compactstyle.h" 0014 0015 #include <KContacts/Addressee> 0016 0017 #include <KConfigGroup> 0018 #include <KLocalizedString> 0019 #include <KSharedConfig> 0020 #include <QCheckBox> 0021 #include <QPrinter> 0022 #include <QTextDocument> 0023 0024 using namespace KABPrinting; 0025 using namespace KAddressBookImportExport; 0026 0027 const char *CompactStyleConfigSectionName = "CompactStyle"; 0028 const char *WithAlternating = "WithAlternating"; 0029 const char *WithHomeAddress = "WithHomeAddress"; 0030 const char *WithBusinessAddress = "WithBusinessAddress"; 0031 const char *WithBirthday = "WithBirthday"; 0032 const char *WithEMail = "WithEMail"; 0033 const char *FirstColor = "FirstColor"; 0034 const char *SecondColor = "SecondColor"; 0035 0036 namespace KABPrinting 0037 { 0038 class CompactStyleForm : public QWidget, public Ui::CompactStyleForm_Base 0039 { 0040 public: 0041 explicit CompactStyleForm(QWidget *parent) 0042 : QWidget(parent) 0043 { 0044 setObjectName(QLatin1StringView("AppearancePage")); 0045 setupUi(this); 0046 } 0047 }; 0048 } 0049 0050 QString CompactStyle::contactsToHtml(const KContacts::Addressee::List &contacts) const 0051 { 0052 // collect the fields are need to print 0053 ContactFields::Fields fields; 0054 fields << ContactFields::FormattedName; 0055 if (this->withHomeAddress) { 0056 fields << ContactFields::HomeAddressStreet << ContactFields::HomeAddressPostalCode << ContactFields::HomeAddressLocality << ContactFields::HomePhone 0057 << ContactFields::MobilePhone; 0058 } 0059 if (this->withBusinessAddress) { 0060 fields << ContactFields::BusinessAddressStreet << ContactFields::BusinessAddressPostalCode << ContactFields::BusinessAddressLocality 0061 << ContactFields::BusinessPhone; 0062 } 0063 if (this->withEMail) { 0064 fields << ContactFields::PreferredEmail << ContactFields::Email2; 0065 } 0066 if (this->withBirthday) { 0067 fields << ContactFields::Birthday; 0068 } 0069 0070 QString content = QStringLiteral("<html>\n"); 0071 content += QLatin1StringView(" <body>\n"); 0072 content += QLatin1StringView(" <table style=\"font-size:50%; border-width: 0px; \"width=\"100%\">\n"); 0073 0074 bool odd = false; 0075 for (const KContacts::Addressee &contact : contacts) { 0076 // get the values 0077 QStringList values; 0078 for (const ContactFields::Field &field : std::as_const(fields)) { 0079 // we need only values with content 0080 const QString value = ContactFields::value(field, contact).trimmed(); 0081 if (!value.isEmpty()) { 0082 values << value; 0083 } 0084 } 0085 0086 content += QLatin1StringView(" <tr>\n"); 0087 QString style = QStringLiteral("background-color:"); 0088 if (this->withAlternating) { 0089 style += (odd) ? this->firstColor.name() : this->secondColor.name(); 0090 } else { 0091 style += QLatin1StringView("#ffffff"); 0092 } 0093 content += 0094 QLatin1StringView(" <td style=\"") + style + QLatin1StringView(";\">") + values.join(QLatin1StringView("; ")) + QLatin1StringView("</td>\n"); 0095 content += QLatin1StringView(" </tr>\n"); 0096 odd = !odd; 0097 } 0098 0099 content += QLatin1StringView(" </table>\n"); 0100 content += QLatin1StringView(" </body>\n"); 0101 content += QLatin1StringView("</html>\n"); 0102 0103 return content; 0104 } 0105 0106 CompactStyle::CompactStyle(PrintingWizard *parent) 0107 : PrintStyle(parent) 0108 , mPageSettings(new CompactStyleForm(parent)) 0109 { 0110 setPreview(QStringLiteral("compact-style.png")); 0111 setPreferredSortOptions(ContactFields::FormattedName, Qt::AscendingOrder); 0112 0113 addPage(mPageSettings, i18n("Compact Style")); 0114 0115 connect(mPageSettings->cbAlternating, &QCheckBox::clicked, this, &CompactStyle::setAlternatingColors); 0116 0117 // set the controls, with the values in config 0118 KConfigGroup config(KSharedConfig::openConfig(), QLatin1StringView(CompactStyleConfigSectionName)); 0119 0120 withAlternating = config.readEntry(WithAlternating, true); 0121 withHomeAddress = config.readEntry(WithHomeAddress, true); 0122 withBusinessAddress = config.readEntry(WithBusinessAddress, false); 0123 withBirthday = config.readEntry(WithBirthday, true); 0124 withEMail = config.readEntry(WithEMail, true); 0125 0126 mPageSettings->cbFirst->setColor(config.readEntry(FirstColor, QColor(220, 220, 220))); 0127 mPageSettings->cbSecond->setColor(config.readEntry(SecondColor, QColor(255, 255, 255))); 0128 mPageSettings->cbAlternating->setChecked(withAlternating); 0129 mPageSettings->cbWithHomeAddress->setChecked(withHomeAddress); 0130 mPageSettings->cbWithBusinessAddress->setChecked(withBusinessAddress); 0131 mPageSettings->cbWithBirthday->setChecked(withBirthday); 0132 mPageSettings->cbWithEMail->setChecked(withEMail); 0133 0134 // set up the color boxes 0135 setAlternatingColors(); 0136 } 0137 0138 CompactStyle::~CompactStyle() = default; 0139 0140 void CompactStyle::print(const KContacts::Addressee::List &contacts, PrintProgress *progress) 0141 { 0142 // from UI to members 0143 withAlternating = mPageSettings->cbAlternating->isChecked(); 0144 firstColor = mPageSettings->cbFirst->color(); 0145 secondColor = mPageSettings->cbSecond->color(); 0146 withHomeAddress = mPageSettings->cbWithHomeAddress->isChecked(); 0147 withBusinessAddress = mPageSettings->cbWithBusinessAddress->isChecked(); 0148 withBirthday = mPageSettings->cbWithBirthday->isChecked(); 0149 withEMail = mPageSettings->cbWithEMail->isChecked(); 0150 0151 // to config 0152 KConfigGroup config(KSharedConfig::openConfig(), QLatin1StringView(CompactStyleConfigSectionName)); 0153 0154 config.writeEntry(WithAlternating, withAlternating); 0155 config.writeEntry(FirstColor, firstColor); 0156 config.writeEntry(SecondColor, secondColor); 0157 config.writeEntry(WithHomeAddress, withHomeAddress); 0158 config.writeEntry(WithBusinessAddress, withBusinessAddress); 0159 config.writeEntry(WithBirthday, withBirthday); 0160 config.writeEntry(WithEMail, withEMail); 0161 config.sync(); 0162 0163 // print 0164 QPrinter *printer = wizard()->printer(); 0165 printer->setPageMargins(QMarginsF(20, 20, 20, 20), QPageLayout::Point); 0166 0167 progress->addMessage(i18n("Setting up document")); 0168 0169 const QString html = contactsToHtml(contacts); 0170 0171 QTextDocument document; 0172 document.setHtml(html); 0173 0174 progress->addMessage(i18n("Printing")); 0175 0176 document.print(printer); 0177 0178 progress->addMessage(i18nc("Finished printing", "Done")); 0179 } 0180 0181 void CompactStyle::setAlternatingColors() 0182 { 0183 mPageSettings->cbFirst->setEnabled(mPageSettings->cbAlternating->isChecked()); 0184 mPageSettings->lbCbFirst->setEnabled(mPageSettings->cbAlternating->isChecked()); 0185 mPageSettings->cbSecond->setEnabled(mPageSettings->cbAlternating->isChecked()); 0186 mPageSettings->lbCbSecond->setEnabled(mPageSettings->cbAlternating->isChecked()); 0187 } 0188 0189 CompactStyleFactory::CompactStyleFactory(PrintingWizard *parent) 0190 : PrintStyleFactory(parent) 0191 { 0192 } 0193 0194 PrintStyle *CompactStyleFactory::create() const 0195 { 0196 return new CompactStyle(mParent); 0197 } 0198 0199 QString CompactStyleFactory::description() const 0200 { 0201 return i18n("Compact Printing Style"); 0202 } 0203 0204 #include "moc_compactstyle.cpp"