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

299 lines
7.7 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.
***************************************************************************/
#ifndef XQITEM_H
#define XQITEM_H
#include <QVariant>
#include <QVector>
#include <QStandardItem>
#include <xqnode.h>
using XQItemList = QList<QStandardItem*>;
//class QStyleOptionViewItem;
class XQItemFactory;
class XQItemType;
/**
* @brief Extends QStandardItem to hold additional
* settings.
*/
class XQItem : public QStandardItem
{
public:
//friend class XQItemType;
//friend class XQItemFactory;
/// Die data(enum role) Infrastruktur wird sowohl für XQItem als auch
/// für den XQItemType verwendet, deshalb definieren wir hier _alle_
/// notwendigen Roles
enum ItemDataRole
{
NoRole = Qt::UserRole + 1,
// das ist ein pointer auf den original-string aus dem XML
ContentRole,
ItemTypeRole,
RenderStyleRole,
EditorTypeRole,
UnitTypeRole,
ContentFormatRole,
// @see qstandarditemmodel.cpp, stolen from there
FlagsRole = Qt::ItemDataRole(Qt::UserRole - 1),
IconRole = Qt::DecorationRole,
// re-start count
FixedChoicesRole = ContentFormatRole+1,
ContentNodeRole,
//?? werden beide gebraucht?
SheetNodeRole,
// das ist der Schlüssel um den content string aus dem contentNode zu holen
//ContentKeyRole,
// weitere, weniger gebräuchlichen Rollen für XQItemType::data()
TypeKeyRole,
//TypeNameRole, nicht so wichtig
/*
renderStyleToStringRole,
editorTypeToStringRole,
unitTypeToStringRole,
*/
RoleEnd
};
// wie wirds gemalt
enum RenderStyle
{
NoRenderStyle,
HiddenStyle,
HeaderStyle,
PlainStyle,
CheckBoxStyle,
ComboBoxStyle,
PickerStyle,
SpinBoxStyle,
ProgressBarStyle,
FormattedStyle,
TreeHeaderStyle,
CustomRenderStyle,
RenderStyleEnd //!< Not a special editor. Keep at end of this enumeration!
// ...
/*
PickerStyle
LineEditStyle ,
UserDefStyle ,
*/
};
// wie wirds editiert
enum EditorType
{
NoEditorType,
LineEditType,
ComboBoxType,
PickerType,
ProgressBarType,
SpinBoxType,
CustomEditorType,
EditorTypeEnd
};
// typische Einheiten
enum UnitType
{
NoUnitType,
Ampere,
Volt,
Ohm,
Watt,
WattPeak,
WattHour,
Farad,
Tesla,
Henry,
Hertz,
Coulomb,
Kelvin,
Percent,
Second,
Meter,
Kg,
ISODate,
UnitTypeEnd
};
//XQItem(int rows, int columns = 1);
XQItem();
XQItem( XQItemType* itemType );
XQItem( XQItemType* itemType, const QString* content );
XQItem( XQItemType* itemType, const QString* content, const XQNodePtr& contentNode );
/*
XQItem(const QString& text);
XQItem(const QIcon& icon, const QString& text);
XQItem(int rows, int columns);
*/
XQItem(const XQItem& other);
XQItem(const QStandardItem& other);
virtual ~XQItem();
//! creates not a clone but a new default item, \see QStandardItemModel::setItemPrototype()
//! -- not used at the moment --
XQItem* clone() const override;
bool isValid() const;
// shortcuts auf XQNodePtr
virtual XQNodePtr contentNode() const;
virtual void setContentNode(const XQNodePtr& contentNode );
virtual XQNodePtr sheetNode() const;
virtual void setSheetNode( const XQNodePtr& sheetNode );
bool hasAttribute( const QString& attribKey ) const;
const QString& attribute( const QString& attribKey, const QString& defaultValue="" ) const;
bool testAttribute( const QString& attribKey, const QString& attribValue ) const;
XQItemType& itemType() const;
void setItemType( XQItemType* itemTypePtr );
// shortcuts für die itemFlags
// __fix! das können die selber !?
void addFlag( Qt::ItemFlag newFlag );
void clearFlag( Qt::ItemFlag newFlag );
//das ist ein Sonderfall: Ein ist ein dereferenzierter Zeiger auf 'unser' Atrribut in
// XQNodePtr, also unserem contentNode(). Das wird hier direkt aufgelöst und nicht auf
// data() umgeleitet.
const QString& content() const;
const QString& contentKey() const;
void setContent( const QString* content );
//
// Convenience-Funktionen zum Memberzugriff, die Implementierung
// läuft über die data()-Methode wie in den QStandardItems. So
// ist sie für XQItem und auch für XQItemType als Deirvat von XQItem gültig.
//
RenderStyle renderStyle() const;
QString renderStyleToString() const;
void setRenderStyle(RenderStyle renderStyle );
EditorType editorType() const;
QString editorTypeToString() const;
void setEditorType(EditorType editorType);
UnitType unitType() const;
QString unitTypeToString() const;
void setUnitType(UnitType unitType);
QString contentFormat() const;
void setContentFormat(const QString& contentFormat);
QStandardItemModel* fixedChoices() const;
QString fixedChoicesToString() const;
void setfixedChoices( QStandardItemModel* newModel );
//
//shortCuts
//
bool isHeaderStyle();
// ...
QVariant data(int role = Qt::DisplayRole ) const override;
void setData(const QVariant &value, int role ) override;
// Das sind die die items im tree links: icon,text, node pointer
//XQItem( const QString& text, XQNodePtr contentNode );
//XQItem( const QString& text, XQItemType::RenderStyle renderStyle );
/*
template<class T>
void setToVariant(T entry, QtExtUserRoles::NTDataRoles role)
{
setData(QVariant::fromValue<T>(entry), role);
}
template<class T>
T getFromVariant(QtExtUserRoles::NTDataRoles role)
{
return data(role).value<T>();
}
*/
///
/// Static convenience methods
///
static const XQItem& xqConstItemFromIndex( const QModelIndex& index );
static XQItem& xqItemFromIndex( const QModelIndex& index );
static XQItem& fallBackDummyItem();
static int fetchItemDataRole( const QString& dataRoleKey );
static QString fetchItemDataRoleName( int dataRole );
static Qt::ItemFlag fetchItemFlag( const QString& flagKey );
static QString fetchItemFlagName( int flag );
static RenderStyle fetchRenderStyle( const QString& styleKey );
static QString fetchRenderStyleToString( RenderStyle renderStyle );
static EditorType fetchEditorType( const QString& editorTypeKey );
static QString fetchEditorTypeToString( EditorType editorType );
static UnitType fetchUnitType( const QString& typeKey );
static QString fetchUnitTypeToString( UnitType );
protected:
using XQItemFlagMap = QMap<QString,int>;
using XQItemDataRoleMap = QMap<QString,int>;
using XQRenderStyleMap = QMap<QString,RenderStyle>;
using XQEditorTypeMap = QMap<QString,EditorType>;
using XQUnitTypeMap = QMap<UnitType, QString>;
using XQPrefixExponentMap = QMap<QString, int>;
static XQItemFlagMap s_ItemFlagMap;
static XQItemDataRoleMap s_ItemDataRoleMap;
static XQRenderStyleMap s_RenderStyleMap;
static XQEditorTypeMap s_EditorTypeMap;
static XQUnitTypeMap s_UnitTypeMap;
static XQPrefixExponentMap s_PrefixExponentMap;
};
Q_DECLARE_METATYPE(XQItem::RenderStyle);
Q_DECLARE_METATYPE(XQItem::EditorType);
Q_DECLARE_METATYPE(XQItem::UnitType);
Q_DECLARE_METATYPE(const QString*);
#endif // XQITEM_H