File indexing completed on 2024-04-21 04:41:02
0001 /* This file is part of the KDE project 0002 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr> 0003 Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net> 0004 0005 This library is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU Library General Public 0007 License as published by the Free Software Foundation; either 0008 version 2 of the License, or (at your option) any later version. 0009 0010 This library is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 Library General Public License for more details. 0014 0015 You should have received a copy of the GNU Library General Public License 0016 along with this library; see the file COPYING.LIB. If not, write to 0017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #include "stringlistedit.h" 0022 0023 #include <QLineEdit> 0024 #include <QDialog> 0025 #include <QPainter> 0026 #include <QVariant> 0027 #include <QPushButton> 0028 #include <QHBoxLayout> 0029 0030 #include "KProperty.h" 0031 0032 class Q_DECL_HIDDEN KPropertyStringListEditor::Private 0033 { 0034 public: 0035 Private() { 0036 } 0037 QLineEdit *edit; 0038 QStringList list; 0039 QPushButton *selectButton; 0040 }; 0041 0042 KPropertyStringListEditor::KPropertyStringListEditor(KProperty *property, QWidget *parent) 0043 : Widget(property, parent), d(new Private) 0044 { 0045 setHasBorders(false); 0046 QHBoxLayout *l = new QHBoxLayout(this); 0047 l->setMargin(0); 0048 l->setSpacing(0); 0049 0050 d->edit = new QLineEdit(this); 0051 d->edit->setReadOnly(true); 0052 d->edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); 0053 d->edit->setMinimumHeight(5); 0054 l->addWidget(d->edit); 0055 0056 d->selectButton = new QPushButton(this); 0057 Utils::setupDotDotDotButton(d->selectButton, tr("Select item"), 0058 tr("Selects one item")); 0059 0060 d->selectButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding); 0061 l->addWidget(d->selectButton); 0062 setFocusWidget(d->selectButton); 0063 0064 connect(d->selectButton, SIGNAL(clicked()), this, SLOT(showEditor())); 0065 } 0066 0067 KPropertyStringListEditor::~KPropertyStringListEditor() 0068 { 0069 delete d; 0070 } 0071 0072 QVariant 0073 KPropertyStringListEditor::value() const 0074 { 0075 return d->list; 0076 } 0077 0078 void 0079 KPropertyStringListEditor::setValue(const QVariant &value, bool emitChange) 0080 { 0081 d->list = value.toStringList(); 0082 d->edit->setText(value.toStringList().join(", ")); 0083 if (emitChange) 0084 emit valueChanged(this); 0085 } 0086 0087 void 0088 KPropertyStringListEditor::drawViewer(QPainter *p, const QColorGroup &cg, const QRect &r, const QVariant &value) 0089 { 0090 Widget::drawViewer(p, cg, r, value.toStringList().join(", ")); 0091 } 0092 0093 void 0094 KPropertyStringListEditor::showEditor() 0095 { 0096 QDialog dialog(this->topLevelWidget()); 0097 dialog.setWindowTitle(tr("Edit List of Items", "Window title")); 0098 dialog.setObjectName("stringlist_dialog"); 0099 dialog.setButtons(QDialog::Ok | QDialog::Cancel); 0100 dialog.setDefaultButton(QDialog::Ok); 0101 dialog.setModal(false); 0102 KEditListBox *edit = new KEditListBox(tr("Contents of %1").arg(property()->captionOrName()), 0103 &dialog, "editlist"); 0104 // PORTING: Verify that widget was added to mainLayout: dialog.setMainWidget(edit); 0105 // Add mainLayout->addWidget(edit); if necessary 0106 edit->insertStringList(d->list); 0107 0108 if (dialog.exec() == QDialog::Accepted) { 0109 d->list = edit->items(); 0110 d->edit->setText(d->list.join(", ")); 0111 emit valueChanged(this); 0112 } 0113 } 0114 0115 void 0116 KPropertyStringListEditor::setReadOnlyInternal(bool readOnly) 0117 { 0118 d->selectButton->setEnabled(!readOnly); 0119 }