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

kmail

  • sources
  • kde-4.14
  • kdepim
  • kmail
  • configuredialog
configagentdelegate.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010 Casey Link <unnamedrambler@gmail.com>
3  Copyright (c) 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
4  Copyright (c) 2006-2008 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "configagentdelegate.h"
23 
24 #include <Akonadi/AgentInstanceModel>
25 #include <Akonadi/AgentInstance>
26 
27 #include <KDebug>
28 #include <KIcon>
29 #include <KLocalizedString>
30 #include <KIconLoader>
31 
32 #include <QUrl>
33 #include <QApplication>
34 #include <QPainter>
35 #include <QTextDocument>
36 #include <QMouseEvent>
37 
38 using Akonadi::AgentInstanceModel;
39 using Akonadi::AgentInstance;
40 
41 static const int s_delegatePaddingSize = 7;
42 
43 struct Icons {
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  , checkMailIcon ( KIcon ( QLatin1String ("mail-receive") ) ) {
50  }
51  QPixmap readyPixmap, syncPixmap, errorPixmap, offlinePixmap;
52  KIcon checkMailIcon;
53 };
54 
55 K_GLOBAL_STATIC ( Icons, s_icons )
56 
57 ConfigAgentDelegate::ConfigAgentDelegate ( QObject *parent )
58  : QStyledItemDelegate ( parent )
59 {
60 }
61 
62 QTextDocument* ConfigAgentDelegate::document ( const QStyleOptionViewItem &option, const QModelIndex &index ) const
63 {
64  if ( !index.isValid() )
65  return 0;
66 
67  const QString name = index.model()->data ( index, Qt::DisplayRole ).toString();
68  int status = index.model()->data ( index, AgentInstanceModel::StatusRole ).toInt();
69  uint progress = index.model()->data ( index, AgentInstanceModel::ProgressRole ).toUInt();
70  const QString statusMessage = index.model()->data ( index, AgentInstanceModel::StatusMessageRole ).toString();
71 
72  QTextDocument *document = new QTextDocument ( 0 );
73 
74  const QSize decorationSize( KIconLoader::global()->currentSize( KIconLoader::Desktop ), KIconLoader::global()->currentSize( KIconLoader::Desktop ) );
75  const QVariant data = index.model()->data ( index, Qt::DecorationRole );
76  if ( data.isValid() && data.type() == QVariant::Icon ) {
77  document->addResource ( QTextDocument::ImageResource, QUrl ( QLatin1String ( "agent_icon" ) ),
78  qvariant_cast<QIcon> ( data ).pixmap ( decorationSize ) );
79  }
80 
81  if ( !index.data ( AgentInstanceModel::OnlineRole ).toBool() )
82  document->addResource ( QTextDocument::ImageResource, QUrl ( QLatin1String ( "status_icon" ) ), s_icons->offlinePixmap );
83  else if ( status == AgentInstance::Idle )
84  document->addResource ( QTextDocument::ImageResource, QUrl ( QLatin1String ( "status_icon" ) ), s_icons->readyPixmap );
85  else if ( status == AgentInstance::Running )
86  document->addResource ( QTextDocument::ImageResource, QUrl ( QLatin1String ( "status_icon" ) ), s_icons->syncPixmap );
87  else
88  document->addResource ( QTextDocument::ImageResource, QUrl ( QLatin1String ( "status_icon" ) ), s_icons->errorPixmap );
89 
90 
91  QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
92  if ( cg == QPalette::Normal && ! ( option.state & QStyle::State_Active ) )
93  cg = QPalette::Inactive;
94 
95  QColor textColor;
96  if ( option.state & QStyle::State_Selected ) {
97  textColor = option.palette.color ( cg, QPalette::HighlightedText );
98  } else {
99  textColor = option.palette.color ( cg, QPalette::Text );
100  }
101 
102  const QString content = QString::fromLatin1 (
103  "<html style=\"color:%1\">"
104  "<body>"
105  "<table>"
106  "<tr>"
107  "<td rowspan=\"2\"><img src=\"agent_icon\">&nbsp;&nbsp;</td>"
108  "<td><b>%2</b></td>"
109  "</tr>" ).arg ( textColor.name().toUpper() ).arg ( name )
110  + QString::fromLatin1 (
111  "<tr>"
112  "<td><img src=\"status_icon\"/> %1 %2</td>"
113  "</tr>" ).arg ( statusMessage ).arg ( status == 1 ? QString::fromLatin1( "(%1%)" ).arg ( progress ) : QLatin1String ( "" ) )
114  + QLatin1String ( "</table></body></html>" );
115 
116  document->setHtml ( content );
117 
118  return document;
119 }
120 
121 void ConfigAgentDelegate::paint ( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
122 {
123  if ( !index.isValid() )
124  return;
125 
126  QTextDocument *doc = document ( option, index );
127  if ( !doc )
128  return;
129 
130  QStyleOptionButton buttonOpt = buttonOption ( option );
131 
132  doc->setTextWidth( option.rect.width() - buttonOpt.rect.width() );
133  painter->setRenderHint ( QPainter::Antialiasing );
134 
135  QPen pen = painter->pen();
136 
137  QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
138  if ( cg == QPalette::Normal && ! ( option.state & QStyle::State_Active ) )
139  cg = QPalette::Inactive;
140 
141  QStyleOptionViewItemV4 opt ( option );
142  opt.showDecorationSelected = true;
143  QApplication::style()->drawPrimitive ( QStyle::PE_PanelItemViewItem, &opt, painter );
144  painter->save();
145  painter->translate ( option.rect.topLeft() );
146 
147  doc->drawContents ( painter );
148 
149  QApplication::style()->drawControl ( QStyle::CE_PushButton, &buttonOpt, painter );
150 
151  painter->restore();
152  painter->setPen ( pen );
153 
154  drawFocus ( painter, option, option.rect );
155  delete doc;
156 }
157 
158 QSize ConfigAgentDelegate::sizeHint ( const QStyleOptionViewItem &option, const QModelIndex & ) const
159 {
160  const int iconHeight = KIconLoader::global()->currentSize(KIconLoader::Desktop) + ( s_delegatePaddingSize*2 ); //icon height + padding either side
161  const int textHeight = option.fontMetrics.height() + qMax( option.fontMetrics.height(), 16 ) + ( s_delegatePaddingSize*2 ); //height of text + icon/text + padding either side
162 
163  return QSize( 1,qMax( iconHeight, textHeight ) ); //any width,the view will give us the whole thing in list mode
164 }
165 
166 QWidget * ConfigAgentDelegate::createEditor ( QWidget *, const QStyleOptionViewItem &, const QModelIndex & ) const
167 {
168  return 0;
169 }
170 
171 bool ConfigAgentDelegate::editorEvent ( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
172 {
173  Q_UNUSED ( model );
174  if ( !index.isValid() )
175  return false;
176  if ( ! ( ( event->type() == QEvent::MouseButtonRelease )
177  || ( event->type() == QEvent::MouseButtonPress )
178  || ( event->type() == QEvent::MouseMove ) ) )
179  return false;
180 
181 
182  QMouseEvent *me = static_cast<QMouseEvent*> ( event );
183  const QPoint mousePos = me->pos() - option.rect.topLeft();
184 
185  QStyleOptionButton buttonOpt = buttonOption ( option );
186 
187  if ( buttonOpt.rect.contains ( mousePos ) ) {
188 
189  switch ( event->type() ) {
190  case QEvent::MouseButtonPress:
191  return false;
192  case QEvent::MouseButtonRelease: {
193  QPoint pos = buttonOpt.rect.bottomLeft() + option.rect.topLeft();
194  const QString ident = index.data ( Akonadi::AgentInstanceModel::InstanceIdentifierRole ).toString();
195  emit optionsClicked ( ident, pos );
196  return true;
197  }
198  default:
199  return false;
200  }
201  }
202  return false;
203 }
204 
205 void ConfigAgentDelegate::drawFocus ( QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect ) const
206 {
207  if ( option.state & QStyle::State_HasFocus ) {
208  QStyleOptionFocusRect o;
209  o.QStyleOption::operator= ( option );
210  o.rect = rect;
211  o.state |= QStyle::State_KeyboardFocusChange;
212  QPalette::ColorGroup cg = ( option.state & QStyle::State_Enabled ) ? QPalette::Normal : QPalette::Disabled;
213  o.backgroundColor = option.palette.color ( cg, ( option.state & QStyle::State_Selected )
214  ? QPalette::Highlight : QPalette::Background );
215  QApplication::style()->drawPrimitive ( QStyle::PE_FrameFocusRect, &o, painter );
216  }
217 }
218 
219 QStyleOptionButton ConfigAgentDelegate::buttonOption ( const QStyleOptionViewItem& option ) const
220 {
221  const QString label = i18n( "Retrieval Options" );
222  QStyleOptionButton buttonOpt;
223  QRect buttonRect = option.rect;
224  int height = option.rect.height() / 2;
225  int width = 22 + option.fontMetrics.width( label ) + 40; // icon size + label size + arrow and padding
226  buttonRect.setTop ( 0/*( option.rect.height() /2 ) - height / 2)*/ ); // center the button vertically
227  buttonRect.setHeight ( height );
228  buttonRect.setLeft ( option.rect.right() - width );
229  buttonRect.setWidth ( width );
230 
231  buttonOpt.rect = buttonRect;
232  buttonOpt.state = option.state;
233  buttonOpt.text = label;
234  buttonOpt.palette = option.palette;
235  buttonOpt.features = QStyleOptionButton::HasMenu;
236  buttonOpt.icon = s_icons->checkMailIcon;
237  buttonOpt.iconSize = QSize ( 22, 22 ); // FIXME don't hardcode this icon size
238 
239  return buttonOpt;
240 }
241 
QModelIndex
QTextDocument::drawContents
void drawContents(QPainter *p, const QRectF &rect)
QEvent
QWidget
QEvent::type
Type type() const
QString::toUpper
QString toUpper() const
QPainter::setRenderHint
void setRenderHint(RenderHint hint, bool on)
QColor::name
QString name() const
ConfigAgentDelegate::sizeHint
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: configagentdelegate.cpp:158
ConfigAgentDelegate::createEditor
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: configagentdelegate.cpp:166
QPainter::save
void save()
QPoint
s_delegatePaddingSize
static const int s_delegatePaddingSize
Definition: configagentdelegate.cpp:41
QMouseEvent
QStyleOptionButton
ConfigAgentDelegate
A delegate for listing the accounts in the account list with kmail specific options.
Definition: configagentdelegate.h:35
QObject::event
virtual bool event(QEvent *e)
QObject::name
const char * name() const
QRect
QModelIndex::isValid
bool isValid() const
ConfigAgentDelegate::editorEvent
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
Definition: configagentdelegate.cpp:171
QVariant::toUInt
uint toUInt(bool *ok) const
QVariant::toInt
int toInt(bool *ok) const
ConfigAgentDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Definition: configagentdelegate.cpp:121
QStyleOptionViewItem
QTextDocument::addResource
void addResource(int type, const QUrl &name, const QVariant &resource)
QObject
QPainter::setPen
void setPen(const QColor &color)
QRect::setTop
void setTop(int y)
QPainter
QRect::setWidth
void setWidth(int width)
QAbstractItemModel::data
virtual QVariant data(const QModelIndex &index, int role) const =0
QString
QColor
ConfigAgentDelegate::optionsClicked
void optionsClicked(const QString &, const QPoint &)
QPixmap
QStyleOptionFocusRect
QSize
QUrl
QPainter::restore
void restore()
QTextDocument::setTextWidth
void setTextWidth(qreal width)
QModelIndex::model
const QAbstractItemModel * model() const
QModelIndex::data
QVariant data(int role) const
QApplication::style
QStyle * style()
QLatin1String
QTextDocument
QRect::setHeight
void setHeight(int height)
QStyle::drawControl
virtual void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const =0
configagentdelegate.h
QVariant::toBool
bool toBool() const
QPainter::translate
void translate(const QPointF &offset)
QStyle::drawPrimitive
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const =0
QAbstractItemModel
QTextDocument::setHtml
void setHtml(const QString &html)
QPen
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QVariant::isValid
bool isValid() const
QMouseEvent::pos
const QPoint & pos() const
QStyleOptionViewItemV4
QVariant::type
Type type() const
QPainter::pen
const QPen & pen() const
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QVariant::toString
QString toString() const
QRect::setLeft
void setLeft(int x)
QVariant
QStyledItemDelegate
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:32 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kmail

Skip menu "kmail"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

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