KFile
kprotocolcombo.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
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"