| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Designer of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include "stringlisteditor.h" |
| 30 | #include <iconloader_p.h> |
| 31 | #include <QtCore/qstringlistmodel.h> |
| 32 | |
| 33 | QT_BEGIN_NAMESPACE |
| 34 | |
| 35 | using namespace qdesigner_internal; |
| 36 | |
| 37 | StringListEditor::StringListEditor(QWidget *parent) |
| 38 | : QDialog(parent), m_model(new QStringListModel(this)) |
| 39 | { |
| 40 | setupUi(this); |
| 41 | setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); |
| 42 | listView->setModel(m_model); |
| 43 | |
| 44 | connect(sender: listView->selectionModel(), |
| 45 | signal: &QItemSelectionModel::currentChanged, |
| 46 | receiver: this, slot: &StringListEditor::currentIndexChanged); |
| 47 | connect(sender: listView->itemDelegate(), |
| 48 | signal: &QAbstractItemDelegate::closeEditor, |
| 49 | receiver: this, slot: &StringListEditor::currentValueChanged); |
| 50 | |
| 51 | QIcon upIcon = createIconSet(name: QString::fromUtf8(str: "up.png" )); |
| 52 | QIcon downIcon = createIconSet(name: QString::fromUtf8(str: "down.png" )); |
| 53 | QIcon minusIcon = createIconSet(name: QString::fromUtf8(str: "minus.png" )); |
| 54 | QIcon plusIcon = createIconSet(name: QString::fromUtf8(str: "plus.png" )); |
| 55 | upButton->setIcon(upIcon); |
| 56 | downButton->setIcon(downIcon); |
| 57 | newButton->setIcon(plusIcon); |
| 58 | deleteButton->setIcon(minusIcon); |
| 59 | |
| 60 | updateUi(); |
| 61 | } |
| 62 | |
| 63 | StringListEditor::~StringListEditor() = default; |
| 64 | |
| 65 | QStringList StringListEditor::getStringList(QWidget *parent, const QStringList &init, int *result) |
| 66 | { |
| 67 | StringListEditor dlg(parent); |
| 68 | dlg.setStringList(init); |
| 69 | int res = dlg.exec(); |
| 70 | if (result) |
| 71 | *result = res; |
| 72 | return (res == QDialog::Accepted) ? dlg.stringList() : init; |
| 73 | } |
| 74 | |
| 75 | void StringListEditor::setStringList(const QStringList &stringList) |
| 76 | { |
| 77 | m_model->setStringList(stringList); |
| 78 | updateUi(); |
| 79 | } |
| 80 | |
| 81 | QStringList StringListEditor::stringList() const |
| 82 | { |
| 83 | return m_model->stringList(); |
| 84 | } |
| 85 | |
| 86 | void StringListEditor::currentIndexChanged(const QModelIndex ¤t, const QModelIndex &previous) |
| 87 | { |
| 88 | Q_UNUSED(previous); |
| 89 | setCurrentIndex(current.row()); |
| 90 | updateUi(); |
| 91 | } |
| 92 | |
| 93 | void StringListEditor::currentValueChanged() |
| 94 | { |
| 95 | setCurrentIndex(currentIndex()); |
| 96 | updateUi(); |
| 97 | } |
| 98 | |
| 99 | void StringListEditor::on_upButton_clicked() |
| 100 | { |
| 101 | int from = currentIndex(); |
| 102 | int to = currentIndex() - 1; |
| 103 | QString value = stringAt(index: from); |
| 104 | removeString(index: from); |
| 105 | insertString(index: to, value); |
| 106 | setCurrentIndex(to); |
| 107 | updateUi(); |
| 108 | } |
| 109 | |
| 110 | void StringListEditor::on_downButton_clicked() |
| 111 | { |
| 112 | int from = currentIndex(); |
| 113 | int to = currentIndex() + 1; |
| 114 | QString value = stringAt(index: from); |
| 115 | removeString(index: from); |
| 116 | insertString(index: to, value); |
| 117 | setCurrentIndex(to); |
| 118 | updateUi(); |
| 119 | } |
| 120 | |
| 121 | void StringListEditor::on_newButton_clicked() |
| 122 | { |
| 123 | int to = currentIndex(); |
| 124 | if (to == -1) |
| 125 | to = count() - 1; |
| 126 | ++to; |
| 127 | insertString(index: to, value: QString()); |
| 128 | setCurrentIndex(to); |
| 129 | updateUi(); |
| 130 | editString(index: to); |
| 131 | } |
| 132 | |
| 133 | void StringListEditor::on_deleteButton_clicked() |
| 134 | { |
| 135 | removeString(index: currentIndex()); |
| 136 | setCurrentIndex(currentIndex()); |
| 137 | updateUi(); |
| 138 | } |
| 139 | |
| 140 | void StringListEditor::on_valueEdit_textEdited(const QString &text) |
| 141 | { |
| 142 | setStringAt(index: currentIndex(), value: text); |
| 143 | } |
| 144 | |
| 145 | void StringListEditor::updateUi() |
| 146 | { |
| 147 | upButton->setEnabled((count() > 1) && (currentIndex() > 0)); |
| 148 | downButton->setEnabled((count() > 1) && (currentIndex() >= 0) && (currentIndex() < (count() - 1))); |
| 149 | deleteButton->setEnabled(currentIndex() != -1); |
| 150 | valueEdit->setEnabled(currentIndex() != -1); |
| 151 | } |
| 152 | |
| 153 | int StringListEditor::currentIndex() const |
| 154 | { |
| 155 | return listView->currentIndex().row(); |
| 156 | } |
| 157 | |
| 158 | void StringListEditor::setCurrentIndex(int index) |
| 159 | { |
| 160 | QModelIndex modelIndex = m_model->index(row: index, column: 0); |
| 161 | if (listView->currentIndex() != modelIndex) |
| 162 | listView->setCurrentIndex(modelIndex); |
| 163 | valueEdit->setText(stringAt(index)); |
| 164 | } |
| 165 | |
| 166 | int StringListEditor::count() const |
| 167 | { |
| 168 | return m_model->rowCount(); |
| 169 | } |
| 170 | |
| 171 | QString StringListEditor::stringAt(int index) const |
| 172 | { |
| 173 | return qvariant_cast<QString>(v: m_model->data(index: m_model->index(row: index, column: 0), role: Qt::DisplayRole)); |
| 174 | } |
| 175 | |
| 176 | void StringListEditor::setStringAt(int index, const QString &value) |
| 177 | { |
| 178 | m_model->setData(index: m_model->index(row: index, column: 0), value); |
| 179 | } |
| 180 | |
| 181 | void StringListEditor::removeString(int index) |
| 182 | { |
| 183 | m_model->removeRows(row: index, count: 1); |
| 184 | } |
| 185 | |
| 186 | void StringListEditor::insertString(int index, const QString &value) |
| 187 | { |
| 188 | m_model->insertRows(row: index, count: 1); |
| 189 | m_model->setData(index: m_model->index(row: index, column: 0), value); |
| 190 | } |
| 191 | |
| 192 | void StringListEditor::editString(int index) |
| 193 | { |
| 194 | listView->edit(index: m_model->index(row: index, column: 0)); |
| 195 | } |
| 196 | |
| 197 | QT_END_NAMESPACE |
| 198 | |