From f86f4d184b2062c078cf7e35aa87c321f004f4ab Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Fri, 19 Apr 2019 13:58:41 +0200 Subject: [PATCH] Add a QML item that shows the FPS --- resources/qml/Cura.qml | 2 +- resources/qml/FPSItem.qml | 81 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 resources/qml/FPSItem.qml diff --git a/resources/qml/Cura.qml b/resources/qml/Cura.qml index e640b25b24..fbaae72336 100644 --- a/resources/qml/Cura.qml +++ b/resources/qml/Cura.qml @@ -197,7 +197,7 @@ UM.MainWindow top: applicationMenu.bottom } } - + Item { id: contentItem diff --git a/resources/qml/FPSItem.qml b/resources/qml/FPSItem.qml new file mode 100644 index 0000000000..9f7dfe8746 --- /dev/null +++ b/resources/qml/FPSItem.qml @@ -0,0 +1,81 @@ +import QtQuick 2.0 +import QtQuick.Window 2.2 +import UM 1.3 as UM + +// This is an QML item that shows the FPS and a running average of the FPS. +Item +{ + id: base + property alias backgroundColor: background.color + property alias textColor: fpsText.color + + property int numMeasurementsToAverage: 3 + + width: fpsText.contentWidth + UM.Theme.getSize("default_margin").height + height: fpsText.contentHeight + UM.Theme.getSize("default_margin").height + + Rectangle + { + id: background + + // We use a trick here to figure out how often we can get a redraw triggered. + // By adding a rotating rectangle, we can increase a counter by one every time we get notified. + // After that, we trigger a timer once every second to look at that number. + property int frameCounter: 0 + property int averageFrameCounter: 0 + property int counter: 0 + property int fps: 0 + property real averageFps: 0.0 + + color: UM.Theme.getColor("primary") + + width: parent.width + height: parent.height + + Rectangle + { + width: 0 + height: 0 + NumberAnimation on rotation + { + from: 0 + to: 360 + duration: 1000 + loops: Animation.Infinite + } + onRotationChanged: parent.frameCounter++; + visible: false + } + + Text + { + id: fpsText + anchors.fill:parent + verticalAlignment: Text.AlignVCenter + horizontalAlignment: Text.AlignHCenter + color: UM.Theme.getColor("text") + font: UM.Theme.getFont("default") + text: "Ø " + parent.averageFps + " | " + parent.fps + " fps" + } + + Timer + { + interval: 1000 + repeat: true + running: true + onTriggered: + { + parent.averageFrameCounter += parent.frameCounter; + parent.fps = parent.frameCounter; + parent.counter++; + parent.frameCounter = 0; + if (parent.counter >= base.numMeasurementsToAverage) + { + parent.averageFps = (parent.averageFrameCounter / parent.counter).toFixed(2) + parent.averageFrameCounter = 0; + parent.counter = 0; + } + } + } + } +} \ No newline at end of file