Skip to content

Commit

Permalink
Initial public commit
Browse files Browse the repository at this point in the history
After 10 months and 164 commits, start with fb3112ed in
github repository.
  • Loading branch information
kgron committed Dec 25, 2015
0 parents commit 0b72c6f
Show file tree
Hide file tree
Showing 112 changed files with 25,453 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .gitignore
@@ -0,0 +1,35 @@
# Documentation

doc/html

# OS X

*.app
*.DS_Store

# C++ objects and libs

*.slo
*.lo
*.o
*.a
*.la
*.lai
*.so
*.dll
*.dylib

# Qt

*.pro.user
*.pro.user.*
*.moc
moc_*.cpp
qrc_*.cpp
ui_*.h
Makefile*
*build-*

# QtCreator

*.autosave
18 changes: 18 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,18 @@
Copyright (c) 2015 QUIt Coding <info@quitcoding.com>
Author: Kaj Grönholm <kaj.gronholm@quitcoding.com>

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
162 changes: 162 additions & 0 deletions README.md
@@ -0,0 +1,162 @@
# QNanoPainter

QNanoPainter is a library for implementing custom C++ Qt [QQuickItems](http://doc.qt.io/qt-5/qquickitem.html). In a nutshell, it's like [QQuickPaintedItem](http://doc.qt.io/qt-5/qquickpainteditem.html), but instead of QPainter offers own QNanoPainter API for drawing. QNanoPainter API is a mixture of QPainter and HTML5 canvas APIs. In other words, it's very close to HTML5 canvas but transformed to object oriented C++ with separate classes. The reason for QNanoPainter existence is to try offer performance, productivity and rendering quality all-in-one for custom Qt5 scene graph items.

QNanoPainter uses excellent [NanoVG](https://github.com/memononen/nanovg) as its rendering backend.


## Screenshots

<img src="doc/images/sc_qnanopainter_1.png" width="24%">
<img src="doc/images/sc_qnanopainter_2.png" width="24%">
<img src="doc/images/sc_events_1.png" width="24%">
<img src="doc/images/sc_helloworld_1.png" width="24%">
<img src="doc/images/sc_gallery_1.png" width="24%">
<img src="doc/images/sc_gallery_2.png" width="24%">
<img src="doc/images/sc_gallery_3.png" width="24%">
<img src="doc/images/sc_gallery_4.png" width="24%">


## Features

Here is a non exhaustive list of QNanoPainter features:

* **Vector drawing**: Path-based drawing of rectangles, circles, lines etc. filled and/or stroked. Brush can be color, gradient or image pattern. Different cap and join options for stoked lines.
* **Images**: Transformed images or rectangular areas of images. Mipmapping for smoother rendering of scaled images.
* **Text**: Contains set of default fonts and supports loading custom truetype fonts. Text aligning and wrapping. Letter-spacing and blur for shadow/glow effect.
* **Antialiasing**: Adjustable antialiasing amount for vector drawing.
* **Pixel aligning**: Drawing and texts can be either freely positioned (smooth animations) or aligned to pixels (sharp rendering).
* **Extra**: Supports high-DPI retina resolutions, global alpha, transforms, painter states etc.


## Usage

Taking QNanoPainter into use in your Qt application is simple:

* Copy 'libqnanopainter' directory into your project. Sources are included directly instead of building separate static/dynamic library which makes usage simple and allows easier customization of QNanoPainter itself.
* In your project .pro file, include QNanoPainter with something like:


```
include(src/libqnanopainter/include.pri)
```

* Implement your QNanoQuickItem and QNanoQuickItemPainter based classes (see more about these below or from available examples).
* Export your item into QML in main.cpp with something like:

```
qmlRegisterType<MyQNanoItem>("MyQNanoItem", 1, 0, "MyQNanoItem");
```

* Use your item in QML:

```
import QtQuick 2.4
import MyQNanoItem 1.0
Item {
...
MyQNanoItem {
anchors.fill: parent
}
}
```

### Implementing custom item

To create own QNanoPainter item you should implement 2 classes:

* **QNanoQuickItem**: This will be the visual QQuickItem providing API towards QML side. Your painter will be created and attached to item in *createItemPainter()* method.
* **QNanoQuickItemPainter**: This will handle the drawing using QNanoPainter API in the *paint()* method. Data with the item will be transferred in *synchronize()* method.

The main reason for two classes is that these will run in separate threads for optimal performance. And because of this, all communication between these two need to happen in *synchronize()* method.

Here is a complete HelloWorld example item:

```C++:
#include "qnanoquickitem.h"
#include "qnanoquickitempainter.h"
// HelloItemPainter contains the painting code
class HelloItemPainter : public QNanoQuickItemPainter
{
Q_OBJECT
public:
HelloItemPainter()
{
}
void paint(QNanoPainter *p)
{
// Paint the background circle
p->beginPath();
p->circle(width()*0.5, height()*0.5, width()*0.46);
QNanoRadialGradient gradient1(width()*0.5, height()*0.4, width()*0.6, width()*0.2);
gradient1.setStartColor("#808080");
gradient1.setEndColor("#404040");
p->setFillStyle(gradient1);
p->fill();
p->setStrokeStyle("#202020");
p->setLineWidth(width()*0.02);
p->stroke();
// Hello text
p->setTextAlign(QNanoPainter::ALIGN_CENTER);
p->setTextBaseline(QNanoPainter::BASELINE_MIDDLE);
QNanoFont font1(QNanoFont::DEFAULT_FONT_BOLD_ITALIC);
font1.setPixelSize(width()*0.08);
p->setFont(font1);
p->setFillStyle("#B0D040");
p->fillText("HELLO", width()*0.5, height()*0.4);
// QNanoPainter text
QNanoFont font2(QNanoFont::DEFAULT_FONT_THIN);
font2.setPixelSize(width()*0.18);
p->setFont(font2);
p->fillText("QNanoPainter", width()*0.5, height()*0.5);
}
};
// HelloItem provides the API towards QML
class HelloItem : public QNanoQuickItem
{
Q_OBJECT
public:
HelloItem(QQuickItem *parent = 0)
: QNanoQuickItem(parent)
{
}
// Reimplement
QNanoQuickItemPainter *createItemPainter() const
{
// Create painter for this item
return new HelloItemPainter();
}
};
```


## API Reference

Sources contain API documentation written with [QDoc](http://doc.qt.io/qt-5/qdoc-index.html). To generate the documentation:

```
> cd doc
> [Qt bin path]/qdoc qnanopainter.qdocconf
```
Documentation is work-in-progress, patches welcome =)


## License

The library is licensed under [zlib license](LICENSE.txt).


## Links

* Uses [Qt framework](http://www.qt.io).
* Uses [NanoVG](http://github.com/memononen/nanovg) library as rendering backend.
* Uses Google Fonts [Roboto](http://www.google.com/fonts/specimen/Roboto) (and [Pacifico](http://www.google.com/fonts/specimen/Pacifico) in gallery example).
* Blog posts:
* [Introducing QNanoPainter](http://kgronholm.blogspot.fi/2015/10/introducing-qnanopainter.html)
Binary file added doc/images/sc_events_1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/sc_gallery_1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/sc_gallery_2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/sc_gallery_3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/sc_gallery_4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/sc_helloworld_1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/sc_qnanopainter_1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/images/sc_qnanopainter_2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions doc/qnanopainter.qdocconf
@@ -0,0 +1,21 @@

project = "QNanoPainter"
description = "QNanoPainter Documentation"

outputdir = html

headerdirs = ../libqnanopainter
headers.fileextensions = *.h

sourcedirs = ../libqnanopainter
sources.fileextensions += *.cpp

# The exampledirs variable specifies the directories containing
# the source code of the example files.

#exampledirs = ../examples

# The imagedirs variable specifies the
# directories containing the images used in the documentation.

#imagedirs = ../images
84 changes: 84 additions & 0 deletions examples/gallery/TopBar.qml
@@ -0,0 +1,84 @@
import QtQuick 2.0

Item {
id: root

property int currentIndex: 0
property int itemCount: 0
property string title

onCurrentIndexChanged: {
switch(root.currentIndex) {
case 0:
title = "Colors and Brushes";
break;
case 1:
title = "Paths, Caps & Joins";
break;
case 2:
title = "States, Transitions and Clipping";
break;
case 3:
title = "Antialiasing and Line Width";
break;
case 4:
title = "Text: Fonts and Styles";
break;
case 5:
title = "Text: Alignment and Wrapping";
break;
case 6:
title = "Images";
break;
}
}

width: parent.width
height: Math.floor(70 * dp)

Rectangle {
anchors.fill: parent
color: "#000000"
opacity: 0.2
}
Text {
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: titleTextItem.top
font.pixelSize: 12 * dp
color: "#909090"
text: "QNanoPainter Gallery"
}
Text {
id: titleTextItem
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 20 * dp
color: "#e0e0e0"
text: title
}
Row {
anchors.top: titleTextItem.bottom
anchors.topMargin: 8 * dp
anchors.horizontalCenter: parent.horizontalCenter
spacing: 6 * dp
Repeater {
model: root.itemCount
Rectangle {
width: 8 * dp
height: width
radius: width/2
color: "#e0e0e0"
border.width: 1
border.color: "#000000"
opacity: index == root.currentIndex ? 0.8 : 0.2
Behavior on opacity {
NumberAnimation {
duration: 200
easing.type: Easing.InOutQuad
}
}
}
}
}
}

27 changes: 27 additions & 0 deletions examples/gallery/deployment.pri
@@ -0,0 +1,27 @@
android-no-sdk {
target.path = /data/user/qt
export(target.path)
INSTALLS += target
} else:android {
x86 {
target.path = /libs/x86
} else: armeabi-v7a {
target.path = /libs/armeabi-v7a
} else {
target.path = /libs/armeabi
}
export(target.path)
INSTALLS += target
} else:unix {
isEmpty(target.path) {
qnx {
target.path = /tmp/$${TARGET}/bin
} else {
target.path = /opt/$${TARGET}/bin
}
export(target.path)
}
INSTALLS += target
}

export(INSTALLS)
Binary file added examples/gallery/fonts/Pacifico.ttf
Binary file not shown.
22 changes: 22 additions & 0 deletions examples/gallery/gallery.pro
@@ -0,0 +1,22 @@
TEMPLATE = app

QT += qml quick

SOURCES += main.cpp \
src/galleryitem.cpp \
src/galleryitempainter.cpp

HEADERS += \
src/galleryitem.h \
src/galleryitempainter.h

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Include QNanoPainter
include(../../libqnanopainter/include.pri)

# Default rules for deployment.
include(deployment.pri)
Binary file added examples/gallery/images/face.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/gallery/images/pattern1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/gallery/images/pattern2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/gallery/images/quit_logo2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions examples/gallery/main.cpp
@@ -0,0 +1,15 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "src/galleryitem.h"

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);

QQmlApplicationEngine engine;
qmlRegisterType<GalleryItem>("NanoVGQuick", 1, 0, "GalleryItem");

engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

return app.exec();
}

0 comments on commit 0b72c6f

Please sign in to comment.