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