Timeline: Use formatTime also from QML

Change-Id: I14c8e8a216008c2dafaa2d42bd3237d33cc2b781
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Ulf Hermann
2016-12-19 11:29:40 +01:00
parent 18332d15b5
commit 6fa4722020
6 changed files with 41 additions and 43 deletions

View File

@@ -341,6 +341,7 @@ Rectangle {
startTime: zoomControl.selectionStart startTime: zoomControl.selectionStart
duration: zoomControl.selectionDuration duration: zoomControl.selectionDuration
endTime: zoomControl.selectionEnd endTime: zoomControl.selectionEnd
referenceDuration: zoomControl.rangeDuration
showDuration: selectionRange.rangeWidth > 1 showDuration: selectionRange.rangeWidth > 1
hasContents: selectionRangeMode && hasContents: selectionRangeMode &&
selectionRange.creationState !== selectionRange.creationInactive selectionRange.creationState !== selectionRange.creationInactive

View File

@@ -25,6 +25,7 @@
import QtQuick 2.1 import QtQuick 2.1
import TimelineTheme 1.0 import TimelineTheme 1.0
import TimelineTimeFormatter 1.0
Item { Item {
id: selectionRangeDetails id: selectionRangeDetails
@@ -32,9 +33,10 @@ Item {
signal recenter signal recenter
signal close signal close
property string startTime property double startTime
property string endTime property double endTime
property string duration property double duration
property double referenceDuration
property bool showDuration property bool showDuration
property bool hasContents property bool hasContents
@@ -48,16 +50,6 @@ Item {
onHeightChanged: fitInView(); onHeightChanged: fitInView();
} }
function detailedPrintTime( t )
{
if (t <= 0) return "0";
if (t<1000) return t+" ns";
t = Math.floor(t/1000);
if (t<1000) return t+" μs";
if (t<1e6) return (t/1000) + " ms";
return (t/1e6) + " s";
}
function fitInView() { function fitInView() {
// don't reposition if it does not fit // don't reposition if it does not fit
if (parent.width < width || parent.height < height) if (parent.width < width || parent.height < height)
@@ -108,11 +100,11 @@ Item {
id: details id: details
property var contents: [ property var contents: [
qsTr("Start") + ":", qsTr("Start") + ":",
detailedPrintTime(startTime), TimeFormatter.format(startTime, referenceDuration),
(qsTr("End") + ":"), (qsTr("End") + ":"),
detailedPrintTime(endTime), TimeFormatter.format(endTime, referenceDuration),
(qsTr("Duration") + ":"), (qsTr("Duration") + ":"),
detailedPrintTime(duration) TimeFormatter.format(duration, referenceDuration)
] ]
model: showDuration ? 6 : 2 model: showDuration ? 6 : 2

View File

@@ -25,6 +25,7 @@
import QtQuick 2.1 import QtQuick 2.1
import TimelineTheme 1.0 import TimelineTheme 1.0
import TimelineTimeFormatter 1.0
Item { Item {
id: timeDisplay id: timeDisplay
@@ -48,32 +49,6 @@ Item {
property int contentX property int contentX
property int offsetX: contentX + Math.round((windowStart % timePerBlock) * spacing) property int offsetX: contentX + Math.round((windowStart % timePerBlock) * spacing)
readonly property var timeUnits: ["μs", "ms", "s"]
function prettyPrintTime(t, rangeDuration) {
if (rangeDuration < 1)
return ""
var round = 1;
var barrier = 1;
for (var i = 0; i < timeUnits.length; ++i) {
barrier *= 1000;
if (rangeDuration < barrier)
round *= 1000;
else if (rangeDuration < barrier * 10)
round *= 100;
else if (rangeDuration < barrier * 100)
round *= 10;
if (t < barrier * 1000)
return Math.floor(t / (barrier / round)) / round + timeUnits[i];
}
t /= barrier;
var m = Math.floor(t / 60);
var s = Math.floor((t - m * 60) * round) / round;
return m + "m" + s + "s";
}
Rectangle { Rectangle {
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
@@ -121,7 +96,7 @@ Item {
font.pixelSize: timeDisplay.fontSize font.pixelSize: timeDisplay.fontSize
anchors.leftMargin: timeDisplay.textMargin anchors.leftMargin: timeDisplay.textMargin
verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
text: prettyPrintTime(column.blockStartTime, timeDisplay.rangeDuration) text: TimeFormatter.format(column.blockStartTime, timeDisplay.rangeDuration)
visible: width > 0 visible: width > 0
color: Theme.color(Theme.PanelTextColorLight) color: Theme.color(Theme.PanelTextColorLight)
} }

View File

@@ -24,6 +24,7 @@
****************************************************************************/ ****************************************************************************/
#include "timelineformattime.h" #include "timelineformattime.h"
#include <QtQml>
namespace Timeline { namespace Timeline {
@@ -100,4 +101,18 @@ QString formatTime(qint64 timestamp, qint64 reference)
} }
} }
static QObject *createFormatter(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
return new TimeFormatter;
}
void TimeFormatter::setupTimeFormatter()
{
static const int typeIndex = qmlRegisterSingletonType<TimeFormatter>(
"TimelineTimeFormatter", 1, 0, "TimeFormatter", createFormatter);
Q_UNUSED(typeIndex);
}
} }

View File

@@ -26,9 +26,22 @@
#include "timeline_global.h" #include "timeline_global.h"
#include <QString> #include <QString>
#include <QObject>
#include <limits> #include <limits>
namespace Timeline { namespace Timeline {
QString TIMELINE_EXPORT formatTime(qint64 timestamp, QString TIMELINE_EXPORT formatTime(qint64 timestamp,
qint64 reference = std::numeric_limits<qint64>::max()); qint64 reference = std::numeric_limits<qint64>::max());
class TIMELINE_EXPORT TimeFormatter : public QObject {
Q_OBJECT
public:
Q_INVOKABLE QString format(qint64 timestamp, qint64 reference)
{
return formatTime(timestamp, reference);
}
static void setupTimeFormatter();
};
} }

View File

@@ -47,6 +47,7 @@
#include "timeline/timelinerenderer.h" #include "timeline/timelinerenderer.h"
#include "timeline/timelineoverviewrenderer.h" #include "timeline/timelineoverviewrenderer.h"
#include "timeline/timelinetheme.h" #include "timeline/timelinetheme.h"
#include "timeline/timelineformattime.h"
#include <aggregation/aggregate.h> #include <aggregation/aggregate.h>
// Needed for the load&save actions in the context menu // Needed for the load&save actions in the context menu
@@ -166,6 +167,7 @@ QmlProfilerTraceView::QmlProfilerTraceView(QWidget *parent, QmlProfilerViewManag
setMinimumHeight(170); setMinimumHeight(170);
Timeline::TimelineTheme::setupTheme(d->m_mainView->engine()); Timeline::TimelineTheme::setupTheme(d->m_mainView->engine());
Timeline::TimeFormatter::setupTimeFormatter();
d->m_mainView->rootContext()->setContextProperty(QLatin1String("timelineModelAggregator"), d->m_mainView->rootContext()->setContextProperty(QLatin1String("timelineModelAggregator"),
d->m_modelProxy); d->m_modelProxy);