LimeReport/limereport/objectinspector/propertyItems/lrflagspropitem.cpp

159 lines
7.2 KiB
C++
Raw Normal View History

2016-02-17 10:11:00 +03:00
/***************************************************************************
* This file is part of the Lime Report project *
2021-08-18 20:21:36 +03:00
* Copyright (C) 2021 by Alexander Arin *
2016-02-17 10:11:00 +03:00
* arin_a@bk.ru *
* *
** GNU General Public License Usage **
* *
* This library 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 3 of the License, or *
* (at your option) any later version. *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
** GNU Lesser General Public License **
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library. *
* If not, see <http://www.gnu.org/licenses/>. *
* *
* This library 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. *
****************************************************************************/
#include "lrflagspropitem.h"
#include "lrenumpropitem.h"
#include "lrboolpropitem.h"
#include "../editors/lrcheckboxeditor.h"
#include "lrobjectitemmodel.h"
#include <QIcon>
#include <QImage>
#include <QPainter>
#include <QStyle>
#include <QStylePainter>
namespace {
LimeReport::ObjectPropItem * createFlagsPropItem(
QObject *object, LimeReport::ObjectPropItem::ObjectsList* objects, const QString& name, const QString& displayName, const QVariant& data, LimeReport::ObjectPropItem* parent, bool readonly)
{
return new LimeReport::FlagsPropItem(object, objects, name, displayName, data, parent, readonly);
}
2016-10-18 15:00:26 +03:00
bool VARIABLE_IS_NOT_USED registred = LimeReport::ObjectPropFactory::instance().registerCreator(
2016-02-17 10:11:00 +03:00
LimeReport::APropIdent("flags",""),QObject::tr("flags"),createFlagsPropItem
);
} // namespace
namespace LimeReport {
void FlagsPropItem::createChildren()
{
QMetaEnum propEnum = object()->metaObject()->property(object()->metaObject()->indexOfProperty(propertyName().toLatin1())).enumerator();
for (int i=0;i<propEnum.keyCount();i++)
{
if ( propEnum.keyToValue(propEnum.key(i)) !=0 ) {
this->appendItem(new LimeReport::FlagPropItem(
object(), objects(), QString(propEnum.key(i)), tr(propEnum.key(i)),
bool((propertyValue().toInt() & propEnum.keyToValue(propEnum.key(i)))==propEnum.keyToValue(propEnum.key(i))),
this, false
)
);
}
2016-02-17 10:11:00 +03:00
}
}
void FlagsPropItem::updateChildren()
{
QMetaEnum propEnum = object()->metaObject()->property(object()->metaObject()->indexOfProperty(propertyName().toLatin1())).enumerator();
for (int i=0;i<propEnum.keyCount();i++)
{
ObjectPropItem* property = findChild(QString(propEnum.key(i)));
if (property)
property->setPropertyValue(bool((propertyValue().toInt() & propEnum.keyToValue(propEnum.key(i)))==propEnum.keyToValue(propEnum.key(i))));
}
}
FlagsPropItem::FlagsPropItem(QObject *object, ObjectPropItem::ObjectsList *objects, const QString &name, const QString &displayName, const QVariant &value, ObjectPropItem *parent, bool readonly)
:ObjectPropItem(object, objects, name, displayName, value, parent, readonly)
{
createChildren();
}
FlagsPropItem::FlagsPropItem(QObject *object, ObjectPropItem::ObjectsList *objects, const QString &name, const QString &displayName, const QVariant &value, ObjectPropItem *parent, bool readonly, QSet<int> acceptableValues)
:ObjectPropItem(object, objects, name, displayName, value, parent, readonly),m_acceptableValues(acceptableValues){}
QString FlagsPropItem::displayValue() const
{
QString result;
QMetaEnum propEnum = object()->metaObject()->property(object()->metaObject()->indexOfProperty(propertyName().toLatin1())).enumerator();
for (int i=0;i<propEnum.keyCount();i++)
{
if ((propEnum.keyToValue(propEnum.key(i)) == 0) ? propertyValue().toInt() == 0 : (propertyValue().toInt() & propEnum.keyToValue(propEnum.key(i))) == propEnum.keyToValue(propEnum.key(i)))
2016-02-17 10:11:00 +03:00
{
if (result.isEmpty()) result+= isTranslateProperty() ? tr(propEnum.key(i)) : propEnum.key(i);
else result=result+" | "+ (isTranslateProperty() ? tr(propEnum.key(i)) : propEnum.key(i));
2016-02-17 10:11:00 +03:00
}
}
return result;
}
void FlagsPropItem::setPropertyValue(QVariant value)
{
ObjectPropItem::setPropertyValue(value);
updateChildren();
}
void FlagsPropItem::slotEnumChanged(QString /*text*/)
{
}
void FlagsPropItem::translateFlagsItem()
{
tr("NoLine");
tr("TopLine");
tr("BottomLine");
tr("LeftLine");
tr("RightLine");
2018-05-24 14:31:10 +03:00
tr("AllLines");
}
2016-02-17 10:11:00 +03:00
FlagPropItem::FlagPropItem(QObject* object, ObjectsList* objects, const QString &propName, const QString &displayName, const QVariant &value, ObjectPropItem* parent, bool readonly)
:BoolPropItem(object, objects, propName,displayName,value,parent,readonly)
{
}
void FlagPropItem::setPropertyEditorData(QWidget *propertyEditor, const QModelIndex &/*index*/) const
{
CheckBoxEditor *editor = qobject_cast<CheckBoxEditor*>(propertyEditor);
editor->setChecked(propertyValue().toBool());
}
void FlagPropItem::setModelData(QWidget *propertyEditor, QAbstractItemModel *model, const QModelIndex &index)
{
bool value = qobject_cast<CheckBoxEditor*>(propertyEditor)->isChecked();
model->setData(index,value);
int flags = object()->property(parent()->propertyName().toLatin1()).toInt();
if (value) flags = flags | valueByName(propertyName());
else if (flags & valueByName(propertyName())) flags = flags ^ valueByName(propertyName());
2016-02-17 10:39:17 +03:00
setValueToObject(parent()->propertyName(),flags);
2016-02-17 10:11:00 +03:00
parent()->setPropertyValue(flags);
}
int FlagPropItem::valueByName(const QString& typeName)
{
QMetaEnum propEnum = object()->metaObject()->property(object()->metaObject()->indexOfProperty(parent()->propertyName().toLatin1())).enumerator();
return propEnum.keyToValue(typeName.toLatin1());
}
} // namespace LimeReport