Skip to content

Commit 2b3090c

Browse files
committed
[WIP] Use image provider to load icons
Not implemented AwesomeIcons Some improvements can be made in properties like "name" and "source" to make the code even simpler
1 parent da9ee1c commit 2b3090c

11 files changed

+89
-53
lines changed

demo/IconsDemo.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ Item {
166166

167167
Repeater {
168168
id: awesomeList
169-
model: Object.keys(awesomeIcon.icons)
169+
//model: Object.keys(awesomeIcon.icons)
170170
delegate: Item {
171171
width: section.state == "list" ? Units.dp(240) : icon.size
172172
height: icon.size

modules/Material/Action.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Controls.Action {
4444
\sa iconName
4545
\sa Icon
4646
*/
47-
property string iconSource: "icon://" + iconName
47+
property string iconSource: "image://material/" + iconName
4848

4949
/*!
5050
The text displayed for the action.

modules/Material/ActionBar.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ Item {
290290

291291
color: Theme.lightDark(actionBar.backgroundColor, Theme.light.iconColor,
292292
Theme.dark.iconColor)
293-
size: iconSource == "icon://content/add" ? Units.dp(27) : Units.dp(24)
293+
size: Units.dp(24)
294294

295295
anchors.verticalCenter: parent ? parent.verticalCenter : undefined
296296
}

modules/Material/Icon.qml

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Item {
4848
4949
\sa name
5050
*/
51-
property string source: "icon://" + name
51+
property string source: name ? "image://material/" + name : ""
5252

5353
property bool valid: source.indexOf("icon://awesome/") == 0
5454
? awesomeIcon.valid : image.status == Image.Ready
@@ -58,31 +58,14 @@ Item {
5858
width: size
5959
height: size
6060

61-
property bool colorize: icon.source.indexOf("icon://") === 0 || icon.source.indexOf(".color.") === -1
61+
property bool colorize: icon.source.indexOf("image://material/") === 0 || icon.source.indexOf(".color.") === -1
6262

6363
Image {
6464
id: image
6565

66-
anchors.fill: parent
67-
visible: source != "" && !colorize
68-
69-
source: {
70-
if (icon.source.indexOf("icon://") == 0) {
71-
var name = icon.source.substring(7)
72-
var list = name.split("/");
73-
74-
if (name == "" || list[0] === "awesome")
75-
return "";
76-
return Qt.resolvedUrl("icons/%1/%2.svg".arg(list[0]).arg(list[1]));
77-
} else {
78-
return icon.source
79-
}
80-
}
66+
visible: !colorize
8167

82-
sourceSize {
83-
width: size * Screen.devicePixelRatio
84-
height: size * Screen.devicePixelRatio
85-
}
68+
source: icon.source
8669
}
8770

8871
ColorOverlay {
@@ -95,26 +78,4 @@ Item {
9578
visible: image.source != "" && colorize
9679
opacity: icon.color.a
9780
}
98-
99-
AwesomeIcon {
100-
id: awesomeIcon
101-
102-
anchors.centerIn: parent
103-
size: icon.size * 0.9
104-
visible: name != ""
105-
color: icon.color
106-
107-
name: {
108-
if (icon.source.indexOf("icon://") == 0) {
109-
var name = icon.source.substring(7)
110-
var list = name.split("/")
111-
112-
if (list[0] === "awesome") {
113-
return list[1]
114-
}
115-
}
116-
117-
return ""
118-
}
119-
}
12081
}

modules/Material/IconButton.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Item {
3030

3131
property Action action
3232
property string iconName
33-
property string iconSource: action ? action.iconSource : "icon://" + iconName
33+
property string iconSource: action ? action.iconSource : "image://material/" + iconName
3434
property bool hoverAnimation: action ? action.hoverAnimation : false
3535
property alias color: icon.color
3636
property alias size: icon.size

modules/Material/Tab.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ Controls.Tab {
4343
\sa iconName
4444
\sa Icon
4545
*/
46-
property string iconSource: "icon://" + iconName
46+
property string iconSource: iconName ? "image://material/" + iconName : ""
4747
}
4848

modules/Material/TabBar.qml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,13 @@ Item {
153153
Icon {
154154
anchors.verticalCenter: parent.verticalCenter
155155

156-
source: tabItem.tab.hasOwnProperty("iconSource")
157-
? tabItem.tab.iconSource : tabItem.tab.hasOwnProperty("iconName")
158-
? "icon://" + tabItem.tab.iconName : ""
156+
source: tabItem.tab.hasOwnProperty("iconSource")
157+
? tabItem.tab.iconSource : ""
159158
color: tabItem.selected
160159
? darkBackground ? Theme.dark.iconColor : Theme.light.accentColor
161160
: darkBackground ? Theme.dark.shade(tab.enabled ? 0.6 : 0.2) : Theme.light.shade(tab.enabled ? 0.6 : 0.2)
162161

163-
visible: source != "" && source != "icon://"
162+
visible: source != ""
164163

165164
Behavior on color {
166165
ColorAnimation { duration: 200 }

modules/Material/qmldir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module Material
2+
plugin materialiconprovider
23
Action 0.1 Action.qml
34
ActionBar 0.1 ActionBar.qml
45
ActionButton 0.1 ActionButton.qml

plugins/materialiconprovider.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* QML Material - An application framework implementing Material Design.
3+
* Copyright (C) 2015 Ricardo Vieira <[email protected]>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as
7+
* published by the Free Software Foundation, either version 2.1 of the
8+
* License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <qqmlextensionplugin.h>
20+
21+
#include <qqmlengine.h>
22+
#include <qquickimageprovider.h>
23+
#include <QImage>
24+
#include <QPainter>
25+
26+
class MaterialIconProvider : public QQuickImageProvider
27+
{
28+
public:
29+
MaterialIconProvider()
30+
: QQuickImageProvider(QQuickImageProvider::Pixmap)
31+
{
32+
}
33+
34+
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
35+
{
36+
int width = 100;
37+
int height = 50;
38+
QImage icon = QImage("/lib/qt/qml/Material/icons/" + id + ".svg");
39+
40+
if (size)
41+
*size = QSize(width, height);
42+
43+
return QPixmap::fromImage(icon);
44+
}
45+
};
46+
47+
class ImageProviderExtensionPlugin : public QQmlExtensionPlugin
48+
{
49+
Q_OBJECT
50+
Q_PLUGIN_METADATA(IID "Material")
51+
public:
52+
void registerTypes(const char *uri)
53+
{
54+
Q_UNUSED(uri);
55+
}
56+
57+
void initializeEngine(QQmlEngine *engine, const char *uri)
58+
{
59+
Q_UNUSED(uri);
60+
engine->addImageProvider("material", new MaterialIconProvider);
61+
}
62+
63+
};
64+
65+
#include "materialiconprovider.moc"

plugins/plugins.pro

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
TEMPLATE = lib
2+
CONFIG += plugin
3+
QT += qml quick
4+
5+
TARGET = materialiconprovider
6+
7+
SOURCES += materialiconprovider.cpp
8+
9+
target.path = $$[QT_INSTALL_QML]/Material
10+
INSTALLS = target

qml-material.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
TEMPLATE = subdirs
2-
SUBDIRS = modules/Material modules/Material/Extras modules/QtQuick/Controls/Styles/Material tests
2+
SUBDIRS = modules/Material modules/Material/Extras modules/QtQuick/Controls/Styles/Material plugins tests
33

44
OTHER_FILES = README.md CHANGELOG.md

0 commit comments

Comments
 (0)