KProperty

stringlistedit.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2004 Cedric Pasteur <[email protected]>
3  Copyright (C) 2004 Alexander Dymo <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19 */
20 
21 #include "stringlistedit.h"
22 
23 #include <QLineEdit>
24 #include <QDialog>
25 #include <QPainter>
26 #include <QVariant>
27 #include <QPushButton>
28 #include <QHBoxLayout>
29 
30 #include "KProperty.h"
31 
32 class Q_DECL_HIDDEN KPropertyStringListEditor::Private
33 {
34 public:
35  Private() {
36  }
37  QLineEdit *edit;
39  QPushButton *selectButton;
40 };
41 
42 KPropertyStringListEditor::KPropertyStringListEditor(KProperty *property, QWidget *parent)
43  : Widget(property, parent), d(new Private)
44 {
45  setHasBorders(false);
46  QHBoxLayout *l = new QHBoxLayout(this);
47  l->setMargin(0);
48  l->setSpacing(0);
49 
50  d->edit = new QLineEdit(this);
51  d->edit->setReadOnly(true);
52  d->edit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
53  d->edit->setMinimumHeight(5);
54  l->addWidget(d->edit);
55 
56  d->selectButton = new QPushButton(this);
57  Utils::setupDotDotDotButton(d->selectButton, tr("Select item"),
58  tr("Selects one item"));
59 
60  d->selectButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
61  l->addWidget(d->selectButton);
62  setFocusWidget(d->selectButton);
63 
64  connect(d->selectButton, SIGNAL(clicked()), this, SLOT(showEditor()));
65 }
66 
67 KPropertyStringListEditor::~KPropertyStringListEditor()
68 {
69  delete d;
70 }
71 
73 KPropertyStringListEditor::value() const
74 {
75  return d->list;
76 }
77 
78 void
79 KPropertyStringListEditor::setValue(const QVariant &value, bool emitChange)
80 {
81  d->list = value.toStringList();
82  d->edit->setText(value.toStringList().join(", "));
83  if (emitChange)
84  emit valueChanged(this);
85 }
86 
87 void
88 KPropertyStringListEditor::drawViewer(QPainter *p, const QColorGroup &cg, const QRect &r, const QVariant &value)
89 {
90  Widget::drawViewer(p, cg, r, value.toStringList().join(", "));
91 }
92 
93 void
94 KPropertyStringListEditor::showEditor()
95 {
96  QDialog dialog(this->topLevelWidget());
97  dialog.setWindowTitle(tr("Edit List of Items", "Window title"));
98  dialog.setObjectName("stringlist_dialog");
99  dialog.setButtons(QDialog::Ok | QDialog::Cancel);
100  dialog.setDefaultButton(QDialog::Ok);
101  dialog.setModal(false);
102  KEditListBox *edit = new KEditListBox(tr("Contents of %1").arg(property()->captionOrName()),
103  &dialog, "editlist");
104  // PORTING: Verify that widget was added to mainLayout: dialog.setMainWidget(edit);
105  // Add mainLayout->addWidget(edit); if necessary
106  edit->insertStringList(d->list);
107 
108  if (dialog.exec() == QDialog::Accepted) {
109  d->list = edit->items();
110  d->edit->setText(d->list.join(", "));
111  emit valueChanged(this);
112  }
113 }
114 
115 void
116 KPropertyStringListEditor::setReadOnlyInternal(bool readOnly)
117 {
118  d->selectButton->setEnabled(!readOnly);
119 }
QString join(const QString &separator) const const
void insertStringList(const QStringList &list, int index=-1)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
The base class representing a single property.
Definition: KProperty.h:95
QStringList items() const
QStringList toStringList() const const
void setMargin(int margin)
void setSpacing(int spacing)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Aug 11 2020 23:05:38 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.