• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
  • contact
  • editor
customfieldsdelegate.cpp
1/*
2 This file is part of Akonadi Contact.
3
4 Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20*/
21
22#include "customfieldsdelegate.h"
23
24#include "customfieldsmodel.h"
25
26#include <kicon.h>
27#include <klocalizedstring.h>
28
29#include <QDateEdit>
30#include <QDateTimeEdit>
31#include <QCheckBox>
32#include <QSpinBox>
33#include <QTimeEdit>
34
35CustomFieldsDelegate::CustomFieldsDelegate(QObject *parent)
36 : QStyledItemDelegate(parent)
37{
38}
39
40CustomFieldsDelegate::~CustomFieldsDelegate()
41{
42}
43
44QWidget *CustomFieldsDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &item, const QModelIndex &index) const
45{
46 if (index.column() == 1) {
47 const CustomField::Type type = static_cast<CustomField::Type>(index.data(CustomFieldsModel::TypeRole).toInt());
48
49 switch (type) {
50 case CustomField::TextType:
51 case CustomField::UrlType:
52 default:
53 return QStyledItemDelegate::createEditor(parent, item, index);
54 break;
55 case CustomField::NumericType: {
56 QSpinBox *editor = new QSpinBox(parent);
57 editor->setFrame(false);
58 editor->setAutoFillBackground(true);
59 return editor;
60 break;
61 }
62 case CustomField::BooleanType: {
63 QCheckBox *editor = new QCheckBox(parent);
64 return editor;
65 break;
66 }
67 case CustomField::DateType: {
68 QDateEdit *editor = new QDateEdit(parent);
69 editor->setFrame(false);
70 editor->setAutoFillBackground(true);
71 return editor;
72 break;
73 }
74 case CustomField::TimeType: {
75 QTimeEdit *editor = new QTimeEdit(parent);
76 editor->setFrame(false);
77 editor->setAutoFillBackground(true);
78 return editor;
79 break;
80 }
81 case CustomField::DateTimeType: {
82 QDateTimeEdit *editor = new QDateTimeEdit(parent);
83 editor->setFrame(false);
84 editor->setAutoFillBackground(true);
85 return editor;
86 break;
87 }
88 }
89 } else {
90 return QStyledItemDelegate::createEditor(parent, item, index);
91 }
92}
93
94void CustomFieldsDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
95{
96 if (index.column() == 1) {
97 const CustomField::Type type = static_cast<CustomField::Type>(index.data(CustomFieldsModel::TypeRole).toInt());
98
99 switch (type) {
100 case CustomField::TextType:
101 case CustomField::UrlType:
102 QStyledItemDelegate::setEditorData(editor, index);
103 break;
104 case CustomField::NumericType: {
105 QSpinBox *widget = qobject_cast<QSpinBox *>(editor);
106 widget->setValue(index.data(Qt::EditRole).toInt());
107 break;
108 }
109 case CustomField::BooleanType: {
110 QCheckBox *widget = qobject_cast<QCheckBox *>(editor);
111 widget->setChecked(index.data(Qt::EditRole).toString() == QLatin1String("true"));
112 break;
113 }
114 case CustomField::DateType: {
115 QDateEdit *widget = qobject_cast<QDateEdit *>(editor);
116 widget->setDisplayFormat(QLatin1String("dd.MM.yyyy"));
117 widget->setDate(QDate::fromString(index.data(Qt::EditRole).toString(), Qt::ISODate));
118 break;
119 }
120 case CustomField::TimeType: {
121 QTimeEdit *widget = qobject_cast<QTimeEdit *>(editor);
122 widget->setDisplayFormat(QLatin1String("hh:mm"));
123 widget->setTime(QTime::fromString(index.data(Qt::EditRole).toString(), Qt::ISODate));
124 break;
125 }
126 case CustomField::DateTimeType: {
127 QDateTimeEdit *widget = qobject_cast<QDateTimeEdit *>(editor);
128 widget->setDisplayFormat(QLatin1String("dd.MM.yyyy hh:mm"));
129 widget->setDateTime(QDateTime::fromString(index.data(Qt::EditRole).toString(), Qt::ISODate));
130 break;
131 }
132 }
133 } else {
134 QStyledItemDelegate::setEditorData(editor, index);
135 }
136}
137
138void CustomFieldsDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
139{
140 if (index.column() == 1) {
141 const CustomField::Type type = static_cast<CustomField::Type>(index.data(CustomFieldsModel::TypeRole).toInt());
142
143 switch (type) {
144 case CustomField::TextType:
145 case CustomField::UrlType:
146 QStyledItemDelegate::setModelData(editor, model, index);
147 break;
148 case CustomField::NumericType: {
149 QSpinBox *widget = qobject_cast<QSpinBox *>(editor);
150 model->setData(index, QString::number(widget->value()));
151 break;
152 }
153 case CustomField::BooleanType: {
154 QCheckBox *widget = qobject_cast<QCheckBox *>(editor);
155 model->setData(index, widget->isChecked() ? QLatin1String("true") : QLatin1String("false"));
156 break;
157 }
158 case CustomField::DateType: {
159 QDateEdit *widget = qobject_cast<QDateEdit *>(editor);
160 model->setData(index, widget->date().toString(Qt::ISODate));
161 break;
162 }
163 case CustomField::TimeType: {
164 QTimeEdit *widget = qobject_cast<QTimeEdit *>(editor);
165 model->setData(index, widget->time().toString(Qt::ISODate));
166 break;
167 }
168 case CustomField::DateTimeType: {
169 QDateTimeEdit *widget = qobject_cast<QDateTimeEdit *>(editor);
170 model->setData(index, widget->dateTime().toString(Qt::ISODate));
171 break;
172 }
173 }
174 } else {
175 QStyledItemDelegate::setModelData(editor, model, index);
176 }
177}
178
179void CustomFieldsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
180{
181 //TODO: somehow mark local/global/external fields
182 QStyledItemDelegate::paint(painter, option, index);
183}
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jul 21 2022 00:00:00 by doxygen 1.9.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal