• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • kdelibs
  • 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(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     painter.drawText(QRect(BorderWidth, 0, textWidth, buttonHeight), Qt::AlignCenter, text());
00135 }
00136 
00137 void KUrlNavigatorProtocolCombo::setProtocol(QAction* action)
00138 {
00139     const QString protocol = action->data().toString();
00140     setText(protocol);
00141     emit activated(protocol);
00142 }
00143 
00144 void KUrlNavigatorProtocolCombo::updateMenu()
00145 {
00146     initializeCategories();
00147     qSort(m_protocols);
00148 
00149     // move all protocols into the corresponding category of 'items'
00150     QList<QString> items[CategoryCount];
00151     foreach (const QString& protocol, m_protocols) {
00152         if (m_categories.contains(protocol)) {
00153             const ProtocolCategory category = m_categories.value(protocol);
00154             items[category].append(protocol);
00155         } else {
00156             items[OtherCategory].append(protocol);
00157         }
00158     }
00159 
00160     // Create the menu that includes all entries from 'items'. The categories
00161     // CoreCategory and PlacesCategory are placed at the top level, the remaining
00162     // categories are placed in sub menus.
00163     QMenu* menu = m_menu;
00164     for (int category = 0; category < CategoryCount; ++category) {
00165         if (items[category].count() > 0) {
00166             switch (category) {
00167             case DevicesCategory:
00168                 menu = m_menu->addMenu(i18nc("@item:inmenu", "Devices"));
00169                 break;
00170 
00171             case SubversionCategory:
00172                 menu = m_menu->addMenu(i18nc("@item:inmenu", "Subversion"));
00173                 break;
00174 
00175             case OtherCategory:
00176                 menu = m_menu->addMenu(i18nc("@item:inmenu", "Other"));
00177                 break;
00178 
00179             case CoreCategory:
00180             case PlacesCategory:
00181             default:
00182                 break;
00183             }
00184 
00185             foreach (const QString& protocol, items[category]) {
00186                 QAction* action = menu->addAction(protocol);
00187                 action->setData(protocol);
00188             }
00189 
00190             if (menu == m_menu) {
00191                 menu->addSeparator();
00192             }
00193         }
00194     }
00195 }
00196 
00197 void KUrlNavigatorProtocolCombo::initializeCategories()
00198 {
00199     if (m_categories.isEmpty()) {
00200         m_categories.insert("file", CoreCategory);
00201         m_categories.insert("ftp", CoreCategory);
00202         m_categories.insert("fish", CoreCategory);
00203         m_categories.insert("nfs", CoreCategory);
00204         m_categories.insert("sftp", CoreCategory);
00205         m_categories.insert("smb", CoreCategory);
00206         m_categories.insert("webdav", CoreCategory);
00207 
00208         m_categories.insert("desktop", PlacesCategory);
00209         m_categories.insert("fonts", PlacesCategory);
00210         m_categories.insert("programs", PlacesCategory);
00211         m_categories.insert("settings", PlacesCategory);
00212         m_categories.insert("trash", PlacesCategory);
00213 
00214         m_categories.insert("floppy", DevicesCategory);
00215         m_categories.insert("camera", DevicesCategory);
00216         m_categories.insert("remote", DevicesCategory);
00217 
00218         m_categories.insert("svn", SubversionCategory);
00219         m_categories.insert("svn+file", SubversionCategory);
00220         m_categories.insert("svn+http", SubversionCategory);
00221         m_categories.insert("svn+https", SubversionCategory);
00222         m_categories.insert("svn+ssh", SubversionCategory);
00223     }
00224 }
00225 
00226 } // namespace KDEPrivate
00227 
00228 #include "kurlnavigatorprotocolcombo_p.moc"

KFile

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

kdelibs

Skip menu "kdelibs"
  • 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
Generated for kdelibs by doxygen 1.7.5
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