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

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