Provide KFormat wrapper in PlasmaCore for unit conversion
REVIEW: 115284
This commit is contained in:
parent
c0f53545eb
commit
3c0007db22
@ -25,6 +25,7 @@ set(corebindings_SRCS
|
||||
dialog.cpp
|
||||
tooltip.cpp
|
||||
tooltipdialog.cpp
|
||||
formats.cpp
|
||||
serviceoperationstatus.cpp
|
||||
dataenginebindings.cpp
|
||||
iconitem.cpp
|
||||
|
@ -40,6 +40,7 @@
|
||||
#include "theme.h"
|
||||
#include "dialog.h"
|
||||
#include "iconitem.h"
|
||||
#include "formats.h"
|
||||
#include "serviceoperationstatus.h"
|
||||
|
||||
#include "tooltip.h"
|
||||
@ -91,6 +92,7 @@ void CoreBindingsPlugin::registerTypes(const char *uri)
|
||||
qRegisterMetaType<Plasma::Service*>("Service");
|
||||
qmlRegisterInterface<Plasma::ServiceJob>("ServiceJob");
|
||||
qRegisterMetaType<Plasma::ServiceJob*>("ServiceJob");
|
||||
qmlRegisterType<Formats>(uri, 2, 0, "Formats");
|
||||
qmlRegisterType<ServiceOperationStatus>(uri, 2, 0, "ServiceOperationStatus");
|
||||
qmlRegisterType<QAbstractItemModel>();
|
||||
#if 0
|
||||
|
57
src/declarativeimports/core/formats.cpp
Normal file
57
src/declarativeimports/core/formats.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2014 Bhushan Shah <bhush94@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of
|
||||
* the License or (at your option) version 3 or any later version
|
||||
* accepted by the membership of KDE e.V. (or its successor approved
|
||||
* by the membership of KDE e.V.), which shall act as a proxy
|
||||
* defined in Section 14 of version 3 of the license.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
#include "formats.h"
|
||||
|
||||
Formats::Formats(QObject* parent)
|
||||
: QObject(parent)
|
||||
, m_format()
|
||||
{
|
||||
}
|
||||
|
||||
QString Formats::formatByteSize(double size, int precision) const
|
||||
{
|
||||
return m_format.formatByteSize(size, precision);
|
||||
}
|
||||
|
||||
QString Formats::formatDuration(quint64 msecs) const
|
||||
{
|
||||
return m_format.formatDuration(msecs);
|
||||
}
|
||||
|
||||
QString Formats::formatDecimalDuration(quint64 msecs, int decimalPlaces) const
|
||||
{
|
||||
return m_format.formatDecimalDuration(msecs, decimalPlaces);
|
||||
}
|
||||
|
||||
QString Formats::formatSpelloutDuration(quint64 msecs) const
|
||||
{
|
||||
return m_format.formatSpelloutDuration(msecs);
|
||||
}
|
||||
|
||||
QString Formats::formatRelativeDate(const QDate &date, QLocale::FormatType format) const
|
||||
{
|
||||
return m_format.formatRelativeDate(date, format);
|
||||
}
|
||||
|
||||
QString Formats::formatRelativeDateTime(const QDateTime &dateTime, QLocale::FormatType format) const
|
||||
{
|
||||
return m_format.formatRelativeDateTime(dateTime, format);
|
||||
}
|
103
src/declarativeimports/core/formats.h
Normal file
103
src/declarativeimports/core/formats.h
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright 2014 Bhushan Shah <bhush94@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of
|
||||
* the License or (at your option) version 3 or any later version
|
||||
* accepted by the membership of KDE e.V. (or its successor approved
|
||||
* by the membership of KDE e.V.), which shall act as a proxy
|
||||
* defined in Section 14 of version 3 of the license.
|
||||
*
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
#ifndef FORMATS_H
|
||||
#define FORMATS_H
|
||||
|
||||
#include <KFormat>
|
||||
|
||||
class Formats : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
explicit Formats (QObject* parent = 0);
|
||||
|
||||
/**
|
||||
* Converts size from bytes to the appropriate string representation
|
||||
*/
|
||||
Q_INVOKABLE QString formatByteSize(double size, int precision = 1) const;
|
||||
|
||||
/**
|
||||
* Given a number of milliseconds, converts that to a string containing
|
||||
* the localized equivalent, e.g. 1:23:45
|
||||
*/
|
||||
Q_INVOKABLE QString formatDuration(quint64 msecs) const;
|
||||
|
||||
/**
|
||||
* Given a number of milliseconds, converts that to a string containing
|
||||
* the localized equivalent to the requested decimal places.
|
||||
*
|
||||
* e.g. given formatDuration(60000), returns "1.0 minutes"
|
||||
*/
|
||||
Q_INVOKABLE QString formatDecimalDuration(quint64 msecs, int decimalPlaces = 2) const;
|
||||
|
||||
/**
|
||||
* Given a number of milliseconds, converts that to a spell-out string containing
|
||||
* the localized equivalent.
|
||||
*
|
||||
* e.g. given formatSpelloutDuration(60001) returns "1 minute"
|
||||
* given formatSpelloutDuration(62005) returns "1 minute and 2 seconds"
|
||||
* given formatSpelloutDuration(90060000) returns "1 day and 1 hour"
|
||||
*
|
||||
* Units not interesting to the user, for example seconds or minutes when the first
|
||||
* unit is day, are not returned because they are irrelevant. The same applies for
|
||||
* seconds when the first unit is hour.
|
||||
*
|
||||
*/
|
||||
Q_INVOKABLE QString formatSpelloutDuration(quint64 msecs) const;
|
||||
|
||||
/**
|
||||
* Returns a string formatted to a relative date style.
|
||||
*
|
||||
* If the date falls within one week before or after the current date
|
||||
* then a relative date string will be returned, such as:
|
||||
* * Yesterday
|
||||
* * Today
|
||||
* * Tomorrow
|
||||
* * Last Tuesday
|
||||
* * Next Wednesday
|
||||
*
|
||||
* If the date falls outside this period then the format is used
|
||||
*/
|
||||
Q_INVOKABLE QString formatRelativeDate(const QDate &date, QLocale::FormatType format) const;
|
||||
|
||||
/**
|
||||
* Returns a string formatted to a relative datetime style.
|
||||
*
|
||||
* If the dateTime falls within one week before or after the current date
|
||||
* then a relative date string will be returned, such as:
|
||||
* * Yesterday, 3:00pm
|
||||
* * Today, 3:00pm
|
||||
* * Tomorrow, 3:00pm
|
||||
* * Last Tuesday, 3:00pm
|
||||
* * Next Wednesday, 3:00pm
|
||||
*
|
||||
* If the datetime falls outside this period then the format is used
|
||||
*/
|
||||
Q_INVOKABLE QString formatRelativeDateTime(const QDateTime &dateTime, QLocale::FormatType format) const;
|
||||
|
||||
private:
|
||||
|
||||
KFormat m_format;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user