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

akonadi

  • akonadi
agentinstancewidget.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 "agentinstancewidget.h"
21 
22 #include "agentfilterproxymodel.h"
23 #include "agentinstance.h"
24 #include "agentinstancemodel.h"
25 
26 #include <KIcon>
27 #include <KIconLoader>
28 
29 #include <QtCore/QUrl>
30 #include <QApplication>
31 #include <QHBoxLayout>
32 #include <QListView>
33 #include <QPainter>
34 
35 namespace Akonadi {
36 namespace Internal {
37 
38 static void iconsEarlyCleanup();
39 
40 struct Icons
41 {
42  Icons()
43  : readyPixmap( KIcon( QLatin1String( "user-online" ) ).pixmap( QSize( 16, 16 ) ) )
44  , syncPixmap( KIcon( QLatin1String( "network-connect" ) ).pixmap( QSize( 16, 16 ) ) )
45  , errorPixmap( KIcon( QLatin1String( "dialog-error" ) ).pixmap( QSize( 16, 16 ) ) )
46  , offlinePixmap( KIcon( QLatin1String( "network-disconnect" ) ).pixmap( QSize( 16, 16 ) ) )
47  {
48  qAddPostRoutine( iconsEarlyCleanup );
49  }
50  QPixmap readyPixmap, syncPixmap, errorPixmap, offlinePixmap;
51 };
52 
53 K_GLOBAL_STATIC( Icons, s_icons )
54 
55 // called as a Qt post routine, to prevent pixmap leaking
56 void iconsEarlyCleanup() {
57  Icons * const ic = s_icons;
58  ic->readyPixmap = ic->syncPixmap = ic->errorPixmap = ic->offlinePixmap = QPixmap();
59 }
60 
61 static const int s_delegatePaddingSize = 7;
62 
67 class AgentInstanceWidgetDelegate : public QAbstractItemDelegate
68 {
69  public:
70  AgentInstanceWidgetDelegate( QObject *parent = 0 );
71 
72  virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
73  virtual QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
74 };
75 
76 }
77 
78 using Akonadi::Internal::AgentInstanceWidgetDelegate;
79 
83 class AgentInstanceWidget::Private
84 {
85  public:
86  Private( AgentInstanceWidget *parent )
87  : mParent( parent ), mView( 0 ), mModel( 0 ), proxy( 0 )
88  {
89  }
90 
91  void currentAgentInstanceChanged( const QModelIndex&, const QModelIndex& );
92  void currentAgentInstanceDoubleClicked( const QModelIndex& );
93  void currentAgentInstanceClicked( const QModelIndex &currentIndex );
94 
95  AgentInstanceWidget *mParent;
96  QListView *mView;
97  AgentInstanceModel *mModel;
98  AgentFilterProxyModel *proxy;
99 };
100 
101 void AgentInstanceWidget::Private::currentAgentInstanceChanged( const QModelIndex &currentIndex, const QModelIndex &previousIndex )
102 {
103  AgentInstance currentInstance;
104  if ( currentIndex.isValid() ) {
105  currentInstance = currentIndex.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
106  }
107 
108  AgentInstance previousInstance;
109  if ( previousIndex.isValid() ) {
110  previousInstance = previousIndex.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
111  }
112 
113  emit mParent->currentChanged( currentInstance, previousInstance );
114 }
115 
116 void AgentInstanceWidget::Private::currentAgentInstanceDoubleClicked( const QModelIndex &currentIndex )
117 {
118  AgentInstance currentInstance;
119  if ( currentIndex.isValid() ) {
120  currentInstance = currentIndex.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
121  }
122 
123  emit mParent->doubleClicked( currentInstance );
124 }
125 
126 void AgentInstanceWidget::Private::currentAgentInstanceClicked( const QModelIndex &currentIndex )
127 {
128  AgentInstance currentInstance;
129  if ( currentIndex.isValid() ) {
130  currentInstance = currentIndex.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
131  }
132 
133  emit mParent->clicked( currentInstance );
134 }
135 
136 AgentInstanceWidget::AgentInstanceWidget( QWidget *parent )
137  : QWidget( parent ), d( new Private( this ) )
138 {
139  QHBoxLayout *layout = new QHBoxLayout( this );
140  layout->setMargin( 0 );
141 
142  d->mView = new QListView( this );
143  d->mView->setContextMenuPolicy( Qt::NoContextMenu );
144  d->mView->setItemDelegate( new Internal::AgentInstanceWidgetDelegate( d->mView ) );
145  d->mView->setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
146  d->mView->setAlternatingRowColors( true );
147  d->mView->setSelectionMode( QAbstractItemView::ExtendedSelection );
148  layout->addWidget( d->mView );
149 
150  d->mModel = new AgentInstanceModel( this );
151 
152  d->proxy = new AgentFilterProxyModel( this );
153  d->proxy->setSourceModel( d->mModel );
154  d->mView->setModel( d->proxy );
155 
156  d->mView->selectionModel()->setCurrentIndex( d->mView->model()->index( 0, 0 ), QItemSelectionModel::Select );
157  d->mView->scrollTo( d->mView->model()->index( 0, 0 ) );
158 
159  connect( d->mView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
160  this, SLOT(currentAgentInstanceChanged(QModelIndex,QModelIndex)) );
161  connect( d->mView, SIGNAL(doubleClicked(QModelIndex)),
162  this, SLOT(currentAgentInstanceDoubleClicked(QModelIndex)) );
163  connect( d->mView, SIGNAL(clicked(QModelIndex)),
164  this, SLOT(currentAgentInstanceClicked(QModelIndex)) );
165 }
166 
167 AgentInstanceWidget::~AgentInstanceWidget()
168 {
169  delete d;
170 }
171 
172 AgentInstance AgentInstanceWidget::currentAgentInstance() const
173 {
174  QItemSelectionModel *selectionModel = d->mView->selectionModel();
175  if ( !selectionModel ) {
176  return AgentInstance();
177  }
178 
179  QModelIndex index = selectionModel->currentIndex();
180  if ( !index.isValid() ) {
181  return AgentInstance();
182  }
183 
184  return index.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>();
185 }
186 
187 QList<AgentInstance> AgentInstanceWidget::selectedAgentInstances() const
188 {
189  QList<AgentInstance> list;
190  QItemSelectionModel *selectionModel = d->mView->selectionModel();
191  if ( !selectionModel ) {
192  return list;
193  }
194 
195  const QModelIndexList indexes = selectionModel->selection().indexes();
196 
197  foreach ( const QModelIndex &index, indexes ) {
198  list.append( index.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>() );
199  }
200 
201  return list;
202 }
203 
204 QAbstractItemView* AgentInstanceWidget::view() const
205 {
206  return d->mView;
207 }
208 
209 AgentFilterProxyModel* AgentInstanceWidget::agentFilterProxyModel() const
210 {
211  return d->proxy;
212 }
213 
214 AgentInstanceWidgetDelegate::AgentInstanceWidgetDelegate( QObject *parent )
215  : QAbstractItemDelegate( parent )
216 {
217 }
218 
219 void AgentInstanceWidgetDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
220 {
221  if ( !index.isValid() ) {
222  return;
223  }
224 
225  QStyle *style = QApplication::style();
226  style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
227 
228  QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
229  const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
230  int status = index.model()->data( index, AgentInstanceModel::StatusRole ).toInt();
231  uint progress = index.model()->data( index, AgentInstanceModel::ProgressRole ).toUInt();
232  QString statusMessage = index.model()->data( index, AgentInstanceModel::StatusMessageRole ).toString();
233 
234  QPixmap statusPixmap;
235 
236  if ( !index.data( AgentInstanceModel::OnlineRole ).toBool() ) {
237  statusPixmap = s_icons->offlinePixmap;
238  } else if ( status == AgentInstance::Idle ) {
239  statusPixmap = s_icons->readyPixmap;
240  } else if ( status == AgentInstance::Running ) {
241  statusPixmap = s_icons->syncPixmap;
242  } else {
243  statusPixmap = s_icons->errorPixmap;
244  }
245 
246  if (status == 1) {
247  statusMessage.append(QString::fromLatin1(" (%1%)").arg(progress));
248  }
249 
250  QRect innerRect = option.rect.adjusted( s_delegatePaddingSize, s_delegatePaddingSize, -s_delegatePaddingSize, -s_delegatePaddingSize ); //add some padding round entire delegate
251 
252  const QSize decorationSize( KIconLoader::global()->currentSize( KIconLoader::Desktop ), KIconLoader::global()->currentSize( KIconLoader::Desktop ) );
253  const QSize statusIconSize = QSize(16,16);//= KIconLoader::global()->currentSize(KIconLoader::Small);
254 
255  QFont nameFont = option.font;
256  nameFont.setBold(true);
257 
258  QFont statusTextFont = option.font;
259  const QRect decorationRect( innerRect.left(), innerRect.top(), decorationSize.width(), innerRect.height() );
260  const QRect nameTextRect( decorationRect.topRight() + QPoint( 4, 0 ), innerRect.topRight() + QPoint( 0, innerRect.height() / 2) );
261  const QRect statusTextRect( decorationRect.bottomRight() + QPoint( 4, - innerRect.height()/2 ), innerRect.bottomRight() );
262 
263  const QPixmap iconPixmap = icon.pixmap(decorationSize);
264 
265  QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
266  if ( cg == QPalette::Normal && ! ( option.state & QStyle::State_Active ) ) {
267  cg = QPalette::Inactive;
268  }
269 
270  if ( option.state & QStyle::State_Selected ) {
271  painter->setPen(option.palette.color( cg, QPalette::HighlightedText ) );
272  } else {
273  painter->setPen(option.palette.color( cg, QPalette::Text ) );
274  }
275 
276  painter->drawPixmap( style->itemPixmapRect( decorationRect, Qt::AlignCenter, iconPixmap ), iconPixmap );
277 
278  painter->setFont(nameFont);
279  painter->drawText(nameTextRect, Qt::AlignVCenter | Qt::AlignLeft, name);
280 
281  painter->setFont(statusTextFont);
282  painter->drawText(statusTextRect.adjusted( statusIconSize.width() + 4, 0, 0, 0 ), Qt::AlignVCenter | Qt::AlignLeft, statusMessage );
283  painter->drawPixmap(style->itemPixmapRect( statusTextRect, Qt::AlignVCenter | Qt::AlignLeft, statusPixmap ), statusPixmap );
284 }
285 
286 QSize AgentInstanceWidgetDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
287 {
288  Q_UNUSED ( index );
289 
290  const int iconHeight = KIconLoader::global()->currentSize(KIconLoader::Desktop) + ( s_delegatePaddingSize*2 ); //icon height + padding either side
291  const int textHeight = option.fontMetrics.height() + qMax( option.fontMetrics.height(), 16 ) + ( s_delegatePaddingSize*2 ); //height of text + icon/text + padding either side
292 
293  return QSize( 1,qMax( iconHeight, textHeight ) ); //any width,the view will give us the whole thing in list mode
294 }
295 
296 }
297 
298 #include "moc_agentinstancewidget.cpp"
Akonadi::AgentInstanceModel::InstanceRole
The agent instance itself.
Definition: agentinstancemodel.h:64
Akonadi::AgentInstanceWidget::currentChanged
void currentChanged(const Akonadi::AgentInstance &current, const Akonadi::AgentInstance &previous)
This signal is emitted whenever the current agent instance changes.
Akonadi::AgentInstanceWidget::currentAgentInstance
AgentInstance currentAgentInstance() const
Returns the current agent instance or an invalid agent instance if no agent instance is selected...
Definition: agentinstancewidget.cpp:172
Akonadi::AgentInstance::Idle
The agent instance does currently nothing.
Definition: agentinstance.h:77
Akonadi::AgentInstanceModel::StatusMessageRole
A textual presentation of the current status.
Definition: agentinstancemodel.h:67
Akonadi::AgentInstanceModel::OnlineRole
The current online/offline status.
Definition: agentinstancemodel.h:69
Akonadi::AgentInstanceModel::StatusRole
The current status (numerical) of the instance.
Definition: agentinstancemodel.h:66
Akonadi::AgentInstanceWidget::agentFilterProxyModel
AgentFilterProxyModel * agentFilterProxyModel() const
Returns the agent filter proxy model, use this to filter by agent mimetype or capabilities.
Definition: agentinstancewidget.cpp:209
Akonadi::AgentInstance::Running
The agent instance is working on something.
Definition: agentinstance.h:78
Akonadi::AgentInstanceWidget::selectedAgentInstances
QList< AgentInstance > selectedAgentInstances() const
Returns the selected agent instances.
Definition: agentinstancewidget.cpp:187
Akonadi::AgentInstanceWidget::AgentInstanceWidget
AgentInstanceWidget(QWidget *parent=0)
Creates a new agent instance widget.
Definition: agentinstancewidget.cpp:136
Akonadi::AgentInstanceModel
Provides a data model for agent instances.
Definition: agentinstancemodel.h:50
Akonadi::AgentInstanceWidget::view
QAbstractItemView * view() const
Returns the view used in the widget.
Definition: agentinstancewidget.cpp:204
Akonadi::AgentInstanceModel::ProgressRole
The current progress (numerical in percent) of an operation.
Definition: agentinstancemodel.h:68
Akonadi::AgentFilterProxyModel
A proxy model for filtering AgentType or AgentInstance.
Definition: agentfilterproxymodel.h:52
Akonadi::AgentInstanceWidget::doubleClicked
void doubleClicked(const Akonadi::AgentInstance &current)
This signal is emitted whenever there is a double click on an agent instance.
Akonadi::AgentInstance
A representation of an agent instance.
Definition: agentinstance.h:62
Akonadi::AgentInstanceWidget::clicked
void clicked(const Akonadi::AgentInstance &current)
This signal is emitted whenever there is a click on an agent instance.
Akonadi::AgentInstanceWidget::~AgentInstanceWidget
~AgentInstanceWidget()
Destroys the agent instance widget.
Definition: agentinstancewidget.cpp:167
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Mon Jul 21 2014 08:03:49 by doxygen 1.8.6 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.13.3 API Reference

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