fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kfile / kurlbutton.cpp
blob68f0bd8ceaf845a86cd081849b7c54fa33a6bd8c
1 /*****************************************************************************
2 * Copyright (C) 2006 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 version 2 as published by the Free Software Foundation. *
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 *****************************************************************************/
20 #include "kurlbutton_p.h"
22 #include "kurlnavigator.h"
24 #include <kcolorscheme.h>
25 #include <kicon.h>
26 #include <klocale.h>
27 #include <kmenu.h>
29 #include <QApplication>
30 #include <QClipboard>
31 #include <QMimeData>
32 #include <QStyle>
33 #include <QStyleOptionFocusRect>
35 KUrlButton::KUrlButton(KUrlNavigator* parent) :
36 QPushButton(parent),
37 m_displayHint(0),
38 m_urlNavigator(parent)
40 setFocusPolicy(Qt::NoFocus);
41 setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
42 setMinimumHeight(parent->minimumHeight());
44 connect(this, SIGNAL(pressed()), parent, SLOT(requestActivation()));
47 KUrlButton::~KUrlButton()
51 void KUrlButton::setDisplayHintEnabled(DisplayHint hint,
52 bool enable)
54 if (enable) {
55 m_displayHint = m_displayHint | hint;
56 } else {
57 m_displayHint = m_displayHint & ~hint;
59 update();
62 bool KUrlButton::isDisplayHintEnabled(DisplayHint hint) const
64 return (m_displayHint & hint) > 0;
67 void KUrlButton::enterEvent(QEvent* event)
69 QPushButton::enterEvent(event);
70 setDisplayHintEnabled(EnteredHint, true);
71 update();
74 void KUrlButton::leaveEvent(QEvent* event)
76 QPushButton::leaveEvent(event);
77 setDisplayHintEnabled(EnteredHint, false);
78 update();
81 void KUrlButton::contextMenuEvent(QContextMenuEvent* event)
83 Q_UNUSED(event);
84 m_urlNavigator->requestActivation();
86 KMenu popup(this);
88 // provide 'Copy' action, which copies the current URL of
89 // the URL navigator into the clipboard
90 QAction* copyAction = popup.addAction(KIcon("edit-copy"), i18n("Copy"));
92 // provide 'Paste' action, which copies the current clipboard text
93 // into the URL navigator
94 QAction* pasteAction = popup.addAction(KIcon("edit-paste"), i18n("Paste"));
95 QClipboard* clipboard = QApplication::clipboard();
96 pasteAction->setEnabled(!clipboard->text().isEmpty());
98 popup.addSeparator();
100 // provide radiobuttons for toggling between the edit and the navigation mode
101 QAction* editAction = popup.addAction(i18n("Edit"));
102 editAction->setCheckable(true);
104 QAction* navigateAction = popup.addAction(i18n("Navigate"));
105 navigateAction->setCheckable(true);
107 QActionGroup* modeGroup = new QActionGroup(&popup);
108 modeGroup->addAction(editAction);
109 modeGroup->addAction(navigateAction);
110 if (m_urlNavigator->isUrlEditable()) {
111 editAction->setChecked(true);
112 } else {
113 navigateAction->setChecked(true);
116 popup.addSeparator();
118 // allow showing of the full path
119 QAction* showFullPathAction = popup.addAction(i18n("Show Full Path"));
120 showFullPathAction->setCheckable(true);
121 showFullPathAction->setChecked(m_urlNavigator->showFullPath());
123 QAction* activatedAction = popup.exec(QCursor::pos());
124 if (activatedAction == copyAction) {
125 QMimeData* mimeData = new QMimeData();
126 mimeData->setText(m_urlNavigator->url().prettyUrl());
127 clipboard->setMimeData(mimeData);
128 } else if (activatedAction == pasteAction) {
129 m_urlNavigator->setUrl(KUrl(clipboard->text()));
130 } else if (activatedAction == editAction) {
131 m_urlNavigator->setUrlEditable(true);
132 } else if (activatedAction == navigateAction) {
133 m_urlNavigator->setUrlEditable(false);
134 } else if (activatedAction == showFullPathAction) {
135 m_urlNavigator->setShowFullPath(showFullPathAction->isChecked());
139 void KUrlButton::drawHoverBackground(QPainter* painter)
141 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
142 isDisplayHintEnabled(DraggedHint) ||
143 isDisplayHintEnabled(PopupActiveHint);
145 QColor backgroundColor = isHighlighted ? palette().color(QPalette::Highlight) : Qt::transparent;
146 if (!urlNavigator()->isActive() && isHighlighted) {
147 backgroundColor.setAlpha(128);
150 if (backgroundColor != Qt::transparent) {
151 // TODO: the backgroundColor should be applied to the style
152 QStyleOptionViewItemV4 option;
153 option.initFrom(this);
154 option.state = QStyle::State_Enabled | QStyle::State_MouseOver;
155 option.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
156 style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, this);
160 QColor KUrlButton::foregroundColor() const
162 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
163 isDisplayHintEnabled(DraggedHint) ||
164 isDisplayHintEnabled(PopupActiveHint);
166 QColor foregroundColor = palette().color(foregroundRole());
167 const bool isActive = m_urlNavigator->isActive();
169 int alpha = isActive ? 255 : 128;
170 if ((!isDisplayHintEnabled(ActivatedHint) || !isActive) && !isHighlighted) {
171 alpha -= alpha / 4;
173 foregroundColor.setAlpha(alpha);
175 return foregroundColor;
178 #include "kurlbutton_p.moc"