add ability do not translate strings in DateTime formatter

option X-AW-Translate, ApiVer 2
This commit is contained in:
Evgenii Alekseev 2016-07-23 19:49:34 +03:00
parent 9e59b22c32
commit 064cd7e44f
5 changed files with 72 additions and 3 deletions

View File

@ -54,7 +54,7 @@ QString AWDateTimeFormatter::convert(const QVariant &_value) const
{
qCDebug(LOG_LIB) << "Convert value" << _value;
return _value.toDateTime().toString(m_format);
return m_locale.toString(_value.toDateTime(), m_format);
}
@ -67,6 +67,7 @@ AWDateTimeFormatter *AWDateTimeFormatter::copy(const QString _fileName,
= new AWDateTimeFormatter(static_cast<QWidget *>(parent()), _fileName);
AWAbstractFormatter::copyDefaults(item);
item->setFormat(format());
item->setTranslateString(translateString());
item->setNumber(_number);
return item;
@ -79,6 +80,12 @@ QString AWDateTimeFormatter::format() const
}
bool AWDateTimeFormatter::translateString() const
{
return m_translate;
}
void AWDateTimeFormatter::setFormat(const QString _format)
{
qCDebug(LOG_LIB) << "Set format" << _format;
@ -87,6 +94,15 @@ void AWDateTimeFormatter::setFormat(const QString _format)
}
void AWDateTimeFormatter::setTranslateString(const bool _translate)
{
qCDebug(LOG_LIB) << "Set translate string" << _translate;
m_translate = _translate;
initLocale();
}
void AWDateTimeFormatter::readConfiguration()
{
AWAbstractFormatter::readConfiguration();
@ -95,6 +111,8 @@ void AWDateTimeFormatter::readConfiguration()
settings.beginGroup(QString("Desktop Entry"));
setFormat(settings.value(QString("X-AW-Format"), format()).toString());
setTranslateString(
settings.value(QString("X-AW-Translate"), translateString()).toBool());
settings.endGroup();
bumpApi(AWEFAPI);
@ -109,6 +127,8 @@ int AWDateTimeFormatter::showConfiguration(const QVariant args)
ui->lineEdit_comment->setText(comment());
ui->label_typeValue->setText(QString("DateTime"));
ui->lineEdit_format->setText(format());
ui->checkBox_translate->setCheckState(translateString() ? Qt::Checked
: Qt::Unchecked);
int ret = exec();
if (ret != 1)
@ -117,6 +137,7 @@ int AWDateTimeFormatter::showConfiguration(const QVariant args)
setComment(ui->lineEdit_comment->text());
setStrType(ui->label_typeValue->text());
setFormat(ui->lineEdit_format->text());
setTranslateString(ui->checkBox_translate->checkState() == Qt::Checked);
writeConfiguration();
return ret;
@ -132,16 +153,24 @@ void AWDateTimeFormatter::writeConfiguration() const
settings.beginGroup(QString("Desktop Entry"));
settings.setValue(QString("X-AW-Format"), format());
settings.setValue(QString("X-AW-Translate"), translateString());
settings.endGroup();
settings.sync();
}
void AWDateTimeFormatter::initLocale()
{
m_locale = m_translate ? QLocale::system() : QLocale::c();
}
void AWDateTimeFormatter::translate()
{
ui->label_name->setText(i18n("Name"));
ui->label_comment->setText(i18n("Comment"));
ui->label_type->setText(i18n("Type"));
ui->label_format->setText(i18n("Format"));
ui->checkBox_translate->setText(i18n("Transalte strings"));
}

View File

@ -19,6 +19,7 @@
#define AWDATETIMEFORMATTER_H
#include "awabstractformatter.h"
#include <QLocale>
namespace Ui
@ -30,6 +31,8 @@ class AWDateTimeFormatter : public AWAbstractFormatter
{
Q_OBJECT
Q_PROPERTY(QString format READ format WRITE setFormat)
Q_PROPERTY(
bool translateString READ translateString WRITE setTranslateString)
public:
explicit AWDateTimeFormatter(QWidget *parent,
@ -39,7 +42,9 @@ public:
AWDateTimeFormatter *copy(const QString _fileName, const int _number);
// properties
QString format() const;
bool translateString() const;
void setFormat(const QString _format);
void setTranslateString(const bool _translate);
public slots:
void readConfiguration();
@ -48,9 +53,12 @@ public slots:
private:
Ui::AWDateTimeFormatter *ui = nullptr;
void initLocale();
void translate();
// properties
QLocale m_locale;
QString m_format = QString();
bool m_translate = true;
};

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>420</width>
<height>157</height>
<height>185</height>
</rect>
</property>
<property name="windowTitle">
@ -104,6 +104,36 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="layout_translate">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="checkBox_translate">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Translate strings</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">

View File

@ -56,10 +56,12 @@ void TestAWDateTimeFormatter::test_conversion()
void TestAWDateTimeFormatter::test_copy()
{
formatter->setTranslateString(false);
AWDateTimeFormatter *newFormatter
= formatter->copy(QString("/dev/null"), 1);
QCOMPARE(newFormatter->format(), formatter->format());
QCOMPARE(newFormatter->translateString(), formatter->translateString());
QCOMPARE(newFormatter->number(), 1);
delete newFormatter;

View File

@ -35,7 +35,7 @@
// extweather api version
#define AWEWAPI 3
// formatter api version
#define AWEFAPI 1
#define AWEFAPI 2
// network requests timeout, ms
#define REQUEST_TIMEOUT 3000
// available time keys