File indexing completed on 2024-05-12 05:09:48
0001 /* -*- c++ -*- 0002 0003 kwidgetlister.cpp 0004 0005 This file is part of libkdepim. 0006 Copyright (c) 2001 Marc Mutz <mutz@kde.org> 0007 0008 This library is free software; you can redistribute it and/or 0009 modify it under the terms of the GNU General Public License, 0010 version 2, as published by the Free Software Foundation. 0011 0012 This library is distributed in the hope that it will be useful, 0013 but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 General Public License for more details. 0016 0017 You should have received a copy of the GNU General Public License 0018 along with this library; if not, write to the Free Software 0019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 0020 0021 In addition, as a special exception, the copyright holders give 0022 permission to link the code of this library with any edition of 0023 the Qt library by Trolltech AS, Norway (or with modified versions 0024 of Qt that use the same license as Qt), and distribute linked 0025 combinations including the two. You must obey the GNU General 0026 Public License in all respects for all of the code used other than 0027 Qt. If you modify this file, you may extend this exception to 0028 your version of the file, but you are not obligated to do so. If 0029 you do not wish to do so, delete this exception statement from 0030 your version. 0031 */ 0032 0033 #include "kwidgetlister.h" 0034 0035 #include <KLocalizedString> 0036 #include <KStandardGuiItem> 0037 0038 #include <QPushButton> 0039 #include <QVBoxLayout> 0040 #include <QHBoxLayout> 0041 0042 #include <assert.h> 0043 0044 KWidgetLister::KWidgetLister( int minWidgets, int maxWidgets, QWidget *parent, const char * ) 0045 : QWidget( parent ) 0046 { 0047 0048 mMinWidgets = qMax( minWidgets, 1 ); 0049 mMaxWidgets = qMax( maxWidgets, mMinWidgets + 1 ); 0050 0051 //--------- the button box 0052 mLayout = new QVBoxLayout( this ); 0053 mLayout->setMargin( 0 ); 0054 mLayout->setSpacing( 4 ); 0055 // mLayout->setSizeConstraint(QLayout::SetFixedSize); 0056 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed); 0057 0058 mButtonBox = new QWidget( this ); 0059 QHBoxLayout* mButtonBoxHBoxLayout = new QHBoxLayout( mButtonBox ); 0060 mButtonBoxHBoxLayout->setMargin( 0 ); 0061 mLayout->addWidget( mButtonBox ); 0062 0063 mBtnMore = new QPushButton( mButtonBox ); 0064 KGuiItem::assign( mBtnMore, KGuiItem( i18nc( "more widgets", "More" ), 0065 QStringLiteral("list-add") ) ); 0066 mButtonBoxHBoxLayout->addWidget( mBtnMore ); 0067 mButtonBoxHBoxLayout->setStretchFactor( mBtnMore, 0 ); 0068 0069 mBtnFewer = new QPushButton( mButtonBox ); 0070 KGuiItem::assign( mBtnFewer, KGuiItem( i18nc( "fewer widgets", "Fewer" ), 0071 QStringLiteral("list-remove") ) ); 0072 mButtonBoxHBoxLayout->addWidget( mBtnFewer ); 0073 mButtonBoxHBoxLayout->setStretchFactor( mBtnFewer, 0 ); 0074 0075 QWidget *spacer = new QWidget( mButtonBox ); 0076 mButtonBoxHBoxLayout->addWidget( spacer ); 0077 mButtonBoxHBoxLayout->setStretchFactor( spacer, 1 ); 0078 0079 mBtnClear = new QPushButton( mButtonBox ); 0080 KGuiItem::assign( mBtnClear, KStandardGuiItem::clear() ); 0081 mButtonBoxHBoxLayout->addWidget( mBtnClear ); 0082 // FIXME a useful whats this. KStandardGuiItem::clear() returns a text with an edit box 0083 mBtnClear->setWhatsThis( QString() ); 0084 mButtonBoxHBoxLayout->setStretchFactor( mBtnClear, 0 ); 0085 0086 //---------- connect everything 0087 connect( mBtnMore, &QAbstractButton::clicked, 0088 this, &KWidgetLister::slotMore ); 0089 connect( mBtnFewer, &QAbstractButton::clicked, 0090 this, &KWidgetLister::slotFewer ); 0091 connect( mBtnClear, &QAbstractButton::clicked, 0092 this, &KWidgetLister::slotClear ); 0093 0094 enableControls(); 0095 } 0096 0097 KWidgetLister::~KWidgetLister() 0098 { 0099 qDeleteAll( mWidgetList ); 0100 mWidgetList.clear(); 0101 } 0102 0103 void KWidgetLister::slotMore() 0104 { 0105 // the class should make certain that slotMore can't 0106 // be called when mMaxWidgets are on screen. 0107 assert( (int)mWidgetList.count() < mMaxWidgets ); 0108 0109 addWidgetAtEnd(); 0110 // adjustSize(); 0111 enableControls(); 0112 } 0113 0114 void KWidgetLister::slotFewer() 0115 { 0116 // the class should make certain that slotFewer can't 0117 // be called when mMinWidgets are on screen. 0118 assert( (int)mWidgetList.count() > mMinWidgets ); 0119 0120 removeLastWidget(); 0121 // adjustSize(); 0122 enableControls(); 0123 } 0124 0125 void KWidgetLister::slotClear() 0126 { 0127 setNumberOfShownWidgetsTo( mMinWidgets ); 0128 0129 // clear remaining widgets 0130 foreach ( QWidget *widget, mWidgetList ) { 0131 clearWidget( widget ); 0132 } 0133 0134 // adjustSize(); 0135 enableControls(); 0136 emit clearWidgets(); 0137 } 0138 0139 void KWidgetLister::addWidgetAtEnd( QWidget *w ) 0140 { 0141 if ( !w ) { 0142 w = this->createWidget( this ); 0143 } 0144 0145 mLayout->insertWidget( mLayout->indexOf( mButtonBox ), w ); 0146 mWidgetList.append( w ); 0147 w->show(); 0148 enableControls(); 0149 emit widgetAdded( w ); 0150 } 0151 0152 void KWidgetLister::removeLastWidget() 0153 { 0154 // The layout will take care that the 0155 // widget is removed from screen, too. 0156 delete mWidgetList.takeLast(); 0157 enableControls(); 0158 emit widgetRemoved(); 0159 } 0160 0161 void KWidgetLister::clearWidget( QWidget *w ) 0162 { 0163 Q_UNUSED( w ); 0164 } 0165 0166 QWidget *KWidgetLister::createWidget( QWidget *parent ) 0167 { 0168 return new QWidget( parent ); 0169 } 0170 0171 void KWidgetLister::setNumberOfShownWidgetsTo( int aNum ) 0172 { 0173 int superfluousWidgets = qMax( (int)mWidgetList.count() - aNum, 0 ); 0174 int missingWidgets = qMax( aNum - (int)mWidgetList.count(), 0 ); 0175 0176 // remove superfluous widgets 0177 for ( ; superfluousWidgets ; superfluousWidgets-- ) { 0178 removeLastWidget(); 0179 } 0180 0181 // add missing widgets 0182 for ( ; missingWidgets ; missingWidgets-- ) { 0183 addWidgetAtEnd(); 0184 } 0185 } 0186 0187 void KWidgetLister::enableControls() 0188 { 0189 int count = mWidgetList.count(); 0190 bool isMaxWidgets = ( count >= mMaxWidgets ); 0191 bool isMinWidgets = ( count <= mMinWidgets ); 0192 0193 mBtnMore->setEnabled( !isMaxWidgets ); 0194 mBtnFewer->setEnabled( !isMinWidgets ); 0195 }