0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-27 09:58:11 +03:00
LimeReport/limereport/items/lrhorizontallayout.cpp

245 lines
9.3 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 "lrdesignelementsfactory.h"
#include <QDebug>
#include <QObject>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include "lrbasedesignintf.h"
const QString xmlTag = "HLayout";
namespace {
LimeReport::BaseDesignIntf *createHLayout(QObject *owner, LimeReport::BaseDesignIntf *parent)
{
return new LimeReport::HorizontalLayout(owner, parent);
}
2016-10-18 15:00:26 +03:00
bool VARIABLE_IS_NOT_USED registred = LimeReport::DesignElementsFactory::instance().registerCreator(
2016-02-17 10:11:00 +03:00
xmlTag,
2016-02-17 10:19:50 +03:00
LimeReport::ItemAttribs(QObject::tr("HLayout"), LimeReport::Const::bandTAG),
2016-02-17 10:11:00 +03:00
createHLayout
);
}
namespace LimeReport {
2018-06-21 14:29:00 +03:00
bool horizontalLessThen(BaseDesignIntf *c1, BaseDesignIntf* c2){
2016-02-17 10:11:00 +03:00
return c1->pos().x()<c2->pos().x();
}
HorizontalLayout::HorizontalLayout(QObject *owner, QGraphicsItem *parent)
2018-06-21 14:29:00 +03:00
: AbstractLayout(xmlTag, owner, parent)
{}
2016-02-17 10:11:00 +03:00
HorizontalLayout::~HorizontalLayout()
2018-06-21 14:29:00 +03:00
{}
2016-02-17 10:11:00 +03:00
BaseDesignIntf *HorizontalLayout::createSameTypeItem(QObject *owner, QGraphicsItem *parent)
{
return new LimeReport::HorizontalLayout(owner, parent);
}
bool HorizontalLayout::canBeSplitted(int height) const
{
foreach(QGraphicsItem* qgItem,childItems()){
BaseDesignIntf* item=dynamic_cast<BaseDesignIntf*>(qgItem);
if (item)
2018-06-21 14:29:00 +03:00
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)
{
HorizontalLayout* upperPart = dynamic_cast<HorizontalLayout*>(createSameTypeItem(owner,parent));
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();
} else {
item->cloneEmpty(sliceHeight,upperPart,upperPart);
item->setPos(item->pos().x(),item->pos().y()+((height+1)-item->geometry().top()));
}
}
}
foreach(BaseDesignIntf* item, upperPart->childBaseItems()){
item->setHeight((maxHeight<height)?maxHeight:height);
}
upperPart->setHeight(height);
return upperPart;
}
BaseDesignIntf *HorizontalLayout::cloneBottomPart(int height, QObject *owner, QGraphicsItem *parent)
{
qreal maxHeight = 0;
HorizontalLayout* bottomPart = dynamic_cast<HorizontalLayout*>(createSameTypeItem(owner,parent));
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())
maxHeight = tmpItem->height();
}
}
if (!bottomPart->isEmpty()){
foreach (BaseDesignIntf* item, bottomPart->childBaseItems()) {
item->setHeight(maxHeight);
}
bottomPart->setHeight(maxHeight);
}
return bottomPart;
}
2016-02-17 10:28:27 +03:00
void HorizontalLayout::setItemAlign(const BaseDesignIntf::ItemAlign &itemAlign)
{
if (itemAlign == ParentWidthItemAlign)
setLayoutType(Table);
BaseDesignIntf::setItemAlign(itemAlign);
}
2019-02-02 00:28:30 +03:00
void HorizontalLayout::sortChildren()
{
2021-08-23 08:07:08 +03:00
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;
2019-01-14 23:36:27 +03:00
qreal w = spaceBorder*2;
2016-02-17 10:11:00 +03:00
qreal h = 0;
int visibleItemCount = 0;
2018-06-21 14:29:00 +03:00
foreach(BaseDesignIntf* item, layoutsChildren()){
if (item->isEmpty() && hideEmptyItems()) item->setVisible(false);
2016-04-20 19:59:33 +03:00
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
}
2019-01-14 23:36:27 +03:00
if (h>0) setHeight(h+spaceBorder*2);
if (layoutType() == Layout)
2019-02-06 18:41:56 +03:00
setWidth(w + layoutSpacingMM() * (visibleItemCount-1));
2019-01-14 23:36:27 +03:00
else{
relocateChildren();
if (!isRelocating()){
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;
2019-02-05 22:34:38 +03:00
if (layoutsChildren().count() < childItems().size()-1){
2018-06-21 14:29:00 +03:00
layoutsChildren().clear();
2016-02-17 10:11:00 +03:00
foreach (BaseDesignIntf* item, childBaseItems()) {
2018-06-21 14:29:00 +03:00
layoutsChildren().append(item);
2016-02-17 10:11:00 +03:00
}
}
2021-08-23 08:07:08 +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()) {
2017-09-21 22:02:13 +03:00
if (item->isVisible() || itemMode() == DesignMode){
2016-04-20 19:59:33 +03:00
item->setPos(curX,spaceBorder);
2019-02-06 18:41:56 +03:00
curX += item->width() + layoutSpacingMM();
2016-04-20 19:59:33 +03:00
item->setHeight(height()-(spaceBorder * 2));
}
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
}
2016-02-17 10:28:27 +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;
2018-06-21 14:29:00 +03:00
foreach(BaseDesignIntf* item, layoutsChildren()){
2017-09-21 22:02:13 +03:00
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
2019-02-06 18:41:56 +03:00
itemsSumSize += layoutSpacingMM() * (visibleItemsCount-1);
2019-01-14 23:36:27 +03:00
if (itemMode() == DesignMode && !layoutsChildren().isEmpty()){
qreal delta = (width() - (itemsSumSize+spaceBorder*2));
layoutsChildren().last()->setWidth(layoutsChildren().last()->width()+delta);
} else {
qreal delta = (width() - (itemsSumSize+spaceBorder*2)) / (visibleItemsCount!=0 ? visibleItemsCount : 1);
for (int i=0; i<layoutsChildren().size(); ++i){
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());
}
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
}
2018-06-21 14:29:00 +03:00
void HorizontalLayout::insertItemInLayout(BaseDesignIntf* item)
2016-02-17 10:11:00 +03:00
{
2018-06-21 14:29:00 +03:00
foreach (BaseDesignIntf* child, childBaseItems()) {
if (child->pos() == item->pos()){
int index = layoutsChildren().indexOf(child)-1;
layoutsChildren().insert(index, item);
child->setPos(item->pos().x()+item->width(), 0);
break;
}
2016-02-17 10:11:00 +03:00
}
}
} // namespace LimeReport