Changeset View
Changeset View
Standalone View
Standalone View
plugins/meson/settings/mesonlisteditor.cpp
- This file was added.
1 | /* This file is part of KDevelop | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Daniel Mensinger <daniel@mensinger-ka.de> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "mesonlisteditor.h" | ||||
21 | #include "ui_mesonlisteditor.h" | ||||
22 | | ||||
23 | QListWidgetItem* genItem(QString const& label) | ||||
24 | { | ||||
25 | QListWidgetItem* item = new QListWidgetItem(label); | ||||
26 | item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled); | ||||
27 | return item; | ||||
28 | } | ||||
29 | | ||||
30 | MesonListEditor::MesonListEditor(QStringList content, QWidget* parent) | ||||
31 | : QDialog(parent) | ||||
32 | { | ||||
33 | m_ui = new Ui::MesonListEditor; | ||||
34 | m_ui->setupUi(this); | ||||
35 | | ||||
36 | for (auto const& i : content) { | ||||
37 | m_ui->array->addItem(genItem(i)); | ||||
38 | } | ||||
39 | | ||||
40 | currentItemChanged(); | ||||
41 | } | ||||
42 | | ||||
43 | MesonListEditor::~MesonListEditor() | ||||
44 | { | ||||
45 | if (m_ui) { | ||||
46 | delete m_ui; | ||||
47 | } | ||||
48 | } | ||||
49 | | ||||
50 | QStringList MesonListEditor::content() const | ||||
51 | { | ||||
52 | QStringList content; | ||||
53 | content.reserve(m_ui->array->count()); | ||||
54 | for (int i = 0; i < m_ui->array->count(); ++i) { | ||||
55 | content << m_ui->array->item(i)->text(); | ||||
56 | } | ||||
57 | return content; | ||||
58 | } | ||||
59 | | ||||
60 | void MesonListEditor::add() | ||||
61 | { | ||||
62 | auto* item = genItem(i18n("<NEW>")); | ||||
63 | m_ui->array->addItem(item); | ||||
64 | m_ui->array->setCurrentItem(item); | ||||
65 | m_ui->array->editItem(item); | ||||
66 | } | ||||
67 | | ||||
68 | void MesonListEditor::moveItem(int src, int dst) | ||||
69 | { | ||||
70 | auto* item = m_ui->array->takeItem(src); | ||||
71 | if (!item) { | ||||
72 | return; | ||||
73 | } | ||||
74 | | ||||
75 | m_ui->array->insertItem(dst, item); | ||||
76 | m_ui->array->setCurrentItem(item); | ||||
77 | } | ||||
78 | | ||||
79 | void MesonListEditor::remove() | ||||
80 | { | ||||
81 | qDeleteAll(m_ui->array->selectedItems()); | ||||
82 | } | ||||
83 | | ||||
84 | void MesonListEditor::first() | ||||
85 | { | ||||
86 | int row = m_ui->array->currentRow(); | ||||
87 | moveItem(row, 0); | ||||
88 | } | ||||
89 | | ||||
90 | void MesonListEditor::up() | ||||
91 | { | ||||
92 | int row = m_ui->array->currentRow(); | ||||
93 | moveItem(row, row - 1); | ||||
94 | } | ||||
95 | | ||||
96 | void MesonListEditor::down() | ||||
97 | { | ||||
98 | int row = m_ui->array->currentRow(); | ||||
99 | moveItem(row, row + 1); | ||||
100 | } | ||||
101 | | ||||
102 | void MesonListEditor::last() | ||||
103 | { | ||||
104 | int row = m_ui->array->currentRow(); | ||||
105 | moveItem(row, m_ui->array->count() - 1); | ||||
106 | } | ||||
107 | | ||||
108 | void MesonListEditor::currentItemChanged() | ||||
109 | { | ||||
110 | auto* current = m_ui->array->currentItem(); | ||||
111 | if (!current || m_ui->array->count() == 0) { | ||||
112 | m_ui->b_first->setDisabled(true); | ||||
113 | m_ui->b_up->setDisabled(true); | ||||
114 | m_ui->b_down->setDisabled(true); | ||||
115 | m_ui->b_last->setDisabled(true); | ||||
116 | m_ui->b_del->setDisabled(true); | ||||
117 | return; | ||||
118 | } | ||||
119 | | ||||
120 | int row = m_ui->array->row(current); | ||||
121 | | ||||
122 | m_ui->b_del->setDisabled(false); | ||||
123 | | ||||
124 | if (m_ui->array->count() < 2) { | ||||
125 | m_ui->b_first->setDisabled(true); | ||||
126 | m_ui->b_up->setDisabled(true); | ||||
127 | m_ui->b_down->setDisabled(true); | ||||
128 | m_ui->b_last->setDisabled(true); | ||||
129 | } else if (row == 0) { | ||||
130 | m_ui->b_first->setDisabled(true); | ||||
131 | m_ui->b_up->setDisabled(true); | ||||
132 | m_ui->b_down->setDisabled(false); | ||||
133 | m_ui->b_last->setDisabled(false); | ||||
134 | } else if (row >= m_ui->array->count() - 1) { | ||||
135 | m_ui->b_first->setDisabled(false); | ||||
136 | m_ui->b_up->setDisabled(false); | ||||
137 | m_ui->b_down->setDisabled(true); | ||||
138 | m_ui->b_last->setDisabled(true); | ||||
139 | } else { | ||||
140 | m_ui->b_first->setDisabled(false); | ||||
141 | m_ui->b_up->setDisabled(false); | ||||
142 | m_ui->b_down->setDisabled(false); | ||||
143 | m_ui->b_last->setDisabled(false); | ||||
144 | } | ||||
145 | } |