initial commit

This commit is contained in:
k-a-z-u
2019-10-02 10:37:38 +02:00
parent 659f8b661b
commit 4cdf30bc30
6 changed files with 270 additions and 0 deletions

34
DoP.pro Normal file
View File

@@ -0,0 +1,34 @@
QT += quick
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
Painty.cpp \
main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
Painty.h

61
Painty.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include "Painty.h"
#include <cmath>
Painty::Painty()
{
}
void Painty::setXY(int idx, float x, float y) {
if (points.size() < idx+1) {points.resize(idx+1);}
points[idx] = QPointF(x,y);
emit update();
}
void Painty::setSigma(double s) {
this->sigma = s;
emit update();
}
#include <QPainter>
void Painty::paint(QPainter* p) {
p->setPen(Qt::blue);
p->drawLine(0,0,100,100);
const double s = sigma;
static QImage img(width(), height(), QImage::Format_ARGB32);
std::vector<double> vec;
if (points.size() < 3) {return;}
double max = 0;
for (int y = 0; y < height(); ++y) {
for (int x = 0; x < width(); ++x) {
double sum = 0;
for (const QPointF p : points) {
const double dx = x-p.x();
const double dy = y-p.y();
const double dist = std::sqrt(dx*dx+dy*dy);
sum += 1.0 / std::sqrt(2*M_PI*s*s) * std::exp(- (dist*dist)/(2*s*s) );
}
vec.push_back(sum);
if (sum > max) {max = sum;}
}
}
for (size_t idx = 0; idx < vec.size(); ++idx) {
int x = idx % (int) width();
int y = idx / width();
double h = 1.0 - vec[idx]/max;
QColor c = QColor::fromHsvF(h, 0.5, 1);
img.setPixelColor(x, y, c);
}
p->drawImage(0, 0, img);
}

34
Painty.h Normal file
View File

@@ -0,0 +1,34 @@
#ifndef PAINTY_H
#define PAINTY_H
#include <QQuickPaintedItem>
#include <QVariant>
#include <QObject>
#include <vector>
class Painty : public QQuickPaintedItem {
Q_OBJECT
//Q_PROPERTY(QVariant valX WRITE setX)
//Q_PROPERTY(QVariant valY WRITE setY)
std::vector<QPointF> points;
double sigma = 10;
public:
Painty();
void paint(QPainter *painter) override;
Q_INVOKABLE void setXY(int idx, float x, float y);
Q_INVOKABLE void setSigma(double s);
// void setX(QVariant v);
// void setY(QVariant v);
};
#endif // PAINTY_H

22
main.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "Painty.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<Painty>("bla", 1, 0, "Painty");
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}

114
main.qml Normal file
View File

@@ -0,0 +1,114 @@
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import bla 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
property var valuesX: [];
property var valuesY: [];
property int numBalls: 3;
Painty {
id: painty;
anchors.fill: parent;
Row {
Button {
text: "+";
onClicked: {
++numBalls;
}
}
Button {
text: "-";
onClicked: {
--numBalls;
}
}
Slider {
from: 10;
to: 200;
onValueChanged: {
painty.setSigma(value);
}
}
}
Repeater {
model: numBalls;
Rectangle {
width: 16;
height: 16;
x: (index+3) * 32;
y: (index+3) * 32;
color: "red"
//Drag.active: parent
MouseArea {
anchors.fill: parent;
drag.target: parent;
onPositionChanged: {
//valuesX[index] = parent.x;
//valuesY[index] = parent.y;
//console.log(parent.x + " " + parent.y);
//can.requestPaint();
painty.setXY(index, parent.x, parent.y);
}
}
}
}
/*
onPaint: {
var ctx = getContext("2d");
ctx.fillStyle = Qt.rgba(0, 0, 0, 1);
ctx.fillRect(0, 0, width, height);
var w = 300;
var h = 300;
var img = context.createImageData(w, h);
var s = 1;
for (var y = 0; y < h; ++y) {
for (var x = 0; x < w; ++x) {
var sum = 0;
for (var idx in valuesX) {
var x1 = valuesX[idx];
var y1 = valuesY[idx];
var dx = x-x1;
var dy = y-y1;
var dist = Math.sqrt(dx*dx + dy*dy);
//var p = 1/Math.sqrt(2*Math.PI*s*s)*Math.exp(-(dist*dist)/(2*s*s));
sum += 0;
}
img.data[(x+y*w)*4+0] = 128;
img.data[(x+y*w)*4+1] = 64;
img.data[(x+y*w)*4+2] = 32;
img.data[(x+y*w)*4+3] = 255;
}
}
ctx.putImageData(img, 0, 0);
}
*/
}
}

5
qml.qrc Normal file
View File

@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>