0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-23 16:22:58 +03:00
LimeReport/limereport/lrreporttranslation.cpp

198 lines
6.8 KiB
C++
Raw Normal View History

#include "lrreporttranslation.h"
#include "lrbasedesignintf.h"
#include "lrpagedesignintf.h"
namespace LimeReport {
ReportTranslation::ReportTranslation(QLocale::Language language, QList<PageDesignIntf*> pages):
m_language(language)
{
foreach (PageDesignIntf* page, pages) {
m_pagesTranslation.append(createPageTranslation(page));
}
}
ReportTranslation::ReportTranslation(const ReportTranslation& reportTranslation):
m_language(reportTranslation.m_language)
{
foreach (PageTranslation* pageTranslation, reportTranslation.m_pagesTranslation) {
m_pagesTranslation.append(pageTranslation);
}
}
ReportTranslation::~ReportTranslation()
{
foreach (PageTranslation* page, m_pagesTranslation) {
delete page;
}
m_pagesTranslation.clear();
}
PageTranslation* ReportTranslation::createPageTranslation(PageDesignIntf* page)
{
PageTranslation* pageTranslation = new PageTranslation;
2019-07-24 03:13:23 +03:00
pageTranslation->checked = true;
pageTranslation->pageName = page->pageItem()->objectName();
foreach (BaseDesignIntf* item, page->pageItem()->allChildBaseItems()) {
2017-08-05 01:38:19 +03:00
createItemTranslation(item, pageTranslation);
}
return pageTranslation;
}
void ReportTranslation::createItemTranslation(BaseDesignIntf* item,
PageTranslation* pageTranslation)
{
QMap<QString, QString> stringsForTranslation = item->getStringForTranslation();
if (!stringsForTranslation.isEmpty()) {
2017-08-05 01:38:19 +03:00
ItemTranslation* itemTranslation = new ItemTranslation;
2019-07-24 03:13:23 +03:00
itemTranslation->checked = true;
2017-08-05 01:38:19 +03:00
itemTranslation->itemName = item->objectName();
foreach (QString propertyName, stringsForTranslation.keys()) {
2017-08-05 01:38:19 +03:00
PropertyTranslation* propertyTranslation = new PropertyTranslation;
propertyTranslation->propertyName = propertyName;
propertyTranslation->value = stringsForTranslation.value(propertyName);
propertyTranslation->sourceValue = stringsForTranslation.value(propertyName);
propertyTranslation->checked = false;
propertyTranslation->sourceHasBeenChanged = false;
itemTranslation->propertyesTranslation.append(propertyTranslation);
}
pageTranslation->itemsTranslation.insert(itemTranslation->itemName, itemTranslation);
}
}
2019-07-24 03:13:23 +03:00
PageTranslation* ReportTranslation::findPageTranslation(const QString& pageName)
2017-08-05 01:38:19 +03:00
{
foreach (PageTranslation* page, m_pagesTranslation) {
if (page->pageName.compare(pageName) == 0) {
2017-08-05 01:38:19 +03:00
return page;
}
}
return 0;
}
void ReportTranslation::renamePage(const QString& oldName, const QString& newName)
2019-07-24 03:13:23 +03:00
{
PageTranslation* page = findPageTranslation(oldName);
if (page) {
2019-07-24 03:13:23 +03:00
page->pageName = newName;
}
}
void ReportTranslation::invalidatePages()
{
foreach (PageTranslation* page, m_pagesTranslation) {
2019-07-24 03:13:23 +03:00
page->checked = false;
}
}
void ReportTranslation::clearInvalidPages()
{
QList<PageTranslation*>::Iterator it = m_pagesTranslation.begin();
while (it != m_pagesTranslation.end()) {
if (!(*it)->checked) {
2019-07-24 03:13:23 +03:00
delete *it;
it = m_pagesTranslation.erase(it);
} else
++it;
2019-07-24 03:13:23 +03:00
}
}
2017-08-05 01:38:19 +03:00
void ReportTranslation::updatePageTranslation(PageDesignIntf* page)
{
2018-08-23 00:49:38 +03:00
PageTranslation* pageTranslation = findPageTranslation(page->pageItem()->objectName());
if (!pageTranslation) {
pageTranslation = createPageTranslation(page);
m_pagesTranslation.append(pageTranslation);
2017-08-05 01:38:19 +03:00
}
if (pageTranslation) {
2019-07-24 03:13:23 +03:00
pageTranslation->checked = true;
foreach (ItemTranslation* item, pageTranslation->itemsTranslation) {
2019-07-24 03:13:23 +03:00
item->checked = false;
}
foreach (BaseDesignIntf* item, page->pageItem()->allChildBaseItems()) {
QMap<QString, QString> stringsForTranslation = item->getStringForTranslation();
if (!stringsForTranslation.isEmpty()) {
ItemTranslation* itemTranslation
= pageTranslation->itemsTranslation.value(item->objectName());
if (itemTranslation) {
foreach (QString propertyName, stringsForTranslation.keys()) {
PropertyTranslation* propertyTranslation
= itemTranslation->findProperty(propertyName);
bool translated
= propertyTranslation->sourceValue != propertyTranslation->value;
2018-09-01 12:36:25 +03:00
if (propertyTranslation->checked)
propertyTranslation->sourceHasBeenChanged
= propertyTranslation->sourceValue
!= stringsForTranslation.value(propertyName);
2018-09-01 12:36:25 +03:00
if (propertyTranslation->sourceHasBeenChanged)
propertyTranslation->checked = false;
propertyTranslation->sourceValue
= stringsForTranslation.value(propertyName);
if (!translated)
propertyTranslation->value = propertyTranslation->sourceValue;
2017-08-05 01:38:19 +03:00
}
2019-07-24 03:13:23 +03:00
itemTranslation->checked = true;
2017-08-05 01:38:19 +03:00
} else {
createItemTranslation(item, pageTranslation);
2017-08-05 01:38:19 +03:00
}
}
}
2019-07-24 03:13:23 +03:00
QHash<QString, ItemTranslation*>::Iterator it = pageTranslation->itemsTranslation.begin();
while (it != pageTranslation->itemsTranslation.end()) {
2019-07-24 03:13:23 +03:00
if (!it.value()->checked) {
delete it.value();
it = pageTranslation->itemsTranslation.erase(it);
} else
++it;
2019-07-24 03:13:23 +03:00
}
}
}
QList<PageTranslation*>& ReportTranslation::pagesTranslation() { return m_pagesTranslation; }
PageTranslation* ReportTranslation::createEmptyPageTranslation()
{
PageTranslation* pageTranslation = new PageTranslation;
m_pagesTranslation.append(pageTranslation);
return pageTranslation;
}
QLocale::Language ReportTranslation::language() const { return m_language; }
2017-08-05 01:38:19 +03:00
PropertyTranslation* ItemTranslation::findProperty(const QString& propertyName)
{
foreach (PropertyTranslation* propertyTranslation, propertyesTranslation) {
if (propertyTranslation->propertyName.compare(propertyName) == 0) {
2017-08-05 01:38:19 +03:00
return propertyTranslation;
}
}
return 0;
}
ItemTranslation::~ItemTranslation()
{
foreach (PropertyTranslation* property, propertyesTranslation) {
2017-08-05 01:38:19 +03:00
delete property;
}
}
PageTranslation::~PageTranslation()
{
foreach (ItemTranslation* item, itemsTranslation) {
2017-08-05 01:38:19 +03:00
delete item;
}
}
void PageTranslation::renameItem(const QString& oldName, const QString& newName)
2019-07-24 03:13:23 +03:00
{
ItemTranslation* item = itemsTranslation.value(oldName);
if (item) {
2019-07-24 03:13:23 +03:00
itemsTranslation.remove(oldName);
item->itemName = newName;
itemsTranslation[newName] = item;
}
}
} // namespace LimeReport