mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-24 10:52:49 +00:00
kdeui: drop support for setting custom pixmap for KRatingPainter
KRatingPainter and KRatingWidget are currently not used but holding a reference to a pixmap that can already be (and is) obtained from the icon (constructing QIcon from pixmap is a thing) is just redundant Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
12b8b0b3ae
commit
f197e144ca
4 changed files with 201 additions and 298 deletions
|
@ -41,7 +41,8 @@ public:
|
||||||
bHalfSteps(true),
|
bHalfSteps(true),
|
||||||
alignment(Qt::AlignCenter),
|
alignment(Qt::AlignCenter),
|
||||||
direction(Qt::LeftToRight),
|
direction(Qt::LeftToRight),
|
||||||
spacing(0) {
|
spacing(0)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap getPixmap(int size);
|
QPixmap getPixmap(int size);
|
||||||
|
@ -52,24 +53,17 @@ public:
|
||||||
bool bHalfSteps;
|
bool bHalfSteps;
|
||||||
Qt::Alignment alignment;
|
Qt::Alignment alignment;
|
||||||
Qt::LayoutDirection direction;
|
Qt::LayoutDirection direction;
|
||||||
QPixmap customPixmap;
|
|
||||||
int spacing;
|
int spacing;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
QPixmap KRatingPainter::Private::getPixmap(int size)
|
QPixmap KRatingPainter::Private::getPixmap(int size)
|
||||||
{
|
{
|
||||||
if ( !customPixmap.isNull() ) {
|
|
||||||
return customPixmap.scaled( QSize( size, size ) );
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
QIcon _icon(icon);
|
QIcon _icon(icon);
|
||||||
if (_icon.isNull()) {
|
if (_icon.isNull()) {
|
||||||
_icon = KIcon("rating");
|
_icon = KIcon("rating");
|
||||||
}
|
}
|
||||||
return _icon.pixmap(size);
|
return _icon.pixmap(size);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
KRatingPainter::KRatingPainter()
|
KRatingPainter::KRatingPainter()
|
||||||
|
@ -77,79 +71,61 @@ KRatingPainter::KRatingPainter()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
KRatingPainter::~KRatingPainter()
|
KRatingPainter::~KRatingPainter()
|
||||||
{
|
{
|
||||||
delete d;
|
delete d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int KRatingPainter::maxRating() const
|
int KRatingPainter::maxRating() const
|
||||||
{
|
{
|
||||||
return d->maxRating;
|
return d->maxRating;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool KRatingPainter::halfStepsEnabled() const
|
bool KRatingPainter::halfStepsEnabled() const
|
||||||
{
|
{
|
||||||
return d->bHalfSteps;
|
return d->bHalfSteps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Qt::Alignment KRatingPainter::alignment() const
|
Qt::Alignment KRatingPainter::alignment() const
|
||||||
{
|
{
|
||||||
return d->alignment;
|
return d->alignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Qt::LayoutDirection KRatingPainter::layoutDirection() const
|
Qt::LayoutDirection KRatingPainter::layoutDirection() const
|
||||||
{
|
{
|
||||||
return d->direction;
|
return d->direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QIcon KRatingPainter::icon() const
|
QIcon KRatingPainter::icon() const
|
||||||
{
|
{
|
||||||
return d->icon;
|
return d->icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool KRatingPainter::isEnabled() const
|
bool KRatingPainter::isEnabled() const
|
||||||
{
|
{
|
||||||
return d->isEnabled;
|
return d->isEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QPixmap KRatingPainter::customPixmap() const
|
|
||||||
{
|
|
||||||
return d->customPixmap;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int KRatingPainter::spacing() const
|
int KRatingPainter::spacing() const
|
||||||
{
|
{
|
||||||
return d->spacing;
|
return d->spacing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingPainter::setMaxRating(int max)
|
void KRatingPainter::setMaxRating(int max)
|
||||||
{
|
{
|
||||||
d->maxRating = max;
|
d->maxRating = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingPainter::setHalfStepsEnabled(bool enabled)
|
void KRatingPainter::setHalfStepsEnabled(bool enabled)
|
||||||
{
|
{
|
||||||
d->bHalfSteps = enabled;
|
d->bHalfSteps = enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingPainter::setAlignment(Qt::Alignment align)
|
void KRatingPainter::setAlignment(Qt::Alignment align)
|
||||||
{
|
{
|
||||||
d->alignment = align;
|
d->alignment = align;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingPainter::setLayoutDirection(Qt::LayoutDirection direction)
|
void KRatingPainter::setLayoutDirection(Qt::LayoutDirection direction)
|
||||||
{
|
{
|
||||||
d->direction = direction;
|
d->direction = direction;
|
||||||
|
@ -161,31 +137,22 @@ void KRatingPainter::setIcon( const QIcon& icon )
|
||||||
d->icon = icon;
|
d->icon = icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingPainter::setEnabled(bool enabled)
|
void KRatingPainter::setEnabled(bool enabled)
|
||||||
{
|
{
|
||||||
d->isEnabled = enabled;
|
d->isEnabled = enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingPainter::setCustomPixmap( const QPixmap& pixmap )
|
|
||||||
{
|
|
||||||
d->customPixmap = pixmap;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void KRatingPainter::setSpacing(int s)
|
void KRatingPainter::setSpacing(int s)
|
||||||
{
|
{
|
||||||
d->spacing = qMax(0, s);
|
d->spacing = qMax(0, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingPainter::paint(QPainter* painter, const QRect &rect, int rating, int hoverRating) const
|
void KRatingPainter::paint(QPainter* painter, const QRect &rect, int rating, int hoverRating) const
|
||||||
{
|
{
|
||||||
rating = qMin(rating, d->maxRating);
|
rating = qMin(rating, d->maxRating);
|
||||||
hoverRating = qMin(hoverRating, d->maxRating);
|
hoverRating = qMin(hoverRating, d->maxRating);
|
||||||
|
|
||||||
int numUsedStars = d->bHalfSteps ? d->maxRating/2 : d->maxRating;
|
int numUsedStars = (d->bHalfSteps ? d->maxRating / 2 : d->maxRating);
|
||||||
|
|
||||||
if ( hoverRating > 0 && hoverRating < rating ) {
|
if ( hoverRating > 0 && hoverRating < rating ) {
|
||||||
int tmp = hoverRating;
|
int tmp = hoverRating;
|
||||||
|
@ -196,7 +163,7 @@ void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, in
|
||||||
int usedSpacing = d->spacing;
|
int usedSpacing = d->spacing;
|
||||||
|
|
||||||
// get the rating pixmaps
|
// get the rating pixmaps
|
||||||
int maxHSizeOnePix = ( rect.width() - (numUsedStars-1)*usedSpacing ) / numUsedStars;
|
int maxHSizeOnePix = ((rect.width() - (numUsedStars - 1) * usedSpacing ) / numUsedStars);
|
||||||
QPixmap ratingPix = d->getPixmap(qMin(rect.height(), maxHSizeOnePix));
|
QPixmap ratingPix = d->getPixmap(qMin(rect.height(), maxHSizeOnePix));
|
||||||
|
|
||||||
QPixmap disabledRatingPix = KIconEffect::apply(ratingPix, KIconEffect::ToGray, 1.0, QColor(), QColor(), false);
|
QPixmap disabledRatingPix = KIconEffect::apply(ratingPix, KIconEffect::ToGray, 1.0, QColor(), QColor(), false);
|
||||||
|
@ -208,14 +175,14 @@ void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, in
|
||||||
KIconEffect::semiTransparent( disabledRatingPix );
|
KIconEffect::semiTransparent( disabledRatingPix );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool half = d->bHalfSteps && rating%2;
|
bool half = (d->bHalfSteps && (rating % 2));
|
||||||
int numRatingStars = d->bHalfSteps ? rating/2 : rating;
|
int numRatingStars = (d->bHalfSteps ? rating / 2 : rating);
|
||||||
|
|
||||||
int numHoverStars = 0;
|
int numHoverStars = 0;
|
||||||
bool halfHover = false;
|
bool halfHover = false;
|
||||||
if (hoverRating > 0 && rating != hoverRating && d->isEnabled) {
|
if (hoverRating > 0 && rating != hoverRating && d->isEnabled) {
|
||||||
numHoverStars = d->bHalfSteps ? hoverRating / 2 : hoverRating;
|
numHoverStars = d->bHalfSteps ? hoverRating / 2 : hoverRating;
|
||||||
halfHover = d->bHalfSteps && hoverRating%2;
|
halfHover = d->bHalfSteps && (hoverRating % 2);
|
||||||
hoverPix = KIconEffect::apply(ratingPix, KIconEffect::ToGray, 0.5, QColor(), QColor(), false);
|
hoverPix = KIconEffect::apply(ratingPix, KIconEffect::ToGray, 0.5, QColor(), QColor(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -231,22 +198,20 @@ void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, in
|
||||||
int x = rect.x();
|
int x = rect.x();
|
||||||
if (d->alignment & Qt::AlignRight) {
|
if (d->alignment & Qt::AlignRight) {
|
||||||
x += ( rect.width() - ratingAreaWidth);
|
x += ( rect.width() - ratingAreaWidth);
|
||||||
}
|
} else if (d->alignment & Qt::AlignHCenter) {
|
||||||
else if ( d->alignment & Qt::AlignHCenter ) {
|
x += ((rect.width() - ratingAreaWidth) / 2);
|
||||||
x += ( rect.width() - ratingAreaWidth )/2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int xInc = ratingPix.width() + usedSpacing;
|
int xInc = ratingPix.width() + usedSpacing;
|
||||||
if (d->direction == Qt::RightToLeft) {
|
if (d->direction == Qt::RightToLeft) {
|
||||||
x = rect.width() - ratingPix.width() - x;
|
x = (rect.width() - ratingPix.width() - x);
|
||||||
xInc = -xInc;
|
xInc = -xInc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int y = rect.y();
|
int y = rect.y();
|
||||||
if (d->alignment & Qt::AlignVCenter) {
|
if (d->alignment & Qt::AlignVCenter) {
|
||||||
y += (rect.height() / 2 - ratingPix.height() / 2);
|
y += (rect.height() / 2 - ratingPix.height() / 2);
|
||||||
}
|
} else if (d->alignment & Qt::AlignBottom ) {
|
||||||
else if ( d->alignment & Qt::AlignBottom ) {
|
|
||||||
y += (rect.height() - ratingPix.height() );
|
y += (rect.height() - ratingPix.height() );
|
||||||
}
|
}
|
||||||
for(; i < numRatingStars; ++i) {
|
for(; i < numRatingStars; ++i) {
|
||||||
|
@ -254,12 +219,16 @@ void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, in
|
||||||
x += xInc;
|
x += xInc;
|
||||||
}
|
}
|
||||||
if (half) {
|
if (half) {
|
||||||
painter->drawPixmap( x, y, ratingPix.width()/2, ratingPix.height(),
|
painter->drawPixmap(
|
||||||
|
x, y, ratingPix.width()/2, ratingPix.height(),
|
||||||
d->direction == Qt::RightToLeft ? (numHoverStars > 0 ? hoverPix : disabledRatingPix) : ratingPix,
|
d->direction == Qt::RightToLeft ? (numHoverStars > 0 ? hoverPix : disabledRatingPix) : ratingPix,
|
||||||
0, 0, ratingPix.width()/2, ratingPix.height() );
|
0, 0, ratingPix.width()/2, ratingPix.height()
|
||||||
painter->drawPixmap( x + ratingPix.width()/2, y, ratingPix.width()/2, ratingPix.height(),
|
);
|
||||||
|
painter->drawPixmap(
|
||||||
|
x + ratingPix.width() / 2, y, ratingPix.width() / 2, ratingPix.height(),
|
||||||
d->direction == Qt::RightToLeft ? ratingPix : (numHoverStars > 0 ? hoverPix : disabledRatingPix),
|
d->direction == Qt::RightToLeft ? ratingPix : (numHoverStars > 0 ? hoverPix : disabledRatingPix),
|
||||||
ratingPix.width()/2, 0, ratingPix.width()/2, ratingPix.height() );
|
ratingPix.width() / 2, 0, ratingPix.width() / 2, ratingPix.height()
|
||||||
|
);
|
||||||
x += xInc;
|
x += xInc;
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
@ -268,12 +237,16 @@ void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, in
|
||||||
x += xInc;
|
x += xInc;
|
||||||
}
|
}
|
||||||
if( halfHover ) {
|
if( halfHover ) {
|
||||||
painter->drawPixmap( x, y, ratingPix.width()/2, ratingPix.height(),
|
painter->drawPixmap(
|
||||||
|
x, y, ratingPix.width()/2, ratingPix.height(),
|
||||||
d->direction == Qt::RightToLeft ? disabledRatingPix : hoverPix,
|
d->direction == Qt::RightToLeft ? disabledRatingPix : hoverPix,
|
||||||
0, 0, ratingPix.width()/2, ratingPix.height() );
|
0, 0, ratingPix.width()/2, ratingPix.height()
|
||||||
painter->drawPixmap( x + ratingPix.width()/2, y, ratingPix.width()/2, ratingPix.height(),
|
);
|
||||||
|
painter->drawPixmap(
|
||||||
|
x + ratingPix.width() / 2, y, ratingPix.width() / 2, ratingPix.height(),
|
||||||
d->direction == Qt::RightToLeft ? hoverPix : disabledRatingPix,
|
d->direction == Qt::RightToLeft ? hoverPix : disabledRatingPix,
|
||||||
ratingPix.width()/2, 0, ratingPix.width()/2, ratingPix.height() );
|
ratingPix.width() / 2, 0, ratingPix.width() / 2, ratingPix.height()
|
||||||
|
);
|
||||||
x += xInc;
|
x += xInc;
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
@ -283,12 +256,11 @@ void KRatingPainter::paint( QPainter* painter, const QRect& rect, int rating, in
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int KRatingPainter::ratingFromPosition(const QRect &rect, const QPoint &pos) const
|
int KRatingPainter::ratingFromPosition(const QRect &rect, const QPoint &pos) const
|
||||||
{
|
{
|
||||||
int usedSpacing = d->spacing;
|
int usedSpacing = d->spacing;
|
||||||
int numUsedStars = d->bHalfSteps ? d->maxRating/2 : d->maxRating;
|
int numUsedStars = (d->bHalfSteps ? d->maxRating / 2 : d->maxRating);
|
||||||
int maxHSizeOnePix = ( rect.width() - (numUsedStars-1)*usedSpacing ) / numUsedStars;
|
int maxHSizeOnePix = ((rect.width() - (numUsedStars - 1) * usedSpacing) / numUsedStars);
|
||||||
QPixmap ratingPix = d->getPixmap(qMin(rect.height(), maxHSizeOnePix));
|
QPixmap ratingPix = d->getPixmap(qMin(rect.height(), maxHSizeOnePix));
|
||||||
|
|
||||||
int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1);
|
int ratingAreaWidth = ratingPix.width()*numUsedStars + usedSpacing*(numUsedStars-1);
|
||||||
|
@ -296,25 +268,23 @@ int KRatingPainter::ratingFromPosition( const QRect& rect, const QPoint& pos ) c
|
||||||
QRect usedRect(rect);
|
QRect usedRect(rect);
|
||||||
if (d->alignment & Qt::AlignRight ) {
|
if (d->alignment & Qt::AlignRight ) {
|
||||||
usedRect.setLeft(rect.right() - ratingAreaWidth);
|
usedRect.setLeft(rect.right() - ratingAreaWidth);
|
||||||
}
|
} else if (d->alignment & Qt::AlignHCenter) {
|
||||||
else if ( d->alignment & Qt::AlignHCenter ) {
|
|
||||||
int x = (rect.width() - ratingAreaWidth) / 2;
|
int x = (rect.width() - ratingAreaWidth) / 2;
|
||||||
usedRect.setLeft(rect.left() + x);
|
usedRect.setLeft(rect.left() + x);
|
||||||
usedRect.setRight(rect.right() - x);
|
usedRect.setRight(rect.right() - x);
|
||||||
}
|
} else {
|
||||||
else { // d->alignment & Qt::AlignLeft
|
// d->alignment & Qt::AlignLeft
|
||||||
usedRect.setRight(rect.left() + ratingAreaWidth - 1);
|
usedRect.setRight(rect.left() + ratingAreaWidth - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d->alignment & Qt::AlignBottom) {
|
if (d->alignment & Qt::AlignBottom) {
|
||||||
usedRect.setTop(rect.bottom() - ratingPix.height() + 1);
|
usedRect.setTop(rect.bottom() - ratingPix.height() + 1);
|
||||||
}
|
} else if (d->alignment & Qt::AlignVCenter) {
|
||||||
else if ( d->alignment & Qt::AlignVCenter ) {
|
|
||||||
int x = ( rect.height() - ratingPix.height() )/2;
|
int x = ( rect.height() - ratingPix.height() )/2;
|
||||||
usedRect.setTop( rect.top() + x );
|
usedRect.setTop( rect.top() + x );
|
||||||
usedRect.setBottom( rect.bottom() - x );
|
usedRect.setBottom( rect.bottom() - x );
|
||||||
}
|
} else {
|
||||||
else { // d->alignment & Qt::AlignTop
|
// d->alignment & Qt::AlignTop
|
||||||
usedRect.setBottom(rect.top() + ratingPix.height() - 1);
|
usedRect.setBottom(rect.top() + ratingPix.height() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -322,8 +292,7 @@ int KRatingPainter::ratingFromPosition( const QRect& rect, const QPoint& pos ) c
|
||||||
int x = 0;
|
int x = 0;
|
||||||
if (d->direction == Qt::RightToLeft) {
|
if (d->direction == Qt::RightToLeft) {
|
||||||
x = usedRect.right() - pos.x();
|
x = usedRect.right() - pos.x();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
x = pos.x() - usedRect.left();
|
x = pos.x() - usedRect.left();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,11 +302,8 @@ int KRatingPainter::ratingFromPosition( const QRect& rect, const QPoint& pos ) c
|
||||||
|
|
||||||
return ( int )( ( double )x/one + 0.5 );
|
return ( int )( ( double )x/one + 0.5 );
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void KRatingPainter::paintRating(QPainter *painter, const QRect &rect, Qt::Alignment align, int rating, int hoverRating)
|
void KRatingPainter::paintRating(QPainter *painter, const QRect &rect, Qt::Alignment align, int rating, int hoverRating)
|
||||||
{
|
{
|
||||||
|
@ -347,7 +313,6 @@ void KRatingPainter::paintRating( QPainter* painter, const QRect& rect, Qt::Alig
|
||||||
rp.paint(painter, rect, rating, hoverRating);
|
rp.paint(painter, rect, rating, hoverRating);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int KRatingPainter::getRatingFromPosition(const QRect &rect, Qt::Alignment align, Qt::LayoutDirection direction, const QPoint &pos)
|
int KRatingPainter::getRatingFromPosition(const QRect &rect, Qt::Alignment align, Qt::LayoutDirection direction, const QPoint &pos)
|
||||||
{
|
{
|
||||||
KRatingPainter rp;
|
KRatingPainter rp;
|
||||||
|
|
|
@ -99,7 +99,7 @@ public:
|
||||||
* The icon used to draw a star. In case a custom pixmap has been set
|
* The icon used to draw a star. In case a custom pixmap has been set
|
||||||
* this value is ignored.
|
* this value is ignored.
|
||||||
*
|
*
|
||||||
* \sa setIcon, setCustomPixmap
|
* \sa setIcon
|
||||||
*/
|
*/
|
||||||
QIcon icon() const;
|
QIcon icon() const;
|
||||||
|
|
||||||
|
@ -111,14 +111,6 @@ public:
|
||||||
*/
|
*/
|
||||||
bool isEnabled() const;
|
bool isEnabled() const;
|
||||||
|
|
||||||
/**
|
|
||||||
* The custom pixmap set to draw a star. If no custom
|
|
||||||
* pixmap has been set, an invalid pixmap is returned.
|
|
||||||
*
|
|
||||||
* \sa setCustomPixmap
|
|
||||||
*/
|
|
||||||
QPixmap customPixmap() const;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The spacing between rating pixmaps.
|
* The spacing between rating pixmaps.
|
||||||
*
|
*
|
||||||
|
@ -158,11 +150,6 @@ public:
|
||||||
*/
|
*/
|
||||||
void setEnabled(bool enabled);
|
void setEnabled(bool enabled);
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a custom pixmap.
|
|
||||||
*/
|
|
||||||
void setCustomPixmap( const QPixmap& pixmap );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the spacing between rating pixmaps. Be aware that
|
* Set the spacing between rating pixmaps. Be aware that
|
||||||
* for justified horizontal alignment this values may be
|
* for justified horizontal alignment this values may be
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
#include <QtGui/QPainter>
|
#include <QtGui/QPainter>
|
||||||
#include <QtGui/QPixmap>
|
#include <QtGui/QPixmap>
|
||||||
#include <QtGui/qevent.h>
|
#include <QtGui/QPaintEvent>
|
||||||
#include <QtGui/QImage>
|
#include <QtGui/QImage>
|
||||||
#include <QtGui/QIcon>
|
#include <QtGui/QIcon>
|
||||||
|
|
||||||
|
@ -33,7 +33,8 @@ public:
|
||||||
Private()
|
Private()
|
||||||
: rating(0),
|
: rating(0),
|
||||||
hoverRating(-1),
|
hoverRating(-1),
|
||||||
pixSize( 16 ) {
|
pixSize(16)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int rating;
|
int rating;
|
||||||
|
@ -44,7 +45,6 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
KRatingWidget::KRatingWidget(QWidget *parent)
|
KRatingWidget::KRatingWidget(QWidget *parent)
|
||||||
: QFrame(parent),
|
: QFrame(parent),
|
||||||
d(new Private())
|
d(new Private())
|
||||||
|
@ -58,95 +58,71 @@ KRatingWidget::~KRatingWidget()
|
||||||
delete d;
|
delete d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::setCustomPixmap( const QPixmap& pix )
|
|
||||||
{
|
|
||||||
d->ratingPainter.setCustomPixmap( pix );
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::setIcon(const QIcon &icon)
|
void KRatingWidget::setIcon(const QIcon &icon)
|
||||||
{
|
{
|
||||||
d->ratingPainter.setIcon(icon);
|
d->ratingPainter.setIcon(icon);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::setPixmapSize(int size)
|
void KRatingWidget::setPixmapSize(int size)
|
||||||
{
|
{
|
||||||
d->pixSize = size;
|
d->pixSize = size;
|
||||||
updateGeometry();
|
updateGeometry();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int KRatingWidget::spacing() const
|
int KRatingWidget::spacing() const
|
||||||
{
|
{
|
||||||
return d->ratingPainter.spacing();
|
return d->ratingPainter.spacing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QIcon KRatingWidget::icon() const
|
QIcon KRatingWidget::icon() const
|
||||||
{
|
{
|
||||||
return d->ratingPainter.icon();
|
return d->ratingPainter.icon();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::setSpacing(int s)
|
void KRatingWidget::setSpacing(int s)
|
||||||
{
|
{
|
||||||
d->ratingPainter.setSpacing(s);
|
d->ratingPainter.setSpacing(s);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Qt::Alignment KRatingWidget::alignment() const
|
Qt::Alignment KRatingWidget::alignment() const
|
||||||
{
|
{
|
||||||
return d->ratingPainter.alignment();
|
return d->ratingPainter.alignment();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::setAlignment(Qt::Alignment align)
|
void KRatingWidget::setAlignment(Qt::Alignment align)
|
||||||
{
|
{
|
||||||
d->ratingPainter.setAlignment(align);
|
d->ratingPainter.setAlignment(align);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Qt::LayoutDirection KRatingWidget::layoutDirection() const
|
Qt::LayoutDirection KRatingWidget::layoutDirection() const
|
||||||
{
|
{
|
||||||
return d->ratingPainter.layoutDirection();
|
return d->ratingPainter.layoutDirection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::setLayoutDirection(Qt::LayoutDirection direction)
|
void KRatingWidget::setLayoutDirection(Qt::LayoutDirection direction)
|
||||||
{
|
{
|
||||||
d->ratingPainter.setLayoutDirection(direction);
|
d->ratingPainter.setLayoutDirection(direction);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned int KRatingWidget::rating() const
|
unsigned int KRatingWidget::rating() const
|
||||||
{
|
{
|
||||||
return d->rating;
|
return d->rating;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int KRatingWidget::maxRating() const
|
int KRatingWidget::maxRating() const
|
||||||
{
|
{
|
||||||
return d->ratingPainter.maxRating();
|
return d->ratingPainter.maxRating();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool KRatingWidget::halfStepsEnabled() const
|
bool KRatingWidget::halfStepsEnabled() const
|
||||||
{
|
{
|
||||||
return d->ratingPainter.halfStepsEnabled();
|
return d->ratingPainter.halfStepsEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::setRating(int rating)
|
void KRatingWidget::setRating(int rating)
|
||||||
{
|
{
|
||||||
if (rating != d->rating) {
|
if (rating != d->rating) {
|
||||||
|
@ -158,25 +134,18 @@ void KRatingWidget::setRating( int rating )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::setMaxRating(int max)
|
void KRatingWidget::setMaxRating(int max)
|
||||||
{
|
{
|
||||||
d->ratingPainter.setMaxRating(max);
|
d->ratingPainter.setMaxRating(max);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::setHalfStepsEnabled(bool enabled)
|
void KRatingWidget::setHalfStepsEnabled(bool enabled)
|
||||||
{
|
{
|
||||||
d->ratingPainter.setHalfStepsEnabled(enabled);
|
d->ratingPainter.setHalfStepsEnabled(enabled);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::mousePressEvent(QMouseEvent *e)
|
void KRatingWidget::mousePressEvent(QMouseEvent *e)
|
||||||
{
|
{
|
||||||
if (e->button() == Qt::LeftButton) {
|
if (e->button() == Qt::LeftButton) {
|
||||||
|
@ -185,29 +154,23 @@ void KRatingWidget::mousePressEvent( QMouseEvent* e )
|
||||||
if (!(d->hoverRating % 2)) {
|
if (!(d->hoverRating % 2)) {
|
||||||
if (d->hoverRating == prevRating + 1) {
|
if (d->hoverRating == prevRating + 1) {
|
||||||
setRating( d->hoverRating - 2 );
|
setRating( d->hoverRating - 2 );
|
||||||
}
|
} else if (d->hoverRating == prevRating) {
|
||||||
else if ( d->hoverRating == prevRating ) {
|
|
||||||
setRating( d->hoverRating - 1);
|
setRating( d->hoverRating - 1);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
setRating(d->hoverRating);
|
setRating(d->hoverRating);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if (d->hoverRating == prevRating - 1) {
|
if (d->hoverRating == prevRating - 1) {
|
||||||
setRating(d->hoverRating);
|
setRating(d->hoverRating);
|
||||||
}
|
} else if (d->hoverRating == prevRating) {
|
||||||
else if ( d->hoverRating == prevRating ) {
|
|
||||||
setRating(d->hoverRating - 1);
|
setRating(d->hoverRating - 1);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
setRating(d->hoverRating + 1);
|
setRating(d->hoverRating + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::mouseMoveEvent(QMouseEvent *e)
|
void KRatingWidget::mouseMoveEvent(QMouseEvent *e)
|
||||||
{
|
{
|
||||||
// when moving the mouse we show the user what the result of clicking will be
|
// when moving the mouse we show the user what the result of clicking will be
|
||||||
|
@ -216,16 +179,13 @@ void KRatingWidget::mouseMoveEvent( QMouseEvent* e )
|
||||||
if (!(d->hoverRating % 2)) {
|
if (!(d->hoverRating % 2)) {
|
||||||
if (d->hoverRating == prevHoverRating + 1) {
|
if (d->hoverRating == prevHoverRating + 1) {
|
||||||
d->hoverRating -= 2;
|
d->hoverRating -= 2;
|
||||||
}
|
} else if (d->hoverRating == prevHoverRating) {
|
||||||
else if ( d->hoverRating == prevHoverRating ) {
|
|
||||||
d->hoverRating -= 1;
|
d->hoverRating -= 1;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if (d->hoverRating == prevHoverRating) {
|
if (d->hoverRating == prevHoverRating) {
|
||||||
d->hoverRating -= 1;
|
d->hoverRating -= 1;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
d->hoverRating += 1;
|
d->hoverRating += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -234,14 +194,13 @@ void KRatingWidget::mouseMoveEvent( QMouseEvent* e )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void KRatingWidget::leaveEvent(QEvent *e)
|
||||||
void KRatingWidget::leaveEvent( QEvent* )
|
|
||||||
{
|
{
|
||||||
|
Q_UNUSED(e);
|
||||||
d->hoverRating = -1;
|
d->hoverRating = -1;
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void KRatingWidget::paintEvent(QPaintEvent *e)
|
void KRatingWidget::paintEvent(QPaintEvent *e)
|
||||||
{
|
{
|
||||||
QFrame::paintEvent(e);
|
QFrame::paintEvent(e);
|
||||||
|
@ -250,20 +209,18 @@ void KRatingWidget::paintEvent( QPaintEvent* e )
|
||||||
d->ratingPainter.paint(&p, contentsRect(), d->rating, d->hoverRating);
|
d->ratingPainter.paint(&p, contentsRect(), d->rating, d->hoverRating);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QSize KRatingWidget::sizeHint() const
|
QSize KRatingWidget::sizeHint() const
|
||||||
{
|
{
|
||||||
int numPix = d->ratingPainter.maxRating();
|
int numPix = d->ratingPainter.maxRating();
|
||||||
if( d->ratingPainter.halfStepsEnabled() )
|
if (d->ratingPainter.halfStepsEnabled()) {
|
||||||
numPix /= 2;
|
numPix /= 2;
|
||||||
|
|
||||||
QSize pixSize( d->pixSize, d->pixSize );
|
|
||||||
if ( !d->ratingPainter.customPixmap().isNull() ) {
|
|
||||||
pixSize = d->ratingPainter.customPixmap().size();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return QSize( pixSize.width()*numPix + spacing()*(numPix-1) + frameWidth()*2,
|
const QSize pixSize(d->pixSize, d->pixSize);
|
||||||
pixSize.height() + frameWidth()*2 );
|
return QSize(
|
||||||
|
pixSize.width() * numPix + spacing() * (numPix - 1) + frameWidth() * 2,
|
||||||
|
pixSize.height() + frameWidth() * 2
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "moc_kratingwidget.cpp"
|
#include "moc_kratingwidget.cpp"
|
||||||
|
|
|
@ -51,7 +51,7 @@ class KDEUI_EXPORT KRatingWidget : public QFrame
|
||||||
/**
|
/**
|
||||||
* Creates a new rating widget.
|
* Creates a new rating widget.
|
||||||
*/
|
*/
|
||||||
KRatingWidget( QWidget* parent = 0 );
|
KRatingWidget(QWidget *parent = nullptr);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destructor
|
* Destructor
|
||||||
|
@ -103,10 +103,9 @@ class KDEUI_EXPORT KRatingWidget : public QFrame
|
||||||
bool halfStepsEnabled() const;
|
bool halfStepsEnabled() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The icon used to draw a star. In case a custom pixmap has been set
|
* The icon used to get pixmap from and draw a star.
|
||||||
* this value is ignored.
|
|
||||||
*
|
*
|
||||||
* \sa setIcon, setCustomPixmap
|
* \sa setIcon
|
||||||
*/
|
*/
|
||||||
QIcon icon() const;
|
QIcon icon() const;
|
||||||
|
|
||||||
|
@ -158,11 +157,6 @@ class KDEUI_EXPORT KRatingWidget : public QFrame
|
||||||
*/
|
*/
|
||||||
void setIcon(const QIcon &icon);
|
void setIcon(const QIcon &icon);
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a custom pixmap.
|
|
||||||
*/
|
|
||||||
void setCustomPixmap( const QPixmap& pixmap );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the recommended size of the pixmaps. This is
|
* Set the recommended size of the pixmaps. This is
|
||||||
* only used for the sizeHint. The actual size is always
|
* only used for the sizeHint. The actual size is always
|
||||||
|
|
Loading…
Add table
Reference in a new issue