stylepage.cpp 5.63 KB
Newer Older
1
/*
Allen Winter's avatar
Allen Winter committed
2
  This file is part of KAddressBook.
3
4
5
6
  SPDX-FileCopyrightText: 2002 Anders Lund <anders.lund@lund.tdcadsl.dk>
  SPDX-FileCopyrightText: Tobias Koenig <tokoe@kde.org>

  SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
7
8
9
10
*/

#include "stylepage.h"

11
12
13
14
15
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPixmap>
#include <QVBoxLayout>
16

Laurent Montel's avatar
Laurent Montel committed
17
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
18
#include <QComboBox>
19

20
21
using namespace KAddressBookImportExport;

22
// helper method to sort contact fields by field label
23
static bool contactFieldsNameLesser(ContactFields::Field field, ContactFields::Field otherField)
24
{
Laurent Montel's avatar
Laurent Montel committed
25
    return QString::localeAwareCompare(ContactFields::label(field), ContactFields::label(otherField)) < 0;
26
27
}

Laurent Montel's avatar
Laurent Montel committed
28
StylePage::StylePage(QWidget *parent, const QString &name)
Laurent Montel's avatar
Laurent Montel committed
29
    : QWidget(parent)
30
{
Laurent Montel's avatar
Laurent Montel committed
31
    setObjectName(name);
Laurent Montel's avatar
Laurent Montel committed
32
    initGUI();
33

Laurent Montel's avatar
Laurent Montel committed
34
    initFieldCombo();
35

Laurent Montel's avatar
Laurent Montel committed
36
37
    mSortTypeCombo->addItem(i18nc("@item:inlistbox Ascending sort order", "Ascending"));
    mSortTypeCombo->addItem(i18nc("@item:inlistbox Descending sort order", "Descending"));
38

39
    connect(mStyleCombo, &QComboBox::activated, this, &StylePage::styleChanged);
40
41
42
43
44
45
}

StylePage::~StylePage()
{
}

Laurent Montel's avatar
Laurent Montel committed
46
void StylePage::setPreview(const QPixmap &pixmap)
47
{
Laurent Montel's avatar
Laurent Montel committed
48
49
    if (pixmap.isNull()) {
        mPreview->setText(i18nc("@label", "(No preview available.)"));
Laurent Montel's avatar
Laurent Montel committed
50
    } else {
Laurent Montel's avatar
Laurent Montel committed
51
        mPreview->setPixmap(pixmap);
Laurent Montel's avatar
Laurent Montel committed
52
    }
53
54
}

Laurent Montel's avatar
Laurent Montel committed
55
void StylePage::addStyleName(const QString &name)
56
{
Laurent Montel's avatar
Laurent Montel committed
57
    mStyleCombo->addItem(name);
58
59
60
61
}

void StylePage::clearStyleNames()
{
Laurent Montel's avatar
Laurent Montel committed
62
    mStyleCombo->clear();
63
64
}

65
void StylePage::setSortField(ContactFields::Field field)
66
{
Laurent Montel's avatar
Laurent Montel committed
67
    mFieldCombo->setCurrentIndex(mFields.indexOf(field));
68
69
}

Laurent Montel's avatar
Laurent Montel committed
70
void StylePage::setSortOrder(Qt::SortOrder sortOrder)
71
{
Laurent Montel's avatar
Laurent Montel committed
72
73
    if (sortOrder == Qt::AscendingOrder) {
        mSortTypeCombo->setCurrentIndex(0);
Laurent Montel's avatar
Laurent Montel committed
74
    } else {
Laurent Montel's avatar
Laurent Montel committed
75
        mSortTypeCombo->setCurrentIndex(1);
Laurent Montel's avatar
Laurent Montel committed
76
    }
77
78
}

79
ContactFields::Field StylePage::sortField() const
80
{
Laurent Montel's avatar
Laurent Montel committed
81
    if (mFieldCombo->currentIndex() == -1) {
82
        return ContactFields::GivenName;
Laurent Montel's avatar
Laurent Montel committed
83
    }
84

Laurent Montel's avatar
Laurent Montel committed
85
    return mFields[mFieldCombo->currentIndex()];
86
87
}

88
Qt::SortOrder StylePage::sortOrder() const
89
{
Laurent Montel's avatar
Laurent Montel committed
90
    return mSortTypeCombo->currentIndex() == 0 ? Qt::AscendingOrder : Qt::DescendingOrder;
91
92
93
94
}

void StylePage::initFieldCombo()
{
Laurent Montel's avatar
Laurent Montel committed
95
    mFieldCombo->clear();
96

97
    mFields = ContactFields::allFields();
Laurent Montel's avatar
Laurent Montel committed
98
    mFields.remove(0); // remove ContactFields::Undefined
99

Laurent Montel's avatar
Laurent Montel committed
100
    std::sort(mFields.begin(), mFields.end(), contactFieldsNameLesser);
101

102
103
    ContactFields::Fields::ConstIterator it;
    const ContactFields::Fields::ConstIterator end(mFields.constEnd());
Laurent Montel's avatar
Laurent Montel committed
104
    for (it = mFields.constBegin(); it != end; ++it) {
105
        mFieldCombo->addItem(ContactFields::label(*it));
Laurent Montel's avatar
Laurent Montel committed
106
    }
107
108
109
110
}

