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
This commit is contained in:
Peter Penz 2006-11-24 18:34:52 +00:00
parent 47fa21aff1
commit 4ff8dbf5e9
4 changed files with 46 additions and 32 deletions

View file

@ -106,15 +106,23 @@ KBookmark BookmarkSelector::selectedBookmark() const
return DolphinSettings::instance().bookmark(m_selectedIndex); 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 buttonWidth = width();
const int buttonHeight = height(); const int buttonHeight = height();
QColor backgroundColor; QColor backgroundColor;
QColor foregroundColor; QColor foregroundColor;
const bool isHighlighted = isDisplayHintEnabled(EnteredHint) || const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
isDisplayHintEnabled(DraggedHint); isDisplayHintEnabled(DraggedHint);
if (isHighlighted) { if (isHighlighted) {
backgroundColor = KGlobalSettings::highlightColor(); backgroundColor = KGlobalSettings::highlightColor();
foregroundColor = KGlobalSettings::highlightedTextColor(); foregroundColor = KGlobalSettings::highlightedTextColor();
@ -140,21 +148,19 @@ void BookmarkSelector::drawButton(QPainter* painter)
if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) { if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) {
// dimm the foreground color by mixing it with the background // dimm the foreground color by mixing it with the background
foregroundColor = mixColors(foregroundColor, backgroundColor); foregroundColor = mixColors(foregroundColor, backgroundColor);
painter->setPen(foregroundColor); painter.setPen(foregroundColor);
} }
// draw button backround // draw button backround
painter->setPen(Qt::NoPen); painter.setPen(Qt::NoPen);
painter->setBrush(backgroundColor); painter.setBrush(backgroundColor);
painter->drawRect(0, 0, buttonWidth, buttonHeight); painter.drawRect(0, 0, buttonWidth, buttonHeight);
// draw icon // draw icon
const QPixmap* icon = pixmap(); const QPixmap pixmap = icon().pixmap();
if (icon != 0) { const int x = (buttonWidth - pixmap.width()) / 2;
const int x = (buttonWidth - icon->width()) / 2; const int y = (buttonHeight - pixmap.height()) / 2;
const int y = (buttonHeight - icon->height()) / 2; painter.drawPixmap(x, y, pixmap);
painter->drawPixmap(x, y, *icon);
}
} }
void BookmarkSelector::slotBookmarkActivated(int index) void BookmarkSelector::slotBookmarkActivated(int index)

View file

@ -70,6 +70,9 @@ public:
/** Returns the selected bookmark. */ /** Returns the selected bookmark. */
KBookmark selectedBookmark() const; KBookmark selectedBookmark() const;
/** @see QWidget::sizeHint() */
virtual QSize sizeHint() const;
signals: signals:
/** /**
* Is send when a bookmark has been activated by the user. * 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 * Draws the icon of the selected Url as content of the Url
* selector. * selector.
*/ */
virtual void drawButton(QPainter* painter); virtual void paintEvent(QPaintEvent* event);
private slots: private slots:
/** /**

View file

@ -89,13 +89,15 @@ void UrlNavigatorButton::setIndex(int index)
update(); 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 buttonWidth = width();
const int buttonHeight = height(); const int buttonHeight = height();
@ -127,18 +129,18 @@ void UrlNavigatorButton::drawButton(QPainter* painter)
} }
// draw button background // draw button background
painter->setPen(Qt::NoPen); painter.setPen(Qt::NoPen);
painter->setBrush(backgroundColor); painter.setBrush(backgroundColor);
painter->drawRect(0, 0, buttonWidth, buttonHeight); painter.drawRect(0, 0, buttonWidth, buttonHeight);
int textWidth = buttonWidth; int textWidth = buttonWidth;
if (isDisplayHintEnabled(ActivatedHint) && isActive || isHighlighted) { if (isDisplayHintEnabled(ActivatedHint) && isActive || isHighlighted) {
painter->setPen(foregroundColor); painter.setPen(foregroundColor);
} }
else { else {
// dimm the foreground color by mixing it with the background // dimm the foreground color by mixing it with the background
foregroundColor = mixColors(foregroundColor, backgroundColor); foregroundColor = mixColors(foregroundColor, backgroundColor);
painter->setPen(foregroundColor); painter.setPen(foregroundColor);
} }
if (!isDisplayHintEnabled(ActivatedHint)) { if (!isDisplayHintEnabled(ActivatedHint)) {
@ -150,8 +152,8 @@ void UrlNavigatorButton::drawButton(QPainter* painter)
const int startTopY = middleY - (width - 1); const int startTopY = middleY - (width - 1);
const int startBottomY = middleY + (width - 1); const int startBottomY = middleY + (width - 1);
for (int i = 0; i < width; ++i) { for (int i = 0; i < width; ++i) {
painter->drawLine(startX, startTopY + i, startX + i, startTopY + i); painter.drawLine(startX, startTopY + i, startX + i, startTopY + i);
painter->drawLine(startX, startBottomY - i, startX + i, startBottomY - i); painter.drawLine(startX, startBottomY - i, startX + i, startBottomY - i);
} }
textWidth = startX - border; textWidth = startX - border;
@ -159,7 +161,7 @@ void UrlNavigatorButton::drawButton(QPainter* painter)
const bool clipped = isTextClipped(); const bool clipped = isTextClipped();
const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter; 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) { if (clipped) {
// Blend the right area of the text with the background, as the // 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 greenInc = (foregroundColor.green() - backgroundColor.green()) / blendSteps;
const int blueInc = (foregroundColor.blue() - backgroundColor.blue()) / blendSteps; const int blueInc = (foregroundColor.blue() - backgroundColor.blue()) / blendSteps;
for (int i = 0; i < blendSteps; ++i) { for (int i = 0; i < blendSteps; ++i) {
painter->setClipRect(QRect(textWidth - i, 0, 1, buttonHeight)); painter.setClipRect(QRect(textWidth - i, 0, 1, buttonHeight));
painter->setPen(blendColor); painter.setPen(blendColor);
painter->drawText(QRect(0, 0, textWidth, buttonHeight), align, text()); painter.drawText(QRect(0, 0, textWidth, buttonHeight), align, text());
blendColor.setRgb(blendColor.red() + redInc, blendColor.setRgb(blendColor.red() + redInc,
blendColor.green() + greenInc, blendColor.green() + greenInc,

View file

@ -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 * 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. * the button.
*/ */
class UrlNavigatorButton : public UrlButton class UrlNavigatorButton : public UrlButton
@ -56,10 +56,13 @@ public:
UrlNavigatorButton(int index, UrlNavigator* parent = 0); UrlNavigatorButton(int index, UrlNavigator* parent = 0);
virtual ~UrlNavigatorButton(); virtual ~UrlNavigatorButton();
void setIndex(int index); void setIndex(int index);
int index() const; int index() const { return m_index; }
/** @see QWidget::sizeHint() */
virtual QSize sizeHint() const;
protected: protected:
virtual void drawButton(QPainter* painter); virtual void paintEvent(QPaintEvent* event);
virtual void enterEvent(QEvent* event); virtual void enterEvent(QEvent* event);
virtual void leaveEvent(QEvent* event); virtual void leaveEvent(QEvent* event);
virtual void dropEvent(QDropEvent* event); virtual void dropEvent(QDropEvent* event);