2 This file is part of KAddressBook.
4 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "modelcolumnmanager.h"
24 #include <KContacts/Addressee>
26 #include <KLocalizedString>
30 #include <QContextMenuEvent>
31 #include <QHeaderView>
35 ModelColumnManager::ModelColumnManager(Akonadi::ContactsTreeModel
*model
, QObject
*parent
)
36 : QObject(parent
), mModel(model
), mWidget(Q_NULLPTR
)
40 void ModelColumnManager::load()
42 const QList
<int> settingsColumns
= Settings::contactModelColumns();
43 Akonadi::ContactsTreeModel::Columns columns
;
45 foreach (int column
, settingsColumns
) {
46 columns
.append((Akonadi::ContactsTreeModel::Column
)column
);
49 mModel
->setColumns(columns
);
52 void ModelColumnManager::store()
54 const Akonadi::ContactsTreeModel::Columns columns
= mModel
->columns();
55 QList
<int> settingsColumns
;
57 foreach (int column
, columns
) {
58 settingsColumns
.append((int)column
);
61 Settings::setContactModelColumns(settingsColumns
);
64 void ModelColumnManager::setWidget(QWidget
*widget
)
67 mWidget
->installEventFilter(this);
70 bool ModelColumnManager::eventFilter(QObject
*watched
, QEvent
*event
)
72 if (watched
== mWidget
) {
73 if (event
->type() == QEvent::ContextMenu
) {
76 Akonadi::ContactsTreeModel::Columns columns
= mModel
->columns();
78 QAction
*fullNameAction
= menu
.addAction(i18n("Full Name"));
79 fullNameAction
->setCheckable(true);
80 fullNameAction
->setChecked(
81 columns
.contains(Akonadi::ContactsTreeModel::FullName
));
82 fullNameAction
->setEnabled(false);
84 QAction
*familyNameAction
= menu
.addAction(i18n("Family Name"));
85 familyNameAction
->setCheckable(true);
86 familyNameAction
->setChecked(
87 columns
.contains(Akonadi::ContactsTreeModel::FamilyName
));
89 QAction
*givenNameAction
= menu
.addAction(i18n("Given Name"));
90 givenNameAction
->setCheckable(true);
91 givenNameAction
->setChecked(
92 columns
.contains(Akonadi::ContactsTreeModel::GivenName
));
94 QAction
*birthdayAction
= menu
.addAction(KContacts::Addressee::birthdayLabel());
95 birthdayAction
->setCheckable(true);
96 birthdayAction
->setChecked(
97 columns
.contains(Akonadi::ContactsTreeModel::Birthday
));
99 QAction
*homeAddressAction
= menu
.addAction(i18n("Home Address"));
100 homeAddressAction
->setCheckable(true);
101 homeAddressAction
->setChecked(
102 columns
.contains(Akonadi::ContactsTreeModel::HomeAddress
));
104 QAction
*businessAddressAction
= menu
.addAction(i18n("Business Address"));
105 businessAddressAction
->setCheckable(true);
106 businessAddressAction
->setChecked(
107 columns
.contains(Akonadi::ContactsTreeModel::BusinessAddress
));
109 QAction
*phoneNumbersAction
= menu
.addAction(i18n("Phone Numbers"));
110 phoneNumbersAction
->setCheckable(true);
111 phoneNumbersAction
->setChecked(
112 columns
.contains(Akonadi::ContactsTreeModel::PhoneNumbers
));
114 QAction
*preferredEmailAction
= menu
.addAction(i18n("Preferred EMail"));
115 preferredEmailAction
->setCheckable(true);
116 preferredEmailAction
->setChecked(
117 columns
.contains(Akonadi::ContactsTreeModel::PreferredEmail
));
119 QAction
*allEmailsAction
= menu
.addAction(i18n("All EMails"));
120 allEmailsAction
->setCheckable(true);
121 allEmailsAction
->setChecked(
122 columns
.contains(Akonadi::ContactsTreeModel::AllEmails
));
124 QAction
*organizationAction
= menu
.addAction(KContacts::Addressee::organizationLabel());
125 organizationAction
->setCheckable(true);
126 organizationAction
->setChecked(
127 columns
.contains(Akonadi::ContactsTreeModel::Organization
));
129 QAction
*roleAction
= menu
.addAction(KContacts::Addressee::roleLabel());
130 roleAction
->setCheckable(true);
131 roleAction
->setChecked(
132 columns
.contains(Akonadi::ContactsTreeModel::Role
));
134 QAction
*homepageAction
= menu
.addAction(KContacts::Addressee::urlLabel());
135 homepageAction
->setCheckable(true);
136 homepageAction
->setChecked(
137 columns
.contains(Akonadi::ContactsTreeModel::Homepage
));
139 QAction
*noteAction
= menu
.addAction(KContacts::Addressee::noteLabel());
140 noteAction
->setCheckable(true);
141 noteAction
->setChecked(
142 columns
.contains(Akonadi::ContactsTreeModel::Note
));
144 if (menu
.exec(((QContextMenuEvent
*)event
)->globalPos())) {
145 Akonadi::ContactsTreeModel::Columns columns
;
147 if (fullNameAction
->isChecked()) {
148 columns
<< Akonadi::ContactsTreeModel::FullName
;
150 if (familyNameAction
->isChecked()) {
151 columns
<< Akonadi::ContactsTreeModel::FamilyName
;
153 if (givenNameAction
->isChecked()) {
154 columns
<< Akonadi::ContactsTreeModel::GivenName
;
156 if (birthdayAction
->isChecked()) {
157 columns
<< Akonadi::ContactsTreeModel::Birthday
;
159 if (homeAddressAction
->isChecked()) {
160 columns
<< Akonadi::ContactsTreeModel::HomeAddress
;
162 if (businessAddressAction
->isChecked()) {
163 columns
<< Akonadi::ContactsTreeModel::BusinessAddress
;
165 if (phoneNumbersAction
->isChecked()) {
166 columns
<< Akonadi::ContactsTreeModel::PhoneNumbers
;
168 if (preferredEmailAction
->isChecked()) {
169 columns
<< Akonadi::ContactsTreeModel::PreferredEmail
;
171 if (allEmailsAction
->isChecked()) {
172 columns
<< Akonadi::ContactsTreeModel::AllEmails
;
174 if (organizationAction
->isChecked()) {
175 columns
<< Akonadi::ContactsTreeModel::Organization
;
177 if (roleAction
->isChecked()) {
178 columns
<< Akonadi::ContactsTreeModel::Role
;
180 if (homepageAction
->isChecked()) {
181 columns
<< Akonadi::ContactsTreeModel::Homepage
;
183 if (noteAction
->isChecked()) {
184 columns
<< Akonadi::ContactsTreeModel::Note
;
187 mModel
->setColumns(columns
);
188 QTimer::singleShot(0, this, &ModelColumnManager::adaptHeaderView
);
200 void ModelColumnManager::adaptHeaderView()
202 QHeaderView
*view
= qobject_cast
<QHeaderView
*>(mWidget
);
204 view
->resizeSections(QHeaderView::Stretch
);
206 view
->setDefaultAlignment(mModel
->columns().count() == 1 ? Qt::AlignCenter
: Qt::AlignLeft
);