342 lines
11 KiB
C++
342 lines
11 KiB
C++
#include "formtableitem.h"
|
|
#include "ui_formtableitem.h"
|
|
#include <QPainter>
|
|
#include <QMouseEvent>
|
|
#include <QInputDialog>
|
|
#include <QMessageBox>
|
|
|
|
FormTableItem::FormTableItem(QWidget *parent) :
|
|
QWidget(parent),
|
|
ui(new Ui::FormTableItem)
|
|
{
|
|
ui->setupUi(this);
|
|
}
|
|
|
|
FormTableItem::~FormTableItem()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void FormTableItem::paintEvent(QPaintEvent* event)
|
|
{
|
|
QPainter painter(this);
|
|
QRect rect = this->rect();
|
|
|
|
// 获取RGB值
|
|
int red = m_backColor.red();
|
|
int green = m_backColor.green();
|
|
int blue = m_backColor.blue();
|
|
if(red==255 && green==255 && blue==255)
|
|
{
|
|
//背景(透明)
|
|
painter.fillRect(rect.left(), rect.top(), rect.width(), rect.height(), QColor(255, 255, 255, 0));
|
|
}
|
|
else
|
|
{
|
|
//背景
|
|
painter.fillRect(rect.left(), rect.top(), rect.width(), rect.height(), m_backColor);
|
|
}
|
|
|
|
if(!m_pixmap.isNull())
|
|
{
|
|
painter.drawPixmap(0, 0, m_pixmap);
|
|
}
|
|
|
|
// //测试画框
|
|
// painter.setPen(Qt::red);
|
|
// QRect rectRound(rect.left(), rect.top(), rect.width(), rect.height());
|
|
// painter.drawRect(rectRound);
|
|
|
|
//
|
|
painter.setBrush(Qt::NoBrush); // 确保文字不被填充色遮挡
|
|
painter.setFont(m_txtFont);
|
|
painter.setPen(m_txtColor);
|
|
|
|
if (m_strShowTxt.length()>0)
|
|
{
|
|
QRect rt = rect;
|
|
QString text = "";
|
|
bool bVer = false;
|
|
// if (!m_bVerticaDrawing)
|
|
// {
|
|
// QFontMetrics fm1(m_txtFont);
|
|
// QRect textRect = fm1.boundingRect(m_strShowTxt);
|
|
// if (textRect.width() > rt.width())
|
|
// {
|
|
// bVer = true;
|
|
// }
|
|
// }
|
|
if (m_bVerticaDrawing || bVer)
|
|
{
|
|
for (int i = 0; i < m_strShowTxt.size(); i++) {
|
|
if (!i)text += m_strShowTxt.at(i);
|
|
else {
|
|
text += "\n";
|
|
text += m_strShowTxt.at(i);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// rt.setX(rect.left());
|
|
// rt.setY(rect.top() + rect.height() / 3);
|
|
// rt.setWidth(rect.width());
|
|
// rt.setHeight(rect.height() / 3);
|
|
text = m_strShowTxt;
|
|
}
|
|
//文字
|
|
painter.drawText(rt, Qt::AlignCenter, text);
|
|
}
|
|
|
|
//边框
|
|
BuildBorderLines();
|
|
DrawBorderLines(&painter);
|
|
}
|
|
|
|
void FormTableItem::BuildBorderLines()
|
|
{
|
|
int marginValue=0;
|
|
int outVlaue=0;
|
|
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 FormTableItem::DrawBorderLines(QPainter *paint)
|
|
{
|
|
paint->save();
|
|
//
|
|
switch(m_drawBorder)
|
|
{
|
|
case FormTableItem::All:
|
|
{
|
|
paint->setPen(QPen(Qt::black,TopBorderWidth,(Qt::PenStyle)TopBorderStyle));
|
|
paint->drawLine(m_TopLine);
|
|
paint->setPen(QPen(Qt::black,RightBorderWidth,(Qt::PenStyle)RightBorderStyle));
|
|
paint->drawLine(m_RightLine);
|
|
paint->setPen(QPen(Qt::black,BottomBorderWidth,(Qt::PenStyle)BottomBorderStyle));
|
|
paint->drawLine(m_BottomLine);
|
|
paint->setPen(QPen(Qt::black,LeftBorderWidth,(Qt::PenStyle)LeftBorderStyle));
|
|
paint->drawLine(m_LeftLine);
|
|
m_TopState=m_RightState=m_BottomState=m_LeftState=true;
|
|
}
|
|
break;
|
|
|
|
case FormTableItem::Top:
|
|
{
|
|
paint->setPen(QPen(Qt::black,TopBorderWidth,(Qt::PenStyle)TopBorderStyle));
|
|
paint->drawLine(m_TopLine);
|
|
m_TopState=true;
|
|
m_RightState=m_BottomState=m_LeftState=false;
|
|
}
|
|
break;
|
|
case FormTableItem::Right:
|
|
{
|
|
paint->setPen(QPen(Qt::black,RightBorderWidth,(Qt::PenStyle)RightBorderStyle));
|
|
paint->drawLine(m_RightLine);
|
|
m_RightState=true;
|
|
m_TopState=m_BottomState=m_LeftState=false;
|
|
}
|
|
break;
|
|
case FormTableItem::Bottom:
|
|
{
|
|
paint->setPen(QPen(Qt::black,BottomBorderWidth,(Qt::PenStyle)BottomBorderStyle));
|
|
paint->drawLine(m_BottomLine);
|
|
m_BottomState=true;
|
|
m_TopState=m_RightState=m_LeftState=false;
|
|
}
|
|
break;
|
|
case FormTableItem::Left:
|
|
{
|
|
paint->setPen(QPen(Qt::black,LeftBorderWidth,(Qt::PenStyle)LeftBorderStyle));
|
|
paint->drawLine(m_LeftLine);
|
|
m_LeftState=true;
|
|
m_TopState=m_RightState=m_BottomState=false;
|
|
}
|
|
break;
|
|
|
|
case FormTableItem::TopRight:
|
|
{
|
|
paint->setPen(QPen(Qt::black,TopBorderWidth,(Qt::PenStyle)TopBorderStyle));
|
|
paint->drawLine(m_TopLine);
|
|
paint->setPen(QPen(Qt::black,RightBorderWidth,(Qt::PenStyle)RightBorderStyle));
|
|
paint->drawLine(m_RightLine);
|
|
m_TopState=m_RightState=true;
|
|
m_LeftState=m_BottomState=false;
|
|
}
|
|
break;
|
|
case FormTableItem::RightBottom:
|
|
{
|
|
paint->setPen(QPen(Qt::black,RightBorderWidth,(Qt::PenStyle)RightBorderStyle));
|
|
paint->drawLine(m_RightLine);
|
|
paint->setPen(QPen(Qt::black,BottomBorderWidth,(Qt::PenStyle)BottomBorderStyle));
|
|
paint->drawLine(m_BottomLine);
|
|
m_BottomState=m_RightState=true;
|
|
m_LeftState=m_TopState=false;
|
|
}
|
|
break;
|
|
case FormTableItem::BottomLeft:
|
|
{
|
|
paint->setPen(QPen(Qt::black,BottomBorderWidth,(Qt::PenStyle)BottomBorderStyle));
|
|
paint->drawLine(m_BottomLine);
|
|
paint->setPen(QPen(Qt::black,LeftBorderWidth,(Qt::PenStyle)LeftBorderStyle));
|
|
paint->drawLine(m_LeftLine);
|
|
m_BottomState=m_LeftState=true;
|
|
m_RightState=m_TopState=false;
|
|
}
|
|
break;
|
|
case FormTableItem::LeftTop:
|
|
{
|
|
paint->setPen(QPen(Qt::black,LeftBorderWidth,(Qt::PenStyle)LeftBorderStyle));
|
|
paint->drawLine(m_LeftLine);
|
|
paint->setPen(QPen(Qt::black,TopBorderWidth,(Qt::PenStyle)TopBorderStyle));
|
|
paint->drawLine(m_TopLine);
|
|
m_TopState=m_LeftState=true;
|
|
m_RightState=m_BottomState=false;
|
|
}
|
|
break;
|
|
|
|
case FormTableItem::TopBottom:
|
|
{
|
|
paint->setPen(QPen(Qt::black,TopBorderWidth,(Qt::PenStyle)TopBorderStyle));
|
|
paint->drawLine(m_TopLine);
|
|
paint->setPen(QPen(Qt::black,BottomBorderWidth,(Qt::PenStyle)BottomBorderStyle));
|
|
paint->drawLine(m_BottomLine);
|
|
m_TopState=m_BottomState=true;
|
|
m_RightState=m_LeftState=false;
|
|
}
|
|
break;
|
|
case FormTableItem::LeftRight:
|
|
{
|
|
paint->setPen(QPen(Qt::black,LeftBorderWidth,(Qt::PenStyle)LeftBorderStyle));
|
|
paint->drawLine(m_LeftLine);
|
|
paint->setPen(QPen(Qt::black,RightBorderWidth,(Qt::PenStyle)RightBorderStyle));
|
|
paint->drawLine(m_RightLine);
|
|
m_TopState=m_BottomState=false;
|
|
m_RightState=m_LeftState=true;
|
|
}
|
|
break;
|
|
|
|
case FormTableItem::TopRightBottom:
|
|
{
|
|
paint->setPen(QPen(Qt::black,TopBorderWidth,(Qt::PenStyle)TopBorderStyle));
|
|
paint->drawLine(m_TopLine);
|
|
paint->setPen(QPen(Qt::black,RightBorderWidth,(Qt::PenStyle)RightBorderStyle));
|
|
paint->drawLine(m_RightLine);
|
|
paint->setPen(QPen(Qt::black,BottomBorderWidth,(Qt::PenStyle)BottomBorderStyle));
|
|
paint->drawLine(m_BottomLine);
|
|
m_RightState=m_TopState=m_BottomState=true;
|
|
m_LeftState=false;
|
|
}
|
|
break;
|
|
case FormTableItem::RightBottomLeft:
|
|
{
|
|
paint->setPen(QPen(Qt::black,RightBorderWidth,(Qt::PenStyle)RightBorderStyle));
|
|
paint->drawLine(m_RightLine);
|
|
paint->setPen(QPen(Qt::black,BottomBorderWidth,(Qt::PenStyle)BottomBorderStyle));
|
|
paint->drawLine(m_BottomLine);
|
|
paint->setPen(QPen(Qt::black,LeftBorderWidth,(Qt::PenStyle)LeftBorderStyle));
|
|
paint->drawLine(m_LeftLine);
|
|
m_RightState=m_LeftState=m_BottomState=true;
|
|
m_TopState=false;
|
|
}
|
|
break;
|
|
case FormTableItem::BottomLeftTop:
|
|
{
|
|
paint->setPen(QPen(Qt::black,BottomBorderWidth,(Qt::PenStyle)BottomBorderStyle));
|
|
paint->drawLine(m_BottomLine);
|
|
paint->setPen(QPen(Qt::black,LeftBorderWidth,(Qt::PenStyle)LeftBorderStyle));
|
|
paint->drawLine(m_LeftLine);
|
|
paint->setPen(QPen(Qt::black,TopBorderWidth,(Qt::PenStyle)TopBorderStyle));
|
|
paint->drawLine(m_TopLine);
|
|
m_TopState=m_LeftState=m_BottomState=true;
|
|
m_RightState=false;
|
|
}
|
|
break;
|
|
case FormTableItem::LeftTopRight:
|
|
{
|
|
paint->setPen(QPen(Qt::black,LeftBorderWidth,(Qt::PenStyle)LeftBorderStyle));
|
|
paint->drawLine(m_LeftLine);
|
|
paint->setPen(QPen(Qt::black,TopBorderWidth,(Qt::PenStyle)TopBorderStyle));
|
|
paint->drawLine(m_TopLine);
|
|
paint->setPen(QPen(Qt::black,RightBorderWidth,(Qt::PenStyle)RightBorderStyle));
|
|
paint->drawLine(m_RightLine);
|
|
m_TopState=m_LeftState=m_RightState=true;
|
|
m_BottomState=false;
|
|
}
|
|
break;
|
|
default: //None
|
|
{
|
|
|
|
}
|
|
break;
|
|
}
|
|
//
|
|
paint->restore();
|
|
}
|
|
|
|
|
|
void FormTableItem::mouseDoubleClickEvent(QMouseEvent *event)
|
|
{
|
|
// 检查是否是左键双击
|
|
if (event->button() == Qt::LeftButton)
|
|
{
|
|
if(m_strFormulaText!="")
|
|
{
|
|
QMessageBox::information(nullptr,"提示","公式状态不可修改,如需修改或删除,请到属性区修改!");
|
|
return;
|
|
}
|
|
|
|
bool ok=0;
|
|
QString Formtiom=QInputDialog::getText(NULL, "内容编辑", "内容", QLineEdit::Normal,m_strShowTxt,&ok);
|
|
if(!ok) return;
|
|
|
|
m_strShowTxt = Formtiom;
|
|
update();
|
|
}
|
|
|
|
|
|
// 其他按钮的双击交给基类处理
|
|
QWidget::mouseDoubleClickEvent(event);
|
|
}
|