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>
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.
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.
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
22 #include "configagentdelegate.h"
24 #include <Akonadi/AgentInstanceModel>
25 #include <Akonadi/AgentInstance>
33 #include <QAbstractTextDocumentLayout>
34 #include <QApplication>
35 #include <QHBoxLayout>
38 #include <QTextDocument>
41 using Akonadi::AgentInstanceModel
;
42 using Akonadi::AgentInstance
;
46 : readyPixmap ( KIcon ( QLatin1String ( "user-online" ) ).pixmap ( QSize ( 16, 16 ) ) )
47 , syncPixmap ( KIcon ( QLatin1String ( "network-connect" ) ).pixmap ( QSize ( 16, 16 ) ) )
48 , errorPixmap ( KIcon ( QLatin1String ( "dialog-error" ) ).pixmap ( QSize ( 16, 16 ) ) )
49 , offlinePixmap ( KIcon ( QLatin1String ( "network-disconnect" ) ).pixmap ( QSize ( 16, 16 ) ) )
50 , checkMailIcon ( KIcon ( "mail-receive" ) ) {
52 QPixmap readyPixmap
, syncPixmap
, errorPixmap
, offlinePixmap
;
56 K_GLOBAL_STATIC ( Icons
, s_icons
)
58 ConfigAgentDelegate::ConfigAgentDelegate ( QObject
*parent
)
59 : QStyledItemDelegate ( parent
)
63 QTextDocument
* ConfigAgentDelegate::document ( const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const
65 if ( !index
.isValid() )
68 const QString name
= index
.model()->data ( index
, Qt::DisplayRole
).toString();
69 int status
= index
.model()->data ( index
, AgentInstanceModel::StatusRole
).toInt();
70 uint progress
= index
.model()->data ( index
, AgentInstanceModel::ProgressRole
).toUInt();
71 const QString statusMessage
= index
.model()->data ( index
, AgentInstanceModel::StatusMessageRole
).toString();
72 const QStringList capabilities
= index
.model()->data ( index
, AgentInstanceModel::CapabilitiesRole
).toStringList();
74 QTextDocument
*document
= new QTextDocument ( 0 );
76 const QVariant data
= index
.model()->data ( index
, Qt::DecorationRole
);
77 if ( data
.isValid() && data
.type() == QVariant::Icon
) {
78 document
->addResource ( QTextDocument::ImageResource
, QUrl ( QLatin1String ( "agent_icon" ) ),
79 qvariant_cast
<QIcon
> ( data
).pixmap ( QSize ( 64, 64 ) ) );
82 if ( !index
.data ( AgentInstanceModel::OnlineRole
).toBool() )
83 document
->addResource ( QTextDocument::ImageResource
, QUrl ( QLatin1String ( "status_icon" ) ), s_icons
->offlinePixmap
);
84 else if ( status
== AgentInstance::Idle
)
85 document
->addResource ( QTextDocument::ImageResource
, QUrl ( QLatin1String ( "status_icon" ) ), s_icons
->readyPixmap
);
86 else if ( status
== AgentInstance::Running
)
87 document
->addResource ( QTextDocument::ImageResource
, QUrl ( QLatin1String ( "status_icon" ) ), s_icons
->syncPixmap
);
89 document
->addResource ( QTextDocument::ImageResource
, QUrl ( QLatin1String ( "status_icon" ) ), s_icons
->errorPixmap
);
92 QPalette::ColorGroup cg
= option
.state
& QStyle::State_Enabled
? QPalette::Normal
: QPalette::Disabled
;
93 if ( cg
== QPalette::Normal
&& ! ( option
.state
& QStyle::State_Active
) )
94 cg
= QPalette::Inactive
;
97 if ( option
.state
& QStyle::State_Selected
) {
98 textColor
= option
.palette
.color ( cg
, QPalette::HighlightedText
);
101 textColor
= option
.palette
.color ( cg
, QPalette::Text
);
104 QString content
= QString::fromLatin1 (
105 "<html style=\"color:%1\">"
109 "<td rowspan=\"2\"><img src=\"agent_icon\"> </td>"
111 "</tr>" ).arg ( textColor
.name().toUpper() ).arg ( name
)
112 + QString::fromLatin1 (
114 "<td><img src=\"status_icon\"/> %1 %2</td>"
115 "</tr>" ).arg ( statusMessage
).arg ( status
== 1 ? QString ( QLatin1String ( "(%1%)" ) ).arg ( progress
) : QLatin1String ( "" ) )
116 + QLatin1String ( "</table></body></html>" );
118 document
->setHtml ( content
);
123 void ConfigAgentDelegate::paint ( QPainter
*painter
, const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const
125 if ( !index
.isValid() )
128 QTextDocument
*doc
= document ( option
, index
);
132 QStyleOptionButton buttonOpt
= buttonOption ( option
);
134 doc
->setTextWidth( option
.rect
.width() - buttonOpt
.rect
.width() );
135 painter
->setRenderHint ( QPainter::Antialiasing
);
137 QPen pen
= painter
->pen();
139 QPalette::ColorGroup cg
= option
.state
& QStyle::State_Enabled
? QPalette::Normal
: QPalette::Disabled
;
140 if ( cg
== QPalette::Normal
&& ! ( option
.state
& QStyle::State_Active
) )
141 cg
= QPalette::Inactive
;
143 QStyleOptionViewItemV4
opt ( option
);
144 opt
.showDecorationSelected
= true;
145 QApplication::style()->drawPrimitive ( QStyle::PE_PanelItemViewItem
, &opt
, painter
);
147 painter
->translate ( option
.rect
.topLeft() );
149 doc
->drawContents ( painter
);
151 QApplication::style()->drawControl ( QStyle::CE_PushButton
, &buttonOpt
, painter
);
154 painter
->setPen ( pen
);
156 drawFocus ( painter
, option
, option
.rect
);
160 QSize
ConfigAgentDelegate::sizeHint ( const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const
162 if ( !index
.isValid() )
163 return QSize ( 0, 0 );
165 QTextDocument
*doc
= document ( option
, index
);
167 return QSize ( 0, 0 );
169 const QSize size
= doc
->documentLayout()->documentSize().toSize();
175 QWidget
* ConfigAgentDelegate::createEditor ( QWidget
*, const QStyleOptionViewItem
&, const QModelIndex
& ) const
180 bool ConfigAgentDelegate::editorEvent ( QEvent
* event
, QAbstractItemModel
* model
, const QStyleOptionViewItem
& option
, const QModelIndex
& index
)
183 if ( ! ( ( event
->type() == QEvent::MouseButtonRelease
)
184 || ( event
->type() == QEvent::MouseButtonPress
)
185 || ( event
->type() == QEvent::MouseMove
) ) )
188 if ( !index
.isValid() )
191 QMouseEvent
*me
= static_cast<QMouseEvent
*> ( event
);
192 const QPoint mousePos
= me
->pos() - option
.rect
.topLeft();
194 QStyleOptionButton buttonOpt
= buttonOption ( option
);
196 if ( buttonOpt
.rect
.contains ( mousePos
) ) {
198 switch ( event
->type() ) {
199 case QEvent::MouseButtonPress
:
202 case QEvent::MouseButtonRelease
: {
203 QPoint pos
= buttonOpt
.rect
.bottomLeft() + option
.rect
.topLeft();
204 const QString ident
= index
.data ( Akonadi::AgentInstanceModel::InstanceIdentifierRole
).toString();
205 emit
optionsClicked ( ident
, pos
);
216 void ConfigAgentDelegate::drawFocus ( QPainter
*painter
, const QStyleOptionViewItem
&option
, const QRect
&rect
) const
218 if ( option
.state
& QStyle::State_HasFocus
) {
219 QStyleOptionFocusRect o
;
220 o
.QStyleOption::operator= ( option
);
222 o
.state
|= QStyle::State_KeyboardFocusChange
;
223 QPalette::ColorGroup cg
= ( option
.state
& QStyle::State_Enabled
) ? QPalette::Normal
: QPalette::Disabled
;
224 o
.backgroundColor
= option
.palette
.color ( cg
, ( option
.state
& QStyle::State_Selected
)
225 ? QPalette::Highlight
: QPalette::Background
);
226 QApplication::style()->drawPrimitive ( QStyle::PE_FrameFocusRect
, &o
, painter
);
230 QStyleOptionButton
ConfigAgentDelegate::buttonOption ( const QStyleOptionViewItem
& option
) const
232 QString label
= i18n( "Retrieval Options" );
233 QStyleOptionButton buttonOpt
;
234 QRect buttonRect
= option
.rect
;
235 int height
= option
.rect
.height() / 2;
236 int width
= 22 + option
.fontMetrics
.width( label
) + 40; // icon size + label size + arrow and padding
237 buttonRect
.setTop ( ( option
.rect
.height() / 2 ) - height
/ 2 ); // center the button vertically
238 buttonRect
.setHeight ( height
);
239 buttonRect
.setLeft ( option
.rect
.right() - width
);
240 buttonRect
.setWidth ( width
);
242 buttonOpt
.rect
= buttonRect
;
243 buttonOpt
.state
= option
.state
;
244 buttonOpt
.text
= label
;
245 buttonOpt
.palette
= option
.palette
;
246 buttonOpt
.features
= QStyleOptionButton::HasMenu
;
247 buttonOpt
.icon
= s_icons
->checkMailIcon
;
248 buttonOpt
.iconSize
= QSize ( 22, 22 ); // FIXME don't hardcode this icon size
253 #include "configagentdelegate.moc"