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
resourcebuilder.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6#include "ui4_p.h"
7#include <QtCore/qvariant.h>
8#include <QtCore/qfileinfo.h>
9#include <QtCore/qdir.h>
10#include <QtCore/qdebug.h>
11#include <QtGui/qpixmap.h>
12#include <QtGui/qicon.h>
13
15
16using namespace Qt::StringLiterals;
17
18#ifdef QFORMINTERNAL_NAMESPACE
19namespace QFormInternal {
20#endif
21
22enum { themeDebug = 0 };
23
24QResourceBuilder::QResourceBuilder() = default;
25
26QResourceBuilder::~QResourceBuilder() = default;
27
28int QResourceBuilder::iconStateFlags(const DomResourceIcon *dpi)
29{
30 int rc = 0;
31 // Fix form files broken by QTBUG-115465
32 if (dpi->hasElementNormalOff() && dpi->elementNormalOff()->text() != "."_L1)
33 rc |= NormalOff;
34 if (dpi->hasElementNormalOn())
35 rc |= NormalOn;
36 if (dpi->hasElementDisabledOff())
37 rc |= DisabledOff;
38 if (dpi->hasElementDisabledOn())
39 rc |= DisabledOn;
40 if (dpi->hasElementActiveOff())
41 rc |= ActiveOff;
42 if (dpi->hasElementActiveOn())
43 rc |= ActiveOn;
44 if (dpi->hasElementSelectedOff())
45 rc |= SelectedOff;
46 if (dpi->hasElementSelectedOn())
47 rc |= SelectedOn;
48 return rc;
49}
50
51QVariant QResourceBuilder::loadResource(const QDir &workingDirectory, const DomProperty *property) const
52{
53 switch (property->kind()) {
54 case DomProperty::Pixmap: {
55 const DomResourcePixmap *dpx = property->elementPixmap();
56 QPixmap pixmap(QFileInfo(workingDirectory, dpx->text()).absoluteFilePath());
57 return QVariant::fromValue(pixmap);
58 }
59 case DomProperty::IconSet: {
60 const DomResourceIcon *dpi = property->elementIconSet();
61 if (!dpi->attributeTheme().isEmpty()) {
62 const QString theme = dpi->attributeTheme();
63 const qsizetype themeEnum = theme.at(0).isUpper()
64 ? themeIconNames().indexOf(theme) : -1;
65 if (themeEnum != -1) {
66 const auto themeEnumE = static_cast<QIcon::ThemeIcon>(themeEnum);
67 return QVariant::fromValue(QIcon::fromTheme(themeEnumE));
68 }
69 const bool known = QIcon::hasThemeIcon(theme);
70 if (themeDebug)
71 qDebug("Theme %s known %d", qPrintable(theme), known);
72 if (known)
73 return QVariant::fromValue(QIcon::fromTheme(theme));
74 } // non-empty theme
75 if (const int flags = iconStateFlags(dpi)) { // new, post 4.4 format
76 QIcon icon;
77 if (flags & NormalOff)
78 icon.addFile(QFileInfo(workingDirectory, dpi->elementNormalOff()->text()).absoluteFilePath(), QSize(), QIcon::Normal, QIcon::Off);
79 if (flags & NormalOn)
80 icon.addFile(QFileInfo(workingDirectory, dpi->elementNormalOn()->text()).absoluteFilePath(), QSize(), QIcon::Normal, QIcon::On);
81 if (flags & DisabledOff)
82 icon.addFile(QFileInfo(workingDirectory, dpi->elementDisabledOff()->text()).absoluteFilePath(), QSize(), QIcon::Disabled, QIcon::Off);
83 if (flags & DisabledOn)
84 icon.addFile(QFileInfo(workingDirectory, dpi->elementDisabledOn()->text()).absoluteFilePath(), QSize(), QIcon::Disabled, QIcon::On);
85 if (flags & ActiveOff)
86 icon.addFile(QFileInfo(workingDirectory, dpi->elementActiveOff()->text()).absoluteFilePath(), QSize(), QIcon::Active, QIcon::Off);
87 if (flags & ActiveOn)
88 icon.addFile(QFileInfo(workingDirectory, dpi->elementActiveOn()->text()).absoluteFilePath(), QSize(), QIcon::Active, QIcon::On);
89 if (flags & SelectedOff)
90 icon.addFile(QFileInfo(workingDirectory, dpi->elementSelectedOff()->text()).absoluteFilePath(), QSize(), QIcon::Selected, QIcon::Off);
91 if (flags & SelectedOn)
92 icon.addFile(QFileInfo(workingDirectory, dpi->elementSelectedOn()->text()).absoluteFilePath(), QSize(), QIcon::Selected, QIcon::On);
93 return QVariant::fromValue(icon);
94 }
95 // 4.3 legacy
96 const QIcon icon(QFileInfo(workingDirectory, dpi->text()).absoluteFilePath());
97 return QVariant::fromValue(icon);
98 }
99 break;
100 default:
101 break;
102 }
103 return QVariant();
104}
105
106QVariant QResourceBuilder::toNativeValue(const QVariant &value) const
107{
108 return value;
109}
110
111DomProperty *QResourceBuilder::saveResource(const QDir &workingDirectory, const QVariant &value) const
112{
113 Q_UNUSED(workingDirectory);
114 Q_UNUSED(value);
115 return nullptr;
116}
117
118bool QResourceBuilder::isResourceProperty(const DomProperty *p) const
119{
120 switch (p->kind()) {
121 case DomProperty::Pixmap:
122 case DomProperty::IconSet:
123 return true;
124 default:
125 break;
126 }
127 return false;
128}
129
130bool QResourceBuilder::isResourceType(const QVariant &value) const
131{
132 switch (value.metaType().id()) {
133 case QMetaType::QPixmap:
134 case QMetaType::QIcon:
135 return true;
136 default:
137 break;
138 }
139 return false;
140}
141
142const QStringList &QResourceBuilder::themeIconNames()
143{
144 static const QStringList result = {
145 "AddressBookNew"_L1, "ApplicationExit"_L1, "AppointmentNew"_L1,
146 "CallStart"_L1, "CallStop"_L1, "ContactNew"_L1,
147 "DocumentNew"_L1, "DocumentOpen"_L1, "DocumentOpenRecent"_L1,
148 "DocumentPageSetup"_L1, "DocumentPrint"_L1, "DocumentPrintPreview"_L1,
149 "DocumentProperties"_L1, "DocumentRevert"_L1, "DocumentSave"_L1,
150 "DocumentSaveAs"_L1, "DocumentSend"_L1,
151 "EditClear"_L1, "EditCopy"_L1, "EditCut"_L1, "EditDelete"_L1,
152 "EditFind"_L1, "EditPaste"_L1,
153 "EditRedo"_L1, "EditSelectAll"_L1, "EditUndo"_L1,
154 "FolderNew"_L1,
155 "FormatIndentLess"_L1, "FormatIndentMore"_L1,
156 "FormatJustifyCenter"_L1, "FormatJustifyFill"_L1,
157 "FormatJustifyLeft"_L1, "FormatJustifyRight"_L1,
158 "FormatTextDirectionLtr"_L1, "FormatTextDirectionRtl"_L1,
159 "FormatTextBold"_L1, "FormatTextItalic"_L1,
160 "FormatTextUnderline"_L1, "FormatTextStrikethrough"_L1,
161 "GoDown"_L1, "GoHome"_L1, "GoNext"_L1, "GoPrevious"_L1, "GoUp"_L1,
162 "HelpAbout"_L1, "HelpFaq"_L1,
163 "InsertImage"_L1, "InsertLink"_L1, "InsertText"_L1,
164 "ListAdd"_L1, "ListRemove"_L1,
165 "MailForward"_L1, "MailMarkImportant"_L1, "MailMarkRead"_L1, "MailMarkUnread"_L1,
166 "MailMessageNew"_L1, "MailReplyAll"_L1, "MailReplySender"_L1,
167 "MailSend"_L1,
168 "MediaEject"_L1, "MediaPlaybackPause"_L1, "MediaPlaybackStart"_L1,
169 "MediaPlaybackStop"_L1, "MediaRecord"_L1, "MediaSeekBackward"_L1,
170 "MediaSeekForward"_L1, "MediaSkipBackward"_L1,
171 "MediaSkipForward"_L1,
172 "ObjectRotateLeft"_L1, "ObjectRotateRight"_L1,
173 "ProcessStop"_L1,
174 "SystemLockScreen"_L1, "SystemLogOut"_L1,
175 "SystemSearch"_L1, "SystemReboot"_L1, "SystemShutdown"_L1,
176 "ToolsCheckSpelling"_L1,
177 "ViewFullscreen"_L1, "ViewRefresh"_L1, "ViewRestore"_L1,
178 "WindowClose"_L1, "WindowNew"_L1,
179 "ZoomFitBest"_L1, "ZoomIn"_L1, "ZoomOut"_L1,
180 "AudioCard"_L1, "AudioInputMicrophone"_L1,
181 "Battery"_L1,
182 "CameraPhoto"_L1, "CameraVideo"_L1, "CameraWeb"_L1,
183 "Computer"_L1, "DriveHarddisk"_L1, "DriveOptical"_L1,
184 "InputGaming"_L1, "InputKeyboard"_L1, "InputMouse"_L1,
185 "InputTablet"_L1,
186 "MediaFlash"_L1, "MediaOptical"_L1,
187 "MediaTape"_L1,
188 "MultimediaPlayer"_L1,
189 "NetworkWired"_L1, "NetworkWireless"_L1,
190 "Phone"_L1, "Printer"_L1, "Scanner"_L1, "VideoDisplay"_L1,
191 "AppointmentMissed"_L1, "AppointmentSoon"_L1,
192 "AudioVolumeHigh"_L1, "AudioVolumeLow"_L1, "AudioVolumeMedium"_L1,
193 "AudioVolumeMuted"_L1,
194 "BatteryCaution"_L1, "BatteryLow"_L1,
195 "DialogError"_L1, "DialogInformation"_L1, "DialogPassword"_L1,
196 "DialogQuestion"_L1, "DialogWarning"_L1,
197 "FolderDragAccept"_L1, "FolderOpen"_L1, "FolderVisiting"_L1,
198 "ImageLoading"_L1, "ImageMissing"_L1,
199 "MailAttachment"_L1, "MailUnread"_L1, "MailRead"_L1,
200 "MailReplied"_L1,
201 "MediaPlaylistRepeat"_L1, "MediaPlaylistShuffle"_L1,
202 "NetworkOffline"_L1,
203 "PrinterPrinting"_L1,
204 "SecurityHigh"_L1, "SecurityLow"_L1,
205 "SoftwareUpdateAvailable"_L1, "SoftwareUpdateUrgent"_L1,
206 "SyncError"_L1, "SyncSynchronizing"_L1,
207 "UserAvailable"_L1,
208 "UserOffline"_L1,
209 "WeatherClear"_L1, "WeatherClearNight"_L1, "WeatherFewClouds"_L1,
210 "WeatherFewCloudsNight"_L1, "WeatherFog"_L1, "WeatherShowers"_L1,
211 "WeatherSnow"_L1, "WeatherStorm"_L1
212 };
213
214 return result;
215};
216
217int QResourceBuilder::themeIconIndex(QStringView name)
218{
219 const auto lastQual = name.lastIndexOf("::"_L1);
220 const auto result = lastQual != -1
221 ? themeIconNames().indexOf(name.sliced(lastQual + 2))
222 : themeIconNames().indexOf(name);
223 return int(result);
224}
225
226QString QResourceBuilder::fullyQualifiedThemeIconName(int i)
227{
228 return i >= 0 && i < themeIconNames().size()
229 ? "QIcon::ThemeIcon::"_L1 + themeIconNames().at(i) : QString{};
230}
231
232#ifdef QFORMINTERNAL_NAMESPACE
233} // namespace QFormInternal
234#endif
235
236QT_END_NAMESPACE
Combined button and popup list for selecting options.
#define qPrintable(string)
Definition qstring.h:1683
@ themeDebug