Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
promotionmodel.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
6
7#include <QtDesigner/abstractwidgetdatabase.h>
8#include <QtDesigner/abstractpromotioninterface.h>
9#include <QtDesigner/abstractformeditor.h>
10
11#include <QtGui/qstandarditemmodel.h>
12#include <QtCore/qcoreapplication.h>
13
15
16namespace {
17 using StandardItemList = QList<QStandardItem *>;
18
19 // Model columns.
21
22 // Create a model row.
24 StandardItemList rc;
25 for (int i = 0; i < NumColumns; i++) {
26 rc.push_back(new QStandardItem());
27 }
28 return rc;
29 }
30
31 // Create a model row for a base class (read-only, cannot be selected).
33 StandardItemList rc = modelRow();
34
35 rc[ClassNameColumn]->setText(dbItem->name());
36 for (int i = 0; i < NumColumns; i++) {
37 rc[i]->setFlags(Qt::ItemIsEnabled);
38 }
39 return rc;
40 }
41
42 // Create an editable model row for a promoted class.
45 bool referenced)
46 {
47 qdesigner_internal::PromotionModel::ModelData data;
48 data.baseItem = baseItem;
49 data.promotedItem = dbItem;
50 data.referenced = referenced;
51
52 const QVariant userData = QVariant::fromValue(data);
53
54 StandardItemList rc = modelRow();
55 // name
56 rc[ClassNameColumn]->setText(dbItem->name());
57 rc[ClassNameColumn]->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable);
58 rc[ClassNameColumn]->setData(userData);
59 // header
60 const qdesigner_internal::IncludeSpecification spec = qdesigner_internal::includeSpecification(dbItem->includeFile());
61 rc[IncludeFileColumn]->setText(spec.first);
62 rc[IncludeFileColumn]->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable);
63 rc[IncludeFileColumn]->setData(userData);
64 // global include
65 rc[IncludeTypeColumn]->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable|Qt::ItemIsUserCheckable);
66 rc[IncludeTypeColumn]->setData(userData);
67 rc[IncludeTypeColumn]->setCheckState(spec.second == qdesigner_internal::IncludeGlobal ? Qt::Checked : Qt::Unchecked);
68 // referenced
69 rc[ReferencedColumn]->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
70 rc[ClassNameColumn]->setData(userData);
71 if (!referenced) {
72 //: Usage of promoted widgets
73 static const QString notUsed = QCoreApplication::translate("PromotionModel", "Not used");
74 rc[ReferencedColumn]->setText(notUsed);
75 }
76 return rc;
77 }
78}
79
80namespace qdesigner_internal {
81
82 PromotionModel::PromotionModel(QDesignerFormEditorInterface *core) :
83 m_core(core)
84 {
85 connect(this, &QStandardItemModel::itemChanged, this, &PromotionModel::slotItemChanged);
86 }
87
88 void PromotionModel::initializeHeaders() {
89 setColumnCount(NumColumns);
90 QStringList horizontalLabels(tr("Name"));
91 horizontalLabels += tr("Header file");
92 horizontalLabels += tr("Global include");
93 horizontalLabels += tr("Usage");
94 setHorizontalHeaderLabels (horizontalLabels);
95 }
96
98 using PromotedClasses = QDesignerPromotionInterface::PromotedClasses;
99
100 clear();
101 initializeHeaders();
102
103 // retrieve list of pairs from DB and convert into a tree structure.
104 // Set the item index as user data on the item.
105 const PromotedClasses promotedClasses = m_core->promotion()->promotedClasses();
106
107 if (promotedClasses.isEmpty())
108 return;
109
110 const QSet<QString> usedPromotedClasses = m_core->promotion()->referencedPromotedClassNames();
111
112 QDesignerWidgetDataBaseItemInterface *baseClass = nullptr;
113 QStandardItem *baseItem = nullptr;
114
115 for (auto &pi : promotedClasses) {
116 // Start a new base class?
117 if (baseClass != pi.baseItem) {
118 baseClass = pi.baseItem;
119 const StandardItemList baseRow = baseModelRow(pi.baseItem);
120 baseItem = baseRow.constFirst();
121 appendRow(baseRow);
122 }
123 Q_ASSERT(baseItem);
124 // Append derived
125 baseItem->appendRow(promotedModelRow(pi.baseItem, pi.promotedItem,
126 usedPromotedClasses.contains(pi.promotedItem->name())));
127 }
128 }
129
130 void PromotionModel::slotItemChanged(QStandardItem * changedItem) {
131 // Retrieve DB item
132 const ModelData data = modelData(changedItem);
133 Q_ASSERT(data.isValid());
134 QDesignerWidgetDataBaseItemInterface *dbItem = data.promotedItem;
135 // Change header or type
136 switch (changedItem->column()) {
137 case ClassNameColumn:
138 emit classNameChanged(dbItem, changedItem->text());
139 break;
141 case IncludeFileColumn: {
142 // Get both file and type items via parent.
143 const QStandardItem *baseClassItem = changedItem->parent();
144 const QStandardItem *fileItem = baseClassItem->child(changedItem->row(), IncludeFileColumn);
145 const QStandardItem *typeItem = baseClassItem->child(changedItem->row(), IncludeTypeColumn);
146 emit includeFileChanged(dbItem, buildIncludeFile(fileItem->text(), typeItem->checkState() == Qt::Checked ? IncludeGlobal : IncludeLocal));
147 }
148 break;
149 }
150 }
151
152 PromotionModel::ModelData PromotionModel::modelData(const QStandardItem *item) const
153 {
154 const QVariant userData = item->data();
155 return userData.canConvert<ModelData>() ? userData.value<ModelData>() : ModelData();
156 }
157
158 PromotionModel::ModelData PromotionModel::modelData(const QModelIndex &index) const
159 {
160 return index.isValid() ? modelData(itemFromIndex(index)) : ModelData();
161 }
162
163 QModelIndex PromotionModel::indexOfClass(const QString &className) const {
164 const StandardItemList matches = findItems (className, Qt::MatchFixedString|Qt::MatchCaseSensitive|Qt::MatchRecursive);
165 return matches.isEmpty() ? QModelIndex() : indexFromItem (matches.constFirst());
166 }
167} // namespace qdesigner_internal
168
169QT_END_NAMESPACE
The QDesignerWidgetDataBaseItemInterface class provides an interface that is used to access individua...
ModelData modelData(const QStandardItem *item) const
QModelIndex indexOfClass(const QString &className) const
PromotionModel(QDesignerFormEditorInterface *core)
ModelData modelData(const QModelIndex &index) const
Combined button and popup list for selecting options.
StandardItemList baseModelRow(const QDesignerWidgetDataBaseItemInterface *dbItem)
StandardItemList promotedModelRow(QDesignerWidgetDataBaseItemInterface *baseItem, QDesignerWidgetDataBaseItemInterface *dbItem, bool referenced)
StandardItemList modelRow()
Auxiliary methods to store/retrieve settings.