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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • 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 
35 CustomFieldsDelegate::CustomFieldsDelegate( QObject *parent )
36  : QStyledItemDelegate( parent )
37 {
38 }
39 
40 CustomFieldsDelegate::~CustomFieldsDelegate()
41 {
42 }
43 
44 QWidget* 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  {
57  QSpinBox *editor = new QSpinBox( parent );
58  editor->setFrame( false );
59  editor->setAutoFillBackground( true );
60  return editor;
61  }
62  break;
63  case CustomField::BooleanType:
64  {
65  QCheckBox *editor = new QCheckBox( parent );
66  return editor;
67  }
68  break;
69  case CustomField::DateType:
70  {
71  QDateEdit *editor = new QDateEdit( parent );
72  editor->setFrame( false );
73  editor->setAutoFillBackground( true );
74  return editor;
75  }
76  break;
77  case CustomField::TimeType:
78  {
79  QTimeEdit *editor = new QTimeEdit( parent );
80  editor->setFrame( false );
81  editor->setAutoFillBackground( true );
82  return editor;
83  }
84  break;
85  case CustomField::DateTimeType:
86  {
87  QDateTimeEdit *editor = new QDateTimeEdit( parent );
88  editor->setFrame( false );
89  editor->setAutoFillBackground( true );
90  return editor;
91  }
92  break;
93  }
94  } else {
95  return QStyledItemDelegate::createEditor( parent, item, index );
96  }
97 }
98 
99 void CustomFieldsDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
100 {
101  if ( index.column() == 1 ) {
102  const CustomField::Type type = static_cast<CustomField::Type>( index.data( CustomFieldsModel::TypeRole ).toInt() );
103 
104  switch ( type ) {
105  case CustomField::TextType:
106  case CustomField::UrlType:
107  QStyledItemDelegate::setEditorData( editor, index );
108  break;
109  case CustomField::NumericType:
110  {
111  QSpinBox *widget = qobject_cast<QSpinBox*>( editor );
112  widget->setValue( index.data( Qt::EditRole ).toInt() );
113  }
114  break;
115  case CustomField::BooleanType:
116  {
117  QCheckBox *widget = qobject_cast<QCheckBox*>( editor );
118  widget->setChecked( index.data( Qt::EditRole ).toString() == QLatin1String( "true" ) );
119  }
120  break;
121  case CustomField::DateType:
122  {
123  QDateEdit *widget = qobject_cast<QDateEdit*>( editor );
124  widget->setDisplayFormat( QLatin1String( "dd.MM.yyyy" ) );
125  widget->setDate( QDate::fromString( index.data( Qt::EditRole ).toString(), Qt::ISODate ) );
126  }
127  break;
128  case CustomField::TimeType:
129  {
130  QTimeEdit *widget = qobject_cast<QTimeEdit*>( editor );
131  widget->setDisplayFormat( QLatin1String( "hh:mm" ) );
132  widget->setTime( QTime::fromString( index.data( Qt::EditRole ).toString(), Qt::ISODate ) );
133  }
134  break;
135  case CustomField::DateTimeType:
136  {
137  QDateTimeEdit *widget = qobject_cast<QDateTimeEdit*>( editor );
138  widget->setDisplayFormat( QLatin1String( "dd.MM.yyyy hh:mm" ) );
139  widget->setDateTime( QDateTime::fromString( index.data( Qt::EditRole ).toString(), Qt::ISODate ) );
140  }
141  break;
142  }
143  } else {
144  QStyledItemDelegate::setEditorData( editor, index );
145  }
146 }
147 
148 void CustomFieldsDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
149 {
150  if ( index.column() == 1 ) {
151  const CustomField::Type type = static_cast<CustomField::Type>( index.data( CustomFieldsModel::TypeRole ).toInt() );
152 
153  switch ( type ) {
154  case CustomField::TextType:
155  case CustomField::UrlType:
156  QStyledItemDelegate::setModelData( editor, model, index );
157  break;
158  case CustomField::NumericType:
159  {
160  QSpinBox *widget = qobject_cast<QSpinBox*>( editor );
161  model->setData( index, QString::number( widget->value() ) );
162  }
163  break;
164  case CustomField::BooleanType:
165  {
166  QCheckBox *widget = qobject_cast<QCheckBox*>( editor );
167  model->setData( index, widget->isChecked() ? QLatin1String( "true" ) : QLatin1String( "false" ) );
168  }
169  break;
170  case CustomField::DateType:
171  {
172  QDateEdit *widget = qobject_cast<QDateEdit*>( editor );
173  model->setData( index, widget->date().toString( Qt::ISODate ) );
174  }
175  break;
176  case CustomField::TimeType:
177  {
178  QTimeEdit *widget = qobject_cast<QTimeEdit*>( editor );
179  model->setData( index, widget->time().toString( Qt::ISODate ) );
180  }
181  break;
182  case CustomField::DateTimeType:
183  {
184  QDateTimeEdit *widget = qobject_cast<QDateTimeEdit*>( editor );
185  model->setData( index, widget->dateTime().toString( Qt::ISODate ) );
186  }
187  break;
188  }
189  } else {
190  QStyledItemDelegate::setModelData( editor, model, index );
191  }
192 }
193 
194 void CustomFieldsDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
195 {
196  //TODO: somehow mark local/global/external fields
197  QStyledItemDelegate::paint( painter, option, index );
198 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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