stylepage.cpp 6.9 KB
Newer Older
1
/*
Allen Winter's avatar
Allen Winter committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  This file is part of KAddressBook.
  Copyright (c) 2002 Anders Lund <anders.lund@lund.tdcadsl.dk>
                     Tobias Koenig <tokoe@kde.org>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

  As a special exception, permission is given to link this program
  with any edition of Qt, and distribute the resulting executable,
  without including the source code for Qt in the source distribution.
23
24
25
26
*/

#include "stylepage.h"

27
28
29
30
31
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPixmap>
#include <QVBoxLayout>
32

Laurent Montel's avatar
Laurent Montel committed
33
#include <QComboBox>
Laurent Montel's avatar
Laurent Montel committed
34
#include <KLocalizedString>
35

36
// helper method to sort contact fields by field label
Laurent Montel's avatar
Laurent Montel committed
37
static bool contactFieldsNameLesser(KAddressBookImportExport::KAddressBookImportExportContactFields::Field field, KAddressBookImportExport::KAddressBookImportExportContactFields::Field otherField)
38
{
Laurent Montel's avatar
Laurent Montel committed
39
40
    return QString::localeAwareCompare(KAddressBookImportExport::KAddressBookImportExportContactFields::label(field),
                                       KAddressBookImportExport::KAddressBookImportExportContactFields::label(otherField)) < 0;
41
42
}

Laurent Montel's avatar
Laurent Montel committed
43
StylePage::StylePage(QWidget *parent, const QString &name)
Laurent Montel's avatar
Laurent Montel committed
44
    : QWidget(parent)
45
{
Laurent Montel's avatar
Laurent Montel committed
46
    setObjectName(name);
Laurent Montel's avatar
Laurent Montel committed
47
    initGUI();
48

Laurent Montel's avatar
Laurent Montel committed
49
    initFieldCombo();
50

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

Laurent Montel's avatar
Laurent Montel committed
54
    connect(mStyleCombo, qOverload<int>(&QComboBox::activated), this, &StylePage::styleChanged);
55
56
57
58
59
60
}

StylePage::~StylePage()
{
}

Laurent Montel's avatar
Laurent Montel committed
61
void StylePage::setPreview(const QPixmap &pixmap)
62
{
Laurent Montel's avatar
Laurent Montel committed
63
64
    if (pixmap.isNull()) {
        mPreview->setText(i18nc("@label", "(No preview available.)"));
Laurent Montel's avatar
Laurent Montel committed
65
    } else {
Laurent Montel's avatar
Laurent Montel committed
66
        mPreview->setPixmap(pixmap);
Laurent Montel's avatar
Laurent Montel committed
67
    }
68
69
}

Laurent Montel's avatar
Laurent Montel committed
70
void StylePage::addStyleName(const QString &name)
71
{
Laurent Montel's avatar
Laurent Montel committed
72
    mStyleCombo->addItem(name);
73
74
75
76
}

void StylePage::clearStyleNames()
{
Laurent Montel's avatar
Laurent Montel committed
77
    mStyleCombo->clear();
78
79
}

Laurent Montel's avatar
Laurent Montel committed
80
void StylePage::setSortField(KAddressBookImportExport::KAddressBookImportExportContactFields::Field field)
81
{
Laurent Montel's avatar
Laurent Montel committed
82
    mFieldCombo->setCurrentIndex(mFields.indexOf(field));
83
84
}

Laurent Montel's avatar
Laurent Montel committed
85
void StylePage::setSortOrder(Qt::SortOrder sortOrder)
86
{
Laurent Montel's avatar
Laurent Montel committed
87
88
    if (sortOrder == Qt::AscendingOrder) {
        mSortTypeCombo->setCurrentIndex(0);
Laurent Montel's avatar
Laurent Montel committed
89
    } else {
Laurent Montel's avatar
Laurent Montel committed
90
        mSortTypeCombo->setCurrentIndex(1);
Laurent Montel's avatar
Laurent Montel committed
91
    }
92
93
}

Laurent Montel's avatar
Laurent Montel committed
94
KAddressBookImportExport::KAddressBookImportExportContactFields::Field StylePage::sortField() const
95
{
Laurent Montel's avatar
Laurent Montel committed
96
    if (mFieldCombo->currentIndex() == -1) {
Laurent Montel's avatar
Laurent Montel committed
97
        return KAddressBookImportExport::KAddressBookImportExportContactFields::GivenName;
Laurent Montel's avatar
Laurent Montel committed
98
    }
99

Laurent Montel's avatar
Laurent Montel committed
100
    return mFields[ mFieldCombo->currentIndex() ];
101
102
}

103
Qt::SortOrder StylePage::sortOrder() const
104
{
Laurent Montel's avatar
Laurent Montel committed
105
    return mSortTypeCombo->currentIndex() == 0 ? Qt::AscendingOrder : Qt::DescendingOrder;
106
107
108
109
}

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

Laurent Montel's avatar
Laurent Montel committed
112
113
    mFields = KAddressBookImportExport::KAddressBookImportExportContactFields::allFields();
    mFields.remove(0);   // remove KAddressBookImportExport::KAddressBookImportExportContactFields::Undefined
114

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

Laurent Montel's avatar
Laurent Montel committed
117
    KAddressBookImportExport::KAddressBookImportExportContactFields::Fields::ConstIterator it;
Laurent Montel's avatar
Laurent Montel committed
118
    const KAddressBookImportExport::KAddressBookImportExportContactFields::Fields::ConstIterator end(mFields.constEnd());
Laurent Montel's avatar
Laurent Montel committed
119
    for (it = mFields.constBegin(); it != end; ++it) {
Laurent Montel's avatar
Laurent Montel committed
120
        mFieldCombo->addItem(KAddressBookImportExport::KAddressBookImportExportContactFields::label(*it));
Laurent Montel's avatar
Laurent Montel committed
121
    }
122
123
124
125
}

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

Laurent Montel's avatar
Laurent Montel committed
128
    QGridLayout *topLayout = new QGridLayout(this);
Laurent Montel's avatar
Laurent Montel committed
129

Laurent Montel's avatar
Laurent Montel committed
130
131
    QLabel *label
        = new QLabel(
Laurent Montel's avatar
Laurent Montel committed
132
133
134
135
              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
136
137
138
    topLayout->addWidget(label, 0, 0, 1, 2);

    QGroupBox *group = new QGroupBox(i18nc("@title:group", "Sorting"), this);
Laurent Montel's avatar
Laurent Montel committed
139
    QGridLayout *sortLayout = new QGridLayout();
Laurent Montel's avatar
Laurent Montel committed
140
141
    group->setLayout(sortLayout);
    sortLayout->setAlignment(Qt::AlignTop);
Laurent Montel's avatar
Laurent Montel committed
142

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

Laurent Montel's avatar
Laurent Montel committed
146
    mFieldCombo = new QComboBox(group);
Laurent Montel's avatar
Laurent Montel committed
147
    mFieldCombo->setToolTip(
Laurent Montel's avatar
Laurent Montel committed
148
        i18nc("@info:tooltip", "Select the primary sort field"));
Laurent Montel's avatar
Laurent Montel committed
149
    mFieldCombo->setWhatsThis(
Laurent Montel's avatar
Laurent Montel committed
150
151
152
153
154
        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."));
    sortLayout->addWidget(mFieldCombo, 0, 1);
Laurent Montel's avatar
Laurent Montel committed
155

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

Laurent Montel's avatar
Laurent Montel committed
159
    mSortTypeCombo = new QComboBox(group);
Laurent Montel's avatar
Laurent Montel committed
160
    mSortTypeCombo->setToolTip(
Laurent Montel's avatar
Laurent Montel committed
161
        i18nc("@info:tooltip", "Select the sorting order"));
Laurent Montel's avatar
Laurent Montel committed
162
    mSortTypeCombo->setWhatsThis(
Laurent Montel's avatar
Laurent Montel committed
163
164
165
166
167
        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."));
    sortLayout->addWidget(mSortTypeCombo, 1, 1);
Laurent Montel's avatar
Laurent Montel committed
168

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

Laurent Montel's avatar
Laurent Montel committed
171
    group = new QGroupBox(i18nc("@title:group", "Print Style"), this);
Laurent Montel's avatar
Laurent Montel committed
172
    QVBoxLayout *styleLayout = new QVBoxLayout();
Laurent Montel's avatar
Laurent Montel committed
173
    group->setLayout(styleLayout);
Laurent Montel's avatar
Laurent Montel committed
174

Laurent Montel's avatar
Laurent Montel committed
175
    mStyleCombo = new QComboBox(group);
Laurent Montel's avatar
Laurent Montel committed
176
    mStyleCombo->setToolTip(
Laurent Montel's avatar
Laurent Montel committed
177
        i18nc("@info:tooltip", "Select the print style"));
Laurent Montel's avatar
Laurent Montel committed
178
    mStyleCombo->setWhatsThis(
Laurent Montel's avatar
Laurent Montel committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        i18nc("@info:whatsthis",
              "Choose your desired printing style. See the preview image to help you decide."));

    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);
195
196
}

197
198
int StylePage::printingStyle() const
{
Laurent Montel's avatar
Laurent Montel committed
199
    return mStyleCombo->currentIndex();
200
201
}

Laurent Montel's avatar
Laurent Montel committed
202
void StylePage::setPrintingStyle(int index)
203
{
Laurent Montel's avatar
Laurent Montel committed
204
    mStyleCombo->setCurrentIndex(index);
205
}