This repository was archived by the owner on Sep 5, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsettingsdelegate.cpp
More file actions
109 lines (92 loc) · 3.35 KB
/
Copy pathsettingsdelegate.cpp
File metadata and controls
109 lines (92 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**************************************************************************
RCExportCG
Copyright (C) 2017 Peter Simonsson <peter.simonsson@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#include "settingsdelegate.h"
#include <QComboBox>
#include <QCollator>
SettingsDelegate::SettingsDelegate(QWidget *parent) :
QStyledItemDelegate(parent)
{
}
QWidget *SettingsDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(index.column() == 0)
{
QComboBox *editor = new QComboBox(parent);
editor->setEditable(true);
editor->addItems(m_objects);
return editor;
}
else if(index.column() == 1)
{
QComboBox *editor = new QComboBox(parent);
editor->addItems(m_presets);
connect(editor, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &SettingsDelegate::commitAndCloseEditor);
return editor;
}
return QStyledItemDelegate::createEditor(parent, option, index);
}
void SettingsDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if(index.column() == 0 || index.column() == 1)
{
QComboBox *combo = qobject_cast<QComboBox *>(editor);
combo->setCurrentText(index.data().toString());
}
else
{
QStyledItemDelegate::setEditorData(editor, index);
}
}
void SettingsDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
if(index.column() == 0 || index.column() == 1)
{
QComboBox *combo = qobject_cast<QComboBox *>(editor);
model->setData(index, combo->currentText());
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void SettingsDelegate::commitAndCloseEditor()
{
QComboBox *editor = qobject_cast<QComboBox *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
void SettingsDelegate::setPresets(const QStringList &presets)
{
m_presets = presets;
QCollator collator;
collator.setNumericMode(true);
std::sort(m_presets.begin(), m_presets.end(),
[&collator](const QString &preset1, const QString &preset2) {
return collator.compare(preset1, preset2) < 0;
});
}
void SettingsDelegate::setObjects(const QStringList &objects)
{
m_objects = objects;
QCollator collator;
collator.setNumericMode(true);
std::sort(m_objects.begin(), m_objects.end(),
[&collator](const QString &object1, const QString &object2) {
return collator.compare(object1, object2) < 0;
});
}