• Skip to content
  • Skip to link menu
KDE 4.2 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     popup.addSeparator();
00117     
00118     // allow showing of the full path
00119     QAction* showFullPathAction = popup.addAction(i18n("Show Full Path"));
00120     showFullPathAction->setCheckable(true);
00121     showFullPathAction->setChecked(m_urlNavigator->showFullPath());
00122 
00123     QAction* activatedAction = popup.exec(QCursor::pos());
00124     if (activatedAction == copyAction) {
00125         QMimeData* mimeData = new QMimeData();
00126         mimeData->setText(m_urlNavigator->url().prettyUrl());
00127         clipboard->setMimeData(mimeData);
00128     } else if (activatedAction == pasteAction) {
00129         m_urlNavigator->setUrl(KUrl(clipboard->text()));
00130     } else if (activatedAction == editAction) {
00131         m_urlNavigator->setUrlEditable(true);
00132     } else if (activatedAction == navigateAction) {
00133         m_urlNavigator->setUrlEditable(false);
00134     } else if (activatedAction == showFullPathAction) {
00135         m_urlNavigator->setShowFullPath(showFullPathAction->isChecked());
00136     }
00137 }
00138 
00139 void KUrlButton::drawHoverBackground(QPainter* painter)
00140 {
00141     const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
00142                                isDisplayHintEnabled(DraggedHint) ||
00143                                isDisplayHintEnabled(PopupActiveHint);
00144 
00145     QColor backgroundColor = isHighlighted ? palette().color(QPalette::Highlight) : Qt::transparent;
00146     if (!urlNavigator()->isActive() && isHighlighted) {
00147         backgroundColor.setAlpha(128);
00148     }
00149 
00150     if (backgroundColor != Qt::transparent) {
00151         // TODO: the backgroundColor should be applied to the style
00152         QStyleOptionViewItemV4 option;
00153         option.initFrom(this);
00154         option.state = QStyle::State_Enabled | QStyle::State_MouseOver;
00155         option.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
00156         style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, this);
00157     }
00158 }
00159 
00160 QColor KUrlButton::foregroundColor() const
00161 {
00162     const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
00163                                isDisplayHintEnabled(DraggedHint) ||
00164                                isDisplayHintEnabled(PopupActiveHint);
00165 
00166     QColor foregroundColor = palette().color(foregroundRole());
00167     const bool isActive = m_urlNavigator->isActive();
00168 
00169     int alpha = isActive ? 255 : 128;
00170     if ((!isDisplayHintEnabled(ActivatedHint) || !isActive) && !isHighlighted) {
00171         alpha -= alpha / 4;
00172     }
00173     foregroundColor.setAlpha(alpha);
00174 
00175     return foregroundColor;
00176 }
00177 
00178 #include "kurlbutton_p.moc"

KFile

Skip menu "KFile"
  • Main Page
  • Namespace List
  • 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
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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