• 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
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 AgentFilterProxyModel* AgentInstanceWidget::agentFilterProxyModel() const
212 {
213  return d->proxy;
214 }
215 
216 AgentInstanceWidgetDelegate::AgentInstanceWidgetDelegate( QObject *parent )
217  : QAbstractItemDelegate( parent )
218 {
219 }
220 
221 void AgentInstanceWidgetDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
222 {
223  if ( !index.isValid() ) {
224  return;
225  }
226 
227  QStyle *style = QApplication::style();
228  style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
229 
230  QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
231  const QString name = index.model()->data( index, Qt::DisplayRole ).toString();
232  int status = index.model()->data( index, AgentInstanceModel::StatusRole ).toInt();
233  uint progress = index.model()->data( index, AgentInstanceModel::ProgressRole ).toUInt();
234  QString statusMessage = index.model()->data( index, AgentInstanceModel::StatusMessageRole ).toString();
235 
236  QPixmap statusPixmap;
237 
238  if ( !index.data( AgentInstanceModel::OnlineRole ).toBool() ) {
239  statusPixmap = s_icons->offlinePixmap;
240  } else if ( status == AgentInstance::Idle ) {
241  statusPixmap = s_icons->readyPixmap;
242  } else if ( status == AgentInstance::Running ) {
243  statusPixmap = s_icons->syncPixmap;
244  } else {
245  statusPixmap = s_icons->errorPixmap;
246  }
247 
248  if (status == 1) {
249  statusMessage.append(QString::fromLatin1(" (%1%)").arg(progress));
250  }
251 
252  QRect innerRect = option.rect.adjusted( s_delegatePaddingSize, s_delegatePaddingSize, -s_delegatePaddingSize, -s_delegatePaddingSize ); //add some padding round entire delegate
253 
254  const QSize decorationSize( KIconLoader::global()->currentSize( KIconLoader::Desktop ), KIconLoader::global()->currentSize( KIconLoader::Desktop ) );
255  const QSize statusIconSize = QSize(16,16);//= KIconLoader::global()->currentSize(KIconLoader::Small);
256 
257  QFont nameFont = option.font;
258  nameFont.setBold(true);
259 
260  QFont statusTextFont = option.font;
261  const QRect decorationRect( innerRect.left(), innerRect.top(), decorationSize.width(), innerRect.height() );
262  const QRect nameTextRect( decorationRect.topRight() + QPoint( 4, 0 ), innerRect.topRight() + QPoint( 0, innerRect.height() / 2) );
263  const QRect statusTextRect( decorationRect.bottomRight() + QPoint( 4, - innerRect.height()/2 ), innerRect.bottomRight() );
264 
265  const QPixmap iconPixmap = icon.pixmap(decorationSize);
266 
267  QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
268  if ( cg == QPalette::Normal && ! ( option.state & QStyle::State_Active ) ) {
269  cg = QPalette::Inactive;
270  }
271 
272  if ( option.state & QStyle::State_Selected ) {
273  painter->setPen(option.palette.color( cg, QPalette::HighlightedText ) );
274  } else {
275  painter->setPen(option.palette.color( cg, QPalette::Text ) );
276  }
277 
278  painter->drawPixmap( style->itemPixmapRect( decorationRect, Qt::AlignCenter, iconPixmap ), iconPixmap );
279 
280  painter->setFont(nameFont);
281  painter->drawText(nameTextRect, Qt::AlignVCenter | Qt::AlignLeft, name);
282 
283  painter->setFont(statusTextFont);
284  painter->drawText(statusTextRect.adjusted( statusIconSize.width() + 4, 0, 0, 0 ), Qt::AlignVCenter | Qt::AlignLeft, statusMessage );
285  painter->drawPixmap(style->itemPixmapRect( statusTextRect, Qt::AlignVCenter | Qt::AlignLeft, statusPixmap ), statusPixmap );
286 }
287 
288 QSize AgentInstanceWidgetDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
289 {
290  Q_UNUSED ( index );
291 
292  const int iconHeight = KIconLoader::global()->currentSize(KIconLoader::Desktop) + ( s_delegatePaddingSize*2 ); //icon height + padding either side
293  const int textHeight = option.fontMetrics.height() + qMax( option.fontMetrics.height(), 16 ) + ( s_delegatePaddingSize*2 ); //height of text + icon/text + padding either side
294 
295  return QSize( 1,qMax( iconHeight, textHeight ) ); //any width,the view will give us the whole thing in list mode
296 }
297 
298 }
299 
300 #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:211
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-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