~ubuntu-branches/ubuntu/trusty/digikam/trusty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2006-01-20
 * Description : action categorized view
 *
 * Copyright (C) 2006-2012 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ============================================================ */

#include "actioncategorizedview.h"

// Qt includes

#include <QString>
#include <QPropertyAnimation>
#include <QScrollBar>
#include <QSignalMapper>

// KDE includes

#include <kdebug.h>
#include <kdeversion.h>
#include <kcategorydrawer.h>

namespace Digikam
{

ActionCategorizedView::ActionCategorizedView(QWidget* const parent)
    : KCategorizedView(parent)
{
    m_horizontalScrollAnimation = new QPropertyAnimation(horizontalScrollBar(), "value", this);
    m_verticalScrollAnimation   = new QPropertyAnimation(verticalScrollBar(),   "value", this);
}

ActionCategorizedView::~ActionCategorizedView()
{
}

void ActionCategorizedView::setupIconMode()
{
    setViewMode(QListView::IconMode);
    setMovement(QListView::Static);
#if KDE_IS_VERSION(4,5,0)
    setCategoryDrawer(new KCategoryDrawerV3(this)); // deprecated, but needed for KDE 4.4 compatibility
#else
    setCategoryDrawer(new KCategoryDrawerV2);       // deprecated, but needed for KDE 4.4 compatibility
#endif
    setSelectionMode(QAbstractItemView::SingleSelection);

    setMouseTracking(true);
    viewport()->setAttribute(Qt::WA_Hover);

    setFrameShape(QFrame::NoFrame);
}

void ActionCategorizedView::adjustGridSize()
{
    // Find a suitable grid size. The delegate's size hint does never word-wrap.
    // To keep a suitable width, we want to word wrap.
    setWordWrap(true);
    int maxSize = viewOptions().decorationSize.width() * 4;
    QFontMetrics fm(viewOptions().font);
    QSize grid;

    for (int i = 0; i < model()->rowCount(); ++i)
    {
        const QModelIndex index = model()->index(i, 0);
        const QSize size        = sizeHintForIndex(index);

        if (size.width() > maxSize)
        {
            QString text        = index.data(Qt::DisplayRole).toString();
            QRect unwrappedRect = fm.boundingRect(QRect(0, 0, size.width(), size.height()), Qt::AlignLeft, text);
            QRect wrappedRect   = fm.boundingRect(QRect(0, 0, maxSize, maxSize), Qt::AlignLeft | Qt::TextWordWrap, text);
            grid                = grid.expandedTo(QSize(maxSize, size.height() + wrappedRect.height() - unwrappedRect.height()));
        }
        else
        {
            grid = grid.expandedTo(size);
        }
    }

    //grid += QSize(KDialog::spacingHint(), KDialog::spacingHint());
    setGridSize(grid);
}

int ActionCategorizedView::autoScrollDuration(float relativeDifference, QPropertyAnimation* animation)
{
    const int minimumTime       = 1000;
    const int maxPixelPerSecond = 1000;

    int pixelToScroll           = qAbs(animation->startValue().toInt() - animation->endValue().toInt());
    int factor                  = qMax(1.0f, relativeDifference * 100); // in [1;15]

    int duration                = 1000 * pixelToScroll / maxPixelPerSecond;
    duration                    *= factor;

    return qMax(minimumTime, duration);
}

void ActionCategorizedView::autoScroll(float relativePos, QScrollBar* scrollBar, QPropertyAnimation* animation)
{

    if (scrollBar->minimum() != scrollBar->maximum())
    {
        const float lowerPart = 0.15F;
        const float upperPart = 0.85F;

        if (relativePos > upperPart && scrollBar->value() !=  scrollBar->maximum())
        {
            animation->stop();
            animation->setStartValue(scrollBar->value());
            animation->setEndValue(scrollBar->maximum());
            animation->setDuration(autoScrollDuration(1 - relativePos, animation));
            animation->start();
        }
        else if (relativePos < lowerPart && scrollBar->value() !=  scrollBar->minimum())
        {
            animation->stop();
            animation->setStartValue(scrollBar->value());
            animation->setEndValue(scrollBar->minimum());
            animation->setDuration(autoScrollDuration(relativePos, animation));
            animation->start();
        }
        else
        {
            animation->stop();
        }
    }
}

void ActionCategorizedView::mouseMoveEvent(QMouseEvent* e)
{
    KCategorizedView::mouseMoveEvent(e);
    autoScroll(float(e->pos().x()) / viewport()->width(),  horizontalScrollBar(), m_horizontalScrollAnimation);
    autoScroll(float(e->pos().y()) / viewport()->height(), verticalScrollBar(),   m_verticalScrollAnimation);
}

void ActionCategorizedView::leaveEvent(QEvent* e)
{
    KCategorizedView::leaveEvent(e);
    m_horizontalScrollAnimation->stop();
    m_verticalScrollAnimation->stop();
}

}  // namespace Digikam