1/*****************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Aaron J. Seigo <aseigo@kde.org> *
4 * *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Library General Public *
7 * License as published by the Free Software Foundation; either *
8 * version 2 of the License, or (at your option) any later version. *
9 * *
10 * This library is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * Library General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Library General Public License *
16 * along with this library; see the file COPYING.LIB. If not, write to *
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18 * Boston, MA 02110-1301, USA. *
19 *****************************************************************************/
20
21#include "kurlnavigatorbuttonbase_p.h"
22
23#include <kcolorscheme.h>
24#include <kicon.h>
25#include <klocale.h>
26#include <kmenu.h>
27#include <kurl.h>
28
29#include <QApplication>
30#include <QClipboard>
31#include <QMimeData>
32#include <QStyle>
33#include <QStyleOptionFocusRect>
34
35namespace KDEPrivate
36{
37
38KUrlNavigatorButtonBase::KUrlNavigatorButtonBase(QWidget* parent) :
39 QPushButton(parent),
40 m_active(true),
41 m_displayHint(0)
42{
43 setFocusPolicy(Qt::TabFocus);
44 setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
45 setMinimumHeight(parent->minimumHeight());
46
47 connect(this, SIGNAL(pressed()), parent, SLOT(requestActivation()));
48}
49
50KUrlNavigatorButtonBase::~KUrlNavigatorButtonBase()
51{
52}
53
54void KUrlNavigatorButtonBase::setActive(bool active)
55{
56 if (m_active != active) {
57 m_active = active;
58 update();
59 }
60}
61
62bool KUrlNavigatorButtonBase::isActive() const
63{
64 return m_active;
65}
66
67void KUrlNavigatorButtonBase::setDisplayHintEnabled(DisplayHint hint,
68 bool enable)
69{
70 if (enable) {
71 m_displayHint = m_displayHint | hint;
72 } else {
73 m_displayHint = m_displayHint & ~hint;
74 }
75 update();
76}
77
78bool KUrlNavigatorButtonBase::isDisplayHintEnabled(DisplayHint hint) const
79{
80 return (m_displayHint & hint) > 0;
81}
82
83void KUrlNavigatorButtonBase::focusInEvent(QFocusEvent *event)
84{
85 setDisplayHintEnabled(EnteredHint, true);
86 QPushButton::focusInEvent(event);
87}
88
89void KUrlNavigatorButtonBase::focusOutEvent(QFocusEvent *event)
90{
91 setDisplayHintEnabled(EnteredHint, false);
92 QPushButton::focusOutEvent(event);
93}
94
95void KUrlNavigatorButtonBase::enterEvent(QEvent* event)
96{
97 QPushButton::enterEvent(event);
98 setDisplayHintEnabled(EnteredHint, true);
99 update();
100}
101
102void KUrlNavigatorButtonBase::leaveEvent(QEvent* event)
103{
104 QPushButton::leaveEvent(event);
105 setDisplayHintEnabled(EnteredHint, false);
106 update();
107}
108
109void KUrlNavigatorButtonBase::drawHoverBackground(QPainter* painter)
110{
111 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
112 isDisplayHintEnabled(DraggedHint) ||
113 isDisplayHintEnabled(PopupActiveHint);
114
115 QColor backgroundColor = isHighlighted ? palette().color(QPalette::Highlight) : Qt::transparent;
116 if (!m_active && isHighlighted) {
117 backgroundColor.setAlpha(128);
118 }
119
120 if (backgroundColor != Qt::transparent) {
121 // TODO: the backgroundColor should be applied to the style
122 QStyleOptionViewItemV4 option;
123 option.initFrom(this);
124 option.state = QStyle::State_Enabled | QStyle::State_MouseOver;
125 option.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
126 style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, this);
127 }
128}
129
130QColor KUrlNavigatorButtonBase::foregroundColor() const
131{
132 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
133 isDisplayHintEnabled(DraggedHint) ||
134 isDisplayHintEnabled(PopupActiveHint);
135
136 QColor foregroundColor = palette().color(foregroundRole());
137
138 int alpha = m_active ? 255 : 128;
139 if (!m_active && !isHighlighted) {
140 alpha -= alpha / 4;
141 }
142 foregroundColor.setAlpha(alpha);
143
144 return foregroundColor;
145}
146
147void KUrlNavigatorButtonBase::activate()
148{
149 setActive(true);
150}
151
152} // namespace KDEPrivate
153
154#include "kurlnavigatorbuttonbase_p.moc"
155