SVN commit 692835 by cgilles:
digiKam from trunk (KDE4) : Rating Pop-up menu is now fully suitable using pure QT4 port. Marcel : still a little side effect about background color used to paint regular star pixamp on menu items. I have used Qt:transparent color or palette().color(QPalette::Active, QPalette::Background) without success. Sound like QMenu play with the background color somewhere... Screenshot : http://digikam3rdparty.free.fr/Screenshots/digikamKDE4_04.png CCMAIL: [hidden email] CCMAIL: [hidden email] M +115 -37 ratingpopupmenu.cpp M +3 -1 ratingpopupmenu.h --- trunk/extragear/graphics/digikam/digikam/ratingpopupmenu.cpp #692834:692835 @@ -29,6 +29,11 @@ #include <QPolygon> #include <QWidgetAction> #include <QLabel> +#include <QMouseEvent> +#include <QSize> +#include <QPaintEvent> +#include <QEvent> +#include <QObject> // KDE includes. @@ -45,50 +50,102 @@ namespace Digikam { -RatingPopupMenu::RatingPopupMenu(QWidget* parent) - : QMenu(parent) +class RatingMenuItem : public QWidget { - QPolygon starPolygon; - // Pre-computed star polygon for a 15x15 pixmap. - starPolygon << QPoint(0, 6); - starPolygon << QPoint(5, 5); - starPolygon << QPoint(7, 0); - starPolygon << QPoint(9, 5); - starPolygon << QPoint(14, 6); - starPolygon << QPoint(10, 9); - starPolygon << QPoint(11, 14); - starPolygon << QPoint(7, 11); - starPolygon << QPoint(3, 14); - starPolygon << QPoint(4, 9); + public: - QPixmap starPix(15, 15); - starPix.fill(Qt::transparent); - QPainter p1(&starPix); - p1.setRenderHint(QPainter::Antialiasing, true); - p1.setBrush(ThemeEngine::componentData()->textSpecialRegColor()); - p1.setPen(palette().color(QPalette::Active, QPalette::Foreground)); - p1.drawPolygon(starPolygon, Qt::WindingFill); - p1.end(); - - for (int i = 0 ; i <= RatingMax ; i++) + RatingMenuItem(int star, QWidget *parent=0) : QWidget(parent) { - QPixmap pix(starPix.width() * i, starPix.height()); - pix.fill(Qt::transparent); + m_hightlighted = false; + m_star = star; - QPainter p2(&pix); - p2.drawTiledPixmap(0, 0, i*starPix.width(), pix.height(), starPix); + QPolygon starPolygon; + + // Pre-computed star polygon for a 15x15 pixmap. + starPolygon << QPoint(0, 6); + starPolygon << QPoint(5, 5); + starPolygon << QPoint(7, 0); + starPolygon << QPoint(9, 5); + starPolygon << QPoint(14, 6); + starPolygon << QPoint(10, 9); + starPolygon << QPoint(11, 14); + starPolygon << QPoint(7, 11); + starPolygon << QPoint(3, 14); + starPolygon << QPoint(4, 9); + + // Template of one star. + QPixmap starPix(15, 15); + starPix.fill(Qt::transparent); + QPainter p1(&starPix); + p1.setRenderHint(QPainter::Antialiasing, true); + p1.setBrush(ThemeEngine::componentData()->textSpecialRegColor()); + p1.setPen(palette().color(QPalette::Active, QPalette::Foreground)); + p1.drawPolygon(starPolygon, Qt::WindingFill); + p1.end(); + + // Set widget properties. + setMouseTracking(true); + setAttribute(Qt::WA_DeleteOnClose); + setFixedSize(QSize(4 + starPix.width()*RatingMax, starPix.height()+4)); + + // Regular rating pixmap of m_star. + m_ratingPixReg = QPixmap(contentsRect().size()); + m_ratingPixReg.fill(Qt::transparent); + QPainter p2(&m_ratingPixReg); + p2.drawTiledPixmap(2, 2, m_star*starPix.width(), starPix.height(), starPix); p2.end(); - QWidgetAction *action = new QWidgetAction(this); - QLabel *label = new QLabel(); - if (i < 1) label->setText(i18n("None")); - else label->setPixmap(pix); - action->setDefaultWidget(label); - action->setData(i); - connect(action, SIGNAL(triggered()), this, SLOT(slotRatingTriggered())); - addAction(action); + // Selected rating pixmap of m_star. + m_ratingPixSel = QPixmap(contentsRect().size()); + m_ratingPixSel.fill(palette().color(QPalette::Active, QPalette::Highlight)); + QPainter p3(&m_ratingPixSel); + p3.drawTiledPixmap(2, 2, m_star*starPix.width(), starPix.height(), starPix); + p3.end(); } + + void setHightLighted(bool h) + { + m_hightlighted = h; + update(); + } + + private: + + void paintEvent(QPaintEvent *) + { + QPainter p(this); + p.drawPixmap(contentsRect(), m_hightlighted ? m_ratingPixSel : m_ratingPixReg); + p.end(); + } + + private: + + bool m_hightlighted; + int m_star; + + QPixmap m_ratingPixReg; + QPixmap m_ratingPixSel; +}; + +RatingPopupMenu::RatingPopupMenu(QWidget* parent) + : QMenu(parent) +{ + QAction *action = addAction(i18n("None"), this, SLOT(slotRatingTriggered())); + action->setData(0); + + for (int i = 1 ; i <= RatingMax ; i++) + { + QWidgetAction *action2 = new QWidgetAction(this); + RatingMenuItem *item = new RatingMenuItem(i); + action2->setDefaultWidget(item); + action2->setData(i); + connect(action2, SIGNAL(triggered()), this, SLOT(slotRatingTriggered())); + addAction(action2); + } + + connect(this, SIGNAL(hovered(QAction *)), + this, SLOT(slotHovered(QAction *))); } RatingPopupMenu::~RatingPopupMenu() @@ -97,8 +154,29 @@ void RatingPopupMenu::slotRatingTriggered() { - int r = qobject_cast<QWidgetAction*>(sender())->data().toInt(); + int r = qobject_cast<QAction*>(sender())->data().toInt(); emit signalRatingChanged(r); } +void RatingPopupMenu::slotHovered(QAction *current) +{ + QList<QAction*> list = actions(); + foreach(QAction *action, list) + { + if (action->data().toInt() > 0) + { + QWidget *w = qobject_cast<QWidgetAction*>(action)->defaultWidget(); + RatingMenuItem *mi = dynamic_cast<RatingMenuItem*>(w); + mi->setHightLighted(false); + } + } + + if (current->data().toInt() > 0) + { + QWidget *w = qobject_cast<QWidgetAction*>(current)->defaultWidget(); + RatingMenuItem *mi = dynamic_cast<RatingMenuItem*>(w); + mi->setHightLighted(true); + } +} + } // namespace Digikam --- trunk/extragear/graphics/digikam/digikam/ratingpopupmenu.h #692834:692835 @@ -28,10 +28,11 @@ #include <QMenu> +class QAction; + namespace Digikam { - class RatingPopupMenu : public QMenu { Q_OBJECT @@ -48,6 +49,7 @@ private slots: void slotRatingTriggered(); + void slotHovered(QAction *); }; } // namespace Digikam _______________________________________________ Digikam-devel mailing list [hidden email] https://mail.kde.org/mailman/listinfo/digikam-devel |
Free forum by Nabble | Edit this page |