• 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
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 <QApplication>
25 #include <QHBoxLayout>
26 #include <QListView>
27 #include <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 
58 class AgentTypeWidget::Private
59 {
60  public:
61  Private( AgentTypeWidget *parent )
62  : mParent( parent )
63  {
64  }
65 
66  void currentAgentTypeChanged( const QModelIndex&, const QModelIndex& );
67 
68  void typeActivated( const QModelIndex &index )
69  {
70  if ( index.flags() & ( Qt::ItemIsSelectable | Qt::ItemIsEnabled ) ) {
71  emit mParent->activated();
72  }
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 
88  AgentType previousType;
89  if ( previousIndex.isValid() ) {
90  previousType = previousIndex.data( AgentTypeModel::TypeRole ).value<AgentType>();
91  }
92 
93  emit mParent->currentChanged( currentType, previousType );
94 }
95 
96 AgentTypeWidget::AgentTypeWidget( QWidget *parent )
97  : QWidget( parent ), d( new Private( this ) )
98 {
99  QHBoxLayout *layout = new QHBoxLayout( this );
100  layout->setMargin( 0 );
101 
102  d->mView = new QListView( this );
103  d->mView->setItemDelegate( new AgentTypeWidgetDelegate( d->mView ) );
104  d->mView->setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
105  d->mView->setAlternatingRowColors( true );
106  layout->addWidget( d->mView );
107 
108  d->mModel = new AgentTypeModel( d->mView );
109  d->proxyModel = new AgentFilterProxyModel( this );
110  d->proxyModel->setSourceModel( d->mModel );
111  d->proxyModel->sort( 0 );
112  d->mView->setModel( d->proxyModel );
113 
114  d->mView->selectionModel()->setCurrentIndex( d->mView->model()->index( 0, 0 ), QItemSelectionModel::Select );
115  d->mView->scrollTo( d->mView->model()->index( 0, 0 ) );
116  connect( d->mView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
117  this, SLOT(currentAgentTypeChanged(QModelIndex,QModelIndex)) );
118  connect( d->mView, SIGNAL(activated(QModelIndex)),
119  SLOT(typeActivated(QModelIndex)) );
120 }
121 
122 AgentTypeWidget::~AgentTypeWidget()
123 {
124  delete d;
125 }
126 
127 AgentType AgentTypeWidget::currentAgentType() const
128 {
129  QItemSelectionModel *selectionModel = d->mView->selectionModel();
130  if ( !selectionModel ) {
131  return AgentType();
132  }
133 
134  QModelIndex index = selectionModel->currentIndex();
135  if ( !index.isValid() ) {
136  return AgentType();
137  }
138 
139  return index.data( AgentTypeModel::TypeRole ).value<AgentType>();
140 }
141 
142 AgentFilterProxyModel* AgentTypeWidget::agentFilterProxyModel() const
143 {
144  return d->proxyModel;
145 }
146 
151 AgentTypeWidgetDelegate::AgentTypeWidgetDelegate( QObject *parent )
152  : QAbstractItemDelegate( parent )
153 {
154 }
155 
156 void AgentTypeWidgetDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
157 {
158  if ( !index.isValid() ) {
159  return;
160  }
161 
162  painter->setRenderHint( QPainter::Antialiasing );
163 
164  const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
165  const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
166 
167  const QVariant data = index.model()->data( index, Qt::DecorationRole );
168 
169  QPixmap pixmap;
170  if ( data.isValid() && data.type() == QVariant::Icon ) {
171  pixmap = qvariant_cast<QIcon>( data ).pixmap( 64, 64 );
172  }
173 
174  const QFont oldFont = painter->font();
175  QFont boldFont( oldFont );
176  boldFont.setBold( true );
177  painter->setFont( boldFont );
178  QFontMetrics fm = painter->fontMetrics();
179  int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
180  int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
181  painter->setFont( oldFont );
182 
183  fm = painter->fontMetrics();
184  int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
185  int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
186  int wp = pixmap.width();
187 
188  QStyleOptionViewItemV4 opt( option );
189  opt.showDecorationSelected = true;
190  QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter );
191 
192  QPen pen = painter->pen();
193  QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
194  ? QPalette::Normal : QPalette::Disabled;
195  if ( cg == QPalette::Normal && !( option.state & QStyle::State_Active ) ) {
196  cg = QPalette::Inactive;
197  }
198  if ( option.state & QStyle::State_Selected ) {
199  painter->setPen( option.palette.color( cg, QPalette::HighlightedText ) );
200  } else {
201  painter->setPen( option.palette.color( cg, QPalette::Text ) );
202  }
203 
204  QFont font = painter->font();
205  painter->setFont( option.font );
206 
207  painter->drawPixmap( option.rect.x() + 5, option.rect.y() + 5, pixmap );
208 
209  painter->setFont( boldFont );
210  if ( !name.isEmpty() ) {
211  painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7, wn, hn, Qt::AlignLeft, name );
212  }
213  painter->setFont( oldFont );
214 
215  if ( !comment.isEmpty() ) {
216  painter->drawText( option.rect.x() + 5 + wp + 5, option.rect.y() + 7 + hn, wc, hc, Qt::AlignLeft, comment );
217  }
218 
219  painter->setPen( pen );
220 
221  drawFocus( painter, option, option.rect );
222 }
223 
224 QSize AgentTypeWidgetDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
225 {
226  if ( !index.isValid() ) {
227  return QSize( 0, 0 );
228  }
229 
230  const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
231  const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString();
232 
233  QFontMetrics fm = option.fontMetrics;
234  int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height();
235  int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width();
236  int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height();
237  int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width();
238 
239  int width = 0;
240  int height = 0;
241 
242  if ( !name.isEmpty() ) {
243  height += hn;
244  width = qMax( width, wn );
245  }
246 
247  if ( !comment.isEmpty() ) {
248  height += hc;
249  width = qMax( width, wc );
250  }
251 
252  height = qMax( height, 64 ) + 10;
253  width += 64 + 15;
254 
255  return QSize( width, height );
256 }
257 
258 void AgentTypeWidgetDelegate::drawFocus( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const
259 {
260  if ( option.state & QStyle::State_HasFocus ) {
261  QStyleOptionFocusRect o;
262  o.QStyleOption::operator=( option );
263  o.rect = rect;
264  o.state |= QStyle::State_KeyboardFocusChange;
265  QPalette::ColorGroup cg = ( option.state & QStyle::State_Enabled )
266  ? QPalette::Normal : QPalette::Disabled;
267  o.backgroundColor = option.palette.color( cg, ( option.state & QStyle::State_Selected )
268  ? QPalette::Highlight : QPalette::Background );
269  QApplication::style()->drawPrimitive( QStyle::PE_FrameFocusRect, &o, painter );
270  }
271 }
272 
273 }
274 
275 #include "moc_agenttypewidget.cpp"
Akonadi::AgentTypeWidget::activated
void activated()
This signal is emitted whenever the user activates an agent.
Akonadi::AgentTypeWidget::~AgentTypeWidget
~AgentTypeWidget()
Destroys the agent type widget.
Definition: agenttypewidget.cpp:122
Akonadi::AgentTypeModel::TypeRole
The agent type itself.
Definition: agenttypemodel.h:59
Akonadi::AgentType
A representation of an agent type.
Definition: agenttype.h:58
Akonadi::AgentTypeWidget::currentChanged
void currentChanged(const Akonadi::AgentType &current, const Akonadi::AgentType &previous)
This signal is emitted whenever the current agent type changes.
Akonadi::AgentTypeModel::DescriptionRole
A description of the agent type.
Definition: agenttypemodel.h:61
Akonadi::AgentTypeModel
Provides a data model for agent types.
Definition: agenttypemodel.h:50
Akonadi::AgentTypeWidget::agentFilterProxyModel
AgentFilterProxyModel * agentFilterProxyModel() const
Returns the agent filter proxy model, use this to filter by agent mimetype or capabilities.
Definition: agenttypewidget.cpp:142
Akonadi::AgentFilterProxyModel
A proxy model for filtering AgentType or AgentInstance.
Definition: agentfilterproxymodel.h:52
Akonadi::AgentTypeWidget::currentAgentType
AgentType currentAgentType() const
Returns the current agent type or an invalid agent type if no agent type is selected.
Definition: agenttypewidget.cpp:127
Akonadi::AgentTypeWidget::AgentTypeWidget
AgentTypeWidget(QWidget *parent=0)
Creates a new agent type widget.
Definition: agenttypewidget.cpp:96
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:26 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