void StylePage::initGUI()
{
Laurent Montel's avatar
Laurent Montel committed
111
    setWindowTitle(i18nc("@title:window", "Choose Printing Style"));
Laurent Montel's avatar
Laurent Montel committed
112

113
    auto topLayout = new QGridLayout(this);
Laurent Montel's avatar
Laurent Montel committed
114

Laurent Montel's avatar
Laurent Montel committed
115
116
117
118
119
120
    auto label = new QLabel(i18nc("@label:textbox",
                                  "What should the print look like?\n"
                                  "KAddressBook has several printing styles, designed "
                                  "for different purposes.\n"
                                  "Choose the style that suits your needs below."),
                            this);
Laurent Montel's avatar
Laurent Montel committed
121
122
    topLayout->addWidget(label, 0, 0, 1, 2);

Laurent Montel's avatar
Laurent Montel committed
123
    auto group = new QGroupBox(i18nc("@title:group", "Sorting"), this);
124
    auto sortLayout = new QGridLayout();
Laurent Montel's avatar
Laurent Montel committed
125
126
    group->setLayout(sortLayout);
    sortLayout->setAlignment(Qt::AlignTop);
Laurent Montel's avatar
Laurent Montel committed
127

Laurent Montel's avatar
Laurent Montel committed
128
129
    label = new QLabel(i18nc("@label:listbox", "Criterion:"), group);
    sortLayout->addWidget(label, 0, 0);
Laurent Montel's avatar
Laurent Montel committed
130

Laurent Montel's avatar
Laurent Montel committed
131
    mFieldCombo = new QComboBox(group);
Laurent Montel's avatar
Laurent Montel committed
132
133
134
135
136
    mFieldCombo->setToolTip(i18nc("@info:tooltip", "Select the primary sort field"));
    mFieldCombo->setWhatsThis(i18nc("@info:whatsthis",
                                    "From this list you can select the field on which your contacts are sorted "
                                    "in the print output.  Use the sorting order option to determine if the "
                                    "sort will be in ascending or descending order."));
Laurent Montel's avatar
Laurent Montel committed
137
    sortLayout->addWidget(mFieldCombo, 0, 1);
Laurent Montel's avatar
Laurent Montel committed
138

Laurent Montel's avatar
Laurent Montel committed
139
140
    label = new QLabel(i18nc("@label:listbox", "Order:"), group);
    sortLayout->addWidget(label, 1, 0);
Laurent Montel's avatar
Laurent Montel committed
141

Laurent Montel's avatar
Laurent Montel committed
142
    mSortTypeCombo = new QComboBox(group);
Laurent Montel's avatar
Laurent Montel committed
143
144
145
146
147
    mSortTypeCombo->setToolTip(i18nc("@info:tooltip", "Select the sorting order"));
    mSortTypeCombo->setWhatsThis(i18nc("@info:whatsthis",
                                       "Choose if you want to sort your contacts in ascending or descending order. "
                                       "Use the sorting criterion option to specify on which contact field the sorting "
                                       "will be performed."));
Laurent Montel's avatar
Laurent Montel committed
148
    sortLayout->addWidget(mSortTypeCombo, 1, 1);
Laurent Montel's avatar
Laurent Montel committed
149

Laurent Montel's avatar
Laurent Montel committed
150
    topLayout->addWidget(group, 1, 0);
Laurent Montel's avatar
Laurent Montel committed
151

Laurent Montel's avatar
Laurent Montel committed
152
    group = new QGroupBox(i18nc("@title:group", "Print Style"), this);
153
    auto styleLayout = new QVBoxLayout();
Laurent Montel's avatar
Laurent Montel committed
154
    group->setLayout(styleLayout);
Laurent Montel's avatar
Laurent Montel committed
155

Laurent Montel's avatar
Laurent Montel committed
156
    mStyleCombo = new QComboBox(group);
Laurent Montel's avatar
Laurent Montel committed
157
158
    mStyleCombo->setToolTip(i18nc("@info:tooltip", "Select the print style"));
    mStyleCombo->setWhatsThis(i18nc("@info:whatsthis", "Choose your desired printing style. See the preview image to help you decide."));
Laurent Montel's avatar
Laurent Montel committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172

    styleLayout->addWidget(mStyleCombo);

    mPreview = new QLabel(group);
    QFont font(mPreview->font());
    font.setPointSize(20);
    mPreview->setFont(font);
    mPreview->setScaledContents(true);
    mPreview->setAlignment(Qt::AlignCenter);
    mPreview->setWordWrap(true);
    styleLayout->addWidget(mPreview);

    topLayout->addWidget(group, 1, 1);
    topLayout->setRowStretch(1, 1);
173
174
}

175
176
int StylePage::printingStyle() const
{
Laurent Montel's avatar
Laurent Montel committed
177
    return mStyleCombo->currentIndex();
178
179
}

Laurent Montel's avatar
Laurent Montel committed
180
void StylePage::setPrintingStyle(int index)
181
{
Laurent Montel's avatar
Laurent Montel committed
182
    mStyleCombo->setCurrentIndex(index);
183
}