logplus/logPlus/BorderPreView.cpp
2026-06-24 15:33:12 +08:00

627 lines
21 KiB
C++

#include "BorderPreView.h"
#include <QPainter>
#include <QMouseEvent>
#include <QPair>
#include <QPen>
#define MARGIN_SIZE 5
#define LINECON_SZIE 10
BorderPreView::BorderPreView(QWidget *parent)
: QWidget(parent)
{
m_pCelll=(NULL);
m_selectCurrentStyle=(-1);
m_selectCurrentLineWidth=(-1);
m_isBorderStyle=(false);
m_TopState=m_RightState=m_BottomState=m_LeftState=true;
m_ResetArea=false;
}
BorderPreView::~BorderPreView()
{
}
void BorderPreView::SetBorderStyle(int width,int style)
{
m_selectCurrentStyle=style;
m_selectCurrentLineWidth=width;
m_isBorderStyle=true;
}
void BorderPreView::paintEvent(QPaintEvent * pEvt)
{
QPainter paint;
QRect viewRect=this->rect();
paint.begin(this);
paint.save();
paint.setPen(Qt::NoPen);
paint.setBrush(Qt::white);
paint.drawRect(viewRect);
paint.restore();
// lines horn
DrawBackgroundHorn(&paint);
BuildBorderLines();
if(m_ResetArea)
{
BuidlClickEventAreas();
}
DrawBorderLines(&paint);
paint.end();
}
void BorderPreView::SetCellObject(FormTableItem *pCell)
{
m_pCelll=pCell;
}
void BorderPreView::BuidlClickEventAreas()
{
//m_AreaHandler
int marginValue=LINECON_SZIE+MARGIN_SIZE;
QRect viewRect=rect();
int areaMargin=marginValue/2.0;
int width=areaMargin*2;
int height=viewRect.height()-marginValue*2;
QRectF topRect,leftRect,rightRect,bottomRect;
QPointF point= m_TopLine.p1();
point.setY( areaMargin);
topRect.setTopLeft(point);
topRect.setWidth(height);
topRect.setHeight(width);
point= m_LeftLine.p1();
point.setX(areaMargin);
leftRect.setTopLeft(point);
leftRect.setWidth(width);
leftRect.setHeight(height);
point= m_RightLine.p1();
point.setX(point.x()-areaMargin);
rightRect.setTopLeft(point);
rightRect.setWidth(width);
rightRect.setHeight(height);
point= m_BottomLine.p2();
point.setY(point.y()-areaMargin);
bottomRect.setTopLeft(point);
bottomRect.setWidth(height);
bottomRect.setHeight(width);
m_AreaEventHandler.clear();
m_AreaEventHandler.insert(CellTop,topRect);
m_AreaEventHandler.insert(CellLeft,leftRect);
m_AreaEventHandler.insert(CellBottom,bottomRect);
m_AreaEventHandler.insert(CellRight,rightRect);
}
void BorderPreView::BuildBorderLines()
{
int marginValue=LINECON_SZIE+MARGIN_SIZE;
int outVlaue=5;
QPointF point,p1,p2;
QRect viewRect=rect();
point=viewRect.topLeft();
p1.setX(point.x()+marginValue);
p1.setY(point.y()+marginValue+outVlaue);
point=viewRect.bottomLeft();
p2.setX(point.x()+marginValue);
p2.setY(point.y()-(marginValue+outVlaue));
m_LeftLine.setP1(p1);
m_LeftLine.setP2(p2);
//////////////////////////////////////////////////////////////////////////
point=viewRect.topLeft();
p1.setX(point.x()+marginValue+outVlaue);
p1.setY(point.y()+marginValue);
point=viewRect.topRight();
p2.setX(point.x()-(marginValue+outVlaue));
p2.setY(point.y()+marginValue);
m_TopLine.setP1(p1);
m_TopLine.setP2(p2);
//////////////////////////////////////////////////////////////////////////
point=viewRect.topRight();
p1.setX(point.x()-marginValue);
p1.setY(point.y()+(marginValue+outVlaue));
point=viewRect.bottomRight();
p2.setX(point.x()-marginValue);
p2.setY(point.y()-(marginValue+outVlaue));
m_RightLine.setP1(p1);
m_RightLine.setP2(p2);
//////////////////////////////////////////////////////////////////////////
point=viewRect.bottomRight();
p1.setX(point.x()-(marginValue+outVlaue));
p1.setY(point.y()-marginValue);
point=viewRect.bottomLeft();
p2.setX(point.x()+marginValue+outVlaue);
p2.setY(point.y()-marginValue);
m_BottomLine.setP1(p1);
m_BottomLine.setP2(p2);
//////////////////////////////////////////////////////////////////////////
}
void BorderPreView::DrawBorderLines(QPainter *paint)
{
// CSheet* pHeadSheet =NULL;
// if(m_pDigitizer->m_bSelectGraphicsHead)
// {
// pHeadSheet =m_pDigitizer->m_pDoc->GetHeadSheet( m_pDigitizer->m_curGraphicsHeadSheetIndex );
// }else if(m_pDigitizer->m_bSelectGraphicsFoot)
// {
// pHeadSheet =m_pDigitizer->m_pDoc->GetFootSheet( m_pDigitizer->m_curGraphicsFootSheetIndex );
// }
// m_pSheet=pHeadSheet;
FormTableItem *m_pCell=NULL;
if(m_pCelll) m_pCell=m_pCelll;
// else {
// if(m_pSheet)m_pCell=m_pSheet->GetSelGraphicsHeadCell();
// }
if(!m_pCell) return;
paint->save();
if (m_pCell)
{
switch(m_pCell->m_drawBorder)
{
case FormTableItem::All:
{
paint->setPen(QPen(Qt::black,m_pCell->TopBorderWidth,(Qt::PenStyle)m_pCell->TopBorderStyle));
paint->drawLine(m_TopLine);
paint->setPen(QPen(Qt::black,m_pCell->RightBorderWidth,(Qt::PenStyle)m_pCell->RightBorderStyle));
paint->drawLine(m_RightLine);
paint->setPen(QPen(Qt::black,m_pCell->BottomBorderWidth,(Qt::PenStyle)m_pCell->BottomBorderStyle));
paint->drawLine(m_BottomLine);
paint->setPen(QPen(Qt::black,m_pCell->LeftBorderWidth,(Qt::PenStyle)m_pCell->LeftBorderStyle));
paint->drawLine(m_LeftLine);
m_TopState=m_RightState=m_BottomState=m_LeftState=true;
}
break;
case FormTableItem::Top:
{
paint->setPen(QPen(Qt::black,m_pCell->TopBorderWidth,(Qt::PenStyle)m_pCell->TopBorderStyle));
paint->drawLine(m_TopLine);
m_TopState=true;
m_RightState=m_BottomState=m_LeftState=false;
}
break;
case FormTableItem::Right:
{
paint->setPen(QPen(Qt::black,m_pCell->RightBorderWidth,(Qt::PenStyle)m_pCell->RightBorderStyle));
paint->drawLine(m_RightLine);
m_RightState=true;
m_TopState=m_BottomState=m_LeftState=false;
}
break;
case FormTableItem::Bottom:
{
paint->setPen(QPen(Qt::black,m_pCell->BottomBorderWidth,(Qt::PenStyle)m_pCell->BottomBorderStyle));
paint->drawLine(m_BottomLine);
m_BottomState=true;
m_TopState=m_RightState=m_LeftState=false;
}
break;
case FormTableItem::Left:
{
paint->setPen(QPen(Qt::black,m_pCell->LeftBorderWidth,(Qt::PenStyle)m_pCell->LeftBorderStyle));
paint->drawLine(m_LeftLine);
m_LeftState=true;
m_TopState=m_RightState=m_BottomState=false;
}
break;
case FormTableItem::TopRight:
{
paint->setPen(QPen(Qt::black,m_pCell->TopBorderWidth,(Qt::PenStyle)m_pCell->TopBorderStyle));
paint->drawLine(m_TopLine);
paint->setPen(QPen(Qt::black,m_pCell->RightBorderWidth,(Qt::PenStyle)m_pCell->RightBorderStyle));
paint->drawLine(m_RightLine);
m_TopState=m_RightState=true;
m_LeftState=m_BottomState=false;
}
break;
case FormTableItem::RightBottom:
{
paint->setPen(QPen(Qt::black,m_pCell->RightBorderWidth,(Qt::PenStyle)m_pCell->RightBorderStyle));
paint->drawLine(m_RightLine);
paint->setPen(QPen(Qt::black,m_pCell->BottomBorderWidth,(Qt::PenStyle)m_pCell->BottomBorderStyle));
paint->drawLine(m_BottomLine);
m_BottomState=m_RightState=true;
m_LeftState=m_TopState=false;
}
break;
case FormTableItem::BottomLeft:
{
paint->setPen(QPen(Qt::black,m_pCell->BottomBorderWidth,(Qt::PenStyle)m_pCell->BottomBorderStyle));
paint->drawLine(m_BottomLine);
paint->setPen(QPen(Qt::black,m_pCell->LeftBorderWidth,(Qt::PenStyle)m_pCell->LeftBorderStyle));
paint->drawLine(m_LeftLine);
m_BottomState=m_LeftState=true;
m_RightState=m_TopState=false;
}
break;
case FormTableItem::LeftTop:
{
paint->setPen(QPen(Qt::black,m_pCell->LeftBorderWidth,(Qt::PenStyle)m_pCell->LeftBorderStyle));
paint->drawLine(m_LeftLine);
paint->setPen(QPen(Qt::black,m_pCell->TopBorderWidth,(Qt::PenStyle)m_pCell->TopBorderStyle));
paint->drawLine(m_TopLine);
m_TopState=m_LeftState=true;
m_RightState=m_BottomState=false;
}
break;
case FormTableItem::TopBottom:
{
paint->setPen(QPen(Qt::black,m_pCell->TopBorderWidth,(Qt::PenStyle)m_pCell->TopBorderStyle));
paint->drawLine(m_TopLine);
paint->setPen(QPen(Qt::black,m_pCell->BottomBorderWidth,(Qt::PenStyle)m_pCell->BottomBorderStyle));
paint->drawLine(m_BottomLine);
m_TopState=m_BottomState=true;
m_RightState=m_LeftState=false;
}
break;
case FormTableItem::LeftRight:
{
paint->setPen(QPen(Qt::black,m_pCell->LeftBorderWidth,(Qt::PenStyle)m_pCell->LeftBorderStyle));
paint->drawLine(m_LeftLine);
paint->setPen(QPen(Qt::black,m_pCell->RightBorderWidth,(Qt::PenStyle)m_pCell->RightBorderStyle));
paint->drawLine(m_RightLine);
m_TopState=m_BottomState=false;
m_RightState=m_LeftState=true;
}
break;
case FormTableItem::TopRightBottom:
{
paint->setPen(QPen(Qt::black,m_pCell->TopBorderWidth,(Qt::PenStyle)m_pCell->TopBorderStyle));
paint->drawLine(m_TopLine);
paint->setPen(QPen(Qt::black,m_pCell->RightBorderWidth,(Qt::PenStyle)m_pCell->RightBorderStyle));
paint->drawLine(m_RightLine);
paint->setPen(QPen(Qt::black,m_pCell->BottomBorderWidth,(Qt::PenStyle)m_pCell->BottomBorderStyle));
paint->drawLine(m_BottomLine);
m_RightState=m_TopState=m_BottomState=true;
m_LeftState=false;
}
break;
case FormTableItem::RightBottomLeft:
{
paint->setPen(QPen(Qt::black,m_pCell->RightBorderWidth,(Qt::PenStyle)m_pCell->RightBorderStyle));
paint->drawLine(m_RightLine);
paint->setPen(QPen(Qt::black,m_pCell->BottomBorderWidth,(Qt::PenStyle)m_pCell->BottomBorderStyle));
paint->drawLine(m_BottomLine);
paint->setPen(QPen(Qt::black,m_pCell->LeftBorderWidth,(Qt::PenStyle)m_pCell->LeftBorderStyle));
paint->drawLine(m_LeftLine);
m_RightState=m_LeftState=m_BottomState=true;
m_TopState=false;
}
break;
case FormTableItem::BottomLeftTop:
{
paint->setPen(QPen(Qt::black,m_pCell->BottomBorderWidth,(Qt::PenStyle)m_pCell->BottomBorderStyle));
paint->drawLine(m_BottomLine);
paint->setPen(QPen(Qt::black,m_pCell->LeftBorderWidth,(Qt::PenStyle)m_pCell->LeftBorderStyle));
paint->drawLine(m_LeftLine);
paint->setPen(QPen(Qt::black,m_pCell->TopBorderWidth,(Qt::PenStyle)m_pCell->TopBorderStyle));
paint->drawLine(m_TopLine);
m_TopState=m_LeftState=m_BottomState=true;
m_RightState=false;
}
break;
case FormTableItem::LeftTopRight:
{
paint->setPen(QPen(Qt::black,m_pCell->LeftBorderWidth,(Qt::PenStyle)m_pCell->LeftBorderStyle));
paint->drawLine(m_LeftLine);
paint->setPen(QPen(Qt::black,m_pCell->TopBorderWidth,(Qt::PenStyle)m_pCell->TopBorderStyle));
paint->drawLine(m_TopLine);
paint->setPen(QPen(Qt::black,m_pCell->RightBorderWidth,(Qt::PenStyle)m_pCell->RightBorderStyle));
paint->drawLine(m_RightLine);
m_TopState=m_LeftState=m_RightState=true;
m_BottomState=false;
}
break;
default: //None
{
}
break;
}
}
paint->restore();
}
void BorderPreView::DrawBackgroundHorn(QPainter *paint)
{
QLineF line_topLeft1,line_topLeft2,line_bottomLeft1,line_bottomLeft2,
line_topRight1,line_topRight2,line_bottomRight1,line_bottomRight2;
QPointF point;
QRect viewRect=rect();
point=viewRect.topLeft();
line_topLeft1.setP1(QPointF(point.x()+MARGIN_SIZE,point.y()+MARGIN_SIZE+LINECON_SZIE));
line_topLeft1.setP2(QPointF(point.x()+MARGIN_SIZE+LINECON_SZIE,point.y()+MARGIN_SIZE+LINECON_SZIE));
line_topLeft2.setP1(QPointF(point.x()+MARGIN_SIZE+LINECON_SZIE,point.y()+MARGIN_SIZE));
line_topLeft2.setP2(QPointF(point.x()+MARGIN_SIZE+LINECON_SZIE,point.y()+MARGIN_SIZE+LINECON_SZIE));
point=viewRect.bottomLeft();
line_bottomLeft1.setP1(QPointF(point.x()+MARGIN_SIZE,point.y()-(MARGIN_SIZE+LINECON_SZIE)));
line_bottomLeft1.setP2(QPointF(point.x()+MARGIN_SIZE+LINECON_SZIE,point.y()-(MARGIN_SIZE+LINECON_SZIE)));
line_bottomLeft2.setP1(QPointF(point.x()+MARGIN_SIZE+LINECON_SZIE,point.y()-MARGIN_SIZE));
line_bottomLeft2.setP2(QPointF(point.x()+MARGIN_SIZE+LINECON_SZIE,point.y()-(MARGIN_SIZE+LINECON_SZIE)));
point=viewRect.bottomRight();
line_bottomRight1.setP1(QPointF(point.x()-(MARGIN_SIZE+LINECON_SZIE),point.y()-(MARGIN_SIZE)));
line_bottomRight1.setP2(QPointF(point.x()-(MARGIN_SIZE+LINECON_SZIE),point.y()-(MARGIN_SIZE+LINECON_SZIE)));
line_bottomRight2.setP1(QPointF(point.x()-(MARGIN_SIZE),point.y()-(MARGIN_SIZE+LINECON_SZIE)));
line_bottomRight2.setP2(QPointF(point.x()-(MARGIN_SIZE+LINECON_SZIE),point.y()-(MARGIN_SIZE+LINECON_SZIE)));
point=viewRect.topRight();
line_topRight1.setP1(QPointF(point.x()-(MARGIN_SIZE+LINECON_SZIE),point.y()+(MARGIN_SIZE)));
line_topRight1.setP2(QPointF(point.x()-(MARGIN_SIZE+LINECON_SZIE),point.y()+(MARGIN_SIZE+LINECON_SZIE)));
line_topRight2.setP1(QPointF(point.x()-(MARGIN_SIZE),point.y()+(MARGIN_SIZE+LINECON_SZIE)));
line_topRight2.setP2(QPointF(point.x()-(MARGIN_SIZE+LINECON_SZIE),point.y()+(MARGIN_SIZE+LINECON_SZIE)));
paint->save();
paint->drawLine(line_topLeft1);
paint->drawLine(line_topLeft2);
paint->drawLine(line_bottomLeft1);
paint->drawLine(line_bottomLeft2);
paint->drawLine(line_topRight1);
paint->drawLine(line_topRight2);
paint->drawLine(line_bottomRight1);
paint->drawLine(line_bottomRight2);
paint->restore();
}
void BorderPreView::mouseReleaseEvent(QMouseEvent *pEvt)
{
ClickBorder(pEvt->pos());
}
void BorderPreView::ClickBorder(QPointF point)
{
// CSheet* pHeadSheet =NULL;
// if(m_pDigitizer->m_bSelectGraphicsHead)
// {
// pHeadSheet =m_pDigitizer->m_pDoc->GetHeadSheet( m_pDigitizer->m_curGraphicsHeadSheetIndex );
// }else if(m_pDigitizer->m_bSelectGraphicsFoot)
// {
// pHeadSheet =m_pDigitizer->m_pDoc->GetFootSheet( m_pDigitizer->m_curGraphicsFootSheetIndex );
// }
QMap<BorderDirect,QRectF>::const_iterator itor= m_AreaEventHandler.constBegin();
BorderDirect bdirect=BorderDirect::None;
QRectF rectEvt;
FormTableItem *m_pCell=NULL;
if(m_pCelll) m_pCell=m_pCelll;
// else {
// m_pSheet=pHeadSheet;
// if(m_pSheet)m_pCell=m_pSheet->GetSelGraphicsHeadCell();
// }
if(!m_pCell) return;
for (;itor!=m_AreaEventHandler.end();++itor)
{
rectEvt= itor.value();
if (rectEvt.contains(point))
{
bdirect=itor.key();
break;
}
}
if (bdirect !=None)
{
switch(bdirect)
{
case CellTop:
if (m_selectCurrentStyle !=-1 && m_selectCurrentLineWidth !=-1)
{
m_pCell->TopBorderStyle=m_selectCurrentStyle;
m_pCell->TopBorderWidth=m_selectCurrentLineWidth;
}
if (!m_isBorderStyle)
{
m_TopState=!m_TopState;
}else
{
m_isBorderStyle=false;
}
break;
case CellRight:
if (m_selectCurrentStyle !=-1 && m_selectCurrentLineWidth !=-1)
{
m_pCell->RightBorderStyle=m_selectCurrentStyle;
m_pCell->RightBorderWidth=m_selectCurrentLineWidth;
}
if (!m_isBorderStyle)
{
m_RightState=!m_RightState;
}else
{
m_isBorderStyle=false;
}
break;
case CellBottom:
if (m_selectCurrentStyle !=-1 && m_selectCurrentLineWidth !=-1)
{
m_pCell->BottomBorderStyle=m_selectCurrentStyle;
m_pCell->BottomBorderWidth=m_selectCurrentLineWidth;
}
if (!m_isBorderStyle)
{
m_BottomState=!m_BottomState;
}else
{
m_isBorderStyle=false;
}
break;
case CellLeft:
if (m_selectCurrentStyle !=-1 && m_selectCurrentLineWidth !=-1)
{
m_pCell->LeftBorderStyle=m_selectCurrentStyle;
m_pCell->LeftBorderWidth=m_selectCurrentLineWidth;
}
if (!m_isBorderStyle)
{
m_LeftState=!m_LeftState;
}else
{
m_isBorderStyle=false;
}
break;
default:
break;
}
BindBorderStates();
}
this->update();
}
void BorderPreView::BindBorderStates()
{
// CSheet* pHeadSheet =NULL;
// if(m_pDigitizer->m_bSelectGraphicsHead)
// {
// pHeadSheet =m_pDigitizer->m_pDoc->GetHeadSheet( m_pDigitizer->m_curGraphicsHeadSheetIndex );
// }else if(m_pDigitizer->m_bSelectGraphicsFoot)
// {
// pHeadSheet =m_pDigitizer->m_pDoc->GetFootSheet( m_pDigitizer->m_curGraphicsFootSheetIndex );
// }
FormTableItem *m_pCell=NULL;
if(m_pCelll) m_pCell=m_pCelll;
// else {
// m_pSheet=pHeadSheet;
// if(m_pSheet)m_pCell=m_pSheet->GetSelGraphicsHeadCell();
// }
if (!m_pCell)
{
return;
}
bool isChkTop=m_TopState;//ui.btnTo->isChecked();
bool isChkRight=m_RightState;//ui.btnRight->isChecked();
bool isChkBottom=m_BottomState;//ui.btnBottom->isChecked();
bool isChkLeft=m_LeftState;//ui.btnLeft->isChecked();
FormTableItem::MetaBorder selBorder=FormTableItem::All;
bool isAll = (isChkTop && isChkRight && isChkBottom && isChkLeft);
bool isNone = (!isChkTop && !isChkRight && !isChkBottom && !isChkLeft);
bool isTop = (isChkTop && !isChkRight && !isChkBottom && !isChkLeft);
bool isRight = (!isChkTop && isChkRight && !isChkBottom && !isChkLeft);
bool isBottom = (!isChkTop && !isChkRight && isChkBottom && !isChkLeft);
bool isLeft = (!isChkTop && !isChkRight && !isChkBottom && isChkLeft);
bool isTopRight = (isChkTop && isChkRight && !isChkBottom && !isChkLeft);
bool isRightBottom = (!isChkTop && isChkRight && isChkBottom && !isChkLeft);
bool isBottomLeft = (!isChkTop && !isChkRight && isChkBottom && isChkLeft);
bool isLeftTop = (isChkTop && !isChkRight && !isChkBottom && isChkLeft);
bool isTopBottom = (isChkTop && !isChkRight && isChkBottom && !isChkLeft);
bool isLeftRight = (!isChkTop && isChkRight && !isChkBottom && isChkLeft);
bool isTopRightBottom = (isChkTop && isChkRight && isChkBottom && !isChkLeft);
bool isRightBottomLeft = (!isChkTop && isChkRight && isChkBottom && isChkLeft);
bool isBottomLeftTop = (isChkTop && !isChkRight && isChkBottom && isChkLeft);
bool isLeftTopRight = (isChkTop && isChkRight && !isChkBottom && isChkLeft);
if (isAll)
{
selBorder=FormTableItem::All;
}
if (isNone)
{
selBorder=FormTableItem::None;
}
if (isTop)
{
selBorder=FormTableItem::Top;
}
if (isRight)
{
selBorder=FormTableItem::Right;
}
if (isBottom)
{
selBorder=FormTableItem::Bottom;
}
if (isLeft)
{
selBorder=FormTableItem::Left;
}
if (isTopRight)
{
selBorder=FormTableItem::TopRight;
}
if (isRightBottom)
{
selBorder=FormTableItem::RightBottom;
}
if (isBottomLeft)
{
selBorder=FormTableItem::BottomLeft;
}
if (isLeftTop)
{
selBorder=FormTableItem::LeftTop;
}
if (isTopBottom)
{
selBorder=FormTableItem::TopBottom;
}
if (isLeftRight)
{
selBorder=FormTableItem::LeftRight;
}
if (isTopRightBottom)
{
selBorder=FormTableItem::TopRightBottom;
}
if (isRightBottomLeft)
{
selBorder=FormTableItem::RightBottomLeft;
}
if (isBottomLeftTop)
{
selBorder=FormTableItem::BottomLeftTop;
}
if (isLeftTopRight)
{
selBorder=FormTableItem::LeftTopRight;
}
m_pCell->m_drawBorder=selBorder;
//if(m_pSheet)m_pSheet->SetFormBorders(m_pCell);
}
void BorderPreView::resizeEvent(QResizeEvent *pResizEvt)
{
if (pResizEvt->size().isValid())
{
m_ResetArea=true;
}
}
void BorderPreView::slotSelectLineStyleChanged(int style,int lineWidth)
{
m_selectCurrentStyle=style;
m_selectCurrentLineWidth=lineWidth;
}