Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_cmb_idlist.cc
Go to the documentation of this file.
1/*
2 * kis_cmb_idlist.cc - part of KImageShop/Krayon/Krita
3 *
4 * SPDX-FileCopyrightText: 2005 Boudewijn Rempt (boud@valdyas.org)
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
10
11#include <klocalizedstring.h>
12#include <kis_debug.h>
13#include <KoID.h>
14
15KisCmbIDList::KisCmbIDList(QWidget * parent, const char * name)
16 : QComboBox(parent)
17{
18 setObjectName(name);
19 setEditable(false);
20 connect(this, SIGNAL(activated(int)), this, SLOT(slotIDActivated(int)));
21 connect(this, SIGNAL(highlighted(int)), this, SLOT(slotIDHighlighted(int)));
22}
23
27
28const KoID KisCmbIDList::AutoOptionID = KoID("AUTO", ki18nc("Automatic", "Auto"));
29
30void KisCmbIDList::setIDList(const QList<KoID> & list, bool sorted)
31{
32 m_idList = list;
33 m_sorted = sorted;
34
35 buildItems();
36}
37
38void KisCmbIDList::allowAuto(bool setAuto)
39{
40 m_autoOption = setAuto;
41
42 buildItems();
43}
44
45void KisCmbIDList::setAutoHint(const QString & hint)
46{
47 m_autoHint = hint;
48
49 buildItems();
50}
51
53{
54 qint32 index = QComboBox::currentIndex();
55
56 if (index > m_idList.count() - 1 || index < 0) return KoID();
57
58 return m_idList[index];
59}
60
62{
63 qint32 index = m_idList.indexOf(id);
64
65 if (index >= 0) {
66 QComboBox::setCurrentIndex(index);
67 } else if (id != KoID()) {
68 m_idList.push_back(id);
69 buildItems();
70 QComboBox::setCurrentIndex(m_idList.indexOf(id));
71 }
72}
73
74void KisCmbIDList::setCurrent(const QString & id)
75{
76 for (qint32 index = 0; index < m_idList.count(); ++index) {
77 if (m_idList.at(index).id() == id) {
78 QComboBox::setCurrentIndex(index);
79 break;
80 }
81 }
82}
83
85{
86 if (index > m_idList.count() - 1 || index < 0) return;
87
88 Q_EMIT activated(m_idList[index]);
89}
90
92{
93 if (index > m_idList.count() - 1 || index < 0) return;
94
95 Q_EMIT highlighted(m_idList[index]);
96}
97
99{
100 const KoID selectedID = currentItem();
101
102 clear();
103
104 // m_idList has auto, remove it temporarily.
105 if (m_idList.contains(AutoOptionID)) {
106 m_idList.removeAll(AutoOptionID);
107 }
108
109 // Sort id list..
110 if (m_sorted) {
111 std::sort(m_idList.begin(), m_idList.end(), KoID::compareNames);
112 }
113
114 // Re-add "Auto" option to front of list if auto enabled..
115 if (m_autoOption) {
116 m_idList.insert(0, AutoOptionID);
117 }
118
119 // Populate GUI item list..
120 for (qint32 i = 0; i < m_idList.count(); ++i) {
121 const KoID &id = m_idList.at(i);
122 if (id == AutoOptionID && !m_autoHint.isEmpty()) {
123 addItem(QString("%1 (%2)").arg(id.name(), m_autoHint));
124 } else {
125 addItem(m_idList.at(i).name());
126 }
127 }
128
129 setCurrent(selectedID);
130}
131
132
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
KisCmbIDList(QWidget *parent=0, const char *name=0)
~KisCmbIDList() override
KoID currentItem() const
void slotIDActivated(int index)
void setAutoHint(const QString &hint)
void slotIDHighlighted(int index)
void setIDList(const QList< KoID > &list, bool sorted=true)
setIDList clears the combobox and sorts the given list by user-visible name and then adds the items t...
QString m_autoHint
void activated(const KoID &)
void allowAuto(bool setAuto=true)
allowAuto sets whether the combobox should keep an extra "AUTO" option, where the use allows the prog...
static const KoID AutoOptionID
QList< KoID > m_idList
void highlighted(const KoID &)
void setCurrent(const KoID id)
Definition KoID.h:30
static bool compareNames(const KoID &id1, const KoID &id2)
Definition KoID.h:87