1
/* ============================================================
3
* This file is a part of digiKam project
4
* http://www.digikam.org
7
* Description : action categorized view
9
* Copyright (C) 2006-2012 by Gilles Caulier <caulier dot gilles at gmail dot com>
11
* This program is free software; you can redistribute it
12
* and/or modify it under the terms of the GNU General
13
* Public License as published by the Free Software Foundation;
14
* either version 2, or (at your option)
17
* This program is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
* GNU General Public License for more details.
22
* ============================================================ */
24
#include "actioncategorizedview.h"
29
#include <QPropertyAnimation>
31
#include <QSignalMapper>
36
#include <kdeversion.h>
37
#include <kcategorydrawer.h>
42
ActionCategorizedView::ActionCategorizedView(QWidget* const parent)
43
: KCategorizedView(parent)
45
m_horizontalScrollAnimation = new QPropertyAnimation(horizontalScrollBar(), "value", this);
46
m_verticalScrollAnimation = new QPropertyAnimation(verticalScrollBar(), "value", this);
49
ActionCategorizedView::~ActionCategorizedView()
53
void ActionCategorizedView::setupIconMode()
55
setViewMode(QListView::IconMode);
56
setMovement(QListView::Static);
57
#if KDE_IS_VERSION(4,5,0)
58
setCategoryDrawer(new KCategoryDrawerV3(this)); // deprecated, but needed for KDE 4.4 compatibility
60
setCategoryDrawer(new KCategoryDrawerV2); // deprecated, but needed for KDE 4.4 compatibility
62
setSelectionMode(QAbstractItemView::SingleSelection);
64
setMouseTracking(true);
65
viewport()->setAttribute(Qt::WA_Hover);
67
setFrameShape(QFrame::NoFrame);
70
void ActionCategorizedView::adjustGridSize()
72
// Find a suitable grid size. The delegate's size hint does never word-wrap.
73
// To keep a suitable width, we want to word wrap.
75
int maxSize = viewOptions().decorationSize.width() * 4;
76
QFontMetrics fm(viewOptions().font);
79
for (int i = 0; i < model()->rowCount(); ++i)
81
const QModelIndex index = model()->index(i, 0);
82
const QSize size = sizeHintForIndex(index);
84
if (size.width() > maxSize)
86
QString text = index.data(Qt::DisplayRole).toString();
87
QRect unwrappedRect = fm.boundingRect(QRect(0, 0, size.width(), size.height()), Qt::AlignLeft, text);
88
QRect wrappedRect = fm.boundingRect(QRect(0, 0, maxSize, maxSize), Qt::AlignLeft | Qt::TextWordWrap, text);
89
grid = grid.expandedTo(QSize(maxSize, size.height() + wrappedRect.height() - unwrappedRect.height()));
93
grid = grid.expandedTo(size);
97
//grid += QSize(KDialog::spacingHint(), KDialog::spacingHint());
101
int ActionCategorizedView::autoScrollDuration(float relativeDifference, QPropertyAnimation* animation)
103
const int minimumTime = 1000;
104
const int maxPixelPerSecond = 1000;
106
int pixelToScroll = qAbs(animation->startValue().toInt() - animation->endValue().toInt());
107
int factor = qMax(1.0f, relativeDifference * 100); // in [1;15]
109
int duration = 1000 * pixelToScroll / maxPixelPerSecond;
112
return qMax(minimumTime, duration);
115
void ActionCategorizedView::autoScroll(float relativePos, QScrollBar* scrollBar, QPropertyAnimation* animation)
118
if (scrollBar->minimum() != scrollBar->maximum())
120
const float lowerPart = 0.15F;
121
const float upperPart = 0.85F;
123
if (relativePos > upperPart && scrollBar->value() != scrollBar->maximum())
126
animation->setStartValue(scrollBar->value());
127
animation->setEndValue(scrollBar->maximum());
128
animation->setDuration(autoScrollDuration(1 - relativePos, animation));
131
else if (relativePos < lowerPart && scrollBar->value() != scrollBar->minimum())
134
animation->setStartValue(scrollBar->value());
135
animation->setEndValue(scrollBar->minimum());
136
animation->setDuration(autoScrollDuration(relativePos, animation));
146
void ActionCategorizedView::mouseMoveEvent(QMouseEvent* e)
148
KCategorizedView::mouseMoveEvent(e);
149
autoScroll(float(e->pos().x()) / viewport()->width(), horizontalScrollBar(), m_horizontalScrollAnimation);
150
autoScroll(float(e->pos().y()) / viewport()->height(), verticalScrollBar(), m_verticalScrollAnimation);
153
void ActionCategorizedView::leaveEvent(QEvent* e)
155
KCategorizedView::leaveEvent(e);
156
m_horizontalScrollAnimation->stop();
157
m_verticalScrollAnimation->stop();
160
} // namespace Digikam