1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Designer of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "resourcebuilder_p.h"
52 #include "ui4_p.h"
53 #include <QtCore/qvariant.h>
54 #include <QtCore/qfileinfo.h>
55 #include <QtCore/qdir.h>
56 #include <QtCore/qdebug.h>
57 #include <QtGui/qpixmap.h>
58 #include <QtGui/qicon.h>
59 
60 QT_BEGIN_NAMESPACE
61 
62 #ifdef QFORMINTERNAL_NAMESPACE
63 namespace QFormInternal {
64 #endif
65 
66 enum { themeDebug = 0 };
67 
68 QResourceBuilder::QResourceBuilder() = default;
69 
70 QResourceBuilder::~QResourceBuilder() = default;
71 
iconStateFlags(const DomResourceIcon * dpi)72 int QResourceBuilder::iconStateFlags(const DomResourceIcon *dpi)
73 {
74     int rc = 0;
75     if (dpi->hasElementNormalOff())
76         rc |= NormalOff;
77     if (dpi->hasElementNormalOn())
78         rc |= NormalOn;
79     if (dpi->hasElementDisabledOff())
80         rc |= DisabledOff;
81     if (dpi->hasElementDisabledOn())
82         rc |= DisabledOn;
83     if (dpi->hasElementActiveOff())
84         rc |= ActiveOff;
85     if (dpi->hasElementActiveOn())
86         rc |= ActiveOn;
87     if (dpi->hasElementSelectedOff())
88         rc |= SelectedOff;
89     if (dpi->hasElementSelectedOn())
90         rc |= SelectedOn;
91     return rc;
92 }
93 
loadResource(const QDir & workingDirectory,const DomProperty * property) const94 QVariant QResourceBuilder::loadResource(const QDir &workingDirectory, const DomProperty *property) const
95 {
96     switch (property->kind()) {
97         case DomProperty::Pixmap: {
98             const DomResourcePixmap *dpx = property->elementPixmap();
99             QPixmap pixmap(QFileInfo(workingDirectory, dpx->text()).absoluteFilePath());
100             return QVariant::fromValue(pixmap);
101         }
102         case DomProperty::IconSet: {
103             const DomResourceIcon *dpi = property->elementIconSet();
104             if (!dpi->attributeTheme().isEmpty()) {
105                 const QString theme = dpi->attributeTheme();
106                 const bool known = QIcon::hasThemeIcon(theme);
107                 if (themeDebug)
108                     qDebug("Theme %s known %d", qPrintable(theme), known);
109                 if (known)
110                     return QVariant::fromValue(QIcon::fromTheme(dpi->attributeTheme()));
111             } // non-empty theme
112             if (const int flags = iconStateFlags(dpi)) { // new, post 4.4 format
113                 QIcon icon;
114                 if (flags & NormalOff)
115                     icon.addFile(QFileInfo(workingDirectory, dpi->elementNormalOff()->text()).absoluteFilePath(), QSize(), QIcon::Normal, QIcon::Off);
116                 if (flags & NormalOn)
117                     icon.addFile(QFileInfo(workingDirectory, dpi->elementNormalOn()->text()).absoluteFilePath(), QSize(), QIcon::Normal, QIcon::On);
118                 if (flags & DisabledOff)
119                     icon.addFile(QFileInfo(workingDirectory, dpi->elementDisabledOff()->text()).absoluteFilePath(), QSize(), QIcon::Disabled, QIcon::Off);
120                 if (flags & DisabledOn)
121                     icon.addFile(QFileInfo(workingDirectory, dpi->elementDisabledOn()->text()).absoluteFilePath(), QSize(), QIcon::Disabled, QIcon::On);
122                 if (flags & ActiveOff)
123                     icon.addFile(QFileInfo(workingDirectory, dpi->elementActiveOff()->text()).absoluteFilePath(), QSize(), QIcon::Active, QIcon::Off);
124                 if (flags & ActiveOn)
125                     icon.addFile(QFileInfo(workingDirectory, dpi->elementActiveOn()->text()).absoluteFilePath(), QSize(), QIcon::Active, QIcon::On);
126                 if (flags & SelectedOff)
127                     icon.addFile(QFileInfo(workingDirectory, dpi->elementSelectedOff()->text()).absoluteFilePath(), QSize(), QIcon::Selected, QIcon::Off);
128                 if (flags & SelectedOn)
129                     icon.addFile(QFileInfo(workingDirectory, dpi->elementSelectedOn()->text()).absoluteFilePath(), QSize(), QIcon::Selected, QIcon::On);
130                 return QVariant::fromValue(icon);
131             } else { // 4.3 legacy
132                 const QIcon icon(QFileInfo(workingDirectory, dpi->text()).absoluteFilePath());
133                 return QVariant::fromValue(icon);
134             }
135         }
136             break;
137         default:
138             break;
139     }
140     return QVariant();
141 }
142 
toNativeValue(const QVariant & value) const143 QVariant QResourceBuilder::toNativeValue(const QVariant &value) const
144 {
145     return value;
146 }
147 
saveResource(const QDir & workingDirectory,const QVariant & value) const148 DomProperty *QResourceBuilder::saveResource(const QDir &workingDirectory, const QVariant &value) const
149 {
150     Q_UNUSED(workingDirectory);
151     Q_UNUSED(value);
152     return nullptr;
153 }
154 
isResourceProperty(const DomProperty * p) const155 bool QResourceBuilder::isResourceProperty(const DomProperty *p) const
156 {
157     switch (p->kind()) {
158         case DomProperty::Pixmap:
159         case DomProperty::IconSet:
160             return true;
161         default:
162             break;
163     }
164     return false;
165 }
166 
isResourceType(const QVariant & value) const167 bool QResourceBuilder::isResourceType(const QVariant &value) const
168 {
169     switch (value.type()) {
170         case QVariant::Pixmap:
171         case QVariant::Icon:
172             return true;
173         default:
174             break;
175     }
176     return false;
177 }
178 
179 #ifdef QFORMINTERNAL_NAMESPACE
180 } // namespace QFormInternal
181 #endif
182 
183 QT_END_NAMESPACE
184