0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-25 00:54:39 +03:00
LimeReport/limereport/items/lrverticallayout.cpp

199 lines
6.5 KiB
C++
Raw Normal View History

2018-06-21 14:29:00 +03:00
#include "lrverticallayout.h"
#include "lrbasedesignintf.h"
#include "lrdesignelementsfactory.h"
const QString xmlTag = "VLayout";
namespace {
LimeReport::BaseDesignIntf* createVLayout(QObject* owner, LimeReport::BaseDesignIntf* parent)
2018-06-21 14:29:00 +03:00
{
return new LimeReport::VerticalLayout(owner, parent);
}
bool VARIABLE_IS_NOT_USED registred = LimeReport::DesignElementsFactory::instance().registerCreator(
xmlTag, LimeReport::ItemAttribs(QObject::tr("VLayout"), LimeReport::Const::bandTAG),
createVLayout);
} // namespace
2018-06-21 14:29:00 +03:00
namespace LimeReport {
2018-06-21 14:29:00 +03:00
bool verticalLessThen(BaseDesignIntf* c1, BaseDesignIntf* c2)
{
return c1->pos().y() < c2->pos().y();
2018-06-21 14:29:00 +03:00
}
VerticalLayout::VerticalLayout(QObject* owner, QGraphicsItem* parent):
AbstractLayout(xmlTag, owner, parent)
{
}
2018-06-21 14:29:00 +03:00
VerticalLayout::~VerticalLayout() { }
2018-06-21 14:29:00 +03:00
BaseDesignIntf* VerticalLayout::createSameTypeItem(QObject* owner, QGraphicsItem* parent)
{
return new LimeReport::VerticalLayout(owner, parent);
}
void VerticalLayout::updateLayoutSize()
{
int spaceBorder = (borderLines() != 0) ? borderLineSize() : 0;
int h = spaceBorder * 2;
2018-06-21 14:29:00 +03:00
qreal w = 0;
int visibleItemCount = 0;
foreach (BaseDesignIntf* item, layoutsChildren()) {
if (item->isEmpty() && hideEmptyItems())
item->setVisible(false);
if (item->isVisible()) {
if (w < item->width())
w = item->width();
h += item->height();
visibleItemCount++;
2018-06-21 14:29:00 +03:00
}
}
if (w > 0)
setWidth(w + spaceBorder * 2);
setHeight(h + layoutSpacingMM() * (visibleItemCount - 1));
2018-06-21 14:29:00 +03:00
}
void VerticalLayout::relocateChildren()
{
int spaceBorder = (borderLines() != 0) ? borderLineSize() : 0;
QList<BaseDesignIntf*> newChildren;
if (layoutsChildren().count() < childItems().size() - 1) {
auto oldChildren = layoutsChildren();
2018-06-21 14:29:00 +03:00
layoutsChildren().clear();
foreach (BaseDesignIntf* item, childBaseItems()) {
if (!oldChildren.contains(item)) {
newChildren.append(item);
}
2018-06-21 14:29:00 +03:00
layoutsChildren().append(item);
}
}
std::sort(layoutsChildren().begin(), layoutsChildren().end(), verticalLessThen);
2018-06-21 14:29:00 +03:00
qreal curY = spaceBorder;
setIsRelocating(true);
foreach (BaseDesignIntf* item, layoutsChildren()) {
if (item->isVisible() || itemMode() == DesignMode) {
2018-06-21 14:29:00 +03:00
item->setPos(spaceBorder, curY);
curY += item->height() + layoutSpacingMM();
2018-06-21 14:29:00 +03:00
item->setWidth(width() - (spaceBorder * 2));
}
}
setIsRelocating(false);
for (BaseDesignIntf* item : newChildren) {
2022-02-06 17:14:04 +03:00
connectToLayout(item);
}
2018-06-21 14:29:00 +03:00
}
bool VerticalLayout::canBeSplitted(int height) const
{
if (childItems().isEmpty())
return false;
2018-06-21 14:29:00 +03:00
BaseDesignIntf* item = dynamic_cast<BaseDesignIntf*>(childItems().at(0));
if (item) {
if (item->height() > height)
2018-06-21 14:29:00 +03:00
return item->canBeSplitted(height);
else
return true;
2018-06-21 14:29:00 +03:00
}
return false;
}
BaseDesignIntf* VerticalLayout::cloneUpperPart(int height, QObject* owner, QGraphicsItem* parent)
{
VerticalLayout* upperPart = dynamic_cast<VerticalLayout*>(createSameTypeItem(owner, parent));
2018-06-21 14:29:00 +03:00
upperPart->initFromItem(this);
foreach (BaseDesignIntf* item, childBaseItems()) {
if ((item->geometry().bottom() <= height)) {
item->cloneItem(item->itemMode(), upperPart, upperPart);
2018-06-21 14:29:00 +03:00
} else {
if ((item->geometry().top() < height) && (item->geometry().bottom() > height)) {
2018-06-21 14:29:00 +03:00
int sliceHeight = height - item->geometry().top();
if (item->isSplittable() && item->canBeSplitted(sliceHeight)) {
item->cloneUpperPart(sliceHeight, upperPart, upperPart);
2018-06-21 14:29:00 +03:00
}
}
}
}
upperPart->setHeight(height);
return upperPart;
}
BaseDesignIntf* VerticalLayout::cloneBottomPart(int height, QObject* owner, QGraphicsItem* parent)
{
VerticalLayout* bottomPart = dynamic_cast<VerticalLayout*>(createSameTypeItem(owner, parent));
2018-06-21 14:29:00 +03:00
bottomPart->initFromItem(this);
foreach (BaseDesignIntf* item, childBaseItems()) {
if (item->geometry().bottom() > height) {
2018-06-21 14:29:00 +03:00
int sliceHeight = height - item->geometry().top();
if ((item->geometry().top() < height) && (item->canBeSplitted(sliceHeight))) {
BaseDesignIntf* tmpItem
= item->cloneBottomPart(sliceHeight, bottomPart, bottomPart);
2018-06-21 14:29:00 +03:00
tmpItem->setHeight(sliceHeight);
2020-12-25 23:02:33 +03:00
bottomPart->addChild(tmpItem);
2018-06-21 14:29:00 +03:00
} else {
2020-12-25 23:02:33 +03:00
bottomPart->addChild(item->cloneItem(item->itemMode(), bottomPart, bottomPart));
2018-06-21 14:29:00 +03:00
}
}
}
if (!bottomPart->isEmpty()) {
2019-06-27 10:45:26 +03:00
int currentHeight = 0;
2018-06-21 14:29:00 +03:00
foreach (BaseDesignIntf* item, bottomPart->childBaseItems()) {
currentHeight += item->height();
2018-06-21 14:29:00 +03:00
}
bottomPart->setHeight(currentHeight);
}
return bottomPart;
}
2019-02-02 00:28:30 +03:00
void VerticalLayout::sortChildren()
{
std::sort(layoutsChildren().begin(), layoutsChildren().end(), verticalLessThen);
2019-02-02 00:28:30 +03:00
}
2018-06-21 14:29:00 +03:00
void VerticalLayout::divideSpace()
{
setIsRelocating(true);
qreal itemsSumSize = 0;
int visibleItemsCount = 0;
int spaceBorder = (borderLines() != 0) ? borderLineSize() : 0;
foreach (BaseDesignIntf* item, layoutsChildren()) {
if (item->isVisible() || itemMode() == DesignMode) {
2018-06-21 14:29:00 +03:00
itemsSumSize += item->height();
visibleItemsCount++;
}
}
2019-02-06 18:41:56 +03:00
itemsSumSize += layoutSpacingMM() * (visibleItemsCount - 1);
qreal delta = (height() - (itemsSumSize + spaceBorder * 2))
/ (visibleItemsCount != 0 ? visibleItemsCount : 1);
2018-06-21 14:29:00 +03:00
for (int i = 0; i < layoutsChildren().size(); ++i) {
2018-06-21 14:29:00 +03:00
if (layoutsChildren()[i]->isVisible() || itemMode() == DesignMode)
layoutsChildren()[i]->setHeight(layoutsChildren()[i]->height() + delta);
if ((i + 1) < layoutsChildren().size())
if (layoutsChildren()[i + 1]->isVisible() || itemMode() == DesignMode)
layoutsChildren()[i + 1]->setPos(layoutsChildren()[i + 1]->pos().x(),
layoutsChildren()[i + 1]->pos().y()
+ delta * (i + 1));
2018-06-21 14:29:00 +03:00
}
setIsRelocating(false);
}
void VerticalLayout::placeItemInLayout(BaseDesignIntf* item)
{
if (layoutsChildren().count() > 0)
item->setPos(0, layoutsChildren().last()->pos().y() + layoutsChildren().last()->height());
else
item->setPos(0, 0);
}
2019-02-02 00:28:30 +03:00
} // namespace LimeReport