0
0
mirror of https://github.com/fralx/LimeReport.git synced 2025-01-11 09:08:09 +03:00
LimeReport/limereport/items/lrborderframeeditor.cpp

190 lines
5.8 KiB
C++
Raw Normal View History

2022-10-31 21:20:24 +03:00
#include "lrborderframeeditor.h"
#include "ui_lrborderframeeditor.h"
2022-10-31 21:20:24 +03:00
#include "lrbasedesignintf.h"
#include "lrbordereditor.h"
#include <QDebug>
#include <QGraphicsLineItem>
#include <QMouseEvent>
#include <QPainter>
2022-10-31 21:20:24 +03:00
namespace LimeReport {
BorderFrameEditor::BorderFrameEditor(QWidget* parent):
QWidget(parent),
ui(new Ui::BorderFrameEditor)
2022-10-31 21:20:24 +03:00
{
ui->setupUi(this);
scene = new QGraphicsScene(ui->graphicsView);
QRect vRect = rect();
// Draw corder lines
// topLeft
2022-10-31 21:20:24 +03:00
scene->addLine(10, 5, 10, 10, QPen(Qt::gray));
scene->addLine(5, 10, 10, 10, QPen(Qt::gray));
// bottomLeft
scene->addLine(10, vRect.bottom() - 5, 10, vRect.bottom() - 10, QPen(Qt::gray));
scene->addLine(5, vRect.bottom() - 10, 10, vRect.bottom() - 10, QPen(Qt::gray));
// bottomRight
scene->addLine(vRect.right() - 10, vRect.bottom() - 5, vRect.right() - 10, vRect.bottom() - 10,
QPen(Qt::gray));
scene->addLine(vRect.right() - 5, vRect.bottom() - 10, vRect.right() - 10, vRect.bottom() - 10,
QPen(Qt::gray));
// topRight
2022-10-31 21:20:24 +03:00
scene->addLine(vRect.width() - 10, 5, vRect.width() - 10, 10, QPen(Qt::gray));
scene->addLine(vRect.width() - 5, 10, vRect.width() - 10, 10, QPen(Qt::gray));
scene->setSceneRect(vRect);
ui->graphicsView->setScene(scene);
QGraphicsSimpleTextItem* io = new QGraphicsSimpleTextItem();
2022-10-31 21:20:24 +03:00
io->setAcceptedMouseButtons(Qt::LeftButton);
io->setPos(scene->sceneRect().center());
io->setText(tr("Text"));
scene->addItem(io);
QRectF bR = io->sceneBoundingRect();
io->setPos(scene->sceneRect().center().x() - bR.width() / 2,
scene->sceneRect().center().y() - bR.height() / 2);
connect(this, SIGNAL(borderSideClicked(LimeReport::BaseDesignIntf::BorderSide, bool)), this,
SLOT(slotBorderSideClicked(LimeReport::BaseDesignIntf::BorderSide, bool)));
2022-10-31 21:20:24 +03:00
}
BorderFrameEditor::~BorderFrameEditor() { delete ui; }
2022-10-31 21:20:24 +03:00
void BorderFrameEditor::setPen(QPen pen)
{
m_pen = pen;
updateBorders();
}
QPen BorderFrameEditor::pen() { return m_pen; }
2022-10-31 21:20:24 +03:00
void BorderFrameEditor::setAllLines()
{
if (!topLine)
topLine = createSideLine(LimeReport::BaseDesignIntf::BorderSide::TopLine);
if (!leftLine)
leftLine = createSideLine(LimeReport::BaseDesignIntf::BorderSide::LeftLine);
if (!bottomLine)
bottomLine = createSideLine(LimeReport::BaseDesignIntf::BorderSide::BottomLine);
if (!rightLine)
rightLine = createSideLine(LimeReport::BaseDesignIntf::BorderSide::RightLine);
}
void BorderFrameEditor::unSetAllLines()
{
if (topLine) {
2022-10-31 21:20:24 +03:00
scene->removeItem(topLine);
topLine = NULL;
}
if (leftLine) {
2022-10-31 21:20:24 +03:00
scene->removeItem(leftLine);
leftLine = NULL;
}
if (bottomLine) {
2022-10-31 21:20:24 +03:00
scene->removeItem(bottomLine);
bottomLine = NULL;
}
if (rightLine) {
2022-10-31 21:20:24 +03:00
scene->removeItem(rightLine);
rightLine = NULL;
}
}
void BorderFrameEditor::mousePressEvent(QMouseEvent* event)
2022-10-31 21:20:24 +03:00
{
if (event->x() >= 10 && event->y() < 30)
2022-10-31 21:20:24 +03:00
emit borderSideClicked(BaseDesignIntf::BorderSide::TopLine, !topLine);
if ((event->x() >= 10 && event->x() < 30) && (event->y() > 10))
emit borderSideClicked(BaseDesignIntf::BorderSide::LeftLine, !leftLine);
if (event->x() >= 10 && (event->y() > 80 && event->y() < rect().bottom()))
2022-10-31 21:20:24 +03:00
emit borderSideClicked(BaseDesignIntf::BorderSide::BottomLine, !bottomLine);
if ((event->x() >= 130 && event->x() < rect().width()) && event->y() > 10)
emit borderSideClicked(BaseDesignIntf::BorderSide::RightLine, !rightLine);
}
void BorderFrameEditor::slotBorderSideClicked(BaseDesignIntf::BorderSide side, bool show)
{
switch (side) {
2022-10-31 21:20:24 +03:00
case BaseDesignIntf::BorderSide::TopLine:
if (show) {
if (!topLine)
topLine = createSideLine(side);
2022-10-31 21:20:24 +03:00
} else {
if (topLine)
scene->removeItem(topLine);
2022-10-31 21:20:24 +03:00
topLine = NULL;
}
break;
case BaseDesignIntf::LeftLine:
if (show) {
if (!leftLine)
leftLine = createSideLine(side);
2022-10-31 21:20:24 +03:00
} else {
if (leftLine)
scene->removeItem(leftLine);
2022-10-31 21:20:24 +03:00
leftLine = NULL;
}
break;
case BaseDesignIntf::BottomLine:
if (show) {
if (!bottomLine)
bottomLine = createSideLine(side);
2022-10-31 21:20:24 +03:00
} else {
if (bottomLine)
scene->removeItem(bottomLine);
2022-10-31 21:20:24 +03:00
bottomLine = NULL;
}
break;
case BaseDesignIntf::RightLine:
if (show) {
if (!rightLine)
rightLine = createSideLine(side);
2022-10-31 21:20:24 +03:00
} else {
if (rightLine)
scene->removeItem(rightLine);
2022-10-31 21:20:24 +03:00
rightLine = NULL;
}
}
updateBorders();
}
QGraphicsLineItem* BorderFrameEditor::createSideLine(LimeReport::BaseDesignIntf::BorderSide side)
2022-10-31 21:20:24 +03:00
{
switch (side) {
2022-10-31 21:20:24 +03:00
case BaseDesignIntf::BorderSide::TopLine:
return scene->addLine(QLineF(10, 10, rect().width() - 10, 10), m_pen);
case BaseDesignIntf::BorderSide::LeftLine:
return scene->addLine(QLineF(10, 10, 10, rect().height() - 10), m_pen);
case BaseDesignIntf::BorderSide::RightLine:
return scene->addLine(
QLineF(rect().width() - 10, 10, rect().width() - 10, rect().height() - 10), m_pen);
2022-10-31 21:20:24 +03:00
case BaseDesignIntf::BorderSide::BottomLine:
return scene->addLine(
QLineF(10, rect().bottom() - 10, rect().width() - 10, rect().bottom() - 10), m_pen);
2022-10-31 21:20:24 +03:00
}
}
void BorderFrameEditor::updateBorders()
{
if (topLine)
topLine->setPen(m_pen);
if (leftLine)
leftLine->setPen(m_pen);
if (bottomLine)
bottomLine->setPen(m_pen);
if (rightLine)
rightLine->setPen(m_pen);
2022-10-31 21:20:24 +03:00
}
} // namespace LimeReport