Skip to content

Commit

Permalink
[WIP] Use image provider to load icons
Browse files Browse the repository at this point in the history
Not implemented AwesomeIcons
Some improvements can be made in properties like "name" and "source" to make the code even simpler
  • Loading branch information
ricardomv committed Jan 12, 2016
1 parent b714c9c commit 6e8f92d
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 52 deletions.
2 changes: 1 addition & 1 deletion demo/IconsDemo.qml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Item {

Repeater {
id: awesomeList
model: Object.keys(awesomeIcon.icons)
//model: Object.keys(awesomeIcon.icons)
delegate: Item {
width: section.state == "list" ? Units.dp(240) : icon.size
height: icon.size
Expand Down
2 changes: 1 addition & 1 deletion modules/Material/Action.qml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Controls.Action {
\sa iconName
\sa Icon
*/
property string iconSource: "icon://" + iconName
property string iconSource: "image://material/" + iconName

/*!
The text displayed for the action.
Expand Down
2 changes: 1 addition & 1 deletion modules/Material/ActionBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ Item {

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

anchors.verticalCenter: parent ? parent.verticalCenter : undefined
}
Expand Down
47 changes: 4 additions & 43 deletions modules/Material/Icon.qml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Item {
\sa name
*/
property string source: "icon://" + name
property string source: name ? "image://material/" + name : ""

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

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

Image {
id: image

anchors.fill: parent
visible: source != "" && !colorize

source: {
if (icon.source.indexOf("icon://") == 0) {
var name = icon.source.substring(7)
var list = name.split("/");

if (name == "" || list[0] === "awesome")
return "";
return Qt.resolvedUrl("icons/%1/%2.svg".arg(list[0]).arg(list[1]));
} else {
return icon.source
}
}
visible: !colorize

sourceSize {
width: size * Screen.devicePixelRatio
height: size * Screen.devicePixelRatio
}
source: icon.source
}

ColorOverlay {
Expand All @@ -95,26 +78,4 @@ Item {
visible: image.source != "" && colorize
opacity: icon.color.a
}

AwesomeIcon {
id: awesomeIcon

anchors.centerIn: parent
size: icon.size * 0.9
visible: name != ""
color: icon.color

name: {
if (icon.source.indexOf("icon://") == 0) {
var name = icon.source.substring(7)
var list = name.split("/")

if (list[0] === "awesome") {
return list[1]
}
}

return ""
}
}
}
2 changes: 1 addition & 1 deletion modules/Material/IconButton.qml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Item {

property Action action
property string iconName
property string iconSource: action ? action.iconSource : "icon://" + iconName
property string iconSource: action ? action.iconSource : "image://material/" + iconName
property bool hoverAnimation: action ? action.hoverAnimation : false
property alias color: icon.color
property alias size: icon.size
Expand Down
2 changes: 1 addition & 1 deletion modules/Material/Tab.qml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ Controls.Tab {
\sa iconName
\sa Icon
*/
property string iconSource: "icon://" + iconName
property string iconSource: iconName ? "image://material/" + iconName : ""
}

5 changes: 2 additions & 3 deletions modules/Material/TabBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,12 @@ Item {
anchors.verticalCenter: parent.verticalCenter

source: tabItem.tab.hasOwnProperty("iconSource")
? tabItem.tab.iconSource : tabItem.tab.hasOwnProperty("iconName")
? "icon://" + tabItem.tab.iconName : ""
? tabItem.tab.iconSource : ""
color: tabItem.selected
? darkBackground ? Theme.dark.iconColor : Theme.light.accentColor
: darkBackground ? Theme.dark.shade(tab.enabled ? 0.6 : 0.2) : Theme.light.shade(tab.enabled ? 0.6 : 0.2)

visible: source != "" && source != "icon://"
visible: source != ""

Behavior on color {
ColorAnimation { duration: 200 }
Expand Down
1 change: 1 addition & 0 deletions modules/Material/qmldir
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Material
plugin materialiconprovider
Action 0.1 Action.qml
ActionBar 0.1 ActionBar.qml
ActionButton 0.1 ActionButton.qml
Expand Down
65 changes: 65 additions & 0 deletions plugins/materialiconprovider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* QML Material - An application framework implementing Material Design.
* Copyright (C) 2015 Ricardo Vieira <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <qqmlextensionplugin.h>

#include <qqmlengine.h>
#include <qquickimageprovider.h>
#include <QImage>
#include <QPainter>

class MaterialIconProvider : public QQuickImageProvider
{
public:
MaterialIconProvider()
: QQuickImageProvider(QQuickImageProvider::Pixmap)
{
}

QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
int width = 100;
int height = 50;
QImage icon = QImage("/lib/qt/qml/Material/icons/" + id + ".svg");

if (size)
*size = QSize(width, height);

return QPixmap::fromImage(icon);
}
};

class ImageProviderExtensionPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "Material")
public:
void registerTypes(const char *uri)
{
Q_UNUSED(uri);
}

void initializeEngine(QQmlEngine *engine, const char *uri)
{
Q_UNUSED(uri);
engine->addImageProvider("material", new MaterialIconProvider);
}

};

#include "materialiconprovider.moc"
10 changes: 10 additions & 0 deletions plugins/plugins.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TEMPLATE = lib
CONFIG += plugin
QT += qml quick

TARGET = materialiconprovider

SOURCES += materialiconprovider.cpp

target.path = $$[QT_INSTALL_QML]/Material
INSTALLS = target
2 changes: 1 addition & 1 deletion qml-material.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TEMPLATE = subdirs
SUBDIRS = modules/Material modules/Material/Extras modules/QtQuick/Controls/Styles/Material tests
SUBDIRS = modules/Material modules/Material/Extras modules/QtQuick/Controls/Styles/Material plugins tests

OTHER_FILES = README.md CHANGELOG.md

2 comments on commit 6e8f92d

@SunRain
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start using some CPP code in material project now? Would you consider adding more cpp code in this project?

@iBelieve
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SunRain Yep, see #359.

Please sign in to comment.