• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KFile

kurlbutton.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at>                      *
00003  * Copyright (C) 2006 by Aaron J. Seigo <aseigo@kde.org>                     *
00004  *                                                                           *
00005  * This library is free software; you can redistribute it and/or             *
00006  * modify it under the terms of the GNU Library General Public               *
00007  * License version 2 as published by the Free Software Foundation.           *
00008  *                                                                           *
00009  * This library is distributed in the hope that it will be useful,           *
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00012  * Library General Public License for more details.                          *
00013  *                                                                           *
00014  * You should have received a copy of the GNU Library General Public License *
00015  * along with this library; see the file COPYING.LIB.  If not, write to      *
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
00017  * Boston, MA 02110-1301, USA.                                               *
00018  *****************************************************************************/
00019 
00020 #include "kurlbutton_p.h"
00021 
00022 #include "kurlnavigator.h"
00023 
00024 #include <kcolorscheme.h>
00025 #include <kicon.h>
00026 #include <klocale.h>
00027 #include <kmenu.h>
00028 
00029 #include <QApplication>
00030 #include <QClipboard>
00031 #include <QMimeData>
00032 #include <QStyle>
00033 #include <QStyleOptionFocusRect>
00034 
00035 KUrlButton::KUrlButton(KUrlNavigator* parent) :
00036     QPushButton(parent),
00037     m_displayHint(0),
00038     m_urlNavigator(parent)
00039 {
00040     setFocusPolicy(Qt::NoFocus);
00041     setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
00042     setMinimumHeight(parent->minimumHeight());
00043 
00044     connect(this, SIGNAL(pressed()), parent, SLOT(requestActivation()));
00045 }
00046 
00047 KUrlButton::~KUrlButton()
00048 {
00049 }
00050 
00051 void KUrlButton::setDisplayHintEnabled(DisplayHint hint,
00052                                        bool enable)
00053 {
00054     if (enable) {
00055         m_displayHint = m_displayHint | hint;
00056     } else {
00057         m_displayHint = m_displayHint & ~hint;
00058     }
00059     update();
00060 }
00061 
00062 bool KUrlButton::isDisplayHintEnabled(DisplayHint hint) const
00063 {
00064     return (m_displayHint & hint) > 0;
00065 }
00066 
00067 void KUrlButton::enterEvent(QEvent* event)
00068 {
00069     QPushButton::enterEvent(event);
00070     setDisplayHintEnabled(EnteredHint, true);
00071     update();
00072 }
00073 
00074 void KUrlButton::leaveEvent(QEvent* event)
00075 {
00076     QPushButton::leaveEvent(event);
00077     setDisplayHintEnabled(EnteredHint, false);
00078     update();
00079 }
00080 
00081 void KUrlButton::contextMenuEvent(QContextMenuEvent* event)
00082 {
00083     Q_UNUSED(event);
00084     m_urlNavigator->requestActivation();
00085 
00086     KMenu popup(this);
00087 
00088     // provide 'Copy' action, which copies the current URL of
00089     // the URL navigator into the clipboard
00090     QAction* copyAction = popup.addAction(KIcon("edit-copy"), i18n("Copy"));
00091 
00092     // provide 'Paste' action, which copies the current clipboard text
00093     // into the URL navigator
00094     QAction* pasteAction = popup.addAction(KIcon("edit-paste"), i18n("Paste"));
00095     QClipboard* clipboard = QApplication::clipboard();
00096     pasteAction->setEnabled(!clipboard->text().isEmpty());
00097 
00098     popup.addSeparator();
00099 
00100     // provide radiobuttons for toggling between the edit and the navigation mode
00101     QAction* editAction = popup.addAction(i18n("Edit"));
00102     editAction->setCheckable(true);
00103 
00104     QAction* navigateAction = popup.addAction(i18n("Navigate"));
00105     navigateAction->setCheckable(true);
00106 
00107     QActionGroup* modeGroup = new QActionGroup(&popup);
00108     modeGroup->addAction(editAction);
00109     modeGroup->addAction(navigateAction);
00110     if (m_urlNavigator->isUrlEditable()) {
00111         editAction->setChecked(true);
00112     } else {
00113         navigateAction->setChecked(true);
00114     }
00115 
00116     QAction* activatedAction = popup.exec(QCursor::pos());
00117     if (activatedAction == copyAction) {
00118         QMimeData* mimeData = new QMimeData();
00119         mimeData->setText(m_urlNavigator->url().prettyUrl());
00120         clipboard->setMimeData(mimeData);
00121     } else if (activatedAction == pasteAction) {
00122         m_urlNavigator->setUrl(KUrl(clipboard->text()));
00123     } else if (activatedAction == editAction) {
00124         m_urlNavigator->setUrlEditable(true);
00125     } else if (activatedAction == navigateAction) {
00126         m_urlNavigator->setUrlEditable(false);
00127     }
00128 }
00129 
00130 void KUrlButton::drawHoverBackground(QPainter* painter)
00131 {
00132     const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
00133                                isDisplayHintEnabled(DraggedHint) ||
00134                                isDisplayHintEnabled(PopupActiveHint);
00135 
00136     QColor backgroundColor = isHighlighted ? palette().color(QPalette::Highlight) : Qt::transparent;
00137     if (!urlNavigator()->isActive() && isHighlighted) {
00138         backgroundColor.setAlpha(128);
00139     }
00140 
00141     if (backgroundColor != Qt::transparent) {
00142         // TODO: the backgroundColor should be applied to the style
00143         QStyleOptionViewItemV4 option;
00144         option.initFrom(this);
00145         option.state = QStyle::State_Enabled | QStyle::State_MouseOver;
00146         option.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
00147         style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, this);
00148     }
00149 }
00150 
00151 QColor KUrlButton::foregroundColor() const
00152 {
00153     const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
00154                                isDisplayHintEnabled(DraggedHint) ||
00155                                isDisplayHintEnabled(PopupActiveHint);
00156 
00157     QColor foregroundColor = palette().color(foregroundRole());
00158     const bool isActive = m_urlNavigator->isActive();
00159 
00160     int alpha = isActive ? 255 : 128;
00161     if ((!isDisplayHintEnabled(ActivatedHint) || !isActive) && !isHighlighted) {
00162         alpha -= alpha / 4;
00163     }
00164     foregroundColor.setAlpha(alpha);
00165 
00166     return foregroundColor;
00167 }
00168 
00169 #include "kurlbutton_p.moc"

KFile

Skip menu "KFile"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal