File indexing completed on 2024-05-12 16:39:57
0001 /* This file is part of the KDE project 0002 Copyright (C) 2005-2008 Jarosław Staniek <staniek@kde.org> 0003 0004 This program is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this program; see the file COPYING. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "SmallToolButton.h" 0021 #include "utils.h" 0022 0023 #include <QDebug> 0024 #include <QStyle> 0025 #include <QStyleOption> 0026 #include <QPainter> 0027 #include <QPointer> 0028 #include <QAction> 0029 #include <QIcon> 0030 0031 //! @internal 0032 class Q_DECL_HIDDEN KexiSmallToolButton::Private 0033 { 0034 public: 0035 Private() 0036 : enableSlotButtonToggled(true) 0037 , enableSlotActionToggled(true) { 0038 } 0039 0040 QPointer<QAction> action; 0041 bool enableSlotButtonToggled; 0042 bool enableSlotActionToggled; 0043 }; 0044 0045 //-------------------------------- 0046 0047 KexiSmallToolButton::KexiSmallToolButton(QWidget* parent) 0048 : QToolButton(parent) 0049 , d(new Private) 0050 { 0051 init(); 0052 update(QString(), QIcon()); 0053 } 0054 0055 KexiSmallToolButton::KexiSmallToolButton(const QString& text, QWidget* parent) 0056 : QToolButton(parent) 0057 , d(new Private) 0058 { 0059 init(); 0060 update(text, QIcon()); 0061 } 0062 0063 KexiSmallToolButton::KexiSmallToolButton(const QIcon& icon, const QString& text, 0064 QWidget* parent) 0065 : QToolButton(parent) 0066 , d(new Private) 0067 { 0068 init(); 0069 update(text, icon); 0070 } 0071 0072 KexiSmallToolButton::KexiSmallToolButton(const QIcon& icon, QWidget* parent) 0073 : QToolButton(parent) 0074 , d(new Private) 0075 { 0076 init(); 0077 update(QString(), icon); 0078 QToolButton::setToolButtonStyle(Qt::ToolButtonIconOnly); 0079 } 0080 0081 KexiSmallToolButton::KexiSmallToolButton(QAction* action, QWidget* parent) 0082 : QToolButton(parent) 0083 , d(new Private) 0084 { 0085 d->action = action; 0086 init(); 0087 if (d->action) { 0088 connect(d->action, SIGNAL(changed()), this, SLOT(slotActionChanged())); 0089 update(d->action->text(), d->action->icon(), false); 0090 setEnabled(d->action->isEnabled()); 0091 setToolTip(d->action->toolTip()); 0092 setWhatsThis(d->action->whatsThis()); 0093 setCheckable(d->action->isCheckable()); 0094 if (d->action->menu()) { 0095 setPopupMode(QToolButton::MenuButtonPopup); 0096 setMenu(d->action->menu()); 0097 } else { 0098 connect(this, SIGNAL(toggled(bool)), this, SLOT(slotButtonToggled(bool))); 0099 connect(d->action, SIGNAL(toggled(bool)), this, SLOT(slotActionToggled(bool))); 0100 } 0101 } 0102 connect(this, SIGNAL(clicked()), action, SLOT(trigger())); 0103 updateAction(); 0104 } 0105 0106 KexiSmallToolButton::~KexiSmallToolButton() 0107 { 0108 delete d; 0109 } 0110 0111 void KexiSmallToolButton::updateAction() 0112 { 0113 0114 } 0115 0116 void KexiSmallToolButton::init() 0117 { 0118 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); 0119 QFont f(font()); 0120 f.setPointSizeF(KexiUtils::smallestReadableFont().pointSizeF()); 0121 setFont(f); 0122 setAutoRaise(true); 0123 QToolButton::setToolButtonStyle(Qt::ToolButtonTextBesideIcon); 0124 } 0125 0126 void KexiSmallToolButton::setToolButtonStyle(Qt::ToolButtonStyle style) 0127 { 0128 QToolButton::setToolButtonStyle(style); 0129 update(text(), icon(), false); 0130 } 0131 0132 void KexiSmallToolButton::update(const QString& text, const QIcon& icon, bool tipToo) 0133 { 0134 if (!text.isEmpty() && toolButtonStyle() != Qt::ToolButtonIconOnly) { 0135 if (toolButtonStyle() != Qt::ToolButtonTextOnly) 0136 QToolButton::setToolButtonStyle(Qt::ToolButtonTextBesideIcon); 0137 QToolButton::setText(text); 0138 if (tipToo) 0139 setToolTip(text); 0140 } 0141 if (toolButtonStyle() == Qt::ToolButtonTextOnly) { 0142 QToolButton::setIcon(QIcon()); 0143 } else if (!icon.isNull()) { 0144 QToolButton::setIcon(icon); 0145 } 0146 } 0147 0148 QSize KexiSmallToolButton::sizeHint() const 0149 { 0150 return QToolButton::sizeHint() 0151 - QSize((toolButtonStyle() == Qt::ToolButtonTextBesideIcon) ? 4 : 0, 0); 0152 } 0153 0154 void KexiSmallToolButton::setIcon(const QIcon& icon) 0155 { 0156 update(text(), icon); 0157 } 0158 0159 void KexiSmallToolButton::setIcon(const QString &iconName) 0160 { 0161 setIcon(QIcon::fromTheme(iconName)); 0162 } 0163 0164 void KexiSmallToolButton::setText(const QString& text) 0165 { 0166 update(text, icon()); 0167 } 0168 0169 void KexiSmallToolButton::slotActionChanged() 0170 { 0171 setEnabled(d->action->isEnabled()); 0172 } 0173 0174 void KexiSmallToolButton::slotButtonToggled(bool checked) 0175 { 0176 Q_UNUSED(checked); 0177 if (!d->enableSlotButtonToggled) 0178 return; 0179 //QObject *view = KDbUtils::findParent<QObject*>(this, "KexiView"); 0180 //qDebug() << QString("checked=%1 action=%2 view=%3") 0181 // .arg(checked).arg(d->action ? d->action->text() : QString()) 0182 // .arg(view ? view->objectName() : QString("??")); 0183 d->enableSlotActionToggled = false; 0184 d->enableSlotActionToggled = true; 0185 } 0186 0187 void KexiSmallToolButton::slotActionToggled(bool checked) 0188 { 0189 if (!d->enableSlotActionToggled) 0190 return; 0191 //QObject *view = KDbUtils::findParent<QObject*>(this, "KexiView"); 0192 //qDebug() << QString("checked=%1 action=%2 view=%3") 0193 // .arg(checked).arg(d->action ? d->action->text() : QString()) 0194 // .arg(view ? view->objectName() : QString("??")); 0195 d->enableSlotButtonToggled = false; 0196 setChecked(checked); 0197 d->enableSlotButtonToggled = true; 0198 } 0199 0200 QAction* KexiSmallToolButton::action() const 0201 { 0202 return d->action; 0203 } 0204 0205 //------------------------------------------ 0206 0207 class Q_DECL_HIDDEN KexiToolBarSeparator::Private 0208 { 0209 public: 0210 Private(); 0211 ~Private(); 0212 0213 Qt::Orientation orientation; 0214 }; 0215 0216 KexiToolBarSeparator::Private::Private() 0217 :orientation(Qt::Horizontal) 0218 { 0219 0220 } 0221 0222 KexiToolBarSeparator::Private::~Private() 0223 { 0224 0225 } 0226 0227 KexiToolBarSeparator::KexiToolBarSeparator(QWidget *parent) 0228 : QWidget(parent) 0229 , d(new Private()) 0230 { 0231 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); 0232 //! @todo 0233 setFixedHeight(parent->height() - 6); 0234 } 0235 0236 KexiToolBarSeparator::~KexiToolBarSeparator() 0237 { 0238 delete d; 0239 } 0240 0241 void KexiToolBarSeparator::initStyleOption(QStyleOption *o) const 0242 { 0243 o->initFrom(this); 0244 if (orientation() == Qt::Horizontal) 0245 o->state |= QStyle::State_Horizontal; 0246 } 0247 0248 void KexiToolBarSeparator::setOrientation(Qt::Orientation o) 0249 { 0250 d->orientation = o; 0251 update(); 0252 } 0253 0254 Qt::Orientation KexiToolBarSeparator::orientation() const 0255 { 0256 return d->orientation; 0257 } 0258 0259 QSize KexiToolBarSeparator::sizeHint() const 0260 { 0261 QStyleOption o; 0262 initStyleOption(&o); 0263 const int sepExtent = style()->pixelMetric( 0264 QStyle::PM_ToolBarSeparatorExtent, &o, 0); 0265 return QSize(sepExtent, sepExtent); 0266 } 0267 0268 void KexiToolBarSeparator::paintEvent(QPaintEvent *e) 0269 { 0270 Q_UNUSED(e); 0271 QPainter p(this); 0272 QStyleOption o; 0273 initStyleOption(&o); 0274 style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &o, &p, parentWidget()); 0275 } 0276