-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathFileTypesDelegate.cpp
116 lines (95 loc) · 4.09 KB
/
FileTypesDelegate.cpp
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
110
111
112
113
114
115
116
//-----------------------------------------------------------------------------
// File: FileTypesDelegate.cpp
//-----------------------------------------------------------------------------
// Project: Kactus 2
// Author: Joni-Matti Maatta
// Date: 20.02.2013
//
// Description:
// Delegate which provides widgets for the file types settings view.
//-----------------------------------------------------------------------------
#include "FileTypesDelegate.h"
#include "FileTypeColumns.h"
#include <IPXACTmodels/common/FileType.h>
#include <QComboBox>
#include <QLineEdit>
#include <QStringList>
//-----------------------------------------------------------------------------
// Function: FileTypesDelegate::FileTypesDelegate()
//-----------------------------------------------------------------------------
FileTypesDelegate::FileTypesDelegate(QObject* parent):
QStyledItemDelegate(parent)
{
}
//-----------------------------------------------------------------------------
// Function: FileTypesDelegate::createEditor()
//-----------------------------------------------------------------------------
QWidget* FileTypesDelegate::createEditor(QWidget* parent, QStyleOptionViewItem const& option, QModelIndex const& index) const
{
if (index.column() == FileTypeColumns::NAME)
{
QComboBox* combo = new QComboBox(parent);
// Get all file types that have been defined already.
QStringList usedTypes = index.model()->data(index, Qt::UserRole).toStringList();
usedTypes.append(FileTypes::FILE_TYPES);
usedTypes.append(FileTypes::FILE_TYPES_2022);
usedTypes.removeDuplicates();
combo->addItems(usedTypes);
combo->setEditable(true);
return combo;
}
else if (index.column() == FileTypeColumns::EXTENSIONS)
{
QLineEdit* line = new QLineEdit(parent);
return line;
}
return QStyledItemDelegate::createEditor(parent, option, index);
}
//-----------------------------------------------------------------------------
// Function: FileTypesDelegate::setEditorData()
//-----------------------------------------------------------------------------
void FileTypesDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
if (index.column() == FileTypeColumns::NAME)
{
QComboBox* combo = qobject_cast<QComboBox*>(editor);
Q_ASSERT_X(combo, "FileTypesDelegate::setEditorData", "Type conversion failed for QComboBox");
QString text = index.model()->data(index, Qt::DisplayRole).toString();
combo->setEditText(text);
}
else if (index.column() == FileTypeColumns::EXTENSIONS)
{
QLineEdit* line = qobject_cast<QLineEdit*>(editor);
Q_ASSERT_X(line, "FileTypesDelegate::setEditorData", "Type conversion failed for QLineEdit");
QString text = index.model()->data(index, Qt::DisplayRole).toString();
line->setText(text);
}
else
{
QStyledItemDelegate::setEditorData(editor, index);
}
}
//-----------------------------------------------------------------------------
// Function: FileTypesDelegate::setModelData()
//-----------------------------------------------------------------------------
void FileTypesDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, QModelIndex const& index) const
{
if (index.column() == FileTypeColumns::NAME)
{
QComboBox* combo = qobject_cast<QComboBox*>(editor);
Q_ASSERT_X(combo, "FileTypesDelegate::setEditorData", "Type conversion failed for QComboBox");
QString text = combo->currentText();
model->setData(index, text, Qt::EditRole);
}
else if (index.column() == FileTypeColumns::EXTENSIONS)
{
QLineEdit* line = qobject_cast<QLineEdit*>(editor);
Q_ASSERT_X(line, "FileTypesDelegate::setEditorData", "Type conversion failed for QLineEdit");
QString text = line->text();
model->setData(index, text, Qt::EditRole);
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}