diff options
author | Michele Calgaro <[email protected]> | 2024-10-13 11:56:14 +0900 |
---|---|---|
committer | Michele Calgaro <[email protected]> | 2024-10-29 21:58:42 +0900 |
commit | 2879ff70be9271550477982a1a6371714db38562 (patch) | |
tree | c2054149dba923ab080fe7093432c7663a990111 /src/widgets/ratingwidget.cpp | |
parent | 3eb38d2556f676d1027746f20bf12a1dd74451ef (diff) | |
download | krecipes-2879ff70be9271550477982a1a6371714db38562.tar.gz krecipes-2879ff70be9271550477982a1a6371714db38562.zip |
Rearrange folders structure to remove unnecessary 'krecipes' second level subfolder
Signed-off-by: Michele Calgaro <[email protected]>
(cherry picked from commit 0c8ed6c9a4000af8f48581a81c4b5c2f5b9fd502)
Diffstat (limited to 'src/widgets/ratingwidget.cpp')
-rw-r--r-- | src/widgets/ratingwidget.cpp | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/src/widgets/ratingwidget.cpp b/src/widgets/ratingwidget.cpp new file mode 100644 index 0000000..10f35af --- /dev/null +++ b/src/widgets/ratingwidget.cpp @@ -0,0 +1,164 @@ +/*************************************************************************** + copyright : (C) 2003-2005 by Robby Stephenson + email : [email protected] + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of version 2 of the GNU General Public License as * + * published by the Free Software Foundation; * + * * + ***************************************************************************/ + +#include "ratingwidget.h" + +#include <tdeglobal.h> // needed for KMAX +#include <kiconloader.h> +#include <kdebug.h> + +#include <tqintdict.h> +#include <tqlayout.h> + +namespace { + static const int RATING_WIDGET_MAX_STAR_SIZE = 24; +} + +const TQPixmap& RatingWidget::pixmap(const TQString& value_) { + static TQIntDict<TQPixmap> pixmaps; + if(pixmaps.isEmpty()) { + pixmaps.insert(-1, new TQPixmap()); + } + bool ok; + int n = value_.toInt(&ok); + if(!ok || n < 1 || n > 10) { + return *pixmaps[-1]; + } + if(pixmaps[n]) { + return *pixmaps[n]; + } + + TQString picName = TQString::fromLatin1("stars%1").arg(n); + TQPixmap* pix = new TQPixmap(UserIcon(picName)); + pixmaps.insert(n, pix); + return *pix; +} + +RatingWidget::RatingWidget(int stars, TQWidget* parent_, const char* name_/*=0*/) + : TQHBox(parent_, name_), m_currIndex(-1), m_min(0), m_max(stars*2) { + m_pixOn = UserIcon(TQString::fromLatin1("star_on")); + m_pixOff = UserIcon(TQString::fromLatin1("star_off")); + m_pixHalf = UserIcon(TQString::fromLatin1("star_half")); + setSpacing(0); + + // find maximum width and height + int w = KMAX(RATING_WIDGET_MAX_STAR_SIZE, KMAX(m_pixOn.width(), m_pixOff.width())); + int h = KMAX(RATING_WIDGET_MAX_STAR_SIZE, KMAX(m_pixOn.height(), m_pixOff.height())); + for(int i = 0; i < stars; ++i) { + TQLabel* l = new TQLabel(this); + l->setFixedSize(w, h); + m_widgets.append(l); + } + init(); + + TQBoxLayout* l = dynamic_cast<TQBoxLayout*>(layout()); + if(l) { + l->addStretch(1); + } +} + +void RatingWidget::init() { + m_total = KMIN(m_max/2, static_cast<int>(m_widgets.count())); + uint i = 0; + for( ; static_cast<int>(i) < m_total; ++i) { + m_widgets.at(i)->setPixmap(m_pixOff); + } + for( ; i < m_widgets.count(); ++i) { + m_widgets.at(i)->setPixmap(TQPixmap()); + } + update(); +} + +void RatingWidget::update() { + int i = 0; + for( ; i <= (m_currIndex-1)/2; ++i) { + m_widgets.at(i)->setPixmap(m_pixOn); + } + for( ; i < m_total; ++i) { + m_widgets.at(i)->setPixmap(m_pixOff); + } + + if ( m_currIndex % 2 == 0 ) { + m_widgets.at(m_currIndex/2)->setPixmap(m_pixHalf); + } + + TQHBox::update(); +} + +void RatingWidget::mousePressEvent(TQMouseEvent* event_) { + // only react to left button + if(event_->button() != TQt::LeftButton) { + return; + } + + int idx; + TQWidget* child = childAt(event_->pos()); + bool left = false; + if(child) { + TQRect child_geom_left_half = child->geometry(); + child_geom_left_half.setWidth(child_geom_left_half.width()/2); + if ( child_geom_left_half.contains(event_->pos()) ) + left = true; + + idx = m_widgets.findRef(static_cast<TQLabel*>(child)); + // if the widget is clicked beyond the maximum value, clear it + // remember total and min are values, but index is zero-based! + if(idx > m_total-1) { + idx = -1; + } else if(idx < m_min-1) { + idx = m_min-1; // limit to minimum, remember index is zero-based + } + } else { + idx = -1; + } + + int oldCurrent = m_currIndex; + + m_currIndex = idx*2+1; + + if ( left ) + m_currIndex--; + + if ( oldCurrent != m_currIndex ) { + update(); + emit modified(); + } +} + +void RatingWidget::clear() { + m_currIndex = -1; + update(); +} + +TQString RatingWidget::text() const { + // index is index of the list, which is zero-based. Add 1! + return m_currIndex == -1 ? TQString::null : TQString::number(double(m_currIndex+1)/2); +} + +void RatingWidget::setText(const TQString& text_) { + bool ok; + // text is value, subtract one to get index + m_currIndex =text_.toInt(&ok)-1; + if(ok) { + if(m_currIndex > m_total-1) { + m_currIndex = -1; + } else if(m_currIndex < m_min-1) { + m_currIndex = m_min-1; // limit to minimum, remember index is zero-based + } + } else { + m_currIndex = -1; + } + update(); +} + +#include "ratingwidget.moc" |