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 const int ArrowSize = 10; 00036 } 00037 00038 KUrlNavigatorProtocolCombo::KUrlNavigatorProtocolCombo(const QString& protocol, QWidget* parent) : 00039 KUrlNavigatorButtonBase(parent), 00040 m_menu(0), 00041 m_protocols(), 00042 m_categories() 00043 { 00044 m_menu = new QMenu(this); 00045 connect(m_menu, SIGNAL(triggered(QAction*)), this, SLOT(setProtocol(QAction*))); 00046 setText(protocol); 00047 setMenu(m_menu); 00048 } 00049 00050 void KUrlNavigatorProtocolCombo::setCustomProtocols(const QStringList& protocols) 00051 { 00052 m_protocols = protocols; 00053 m_menu->clear(); 00054 00055 foreach (const QString& protocol, protocols) { 00056 QAction* action = m_menu->addAction(protocol); 00057 action->setData(protocol); 00058 } 00059 } 00060 00061 QSize KUrlNavigatorProtocolCombo::sizeHint() const 00062 { 00063 const QSize size = KUrlNavigatorButtonBase::sizeHint(); 00064 00065 QFontMetrics fontMetrics(font()); 00066 int width = fontMetrics.width(text()); 00067 width += (3 * BorderWidth) + ArrowSize; 00068 00069 return QSize(width, size.height()); 00070 } 00071 00072 void KUrlNavigatorProtocolCombo::setProtocol(const QString& protocol) 00073 { 00074 setText(protocol); 00075 } 00076 00077 QString KUrlNavigatorProtocolCombo::currentProtocol() const 00078 { 00079 return text(); 00080 } 00081 00082 void KUrlNavigatorProtocolCombo::showEvent(QShowEvent* event) 00083 { 00084 KUrlNavigatorButtonBase::showEvent(event); 00085 if (!event->spontaneous() && m_protocols.isEmpty()) { 00086 m_protocols = KProtocolInfo::protocols(); 00087 qSort(m_protocols); 00088 00089 QStringList::iterator it = m_protocols.begin(); 00090 while (it != m_protocols.end()) { 00091 const KUrl url(*it + "://"); 00092 if (!KProtocolManager::supportsListing(url)) { 00093 it = m_protocols.erase(it); 00094 } else { 00095 ++it; 00096 } 00097 } 00098 00099 updateMenu(); 00100 } 00101 } 00102 00103 void KUrlNavigatorProtocolCombo::paintEvent(QPaintEvent* event) 00104 { 00105 Q_UNUSED(event); 00106 00107 QPainter painter(this); 00108 const int buttonWidth = width(); 00109 const int buttonHeight = height(); 00110 00111 drawHoverBackground(&painter); 00112 00113 const QColor fgColor = foregroundColor(); 00114 painter.setPen(fgColor); 00115 00116 // draw arrow 00117 const int arrowX = buttonWidth - ArrowSize - BorderWidth; 00118 const int arrowY = (buttonHeight - ArrowSize) / 2; 00119 00120 QStyleOption option; 00121 option.rect = QRect(arrowX, arrowY, ArrowSize, ArrowSize); 00122 option.palette = palette(); 00123 option.palette.setColor(QPalette::Text, fgColor); 00124 option.palette.setColor(QPalette::WindowText, fgColor); 00125 option.palette.setColor(QPalette::ButtonText, fgColor); 00126 style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &option, &painter, this ); 00127 00128 // draw text 00129 const int textWidth = arrowX - (2 * BorderWidth); 00130 painter.drawText(QRect(BorderWidth, 0, textWidth, buttonHeight), Qt::AlignCenter, text()); 00131 } 00132 00133 void KUrlNavigatorProtocolCombo::setProtocol(QAction* action) 00134 { 00135 const QString protocol = action->data().toString(); 00136 setText(protocol); 00137 emit activated(protocol); 00138 } 00139 00140 void KUrlNavigatorProtocolCombo::updateMenu() 00141 { 00142 initializeCategories(); 00143 qSort(m_protocols); 00144 00145 // move all protocols into the corresponding category of 'items' 00146 QList<QString> items[CategoryCount]; 00147 foreach (const QString& protocol, m_protocols) { 00148 if (m_categories.contains(protocol)) { 00149 const ProtocolCategory category = m_categories.value(protocol); 00150 items[category].append(protocol); 00151 } else { 00152 items[OtherCategory].append(protocol); 00153 } 00154 } 00155 00156 // Create the menu that includes all entries from 'items'. The categories 00157 // CoreCategory and PlacesCategory are placed at the top level, the remaining 00158 // categories are placed in sub menus. 00159 QMenu* menu = m_menu; 00160 for (int category = 0; category < CategoryCount; ++category) { 00161 if (items[category].count() > 0) { 00162 switch (category) { 00163 case DevicesCategory: 00164 menu = m_menu->addMenu(i18nc("@item:inmenu", "Devices")); 00165 break; 00166 00167 case SubversionCategory: 00168 menu = m_menu->addMenu(i18nc("@item:inmenu", "Subversion")); 00169 break; 00170 00171 case OtherCategory: 00172 menu = m_menu->addMenu(i18nc("@item:inmenu", "Other")); 00173 break; 00174 00175 case CoreCategory: 00176 case PlacesCategory: 00177 default: 00178 break; 00179 } 00180 00181 foreach (const QString& protocol, items[category]) { 00182 QAction* action = menu->addAction(protocol); 00183 action->setData(protocol); 00184 } 00185 00186 if (menu == m_menu) { 00187 menu->addSeparator(); 00188 } 00189 } 00190 } 00191 } 00192 00193 void KUrlNavigatorProtocolCombo::initializeCategories() 00194 { 00195 if (m_categories.isEmpty()) { 00196 m_categories.insert("file", CoreCategory); 00197 m_categories.insert("ftp", CoreCategory); 00198 m_categories.insert("fish", CoreCategory); 00199 m_categories.insert("nfs", CoreCategory); 00200 m_categories.insert("sftp", CoreCategory); 00201 m_categories.insert("smb", CoreCategory); 00202 m_categories.insert("webdav", CoreCategory); 00203 00204 m_categories.insert("desktop", PlacesCategory); 00205 m_categories.insert("fonts", PlacesCategory); 00206 m_categories.insert("programs", PlacesCategory); 00207 m_categories.insert("settings", PlacesCategory); 00208 m_categories.insert("trash", PlacesCategory); 00209 00210 m_categories.insert("floppy", DevicesCategory); 00211 m_categories.insert("camera", DevicesCategory); 00212 m_categories.insert("remote", DevicesCategory); 00213 00214 m_categories.insert("svn", SubversionCategory); 00215 m_categories.insert("svn+file", SubversionCategory); 00216 m_categories.insert("svn+http", SubversionCategory); 00217 m_categories.insert("svn+https", SubversionCategory); 00218 m_categories.insert("svn+ssh", SubversionCategory); 00219 } 00220 } 00221 00222 #include "kurlnavigatorprotocolcombo_p.moc"
KDE 4.6 API Reference