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

akonadi

  • akonadi
agenttypewidget.cpp
1 /*
2  Copyright (c) 2006-2008 Tobias Koenig <tokoe@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "agenttypewidget.h"
21 
22 #include <KDebug>
23 
24 #include <QtGui/QApplication>
25 #include <QtGui/QHBoxLayout>
26 #include <QtGui/QListView>
27 #include <QtGui/QPainter>
28 
29 #include "agentfilterproxymodel.h"
30 #include "agenttype.h"
31 #include "agenttypemodel.h"
32 
33 namespace Akonadi {
34 namespace Internal {
35 
39 class AgentTypeWidgetDelegate : public QAbstractItemDelegate
40 {
41  public:
42  AgentTypeWidgetDelegate( QObject *parent = 0 );
43 
44  virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
45  virtual QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
46 
47  private:
48  void drawFocus( QPainter*, const QStyleOptionViewItem&, const QRect& ) const;
49 };
50 
51 }
52 
53 using Akonadi::Internal::AgentTypeWidgetDelegate;
54 
55 
59 class AgentTypeWidget::Private
60 {
61  public:
62  Private( AgentTypeWidget *parent )
63  : mParent( parent )
64  {
65  }
66 
67  void currentAgentTypeChanged( const QModelIndex&, const QModelIndex& );
68 
69  void typeActivated( const QModelIndex &index )
70  {
71  if ( index.flags() & (Qt::ItemIsSelectable | Qt::ItemIsEnabled) )
72  emit mParent->activated();
73  }
74 
75  AgentTypeWidget *mParent;
76  QListView *mView;
77  AgentTypeModel *mModel;
78  AgentFilterProxyModel *proxyModel;
79 };
80 
81 void AgentTypeWidget::Private::currentAgentTypeChanged( const QModelIndex &currentIndex, const QModelIndex &previousIndex )
82 {
83  AgentType currentType;
84  if ( currentIndex.isValid() )
85  currentType = currentIndex.data( AgentTypeModel::TypeRole ).value<AgentType>();
86 
87  AgentType previousType;
88  if ( previousIndex.isValid() )
89  previousType = previousIndex.data( AgentTypeModel::TypeRole ).value<AgentType>();
90 
91  emit mParent->currentChanged( currentType, previousType );
92 }
93 
94 AgentTypeWidget::AgentTypeWidget( QWidget *parent )
95  : QWidget( parent ), d( new Private( this ) )
96 {
97  QHBoxLayout *layout = new QHBoxLayout( this );
98  layout->setMargin( 0 );
99 
100  d->mView = new QListView( this );
101  d->mView->setItemDelegate( new AgentTypeWidgetDelegate( d->mView ) );
102  d->mView->setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
103  d->mView->setAlternatingRowColors( true );
104  layout->addWidget( d->mView );
105 
106  d->mModel = new AgentTypeModel( d->mView );
107  d->proxyModel = new AgentFilterProxyModel( this );
108  d->proxyModel->setSourceModel( d->mModel );
109  d->proxyModel->sort( 0 );
110  d->mView->setModel( d->proxyModel );
111 
112  d->mView->selectionModel()->setCurrentIndex( d->mView->model()->index( 0, 0 ), QItemSelectionModel::Select );
113  d->mView->scrollTo( d->mView->model()->index( 0, 0 ) );
114  connect( d->mView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
115  this, SLOT(currentAgentTypeChanged(QModelIndex,QModelIndex)) );
116  connect( d->mView, SIGNAL(activated(QModelIndex)),
117  SLOT(typeActivated(QModelIndex)) );
118 }
119 
120 AgentTypeWidget::~AgentTypeWidget()
121 {
122  delete d;
123 }
124 
125 AgentType AgentTypeWidget::currentAgentType() const
126 {
127  QItemSelectionModel *selectionModel = d->mView->selectionModel();
128  if ( !selectionModel )
129  return AgentType();
130 
131  QModelIndex index = selectionModel->currentIndex();
132  if ( !index.isValid() )
133  return AgentType();
134 
135  return index.data( AgentTypeModel::TypeRole ).value<AgentType>();
136 }
137 
138 AgentFilterProxyModel* AgentTypeWidget::agentFilterProxyModel() const
139 {
140  return d->proxyModel;
141 }
142 
147 AgentTypeWidgetDelegate::AgentTypeWidgetDelegate( QObject *parent )
148  : QAbstractItemDelegate( parent )
149 {
150 }
151 
152 void AgentTypeWidgetDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
153 {
154  if ( !index.isValid() )
155  return;
156 
157  painter->setRenderHint( QPainter::Antialiasing );
158 
159  const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
160  const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
161 
162  const QVariant data = index.model()->data( index, Qt::DecorationRole );
163 
164  QPixmap pixmap;
165  if ( data.isValid() && data.type() == QVariant::Icon )
166  pixmap = qvariant_cast<QIcon>( data ).pixmap( 64, 64 );
167 
168  const QFont oldFont = painter->font();
169  QFont boldFont( oldFont );
170  boldFont.setBold( true );
171  painter->setFont( boldFont );
172  QFontMetrics fm = painter->fontMetrics();
173  int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
174  int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
175  painter->setFont( oldFont );
176 
177  fm = painter->fontMetrics();
178  int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
179  int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
180  int wp = pixmap.width();
181 
182  QStyleOptionViewItemV4 opt(option);
183  opt.showDecorationSelected = true;
184  QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter );
185 
186  QPen pen = painter->pen();
187  QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
188  ? QPalette::Normal : QPalette::Disabled;
189  if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
190  cg = QPalette::Inactive;
191  if (option.state & QStyle::State_Selected) {
192  painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
193  } else {
194  painter->setPen(option.palette.color(cg, QPalette::Text));
195  }
196 
197  QFont font = painter->font();
198  painter->setFont(option.font);
199 
200  painter->drawPixmap( option.rect.x() + 5, option.rect.y() + 5, pixmap );
201 
202  painter->setFont(boldFont);
203  if ( !name.isEmpty() )
204  painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7, wn, hn, Qt::AlignLeft, name );
205  painter->setFont(oldFont);
206 
207  if ( !comment.isEmpty() )
208  painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7 + hn, wc, hc, Qt::AlignLeft, comment );
209 
210  painter->setPen(pen);
211 
212  drawFocus( painter, option, option.rect );
213 }
214 
215 QSize AgentTypeWidgetDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
216 {
217  if ( !index.isValid() )
218  return QSize( 0, 0 );
219 
220  const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
221  const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
222 
223  QFontMetrics fm = option.fontMetrics;
224  int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
225  int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
226  int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
227  int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
228 
229  int width = 0;
230  int height = 0;
231 
232  if ( !name.isEmpty() ) {
233  height += hn;
234  width = qMax( width, wn );
235  }
236 
237  if ( !comment.isEmpty() ) {
238  height += hc;
239  width = qMax( width, wc );
240  }
241 
242  height = qMax( height, 64 ) + 10;
243  width += 64 + 15;
244 
245  return QSize( width, height );
246 }
247 
248 void AgentTypeWidgetDelegate::drawFocus( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const
249 {
250  if (option.state & QStyle::State_HasFocus) {
251  QStyleOptionFocusRect o;
252  o.QStyleOption::operator=(option);
253  o.rect = rect;
254  o.state |= QStyle::State_KeyboardFocusChange;
255  QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled)
256  ? QPalette::Normal : QPalette::Disabled;
257  o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected)
258  ? QPalette::Highlight : QPalette::Background);
259  QApplication::style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter);
260  }
261 }
262 
263 }
264 
265 #include "agenttypewidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Dec 10 2012 13:48:07 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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