• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

KFile

kurlnavigatorprotocolcombo.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Aaron J. Seigo (<aseigo@kde.org>)               *
00003  *   Copyright (C) 2009 by Peter Penz (<peter.penz@kde.org>)               *
00004  *                                                                         *
00005  *   This library is free software; you can redistribute it and/or         *
00006  *   modify it under the terms of the GNU Lesser General Public            *
00007  *   License as published by the Free Software Foundation; either          *
00008  *   version 2 of the License, or (at your option) any later version.      *
00009  *                                                                         *
00010  *   This library is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00013  *   Lesser General Public License for more details.                       *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Lesser General Public      *
00016  *   License along with this library; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00019  ***************************************************************************/
00020 
00021 #include "kurlnavigatorprotocolcombo_p.h"
00022 
00023 #include <QtGui/QAction>
00024 #include <QtGui/QMenu>
00025 #include <QtGui/QPainter>
00026 #include <QtGui/QPaintEvent>
00027 #include <QtGui/QStyleOption>
00028 
00029 #include <klocale.h>
00030 #include <kprotocolinfo.h>
00031 #include <kprotocolmanager.h>
00032 #include <kurlnavigator.h>
00033 
00034 namespace
00035 {
00036     const int ArrowSize = 10;
00037 }
00038 
00039 namespace KDEPrivate
00040 {
00041 
00042 KUrlNavigatorProtocolCombo::KUrlNavigatorProtocolCombo(const QString& protocol, QWidget* parent) :
00043     KUrlNavigatorButtonBase(parent),
00044     m_menu(0),
00045     m_protocols(),
00046     m_categories()
00047 {
00048     m_menu = new QMenu(this);
00049     connect(m_menu, SIGNAL(triggered(QAction*)), this, SLOT(setProtocol(QAction*)));
00050     setText(protocol);
00051     setMenu(m_menu);
00052 }
00053 
00054 void KUrlNavigatorProtocolCombo::setCustomProtocols(const QStringList& protocols)
00055 {
00056     m_protocols = protocols;
00057     m_menu->clear();
00058 
00059     foreach (const QString& protocol, protocols) {
00060         QAction* action = m_menu->addAction(protocol);
00061         action->setData(protocol);
00062     }
00063 }
00064 
00065 QSize KUrlNavigatorProtocolCombo::sizeHint() const
00066 {
00067     const QSize size = KUrlNavigatorButtonBase::sizeHint();
00068 
00069     QFontMetrics fontMetrics(font());
00070     int width = fontMetrics.width(KGlobal::locale()->removeAcceleratorMarker(text()));
00071     width += (3 * BorderWidth) + ArrowSize;
00072 
00073     return QSize(width, size.height());
00074 }
00075 
00076 void KUrlNavigatorProtocolCombo::setProtocol(const QString& protocol)
00077 {
00078     setText(protocol);
00079 }
00080 
00081 QString KUrlNavigatorProtocolCombo::currentProtocol() const
00082 {
00083     return text();
00084 }
00085 
00086 void KUrlNavigatorProtocolCombo::showEvent(QShowEvent* event)
00087 {
00088     KUrlNavigatorButtonBase::showEvent(event);
00089     if (!event->spontaneous() && m_protocols.isEmpty()) {
00090         m_protocols = KProtocolInfo::protocols();
00091         qSort(m_protocols);
00092 
00093         QStringList::iterator it = m_protocols.begin();
00094         while (it != m_protocols.end()) {
00095             const KUrl url(*it + "://");
00096             if (!KProtocolManager::supportsListing(url)) {
00097                 it = m_protocols.erase(it);
00098             } else {
00099                 ++it;
00100             }
00101         }
00102 
00103         updateMenu();
00104     }
00105 }
00106 
00107 void KUrlNavigatorProtocolCombo::paintEvent(QPaintEvent* event)
00108 {
00109     Q_UNUSED(event);
00110 
00111     QPainter painter(this);
00112     const int buttonWidth  = width();
00113     const int buttonHeight = height();
00114 
00115     drawHoverBackground(&painter);
00116 
00117     const QColor fgColor = foregroundColor();
00118     painter.setPen(fgColor);
00119 
00120     // draw arrow
00121     const int arrowX = buttonWidth - ArrowSize - BorderWidth;
00122     const int arrowY = (buttonHeight - ArrowSize) / 2;
00123 
00124     QStyleOption option;
00125     option.rect = QRect(arrowX, arrowY, ArrowSize, ArrowSize);
00126     option.palette = palette();
00127     option.palette.setColor(QPalette::Text, fgColor);
00128     option.palette.setColor(QPalette::WindowText, fgColor);
00129     option.palette.setColor(QPalette::ButtonText, fgColor);
00130     style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &option, &painter, this );
00131 
00132     // draw text
00133     const int textWidth = arrowX - (2 * BorderWidth);
00134     int alignment = Qt::AlignCenter | Qt::TextShowMnemonic;
00135     if (!style()->styleHint(QStyle::SH_UnderlineShortcut, &option, this)) {
00136         alignment |= Qt::TextHideMnemonic;
00137     }
00138     style()->drawItemText(&painter, QRect(BorderWidth, 0, textWidth, buttonHeight),
00139                           alignment, option.palette, isEnabled(), text());
00140 }
00141 
00142 void KUrlNavigatorProtocolCombo::setProtocol(QAction* action)
00143 {
00144     const QString protocol = action->data().toString();
00145     setText(protocol);
00146     emit activated(protocol);
00147 }
00148 
00149 void KUrlNavigatorProtocolCombo::updateMenu()
00150 {
00151     initializeCategories();
00152     qSort(m_protocols);
00153 
00154     // move all protocols into the corresponding category of 'items'
00155     QList<QString> items[CategoryCount];
00156     foreach (const QString& protocol, m_protocols) {
00157         if (m_categories.contains(protocol)) {
00158             const ProtocolCategory category = m_categories.value(protocol);
00159             items[category].append(protocol);
00160         } else {
00161             items[OtherCategory].append(protocol);
00162         }
00163     }
00164 
00165     // Create the menu that includes all entries from 'items'. The categories
00166     // CoreCategory and PlacesCategory are placed at the top level, the remaining
00167     // categories are placed in sub menus.
00168     QMenu* menu = m_menu;
00169     for (int category = 0; category < CategoryCount; ++category) {
00170         if (items[category].count() > 0) {
00171             switch (category) {
00172             case DevicesCategory:
00173                 menu = m_menu->addMenu(i18nc("@item:inmenu", "Devices"));
00174                 break;
00175 
00176             case SubversionCategory:
00177                 menu = m_menu->addMenu(i18nc("@item:inmenu", "Subversion"));
00178                 break;
00179 
00180             case OtherCategory:
00181                 menu = m_menu->addMenu(i18nc("@item:inmenu", "Other"));
00182                 break;
00183 
00184             case CoreCategory:
00185             case PlacesCategory:
00186             default:
00187                 break;
00188             }
00189 
00190             foreach (const QString& protocol, items[category]) {
00191                 QAction* action = menu->addAction(protocol);
00192                 action->setData(protocol);
00193             }
00194 
00195             if (menu == m_menu) {
00196                 menu->addSeparator();
00197             }
00198         }
00199     }
00200 }
00201 
00202 void KUrlNavigatorProtocolCombo::initializeCategories()
00203 {
00204     if (m_categories.isEmpty()) {
00205         m_categories.insert("file", CoreCategory);
00206         m_categories.insert("ftp", CoreCategory);
00207         m_categories.insert("fish", CoreCategory);
00208         m_categories.insert("nfs", CoreCategory);
00209         m_categories.insert("sftp", CoreCategory);
00210         m_categories.insert("smb", CoreCategory);
00211         m_categories.insert("webdav", CoreCategory);
00212 
00213         m_categories.insert("desktop", PlacesCategory);
00214         m_categories.insert("fonts", PlacesCategory);
00215         m_categories.insert("programs", PlacesCategory);
00216         m_categories.insert("settings", PlacesCategory);
00217         m_categories.insert("trash", PlacesCategory);
00218 
00219         m_categories.insert("floppy", DevicesCategory);
00220         m_categories.insert("camera", DevicesCategory);
00221         m_categories.insert("remote", DevicesCategory);
00222 
00223         m_categories.insert("svn", SubversionCategory);
00224         m_categories.insert("svn+file", SubversionCategory);
00225         m_categories.insert("svn+http", SubversionCategory);
00226         m_categories.insert("svn+https", SubversionCategory);
00227         m_categories.insert("svn+ssh", SubversionCategory);
00228     }
00229 }
00230 
00231 } // namespace KDEPrivate
00232 
00233 #include "kurlnavigatorprotocolcombo_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 19:17:56 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KFile

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

kdelibs-4.8.3 API Reference

Skip menu "kdelibs-4.8.3 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal