1/*****************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This library is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Library General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2 of the License, or (at your option) any later version. *
8 * *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Library General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Library General Public License *
15 * along with this library; see the file COPYING.LIB. If not, write to *
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301, USA. *
18 *****************************************************************************/
19
20#include "kurlnavigatortogglebutton_p.h"
21
22#include <kcolorscheme.h>
23#include <kicon.h>
24#include <klocale.h>
25
26#include <QtGui/QPainter>
27#include <QtGui/QKeyEvent>
28#include <QtGui/QApplication>
29
30namespace KDEPrivate
31{
32
33KUrlNavigatorToggleButton::KUrlNavigatorToggleButton(QWidget* parent) :
34 KUrlNavigatorButtonBase(parent)
35{
36 setCheckable(true);
37 connect(this, SIGNAL(toggled(bool)),
38 this, SLOT(updateToolTip()));
39 connect(this, SIGNAL(clicked(bool)),
40 this, SLOT(updateCursor()));
41 m_pixmap = KIcon("dialog-ok").pixmap(QSize(22, 22).expandedTo(iconSize()));
42 updateToolTip();
43}
44
45KUrlNavigatorToggleButton::~KUrlNavigatorToggleButton()
46{
47}
48
49QSize KUrlNavigatorToggleButton::sizeHint() const
50{
51 QSize size = KUrlNavigatorButtonBase::sizeHint();
52 size.setWidth(m_pixmap.width() + 4);
53 return size;
54}
55
56void KUrlNavigatorToggleButton::enterEvent(QEvent* event)
57{
58 KUrlNavigatorButtonBase::enterEvent(event);
59 updateCursor();
60}
61
62void KUrlNavigatorToggleButton::leaveEvent(QEvent* event)
63{
64 KUrlNavigatorButtonBase::leaveEvent(event);
65 setCursor(Qt::ArrowCursor);
66}
67
68void KUrlNavigatorToggleButton::paintEvent(QPaintEvent* event)
69{
70 QPainter painter(this);
71 painter.setClipRect(event->rect());
72
73 const int buttonWidth = width();
74 const int buttonHeight = height();
75 if (isChecked()) {
76 drawHoverBackground(&painter);
77 const int x = (buttonWidth - m_pixmap.width()) / 2;
78 const int y = (buttonHeight - m_pixmap.height()) / 2;
79 painter.drawPixmap(QRect(x, y, m_pixmap.width(), m_pixmap.height()), m_pixmap);
80 } else if (isDisplayHintEnabled(EnteredHint)) {
81 painter.setPen(Qt::NoPen);
82 painter.setBrush(palette().color(foregroundRole()));
83
84 const int verticalGap = 4;
85 const int caretWidth = 2;
86 const int x = (layoutDirection() == Qt::LeftToRight) ? 0 : width() - caretWidth;
87 painter.drawRect(x, verticalGap, caretWidth, buttonHeight - 2 * verticalGap);
88 }
89}
90
91void KUrlNavigatorToggleButton::updateToolTip()
92{
93 if (isChecked()) {
94 setToolTip(i18n("Click for Location Navigation"));
95 } else {
96 setToolTip(i18n("Click to Edit Location"));
97 }
98}
99
100void KUrlNavigatorToggleButton::updateCursor()
101{
102 setCursor(isChecked() ? Qt::ArrowCursor : Qt::IBeamCursor);
103}
104
105} // namespace KDEPrivate
106
107#include "kurlnavigatortogglebutton_p.moc"
108