File: kratingwidget.cpp

package info (click to toggle)
kwidgetsaddons 5.28.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 62,388 kB
  • ctags: 3,611
  • sloc: cpp: 26,647; python: 682; sh: 23; makefile: 6
file content (267 lines) | stat: -rw-r--r-- 6,758 bytes parent folder | download
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * This file is part of the KDE libraries
 * Copyright (C) 2006-2007 Sebastian Trueg <trueg@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kratingwidget.h"
#include "kratingpainter.h"

#include <QPainter>
#include <QPixmap>
#include <QKeyEvent>
#include <QImage>
#include <QIcon>

class KRatingWidget::Private
{
public:
    Private()
        : rating(0),
          hoverRating(-1),
          pixSize(16)
    {
    }

    int rating;
    int hoverRating;
    int pixSize;

    KRatingPainter ratingPainter;
};

KRatingWidget::KRatingWidget(QWidget *parent)
    : QFrame(parent),
      d(new Private())
{
    setMouseTracking(true);
}

KRatingWidget::~KRatingWidget()
{
    delete d;
}

#ifndef KWIDGETSADDONS_NO_DEPRECATED
void KRatingWidget::setPixmap(const QPixmap &pix)
{
    setCustomPixmap(pix);
}
#endif

void KRatingWidget::setCustomPixmap(const QPixmap &pix)
{
    d->ratingPainter.setCustomPixmap(pix);
    update();
}

void KRatingWidget::setIcon(const QIcon &icon)
{
    d->ratingPainter.setIcon(icon);
    update();
}

void KRatingWidget::setPixmapSize(int size)
{
    d->pixSize = size;
    updateGeometry();
}

int KRatingWidget::spacing() const
{
    return d->ratingPainter.spacing();
}

QIcon KRatingWidget::icon() const
{
    return d->ratingPainter.icon();
}

void KRatingWidget::setSpacing(int s)
{
    d->ratingPainter.setSpacing(s);
    update();
}

Qt::Alignment KRatingWidget::alignment() const
{
    return d->ratingPainter.alignment();
}

void KRatingWidget::setAlignment(Qt::Alignment align)
{
    d->ratingPainter.setAlignment(align);
    update();
}

Qt::LayoutDirection KRatingWidget::layoutDirection() const
{
    return d->ratingPainter.layoutDirection();
}

void KRatingWidget::setLayoutDirection(Qt::LayoutDirection direction)
{
    d->ratingPainter.setLayoutDirection(direction);
    update();
}

unsigned int KRatingWidget::rating() const
{
    return d->rating;
}

int KRatingWidget::maxRating() const
{
    return d->ratingPainter.maxRating();
}

bool KRatingWidget::halfStepsEnabled() const
{
    return d->ratingPainter.halfStepsEnabled();
}

#ifndef KWIDGETSADDONS_NO_DEPRECATED
void KRatingWidget::setRating(unsigned int rating)
{
    setRating((int)rating);
}
#endif

void KRatingWidget::setRating(int rating)
{
    if (rating != d->rating) {
        d->rating = rating;
        d->hoverRating = rating;
        emit ratingChanged(rating);
        emit ratingChanged((unsigned int)rating);
        update();
    }
}

#ifndef KWIDGETSADDONS_NO_DEPRECATED
void KRatingWidget::setMaxRating(unsigned int max)
{
    setMaxRating((int)max);
}
#endif

void KRatingWidget::setMaxRating(int max)
{
    d->ratingPainter.setMaxRating(max);
    update();
}

void KRatingWidget::setHalfStepsEnabled(bool enabled)
{
    d->ratingPainter.setHalfStepsEnabled(enabled);
    update();
}

#ifndef KWIDGETSADDONS_NO_DEPRECATED
void KRatingWidget::setOnlyPaintFullSteps(bool fs)
{
    setHalfStepsEnabled(!fs);
}
#endif

static inline int adjustedHoverRating(bool halfStep, int hoverRating, int rating)
{
    // intentionally skip zero, or half step is disabled.
    if (!halfStep || hoverRating == 0) {
        return hoverRating;
    }

    // See bug 171343, if we click on a star we want it to be full star, click again
    // make it half, click third time make it clear.

    // round up hoverRating to next full star rating
    const int hoveredFullStarRating = hoverRating + (hoverRating % 2);
    // Check if the star under mouse is the last half or whole star of the rating.
    if (hoveredFullStarRating == rating || hoveredFullStarRating == rating + 1) {
        // If current pointed star is not empty, next rating will be rating - 1
        // if we point at 4th star and rating is 8 (4 star), next click will make it 7
        // if we point at 4th star and rating is 7 (3.5 star), next click will make it 6
        hoverRating = rating - 1;
    } else {
        // otherwise make it a full star rating
        hoverRating = hoveredFullStarRating;
    }
    return hoverRating;
}

void KRatingWidget::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        d->hoverRating = adjustedHoverRating(halfStepsEnabled(),
                                             d->ratingPainter.ratingFromPosition(contentsRect(), e->pos()),
                                             d->rating);
        // avoid set a rating to something less than zero, it may happen if widget is scaled and
        // mouse is clicked outside the star region.
        if (d->hoverRating >= 0) {
            setRating(d->hoverRating);
        }
    }
}

void KRatingWidget::mouseMoveEvent(QMouseEvent *e)
{
    // when moving the mouse we show the user what the result of clicking will be
    const int prevHoverRating = d->hoverRating;
    d->hoverRating = adjustedHoverRating(halfStepsEnabled(),
                                         d->ratingPainter.ratingFromPosition(contentsRect(), e->pos()),
                                         d->rating);
    if (d->hoverRating != prevHoverRating) {
        update();
    }
}

void KRatingWidget::leaveEvent(QEvent *)
{
    d->hoverRating = -1;
    update();
}

void KRatingWidget::paintEvent(QPaintEvent *e)
{
    QFrame::paintEvent(e);
    QPainter p(this);
    d->ratingPainter.setEnabled(isEnabled());
    d->ratingPainter.paint(&p, contentsRect(), d->rating, d->hoverRating);
}

QSize KRatingWidget::sizeHint() const
{
    int numPix = d->ratingPainter.maxRating();
    if (d->ratingPainter.halfStepsEnabled()) {
        numPix /= 2;
    }

    QSize pixSize(d->pixSize, d->pixSize);
    if (!d->ratingPainter.customPixmap().isNull()) {
        pixSize = d->ratingPainter.customPixmap().size() / d->ratingPainter.customPixmap().devicePixelRatio();
    }

    return QSize(pixSize.width() * numPix + spacing() * (numPix - 1) + frameWidth() * 2,
                 pixSize.height() + frameWidth() * 2);
}

void KRatingWidget::resizeEvent(QResizeEvent *e)
{
    QFrame::resizeEvent(e);
}

#include "moc_kratingwidget.cpp"