00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "progressspinnerdelegate_p.h"
00023
00024 #include "entitytreemodel.h"
00025
00026 #include <QTimerEvent>
00027 #include <QAbstractItemView>
00028
00029 using namespace Akonadi;
00030
00031 DelegateAnimator::DelegateAnimator(QAbstractItemView *view)
00032 : QObject(view), m_view(view), m_timerId(-1)
00033 {
00034 m_pixmapSequence = KPixmapSequence(QLatin1String("process-working"), 22);
00035 }
00036
00037 void DelegateAnimator::timerEvent(QTimerEvent* event)
00038 {
00039 if (!(event->timerId() == m_timerId && m_view))
00040 return QObject::timerEvent(event);
00041
00042 QRegion region;
00043 foreach (const Animation &animation, m_animations)
00044 {
00045
00046
00047 animation.animate();
00048 region += m_view->visualRect(animation.index);
00049 }
00050
00051 m_view->viewport()->update(region);
00052 }
00053
00054 QPixmap DelegateAnimator::sequenceFrame(const QModelIndex& index)
00055 {
00056 foreach(const Animation &animation, m_animations)
00057 {
00058 if (animation.index == index)
00059 {
00060 return m_pixmapSequence.frameAt(animation.frame);
00061 }
00062 }
00063 return QPixmap();
00064 }
00065
00066
00067 ProgressSpinnerDelegate::ProgressSpinnerDelegate(DelegateAnimator *animator, QObject* parent)
00068 : QStyledItemDelegate(parent), m_animator(animator)
00069 {
00070
00071 }
00072
00073 void ProgressSpinnerDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const
00074 {
00075 QStyledItemDelegate::initStyleOption(option, index);
00076
00077 const Akonadi::Collection collection = index.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
00078
00079 if (!collection.isValid())
00080 {
00081 m_animator->pop(index);
00082 return;
00083 }
00084
00085 if (index.data(Akonadi::EntityTreeModel::FetchStateRole).toInt() != Akonadi::EntityTreeModel::FetchingState)
00086 {
00087 m_animator->pop(index);
00088 return;
00089 }
00090
00091 m_animator->push(index);
00092
00093 if (QStyleOptionViewItemV4 *v4 = qstyleoption_cast<QStyleOptionViewItemV4 *>(option)) {
00094 v4->icon = m_animator->sequenceFrame(index);
00095 }
00096 }
00097
00098 uint Akonadi::qHash(Akonadi::DelegateAnimator::Animation anim)
00099 {
00100 return qHash(anim.index);
00101 }