Krita Source Code Documentation
Loading...
Searching...
No Matches
KisDlgListPicker.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "KisDlgListPicker.h"
8#include "ui_KisDlgListPicker.h"
9#include <kstandardguiitem.h>
10
12{
13
14};
15
17 : QListWidgetItem(text)
18{
19}
20
21bool KisDlgListPickerItem::operator<(const QListWidgetItem &other) const
22{
23 auto data = this->data(Qt::UserRole);
24 auto other_data = other.data(Qt::UserRole);
25
26#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
27 if (data.type() != QVariant::String || other_data.type() != QVariant::String)
28#else
29 if (data.metaType().id() != QMetaType::QString || other_data.metaType().id() != QMetaType::QString)
30#endif
31 return false;
32
33 return data.toString() < other_data.toString();
34}
35
36KisDlgListPicker::KisDlgListPicker(QString windowTitle, QString availableString, QString currentString,
37 QList<QString> available, QList<QVariant> availableData, QList<QIcon> availableIcons,
38 QList<QString> chosen, QList<QVariant> chosenData, QList<QIcon> chosenIcons,
39 QWidget *parent, bool useDataSorting
40 ) :
41 QDialog(parent),
42 ui(new Ui::KisDlgListPicker),
43 m_d(new Private)
44{
45 ui->setupUi(this);
46
47 this->setWindowTitle(windowTitle);
48 ui->label->setText(availableString);
49 ui->label_2->setText(currentString);
50
51 KGuiItem::assign(ui->btnBox->button(QDialogButtonBox::Ok), KStandardGuiItem::ok());
52 KGuiItem::assign(ui->btnBox->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
53
54 int i = 0;
55 Q_FOREACH (QString name, available) {
56 QListWidgetItem *item;
57
58 if (useDataSorting) {
59 item = new KisDlgListPickerItem(name);
60 } else {
61 item = new QListWidgetItem(name);
62 }
63
64 // Set icons only if icons are provided
65 if (availableIcons.length() == available.length()) {
66 item->setIcon(availableIcons[i]);
67 }
68
69 item->setData(Qt::UserRole, availableData[i]);
70 ui->lstAvailable->addItem(item);
71 i++;
72 }
73
74 i = 0;
75 Q_FOREACH (QString name, chosen) {
76 QListWidgetItem *item;
77
78 if (useDataSorting) {
79 item = new KisDlgListPickerItem(name);
80 } else {
81 item = new QListWidgetItem(name);
82 }
83
84 // Set icons only if icons are provided
85 if (chosenIcons.length() == chosen.length()) {
86 item->setIcon(chosenIcons[i]);
87 }
88
89 item->setData(Qt::UserRole, chosenData[i]);
90 ui->lstCurrent->addItem(item);
91 i++;
92 }
93
94 ui->lstAvailable->sortItems(); // sort alphabetically by name
95
96 connect(ui->btnAdd, SIGNAL(clicked()), SLOT(slotMoveRight()));
97 connect(ui->btnRemove, SIGNAL(clicked()), SLOT(slotMoveLeft()));
98
99 connect(ui->btnDown, SIGNAL(clicked()), SLOT(slotMoveDown()));
100 connect(ui->btnUp, SIGNAL(clicked()), SLOT(slotMoveUp()));
101}
102
107
109{
110 ui->btnUp->setEnabled(enable);
111 ui->btnUp->setVisible(enable);
112 ui->btnDown->setEnabled(enable);
113 ui->btnDown->setVisible(enable);
114}
115
117{
118 ui->lstAvailable->setSortingEnabled(enable);
119 ui->lstCurrent->setSortingEnabled(enable);
120}
121
123{
124 QList<QVariant> chosenData;
125 for (int i = 0; i < ui->lstCurrent->count(); i++) {
126 chosenData.append(ui->lstCurrent->item(i)->data(Qt::UserRole));
127 }
128 return chosenData;
129}
130
132{
133 QListWidgetItem *item = ui->lstAvailable->currentItem();
134 if (!item) return;
135
136 const int prevPosition = ui->lstAvailable->row(item) - 1;
137 const int newPosition = ui->lstCurrent->currentRow() + 1;
138
139 ui->lstAvailable->takeItem(ui->lstAvailable->row(item));
140 ui->lstAvailable->setCurrentRow(qMax(0, prevPosition));
141 ui->lstCurrent->insertItem(newPosition, item);
142 ui->lstCurrent->setCurrentItem(item);
143}
144
146{
147 QListWidgetItem *item = ui->lstCurrent->currentItem();
148 if (!item) return;
149
150 const int prevPosition = ui->lstCurrent->row(item) - 1;
151 const int newPosition = ui->lstAvailable->currentRow() + 1;
152
153 ui->lstCurrent->takeItem(ui->lstCurrent->row(item));
154 ui->lstCurrent->setCurrentRow(qMax(0, prevPosition));
155 ui->lstAvailable->insertItem(newPosition, item);
156 ui->lstAvailable->setCurrentItem(item);
157}
158
160{
161 QListWidgetItem *item = ui->lstCurrent->currentItem();
162 if (!item) return;
163
164 int position = ui->lstCurrent->row(item);
165
166 if (position <= 0) return;
167
168 ui->lstCurrent->takeItem(ui->lstCurrent->row(item));
169 ui->lstCurrent->insertItem(position - 1, item);
170 ui->lstCurrent->setCurrentItem(item);
171}
172
174{
175 QListWidgetItem *item = ui->lstCurrent->currentItem();
176 if (!item) return;
177
178 int position = ui->lstCurrent->row(item);
179
180 if (position >= ui->lstCurrent->count() - 1) return;
181
182 ui->lstCurrent->takeItem(ui->lstCurrent->row(item));
183 ui->lstCurrent->insertItem(position + 1, item);
184 ui->lstCurrent->setCurrentItem(item);
185}
186
bool operator<(const QListWidgetItem &other) const override
KisDlgListPickerItem(const QString &text)
void enable_vertical(bool enable)
Enables/Disables vertical move buttons.
void enable_sorting(bool enable)
Enables/Disables sorting of items.
KisDlgListPicker(QString windowTitle, QString availableString, QString currentString, QList< QString > available, QList< QVariant > availableData, QList< QIcon > availableIcons, QList< QString > chosen, QList< QVariant > currentData, QList< QIcon > currentIcons, QWidget *parent=0, bool useaDataSorting=false)
QList< QVariant > getChosenData()
Ui::KisDlgListPicker * ui