From 4ff8dbf5e9628c4ccc24ab1215ae30d2e68ed83d Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Fri, 24 Nov 2006 18:34:52 +0000 Subject: [PATCH] Get rid of using deprecated Q3Button::drawButton() interface. Now the URL navigator looks nearly as nice as within the Qt3version again :-) svn path=/trunk/playground/utils/dolphin/; revision=607457 --- src/bookmarkselector.cpp | 30 ++++++++++++++++++------------ src/bookmarkselector.h | 5 ++++- src/urlnavigatorbutton.cpp | 30 ++++++++++++++++-------------- src/urlnavigatorbutton.h | 13 ++++++++----- 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/bookmarkselector.cpp b/src/bookmarkselector.cpp index 8cd2917dc..73e063fda 100644 --- a/src/bookmarkselector.cpp +++ b/src/bookmarkselector.cpp @@ -106,15 +106,23 @@ KBookmark BookmarkSelector::selectedBookmark() const return DolphinSettings::instance().bookmark(m_selectedIndex); } -void BookmarkSelector::drawButton(QPainter* painter) +QSize BookmarkSelector::sizeHint() const { + const int height = UrlButton::sizeHint().height(); + return QSize(height, height); +} + +void BookmarkSelector::paintEvent(QPaintEvent* event) +{ + QPainter painter(this); + const int buttonWidth = width(); const int buttonHeight = height(); QColor backgroundColor; QColor foregroundColor; const bool isHighlighted = isDisplayHintEnabled(EnteredHint) || - isDisplayHintEnabled(DraggedHint); + isDisplayHintEnabled(DraggedHint); if (isHighlighted) { backgroundColor = KGlobalSettings::highlightColor(); foregroundColor = KGlobalSettings::highlightedTextColor(); @@ -140,21 +148,19 @@ void BookmarkSelector::drawButton(QPainter* painter) if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) { // dimm the foreground color by mixing it with the background foregroundColor = mixColors(foregroundColor, backgroundColor); - painter->setPen(foregroundColor); + painter.setPen(foregroundColor); } // draw button backround - painter->setPen(Qt::NoPen); - painter->setBrush(backgroundColor); - painter->drawRect(0, 0, buttonWidth, buttonHeight); + painter.setPen(Qt::NoPen); + painter.setBrush(backgroundColor); + painter.drawRect(0, 0, buttonWidth, buttonHeight); // draw icon - const QPixmap* icon = pixmap(); - if (icon != 0) { - const int x = (buttonWidth - icon->width()) / 2; - const int y = (buttonHeight - icon->height()) / 2; - painter->drawPixmap(x, y, *icon); - } + const QPixmap pixmap = icon().pixmap(); + const int x = (buttonWidth - pixmap.width()) / 2; + const int y = (buttonHeight - pixmap.height()) / 2; + painter.drawPixmap(x, y, pixmap); } void BookmarkSelector::slotBookmarkActivated(int index) diff --git a/src/bookmarkselector.h b/src/bookmarkselector.h index 3a967b5dc..43c661815 100644 --- a/src/bookmarkselector.h +++ b/src/bookmarkselector.h @@ -70,6 +70,9 @@ public: /** Returns the selected bookmark. */ KBookmark selectedBookmark() const; + /** @see QWidget::sizeHint() */ + virtual QSize sizeHint() const; + signals: /** * Is send when a bookmark has been activated by the user. @@ -82,7 +85,7 @@ protected: * Draws the icon of the selected Url as content of the Url * selector. */ - virtual void drawButton(QPainter* painter); + virtual void paintEvent(QPaintEvent* event); private slots: /** diff --git a/src/urlnavigatorbutton.cpp b/src/urlnavigatorbutton.cpp index 811a11b89..fab2b81e9 100644 --- a/src/urlnavigatorbutton.cpp +++ b/src/urlnavigatorbutton.cpp @@ -89,13 +89,15 @@ void UrlNavigatorButton::setIndex(int index) update(); } -int UrlNavigatorButton::index() const +QSize UrlNavigatorButton::sizeHint() const { - return m_index; + const int width = fontMetrics().width(text()) + (arrowWidth() * 4); + return QSize(width, UrlButton::sizeHint().height()); } -void UrlNavigatorButton::drawButton(QPainter* painter) +void UrlNavigatorButton::paintEvent(QPaintEvent* event) { + QPainter painter(this); const int buttonWidth = width(); const int buttonHeight = height(); @@ -127,18 +129,18 @@ void UrlNavigatorButton::drawButton(QPainter* painter) } // draw button background - painter->setPen(Qt::NoPen); - painter->setBrush(backgroundColor); - painter->drawRect(0, 0, buttonWidth, buttonHeight); + painter.setPen(Qt::NoPen); + painter.setBrush(backgroundColor); + painter.drawRect(0, 0, buttonWidth, buttonHeight); int textWidth = buttonWidth; if (isDisplayHintEnabled(ActivatedHint) && isActive || isHighlighted) { - painter->setPen(foregroundColor); + painter.setPen(foregroundColor); } else { // dimm the foreground color by mixing it with the background foregroundColor = mixColors(foregroundColor, backgroundColor); - painter->setPen(foregroundColor); + painter.setPen(foregroundColor); } if (!isDisplayHintEnabled(ActivatedHint)) { @@ -150,8 +152,8 @@ void UrlNavigatorButton::drawButton(QPainter* painter) const int startTopY = middleY - (width - 1); const int startBottomY = middleY + (width - 1); for (int i = 0; i < width; ++i) { - painter->drawLine(startX, startTopY + i, startX + i, startTopY + i); - painter->drawLine(startX, startBottomY - i, startX + i, startBottomY - i); + painter.drawLine(startX, startTopY + i, startX + i, startTopY + i); + painter.drawLine(startX, startBottomY - i, startX + i, startBottomY - i); } textWidth = startX - border; @@ -159,7 +161,7 @@ void UrlNavigatorButton::drawButton(QPainter* painter) const bool clipped = isTextClipped(); const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter; - painter->drawText(QRect(0, 0, textWidth, buttonHeight), align, text()); + painter.drawText(QRect(0, 0, textWidth, buttonHeight), align, text()); if (clipped) { // Blend the right area of the text with the background, as the @@ -172,9 +174,9 @@ void UrlNavigatorButton::drawButton(QPainter* painter) const int greenInc = (foregroundColor.green() - backgroundColor.green()) / blendSteps; const int blueInc = (foregroundColor.blue() - backgroundColor.blue()) / blendSteps; for (int i = 0; i < blendSteps; ++i) { - painter->setClipRect(QRect(textWidth - i, 0, 1, buttonHeight)); - painter->setPen(blendColor); - painter->drawText(QRect(0, 0, textWidth, buttonHeight), align, text()); + painter.setClipRect(QRect(textWidth - i, 0, 1, buttonHeight)); + painter.setPen(blendColor); + painter.drawText(QRect(0, 0, textWidth, buttonHeight), align, text()); blendColor.setRgb(blendColor.red() + redInc, blendColor.green() + greenInc, diff --git a/src/urlnavigatorbutton.h b/src/urlnavigatorbutton.h index 44671fe2d..5ce54b31d 100644 --- a/src/urlnavigatorbutton.h +++ b/src/urlnavigatorbutton.h @@ -41,11 +41,11 @@ namespace KIO } /** - * @brief Button of the Url navigator which contains one part of an Url. + * @brief Button of the URL navigator which contains one part of an URL. * - * It is possible to drop a various number of items to an Url button. In this case + * It is possible to drop a various number of items to an UrlNavigatorButton. In this case * a context menu is opened where the user must select whether he wants - * to copy, move or link the dropped items to the Url part indicated by + * to copy, move or link the dropped items to the URL part indicated by * the button. */ class UrlNavigatorButton : public UrlButton @@ -56,10 +56,13 @@ public: UrlNavigatorButton(int index, UrlNavigator* parent = 0); virtual ~UrlNavigatorButton(); void setIndex(int index); - int index() const; + int index() const { return m_index; } + + /** @see QWidget::sizeHint() */ + virtual QSize sizeHint() const; protected: - virtual void drawButton(QPainter* painter); + virtual void paintEvent(QPaintEvent* event); virtual void enterEvent(QEvent* event); virtual void leaveEvent(QEvent* event); virtual void dropEvent(QDropEvent* event); -- 2.47.3