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

253 lines
9.5 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 "lrhorizontallayout.h"
#include "lrbasedesignintf.h"
2016-02-17 10:11:00 +03:00
#include "lrdesignelementsfactory.h"
#include <QDebug>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QObject>
2016-02-17 10:11:00 +03:00
const QString xmlTag = "HLayout";
namespace {
LimeReport::BaseDesignIntf* createHLayout(QObject* owner, LimeReport::BaseDesignIntf* parent)
2016-02-17 10:11:00 +03:00
{
return new LimeReport::HorizontalLayout(owner, parent);
}
2016-10-18 15:00:26 +03:00
bool VARIABLE_IS_NOT_USED registred = LimeReport::DesignElementsFactory::instance().registerCreator(
xmlTag, LimeReport::ItemAttribs(QObject::tr("HLayout"), LimeReport::Const::bandTAG),
createHLayout);
} // namespace
2016-02-17 10:11:00 +03:00
namespace LimeReport {
bool horizontalLessThen(BaseDesignIntf* c1, BaseDesignIntf* c2)
{
return c1->pos().x() < c2->pos().x();
2016-02-17 10:11:00 +03:00
}
HorizontalLayout::HorizontalLayout(QObject* owner, QGraphicsItem* parent):
AbstractLayout(xmlTag, owner, parent)
{
}
2016-02-17 10:11:00 +03:00
HorizontalLayout::~HorizontalLayout() { }
2016-02-17 10:11:00 +03:00
BaseDesignIntf* HorizontalLayout::createSameTypeItem(QObject* owner, QGraphicsItem* parent)
2016-02-17 10:11:00 +03:00
{
return new LimeReport::HorizontalLayout(owner, parent);
}
bool HorizontalLayout::canBeSplitted(int height) const
{
foreach (QGraphicsItem* qgItem, childItems()) {
BaseDesignIntf* item = dynamic_cast<BaseDesignIntf*>(qgItem);
2016-02-17 10:11:00 +03:00
if (item)
if (!item->canBeSplitted(height - item->pos().y()))
return false;
2016-02-17 10:11:00 +03:00
}
return true;
}
BaseDesignIntf* HorizontalLayout::cloneUpperPart(int height, QObject* owner, QGraphicsItem* parent)
2016-02-17 10:11:00 +03:00
{
HorizontalLayout* upperPart
= dynamic_cast<HorizontalLayout*>(createSameTypeItem(owner, parent));
2016-02-17 10:11:00 +03:00
upperPart->initFromItem(this);
qreal maxHeight = 0;
foreach (BaseDesignIntf* item, childBaseItems()) {
if ((item->geometry().top() < height) && (item->geometry().bottom() > height)) {
int sliceHeight = height - item->geometry().top();
if (item->canBeSplitted(sliceHeight)) {
BaseDesignIntf* slicedPart
= item->cloneUpperPart(sliceHeight, upperPart, upperPart);
if (maxHeight < slicedPart->height())
maxHeight = slicedPart->height();
2016-02-17 10:11:00 +03:00
} else {
item->cloneEmpty(sliceHeight, upperPart, upperPart);
item->setPos(item->pos().x(),
item->pos().y() + ((height + 1) - item->geometry().top()));
2016-02-17 10:11:00 +03:00
}
}
}
foreach (BaseDesignIntf* item, upperPart->childBaseItems()) {
item->setHeight((maxHeight < height) ? maxHeight : height);
2016-02-17 10:11:00 +03:00
}
upperPart->setHeight(height);
return upperPart;
}
BaseDesignIntf* HorizontalLayout::cloneBottomPart(int height, QObject* owner, QGraphicsItem* parent)
2016-02-17 10:11:00 +03:00
{
qreal maxHeight = 0;
HorizontalLayout* bottomPart
= dynamic_cast<HorizontalLayout*>(createSameTypeItem(owner, parent));
2016-02-17 10:11:00 +03:00
bottomPart->initFromItem(this);
foreach (BaseDesignIntf* item, childBaseItems()) {
if ((item->geometry().top() < height) && (item->geometry().bottom() > height)) {
BaseDesignIntf* tmpItem = item->cloneBottomPart(height, bottomPart, bottomPart);
tmpItem->setPos(tmpItem->pos().x(), 0);
if (maxHeight < tmpItem->height())
2016-02-17 10:11:00 +03:00
maxHeight = tmpItem->height();
}
}
if (!bottomPart->isEmpty()) {
2016-02-17 10:11:00 +03:00
foreach (BaseDesignIntf* item, bottomPart->childBaseItems()) {
item->setHeight(maxHeight);
}
bottomPart->setHeight(maxHeight);
}
return bottomPart;
}
void HorizontalLayout::setItemAlign(const BaseDesignIntf::ItemAlign& itemAlign)
2016-02-17 10:28:27 +03:00
{
if (itemAlign == ParentWidthItemAlign)
setLayoutType(Table);
BaseDesignIntf::setItemAlign(itemAlign);
}
2019-02-02 00:28:30 +03:00
void HorizontalLayout::sortChildren()
{
std::sort(layoutsChildren().begin(), layoutsChildren().end(), horizontalLessThen);
2019-02-02 00:28:30 +03:00
}
2016-02-17 10:11:00 +03:00
void HorizontalLayout::updateLayoutSize()
{
2017-09-21 13:51:03 +03:00
int spaceBorder = (borderLines() != 0) ? borderLineSize() : 0;
qreal w = spaceBorder * 2;
2016-02-17 10:11:00 +03:00
qreal h = 0;
int visibleItemCount = 0;
foreach (BaseDesignIntf* item, layoutsChildren()) {
if (item->isEmpty() && hideEmptyItems())
item->setVisible(false);
if (item->isVisible()) {
if (h < item->height())
h = item->height();
w += item->width();
visibleItemCount++;
2016-04-20 19:59:33 +03:00
}
2016-02-17 10:11:00 +03:00
}
if (h > 0)
setHeight(h + spaceBorder * 2);
2019-01-14 23:36:27 +03:00
if (layoutType() == Layout)
setWidth(w + layoutSpacingMM() * (visibleItemCount - 1));
else {
2019-01-14 23:36:27 +03:00
relocateChildren();
if (!isRelocating()) {
2019-01-14 23:36:27 +03:00
divideSpace();
}
}
2016-02-17 10:11:00 +03:00
}
void HorizontalLayout::relocateChildren()
{
2016-04-20 19:59:33 +03:00
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();
2016-02-17 10:11:00 +03:00
foreach (BaseDesignIntf* item, childBaseItems()) {
if (!oldChildren.contains(item)) {
newChildren.append(item);
}
2018-06-21 14:29:00 +03:00
layoutsChildren().append(item);
2016-02-17 10:11:00 +03:00
}
}
std::sort(layoutsChildren().begin(), layoutsChildren().end(), horizontalLessThen);
2016-04-20 19:59:33 +03:00
qreal curX = spaceBorder;
2018-06-21 14:29:00 +03:00
setIsRelocating(true);
foreach (BaseDesignIntf* item, layoutsChildren()) {
if (item->isVisible() || itemMode() == DesignMode) {
item->setPos(curX, spaceBorder);
2019-02-06 18:41:56 +03:00
curX += item->width() + layoutSpacingMM();
item->setHeight(height() - (spaceBorder * 2));
2016-04-20 19:59:33 +03:00
}
2016-02-17 10:11:00 +03:00
}
2018-06-21 14:29:00 +03:00
setIsRelocating(false);
for (BaseDesignIntf* item : newChildren) {
2022-02-06 17:14:04 +03:00
connectToLayout(item);
}
2016-02-17 10:11:00 +03:00
}
void HorizontalLayout::divideSpace()
{
2018-06-21 14:29:00 +03:00
setIsRelocating(true);
2016-02-17 10:28:27 +03:00
qreal itemsSumSize = 0;
2017-09-21 13:51:03 +03:00
int visibleItemsCount = 0;
int spaceBorder = (borderLines() != 0) ? borderLineSize() : 0;
foreach (BaseDesignIntf* item, layoutsChildren()) {
if (item->isVisible() || itemMode() == DesignMode) {
2017-09-21 13:51:03 +03:00
itemsSumSize += item->width();
visibleItemsCount++;
}
2016-02-17 10:28:27 +03:00
}
2017-09-21 13:51:03 +03:00
itemsSumSize += layoutSpacingMM() * (visibleItemsCount - 1);
if (itemMode() == DesignMode && !layoutsChildren().isEmpty()) {
qreal delta = (width() - (itemsSumSize + spaceBorder * 2));
layoutsChildren().last()->setWidth(layoutsChildren().last()->width() + delta);
2019-01-14 23:36:27 +03:00
} else {
qreal delta = (width() - (itemsSumSize + spaceBorder * 2))
/ (visibleItemsCount != 0 ? visibleItemsCount : 1);
for (int i = 0; i < layoutsChildren().size(); ++i) {
2019-01-14 23:36:27 +03:00
if (layoutsChildren()[i]->isVisible() || itemMode() == DesignMode)
layoutsChildren()[i]->setWidth(layoutsChildren()[i]->width() + delta);
if ((i + 1) < layoutsChildren().size())
if (layoutsChildren()[i + 1]->isVisible() || itemMode() == DesignMode)
layoutsChildren()[i + 1]->setPos(layoutsChildren()[i + 1]->pos().x()
+ delta * (i + 1),
layoutsChildren()[i + 1]->pos().y());
2019-01-14 23:36:27 +03:00
}
2016-02-17 10:11:00 +03:00
}
2018-06-21 14:29:00 +03:00
setIsRelocating(false);
2016-02-17 10:11:00 +03:00
}
2018-06-21 14:29:00 +03:00
void HorizontalLayout::placeItemInLayout(BaseDesignIntf* item)
2016-02-17 10:11:00 +03:00
{
2018-06-21 14:29:00 +03:00
if (layoutsChildren().count() > 0)
item->setPos(layoutsChildren().last()->pos().x() + layoutsChildren().last()->width(), 0);
else
item->setPos(0, 0);
2016-02-17 10:11:00 +03:00
}
} // namespace LimeReport