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

akonadi/contact

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

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 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