Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
stringlisteditor.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5#include <iconloader_p.h>
6#include <QtCore/qstringlistmodel.h>
7
9
10using namespace Qt::StringLiterals;
11
12namespace qdesigner_internal {
13
14StringListEditor::StringListEditor(QWidget *parent)
15 : QDialog(parent), m_model(new QStringListModel(this))
16{
17 setupUi(this);
18 listView->setModel(m_model);
19
20 connect(listView->selectionModel(),
21 &QItemSelectionModel::currentChanged,
22 this, &StringListEditor::currentIndexChanged);
23 connect(listView->itemDelegate(),
24 &QAbstractItemDelegate::closeEditor,
25 this, &StringListEditor::currentValueChanged);
26
27 connect(upButton, &QAbstractButton::clicked, this, &StringListEditor::upButtonClicked);
28 connect(downButton, &QAbstractButton::clicked, this, &StringListEditor::downButtonClicked);
29 connect(newButton, &QAbstractButton::clicked, this, &StringListEditor::newButtonClicked);
30 connect(deleteButton, &QAbstractButton::clicked, this, &StringListEditor::deleteButtonClicked);
31 connect(valueEdit, &QLineEdit::textEdited, this, &StringListEditor::valueEdited);
32
33 QIcon upIcon = createIconSet("up.png"_L1);
34 QIcon downIcon = createIconSet("down.png"_L1);
35 QIcon minusIcon = createIconSet("minus.png"_L1);
36 QIcon plusIcon = createIconSet("plus.png"_L1);
37 upButton->setIcon(upIcon);
38 downButton->setIcon(downIcon);
39 newButton->setIcon(plusIcon);
40 deleteButton->setIcon(minusIcon);
41
42 updateUi();
43}
44
45StringListEditor::~StringListEditor() = default;
46
47QStringList StringListEditor::getStringList(QWidget *parent, const QStringList &init, int *result)
48{
49 StringListEditor dlg(parent);
50 dlg.setStringList(init);
51 int res = dlg.exec();
52 if (result)
53 *result = res;
54 return (res == QDialog::Accepted) ? dlg.stringList() : init;
55}
56
57void StringListEditor::setStringList(const QStringList &stringList)
58{
59 m_model->setStringList(stringList);
60 updateUi();
61}
62
64{
65 return m_model->stringList();
66}
67
68void StringListEditor::currentIndexChanged(const QModelIndex &current, const QModelIndex &previous)
69{
70 Q_UNUSED(previous);
71 setCurrentIndex(current.row());
72 updateUi();
73}
74
75void StringListEditor::currentValueChanged()
76{
77 setCurrentIndex(currentIndex());
78 updateUi();
79}
80
81void StringListEditor::upButtonClicked()
82{
83 int from = currentIndex();
84 int to = currentIndex() - 1;
85 QString value = stringAt(from);
86 removeString(from);
87 insertString(to, value);
88 setCurrentIndex(to);
89 updateUi();
90}
91
92void StringListEditor::downButtonClicked()
93{
94 int from = currentIndex();
95 int to = currentIndex() + 1;
96 QString value = stringAt(from);
97 removeString(from);
98 insertString(to, value);
99 setCurrentIndex(to);
100 updateUi();
101}
102
103void StringListEditor::newButtonClicked()
104{
105 int to = currentIndex();
106 if (to == -1)
107 to = count() - 1;
108 ++to;
109 insertString(to, QString());
110 setCurrentIndex(to);
111 updateUi();
112 editString(to);
113}
114
115void StringListEditor::deleteButtonClicked()
116{
117 removeString(currentIndex());
118 setCurrentIndex(currentIndex());
119 updateUi();
120}
121
122void StringListEditor::valueEdited(const QString &text)
123{
124 setStringAt(currentIndex(), text);
125}
126
127void StringListEditor::updateUi()
128{
129 upButton->setEnabled((count() > 1) && (currentIndex() > 0));
130 downButton->setEnabled((count() > 1) && (currentIndex() >= 0) && (currentIndex() < (count() - 1)));
131 deleteButton->setEnabled(currentIndex() != -1);
132 valueEdit->setEnabled(currentIndex() != -1);
133}
134
135int StringListEditor::currentIndex() const
136{
137 return listView->currentIndex().row();
138}
139
140void StringListEditor::setCurrentIndex(int index)
141{
142 QModelIndex modelIndex = m_model->index(index, 0);
143 if (listView->currentIndex() != modelIndex)
144 listView->setCurrentIndex(modelIndex);
145 valueEdit->setText(stringAt(index));
146}
147
148int StringListEditor::count() const
149{
150 return m_model->rowCount();
151}
152
153QString StringListEditor::stringAt(int index) const
154{
155 return qvariant_cast<QString>(m_model->data(m_model->index(index, 0), Qt::DisplayRole));
156}
157
158void StringListEditor::setStringAt(int index, const QString &value)
159{
160 m_model->setData(m_model->index(index, 0), value);
161}
162
163void StringListEditor::removeString(int index)
164{
165 m_model->removeRows(index, 1);
166}
167
168void StringListEditor::insertString(int index, const QString &value)
169{
170 m_model->insertRows(index, 1);
171 m_model->setData(m_model->index(index, 0), value);
172}
173
174void StringListEditor::editString(int index)
175{
176 listView->edit(m_model->index(index, 0));
177}
178
179} // namespace qdesigner_internal
180
181QT_END_NAMESPACE
void setStringList(const QStringList &stringList)
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.