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

KFile

kprotocolcombo.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Aaron J. Seigo (<aseigo@kde.org>)               *
00003  *                                                                         *
00004  *   This library is free software; you can redistribute it and/or         *
00005  *   modify it under the terms of the GNU Lesser General Public            *
00006  *   License as published by the Free Software Foundation; either          *
00007  *   version 2 of the License, or (at your option) any later version.      *
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  *   Lesser General Public License for more details.                       *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU Lesser General Public      *
00015  *   License along with this library; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00018  ***************************************************************************/
00019 
00020 #include "kprotocolcombo_p.h"
00021 
00022 #include <QtGui/QAction>
00023 #include <QtGui/QMenu>
00024 #include <QtGui/QPainter>
00025 #include <QtGui/QPaintEvent>
00026 #include <QtGui/QStyleOption>
00027 
00028 #include <kdebug.h>
00029 #include <kprotocolinfo.h>
00030 #include <kprotocolmanager.h>
00031 #include <kurlnavigator.h>
00032 
00033 KProtocolCombo::KProtocolCombo(const QString& protocol, KUrlNavigator* parent)
00034     : KUrlButton(parent),
00035       m_protocols(KProtocolInfo::protocols())
00036 {
00037     qSort(m_protocols);
00038     QStringList::iterator it = m_protocols.begin();
00039     menu = new QMenu(this);
00040     while (it != m_protocols.end()) {
00041         const KUrl url(*it + "://");
00042         if (!KProtocolManager::supportsListing(url)) {
00043             it = m_protocols.erase(it);
00044         } else {
00045             ++it;
00046         }
00047     }
00048 
00049     updateMenu();
00050 
00051     connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(setProtocol(QAction*)));
00052     setText(protocol);
00053     setMenu(menu);
00054 }
00055 
00056 void KProtocolCombo::setCustomProtocols(const QStringList &protocols)
00057 {
00058     m_protocols = protocols;
00059 
00060     updateMenu();
00061 }
00062 
00063 QSize KProtocolCombo::sizeHint() const
00064 {
00065     QSize size = KUrlButton::sizeHint();
00066 
00067     QFontMetrics fontMetrics(font());
00068     int width = fontMetrics.width(text());
00069     width += (3 * BorderWidth) + ArrowSize;
00070 
00071     return QSize(width, size.height());
00072 }
00073 
00074 void KProtocolCombo::setProtocol(const QString& protocol)
00075 {
00076     setText(protocol);
00077 }
00078 
00079 QString KProtocolCombo::currentProtocol() const
00080 {
00081     return text();
00082 }
00083 
00084 void KProtocolCombo::paintEvent(QPaintEvent* event)
00085 {
00086     QPainter painter(this);
00087     const int buttonWidth  = width();
00088     const int buttonHeight = height();
00089 
00090     drawHoverBackground(&painter);
00091 
00092     const QColor fgColor = foregroundColor();
00093     painter.setPen(fgColor);
00094 
00095     // draw arrow
00096     const int arrowX = buttonWidth - ArrowSize - BorderWidth;
00097     const int arrowY = (buttonHeight - ArrowSize) / 2;
00098 
00099     QStyleOption option;
00100     option.rect = QRect(arrowX, arrowY, ArrowSize, ArrowSize);
00101     option.palette = palette();
00102     option.palette.setColor(QPalette::Text, fgColor);
00103     option.palette.setColor(QPalette::WindowText, fgColor);
00104     option.palette.setColor(QPalette::ButtonText, fgColor);
00105     style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &option, &painter, this );
00106 
00107     // draw text
00108     const int textWidth = arrowX - (2 * BorderWidth);
00109     painter.drawText(QRect(BorderWidth, 0, textWidth, buttonHeight), Qt::AlignCenter, text());
00110 }
00111 
00112 void KProtocolCombo::setProtocol(QAction* action)
00113 {
00114     const int index = action->data().toInt();
00115     Q_ASSERT((index >= 0) && (index < m_protocols.count()));
00116     const QString protocol = m_protocols[index];
00117     setText(protocol);
00118     emit activated(protocol);
00119 }
00120 
00121 void KProtocolCombo::updateMenu()
00122 {
00123     menu->clear();
00124 
00125     int i = 0;
00126     foreach (const QString &protocol, m_protocols) {
00127         QAction *action = menu->addAction(protocol);
00128         action->setData(i++);
00129     }
00130 }
00131 
00132 #include "kprotocolcombo_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