Files
xtree.ng.zwo/items/xqitemtype.cpp
2025-08-13 18:30:47 +02:00

260 lines
6.0 KiB
C++

/***************************************************************************
source::worx xtree
Copyright © 2024-2025 c.holzheuer
christoph.holzheuer@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) any later version.
***************************************************************************/
//#include <cmath>
#include <xqitemtype.h>
#include <QDateTime>
#include <QDebug>
XQItemTypeMap XQItemType::s_ItemTypeMap;
size_t XQItemType::s_ItemTypeCount = 0;
///
/// XQItemType
///
XQItemType::XQItemType()
: XQItem(nullptr) // vermeide rekursion
{
}
XQItemType::XQItemType( const XQItemType& other)
: XQItem( other )
{
}
XQItemType::~XQItemType()
{
}
QVariant XQItemType::data( int role ) const
{
return QStandardItem::data(role);
}
void XQItemType::setData(const QVariant &value, int role )
{
return QStandardItem::setData(value,role);
}
void XQItemType::replaceAttribute( XQItem* item, const QVariant& newValue, int role )
{
// hat sich überhaupt was geändert?
QVariant oldValue = data(role);
// ja, es hat
if( oldValue != newValue )
{
XQItemType* myClone = new XQItemType(*this);
// Änderungen übernehmen
myClone->setData( newValue, role );
// Gibt es den geänderten ItemType schon?
QString newKey = myClone->makeItemTypeKey();
// jawoll
if( s_ItemTypeMap.contains( newKey ) )
{
// nur abräumen, sonst nix
delete myClone;
}
else
{
// speichern
s_ItemTypeMap.insert( newKey, myClone );
// und ins item übernehmen
item->setItemType( myClone );
/// Obacht! Der alte, geänderte itemType bleibt erhaltent
/// und verrottet ggf. ohne Daseinszweck
}
}
}
QString XQItemType::formatToSI( const QString& valueTxt ) const
{
/*
if( valueTxt.isEmpty() )
return valueTxt;
if( XQItem::ISODate == _unitType )
{
// format iso date
QDateTime dateTime = QDateTime::fromString(valueTxt, Qt::ISODate);
// fixme! make this configurable!
QString format = "dd.MM.yyyy HH:mm:ss"; // You can customize this format
// Format the QDateTime object into a human-readable string
return dateTime.toString(format);
}
QLocale sysLocale = QLocale::system();
sysLocale.setNumberOptions(sysLocale.numberOptions() | QLocale::OmitGroupSeparator);
double dVal = sysLocale.toDouble(valueTxt);
QString strVal, strPrefix;
int exp = (int)::log10f(dVal);
exp = (exp/3)*3;
double nVal = dVal;
if( !s_PrefixExponentMap.key(exp).isEmpty() )
nVal /= ::pow( 10,exp);
strVal = sysLocale.toString(nVal, 'f', 2);
strPrefix = s_PrefixExponentMap.key(exp);
//qDebug() << " convert: " << dVal << " : " << valueTxt << ": " << strVal << ":" << exp << " : " << strPrefix << ": " << nVal;
return QString("%1 %2%3").arg( strVal, strPrefix, unitTypeToString() );
*/
return "fitze!";
}
QString XQItemType::unFormatFromSI(const QString& formText ) const
{
/*
QString input = formText.simplified();
// #1: strip numeric part
if( input.isEmpty() )
return input;
int idx = 0;
for( auto c : input )
{
if( c.isNumber() || c.toLower() == 'e' || c == '.' || c == ',' ||c == '-' || c == '+' )
idx++;
else
break;
}
if(!idx)
return QString("0");
QString numPart = formText.left(idx);
QString unitPart;
//if(idx + 1 < formText.size() )
unitPart = formText.right(idx - 1).simplified();
QLocale sysLocale = QLocale::system();
double dVal = sysLocale.toDouble(numPart);
if( unitPart.size() > 0 )
{
QString prefix = QString(unitPart[0]);
if( s_PrefixExponentMap.contains(prefix) )
dVal *= std::pow( 10.0, s_PrefixExponentMap[prefix] );
}
sysLocale.setNumberOptions(sysLocale.numberOptions() | QLocale::OmitGroupSeparator);
QString result = sysLocale.toString(dVal, 'f', 2);
//qDebug() << " convert: " << numPart << " : " << unitPart << " : " << dVal << " : " << result;
return result;
*/
return "fitze!";
}
///
/// --- statics --------------------------------------------------------------------------
///
XQItemType* XQItemType::staticItemType()
{
static XQItemType s_DummyItemType;
return &s_DummyItemType;
}
//
// Das ist eigentlich Blödsinn, KISS baby KISS!
// Die ItemTypes sollten statisch bleiben, zusätzliche
// oder geänderte Attribute eines Items landen dann
// halt in G.N. in QStadardItem::data Vector!
//
/*
XQItemType* XQItemType::storeItemType(XQItemType* protoType)
{
// haben wir den prototype schon?
QString itemTypeKey = protoType->makeItemTypeKey();
if(s_ItemTypeMap.contains( itemTypeKey ) )
{
// dann weg damit ...
delete protoType;
// ... und die alte Version zurückgeben
return s_ItemTypeMap[itemTypeKey];
}
// sonst: wir speichern den prototype
s_ItemTypeMap.insert( itemTypeKey, protoType);
s_ItemTypeCount++;
qDebug() << " --- ItemTypes: " << s_ItemTypeCount;
return protoType;
}
*/
QString XQItemType::makeItemTypeKey()
{
QString key("%1:%2:%3:%4:%5:%6:%7");
key = key.arg( renderStyleToString() );
key = key.arg( editorTypeToString() );
key = key.arg( unitTypeToString() );
key = key.arg( contentFormat() );
key = key.arg( data(XQItem::FlagsRole).toString() );
key = key.arg( icon().name() );
key = key.arg( fixedChoicesToString() );
/*
static const QList<XQItem::ItemDataRole> roleList
{
XQItem::RenderStyleRole,
XQItem::EditorTypeRole,
XQItem::UnitTypeRole,
XQItem::ContentFormatRole,
XQItem::FlagsRole,
XQItem::IconRole,
XQItem::FixedChoicesRole
};
QString key;
for( const auto role : roleList )
{
qDebug() << " --- YYY trying: " << XQItem::fetchItemDataRoleName( role );
QVariant entry = data(role);
if( !entry.isNull() && entry.isValid())
{
// fckin' sonderlocke:
key += role == XQItem::IconRole ? entry.value<QIcon>().name() : entry.toString();
}
key += ":";
}
*/
//qDebug() << " --- YES: KEY: " << key;
return key;
}