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

akonadi

  • sources
  • kde-4.14
  • 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 
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)
88  , mView(0)
89  , mModel(0)
90  , proxy(0)
91  {
92  }
93 
94  void currentAgentInstanceChanged(const QModelIndex &, const QModelIndex &);
95  void currentAgentInstanceDoubleClicked(const QModelIndex &);
96  void currentAgentInstanceClicked(const QModelIndex &currentIndex);
97 
98  AgentInstanceWidget *mParent;
99  QListView *mView;
100  AgentInstanceModel *mModel;
101  AgentFilterProxyModel *proxy;
102 };
103 
104 void AgentInstanceWidget::Private::currentAgentInstanceChanged(const QModelIndex &currentIndex, const QModelIndex &previousIndex)
105 {
106  AgentInstance currentInstance;
107  if (currentIndex.isValid()) {
108  currentInstance = currentIndex.data(AgentInstanceModel::InstanceRole).value<AgentInstance>();
109  }
110 
111  AgentInstance previousInstance;
112  if (previousIndex.isValid()) {
113  previousInstance = previousIndex.data(AgentInstanceModel::InstanceRole).value<AgentInstance>();
114  }
115 
116  emit mParent->currentChanged(currentInstance, previousInstance);
117 }
118 
119 void AgentInstanceWidget::Private::currentAgentInstanceDoubleClicked(const QModelIndex &currentIndex)
120 {
121  AgentInstance currentInstance;
122  if (currentIndex.isValid()) {
123  currentInstance = currentIndex.data(AgentInstanceModel::InstanceRole).value<AgentInstance>();
124  }
125 
126  emit mParent->doubleClicked(currentInstance);
127 }
128 
129 void AgentInstanceWidget::Private::currentAgentInstanceClicked(const QModelIndex &currentIndex)
130 {
131  AgentInstance currentInstance;
132  if (currentIndex.isValid()) {
133  currentInstance = currentIndex.data(AgentInstanceModel::InstanceRole).value<AgentInstance>();
134  }
135 
136  emit mParent->clicked(currentInstance);
137 }
138 
139 AgentInstanceWidget::AgentInstanceWidget(QWidget *parent)
140  : QWidget(parent)
141  , d(new Private(this))
142 {
143  QHBoxLayout *layout = new QHBoxLayout(this);
144  layout->setMargin(0);
145 
146  d->mView = new QListView(this);
147  d->mView->setContextMenuPolicy(Qt::NoContextMenu);
148  d->mView->setItemDelegate(new Internal::AgentInstanceWidgetDelegate(d->mView));
149  d->mView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
150  d->mView->setAlternatingRowColors(true);
151  d->mView->setSelectionMode(QAbstractItemView::ExtendedSelection);
152  layout->addWidget(d->mView);
153 
154  d->mModel = new AgentInstanceModel(this);
155 
156  d->proxy = new AgentFilterProxyModel(this);
157  d->proxy->setSourceModel(d->mModel);
158  d->mView->setModel(d->proxy);
159 
160  d->mView->selectionModel()->setCurrentIndex(d->mView->model()->index(0, 0), QItemSelectionModel::Select);
161  d->mView->scrollTo(d->mView->model()->index(0, 0));
162 
163  connect(d->mView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
164  this, SLOT(currentAgentInstanceChanged(QModelIndex,QModelIndex)));
165  connect(d->mView, SIGNAL(doubleClicked(QModelIndex)),
166  this, SLOT(currentAgentInstanceDoubleClicked(QModelIndex)));
167  connect(d->mView, SIGNAL(clicked(QModelIndex)),
168  this, SLOT(currentAgentInstanceClicked(QModelIndex)));
169 }
170 
171 AgentInstanceWidget::~AgentInstanceWidget()
172 {
173  delete d;
174 }
175 
176 AgentInstance AgentInstanceWidget::currentAgentInstance() const
177 {
178  QItemSelectionModel *selectionModel = d->mView->selectionModel();
179  if (!selectionModel) {
180  return AgentInstance();
181  }
182 
183  QModelIndex index = selectionModel->currentIndex();
184  if (!index.isValid()) {
185  return AgentInstance();
186  }
187 
188  return index.data(AgentInstanceModel::InstanceRole).value<AgentInstance>();
189 }
190 
191 QList<AgentInstance> AgentInstanceWidget::selectedAgentInstances() const
192 {
193  QList<AgentInstance> list;
194  QItemSelectionModel *selectionModel = d->mView->selectionModel();
195  if (!selectionModel) {
196  return list;
197  }
198 
199  const QModelIndexList indexes = selectionModel->selection().indexes();
200 
201  foreach (const QModelIndex &index, indexes) {
202  list.append(index.data(AgentInstanceModel::InstanceRole).value<AgentInstance>());
203  }
204 
205  return list;
206 }
207 
208 QAbstractItemView *AgentInstanceWidget::view() const
209 {
210  return d->mView;
211 }
212 
213 AgentFilterProxyModel *AgentInstanceWidget::agentFilterProxyModel() const
214 {
215  return d->proxy;
216 }
217 
218 AgentInstanceWidgetDelegate::AgentInstanceWidgetDelegate(QObject *parent)
219  : QAbstractItemDelegate(parent)
220 {
221 }
222 
223 void AgentInstanceWidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
224 {
225  if (!index.isValid()) {
226  return;
227  }
228 
229  QStyle *style = QApplication::style();
230  style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
231 
232  QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
233  const QString name = index.model()->data(index, Qt::DisplayRole).toString();
234  int status = index.model()->data(index, AgentInstanceModel::StatusRole).toInt();
235  uint progress = index.model()->data(index, AgentInstanceModel::ProgressRole).toUInt();
236  QString statusMessage = index.model()->data(index, AgentInstanceModel::StatusMessageRole).toString();
237 
238  QPixmap statusPixmap;
239 
240  if (!index.data(AgentInstanceModel::OnlineRole).toBool()) {
241  statusPixmap = s_icons->offlinePixmap;
242  } else if (status == AgentInstance::Idle) {
243  statusPixmap = s_icons->readyPixmap;
244  } else if (status == AgentInstance::Running) {
245  statusPixmap = s_icons->syncPixmap;
246  } else {
247  statusPixmap = s_icons->errorPixmap;
248  }
249 
250  if (status == 1) {
251  statusMessage.append(QString::fromLatin1(" (%1%)").arg(progress));
252  }
253 
254  QRect innerRect = option.rect.adjusted(s_delegatePaddingSize, s_delegatePaddingSize, -s_delegatePaddingSize, -s_delegatePaddingSize); //add some padding round entire delegate
255 
256  const QSize decorationSize(KIconLoader::global()->currentSize(KIconLoader::Desktop), KIconLoader::global()->currentSize(KIconLoader::Desktop));
257  const QSize statusIconSize = QSize(16, 16); //= KIconLoader::global()->currentSize(KIconLoader::Small);
258 
259  QFont nameFont = option.font;
260  nameFont.setBold(true);
261 
262  QFont statusTextFont = option.font;
263  const QRect decorationRect(innerRect.left(), innerRect.top(), decorationSize.width(), innerRect.height());
264  const QRect nameTextRect(decorationRect.topRight() + QPoint(4, 0), innerRect.topRight() + QPoint(0, innerRect.height() / 2));
265  const QRect statusTextRect(decorationRect.bottomRight() + QPoint(4, - innerRect.height() / 2), innerRect.bottomRight());
266 
267  const QPixmap iconPixmap = icon.pixmap(decorationSize);
268 
269  QPalette::ColorGroup cg = option.state &QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
270  if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) {
271  cg = QPalette::Inactive;
272  }
273 
274  if (option.state & QStyle::State_Selected) {
275  painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
276  } else {
277  painter->setPen(option.palette.color(cg, QPalette::Text));
278  }
279 
280  painter->drawPixmap(style->itemPixmapRect(decorationRect, Qt::AlignCenter, iconPixmap), iconPixmap);
281 
282  painter->setFont(nameFont);
283  painter->drawText(nameTextRect, Qt::AlignVCenter | Qt::AlignLeft, name);
284 
285  painter->setFont(statusTextFont);
286  painter->drawText(statusTextRect.adjusted(statusIconSize.width() + 4, 0, 0, 0), Qt::AlignVCenter | Qt::AlignLeft, statusMessage);
287  painter->drawPixmap(style->itemPixmapRect(statusTextRect, Qt::AlignVCenter | Qt::AlignLeft, statusPixmap), statusPixmap);
288 }
289 
290 QSize AgentInstanceWidgetDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
291 {
292  Q_UNUSED(index);
293 
294  const int iconHeight = KIconLoader::global()->currentSize(KIconLoader::Desktop) + (s_delegatePaddingSize * 2); //icon height + padding either side
295  const int textHeight = option.fontMetrics.height() + qMax(option.fontMetrics.height(), 16) + (s_delegatePaddingSize * 2); //height of text + icon/text + padding either side
296 
297  return QSize(1, qMax(iconHeight, textHeight)); //any width,the view will give us the whole thing in list mode
298 }
299 
300 }
301 
302 #include "moc_agentinstancewidget.cpp"
QWidget::layout
QLayout * layout() const
Akonadi::AgentInstanceModel::InstanceRole
The agent instance itself.
Definition: agentinstancemodel.h:64
QItemSelection::indexes
QModelIndexList indexes() const
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:176
QModelIndex
QWidget
QString::append
QString & append(QChar ch)
QRect::topRight
QPoint topRight() const
QSize::width
int width() const
Akonadi::AgentInstance::Idle
The agent instance does currently nothing.
Definition: agentinstance.h:77
QAbstractItemView
QItemSelectionModel::currentIndex
QModelIndex currentIndex() const
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:213
QFont
QRect::bottomRight
QPoint bottomRight() const
QHBoxLayout
QVariant::value
T value() const
Akonadi::AgentInstance::Running
The agent instance is working on something.
Definition: agentinstance.h:78
QRect::height
int height() const
QPoint
QIcon::pixmap
QPixmap pixmap(const QSize &size, Mode mode, State state) const
QListView
QFont::setBold
void setBold(bool enable)
QRect
QModelIndex::isValid
bool isValid() const
QPainter::setFont
void setFont(const QFont &font)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::append
void append(const T &value)
Akonadi::AgentInstanceWidget::selectedAgentInstances
QList< AgentInstance > selectedAgentInstances() const
Returns the selected agent instances.
Definition: agentinstancewidget.cpp:191
QVariant::toUInt
uint toUInt(bool *ok) const
QVariant::toInt
int toInt(bool *ok) const
QRect::top
int top() const
QStyleOptionViewItem
QObject
QStyle
QPainter::setPen
void setPen(const QColor &color)
QItemSelectionModel::selection
const QItemSelection selection() const
QRect::left
int left() const
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPainter
Akonadi::AgentInstanceWidget::AgentInstanceWidget
AgentInstanceWidget(QWidget *parent=0)
Creates a new agent instance widget.
Definition: agentinstancewidget.cpp:139
QPainter::drawText
void drawText(const QPointF &position, const QString &text)
Akonadi::AgentInstanceModel
Provides a data model for agent instances.
Definition: agentinstancemodel.h:50
QAbstractItemModel::data
virtual QVariant data(const QModelIndex &index, int role) const =0
QString
QList< AgentInstance >
QLayout::setMargin
void setMargin(int margin)
QPixmap
QSize
Akonadi::AgentInstanceWidget::view
QAbstractItemView * view() const
Returns the view used in the widget.
Definition: agentinstancewidget.cpp:208
Akonadi::AgentInstanceModel::ProgressRole
The current progress (numerical in percent) of an operation.
Definition: agentinstancemodel.h:68
QModelIndex::model
const QAbstractItemModel * model() const
QModelIndex::data
QVariant data(int role) const
QApplication::style
QStyle * style()
QLatin1String
Akonadi::AgentFilterProxyModel
A proxy model for filtering AgentType or AgentInstance.
Definition: agentfilterproxymodel.h:52
QVariant::toBool
bool toBool() const
QStyle::drawPrimitive
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const =0
Akonadi::AgentInstanceWidget::doubleClicked
void doubleClicked(const Akonadi::AgentInstance &current)
This signal is emitted whenever there is a double click on an agent instance.
QString::fromLatin1
QString fromLatin1(const char *str, int size)
Akonadi::AgentInstance
A representation of an agent instance.
Definition: agentinstance.h:62
QStyle::itemPixmapRect
virtual QRect itemPixmapRect(const QRect &rectangle, int alignment, const QPixmap &pixmap) const
Akonadi::AgentInstanceWidget::clicked
void clicked(const Akonadi::AgentInstance &current)
This signal is emitted whenever there is a click on an agent instance.
QItemSelectionModel
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
Akonadi::AgentInstanceWidget::~AgentInstanceWidget
~AgentInstanceWidget()
Destroys the agent instance widget.
Definition: agentinstancewidget.cpp:171
QVariant::toString
QString toString() const
QIcon
QAbstractItemDelegate
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:38:02 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
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

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