File indexing completed on 2024-04-28 03:59:06

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2001 Holger Freyther <freyher@yahoo.com>
0004 
0005     based on ideas from Martijn and Simon
0006     many thanks to Simon
0007 
0008     SPDX-License-Identifier: LGPL-2.0-only
0009 */
0010 
0011 #include "kguiitem.h"
0012 
0013 #include <QPushButton>
0014 #include <QSharedData>
0015 
0016 class KGuiItemPrivate : public QSharedData
0017 {
0018 public:
0019     KGuiItemPrivate()
0020     {
0021         m_enabled = true;
0022         m_hasIcon = false;
0023     }
0024 
0025     KGuiItemPrivate(const KGuiItemPrivate &other) = default;
0026 
0027     KGuiItemPrivate &operator=(const KGuiItemPrivate &other) = default;
0028 
0029     QString m_text;
0030     QString m_toolTip;
0031     QString m_whatsThis;
0032     QString m_statusText;
0033     QString m_iconName;
0034     QIcon m_icon;
0035     bool m_hasIcon : 1;
0036     bool m_enabled : 1;
0037 };
0038 
0039 KGuiItem::KGuiItem()
0040     : d(new KGuiItemPrivate)
0041 {
0042 }
0043 
0044 KGuiItem::KGuiItem(const QString &text, const QString &iconName, const QString &toolTip, const QString &whatsThis)
0045     : d(new KGuiItemPrivate)
0046 {
0047     d->m_text = text;
0048     d->m_toolTip = toolTip;
0049     d->m_whatsThis = whatsThis;
0050     setIconName(iconName);
0051 }
0052 
0053 KGuiItem::KGuiItem(const QString &text, const QIcon &icon, const QString &toolTip, const QString &whatsThis)
0054     : d(new KGuiItemPrivate)
0055 {
0056     d->m_text = text;
0057     d->m_toolTip = toolTip;
0058     d->m_whatsThis = whatsThis;
0059     setIcon(icon);
0060 }
0061 
0062 KGuiItem::KGuiItem(const KGuiItem &rhs) = default;
0063 
0064 KGuiItem &KGuiItem::operator=(const KGuiItem &rhs) = default;
0065 
0066 KGuiItem::~KGuiItem() = default;
0067 
0068 QString KGuiItem::text() const
0069 {
0070     return d->m_text;
0071 }
0072 
0073 QString KGuiItem::plainText() const
0074 {
0075     const int len = d->m_text.length();
0076 
0077     if (len == 0) {
0078         return d->m_text;
0079     }
0080 
0081     // Can assume len >= 1 from now on.
0082     QString stripped;
0083 
0084     int resultLength = 0;
0085     stripped.resize(len);
0086 
0087     const QChar *data = d->m_text.unicode();
0088     for (int pos = 0; pos < len; ++pos) {
0089         if (data[pos] != QLatin1Char('&')) {
0090             stripped[resultLength++] = data[pos];
0091         } else if (pos + 1 < len && data[pos + 1] == QLatin1Char('&')) {
0092             stripped[resultLength++] = data[pos++];
0093         }
0094     }
0095 
0096     stripped.truncate(resultLength);
0097 
0098     return stripped;
0099 }
0100 
0101 QIcon KGuiItem::icon() const
0102 {
0103     if (d->m_hasIcon) {
0104         if (!d->m_iconName.isEmpty()) {
0105             return QIcon::fromTheme(d->m_iconName);
0106         } else {
0107             return d->m_icon;
0108         }
0109     }
0110     return QIcon();
0111 }
0112 
0113 QString KGuiItem::iconName() const
0114 {
0115     return d->m_iconName;
0116 }
0117 
0118 QString KGuiItem::toolTip() const
0119 {
0120     return d->m_toolTip;
0121 }
0122 
0123 QString KGuiItem::whatsThis() const
0124 {
0125     return d->m_whatsThis;
0126 }
0127 
0128 bool KGuiItem::isEnabled() const
0129 {
0130     return d->m_enabled;
0131 }
0132 
0133 bool KGuiItem::hasIcon() const
0134 {
0135     return d->m_hasIcon;
0136 }
0137 
0138 void KGuiItem::setText(const QString &text)
0139 {
0140     d->m_text = text;
0141 }
0142 
0143 void KGuiItem::setIcon(const QIcon &icon)
0144 {
0145     d->m_icon = icon;
0146     d->m_iconName.clear();
0147     d->m_hasIcon = !icon.isNull();
0148 }
0149 
0150 void KGuiItem::setIconName(const QString &iconName)
0151 {
0152     d->m_iconName = iconName;
0153     d->m_icon = QIcon();
0154     d->m_hasIcon = !iconName.isEmpty();
0155 }
0156 
0157 void KGuiItem::setToolTip(const QString &toolTip)
0158 {
0159     d->m_toolTip = toolTip;
0160 }
0161 
0162 void KGuiItem::setWhatsThis(const QString &whatsThis)
0163 {
0164     d->m_whatsThis = whatsThis;
0165 }
0166 
0167 void KGuiItem::setEnabled(bool enabled)
0168 {
0169     d->m_enabled = enabled;
0170 }
0171 
0172 void KGuiItem::assign(QPushButton *button, const KGuiItem &item)
0173 {
0174     button->setText(item.d->m_text);
0175     button->setIcon(item.icon());
0176     button->setToolTip(item.d->m_toolTip);
0177     button->setWhatsThis(item.d->m_whatsThis);
0178 }