Commit f1718ab5 authored by Alexander A. Maly's avatar Alexander A. Maly

Refactoring actor Draw

parent 689b19b7
Pipeline #2823 failed with stages
in 1 minute and 55 seconds
......@@ -4,11 +4,15 @@ project(ActorDraw)
kumir2_use_qt(Core Gui Xml Svg)
set(SOURCES
drawview.cpp
drawmodule.cpp
drawscene.cpp
)
set(MOC_HEADERS
drawmodule.h
drawscene.h
drawview.h
)
......
This diff is collapsed.
......@@ -11,164 +11,31 @@ You should change it corresponding to functionality.
// Base class include
#include "drawmodulebase.h"
#include <QDebug>
#include <QLabel>
#include <QGraphicsView>
#include <QGraphicsLineItem>
#include <QColor>
#include <QPointF>
class QTimer;
class QToolButton;
class QGraphicsPolygonItem;
class DrawNavigator;
namespace ActorDraw
{
class DrawModule;
class DrawView : public QGraphicsView
{
Q_OBJECT
public:
DrawView(QWidget *parent = 0) : QGraphicsView(parent)
{
c_scale = 1;
pressed = false;
press_pos = QPoint();
firstResize = true;
net = true;
smallNetLabel = new QLabel(this);
smallNetLabel->hide();
smallNetLabel->setText(trUtf8("Слишком мелкая сетка"));
};
void setDraw(DrawModule *draw, QMutex *mutex)
{
DRAW = draw;
dr_mutex = mutex;
};
double zoom()const
{
return c_scale;
};
void setZoom(double zoom);
void setNet();//RESIZE NET
bool isNet() const
{
return net;
}
void forceRedraw()
{
// horizontalScrollBar()->setValue(horizontalScrollBar()->value() +1);
// horizontalScrollBar()->setValue(horizontalScrollBar()->value()-1);
qDebug() << "ForceREDDR";
QGraphicsView::resetCachedContent();
QGraphicsView::update();
QGraphicsView::repaint();
// verticalScrollBar()->setValue(horizontalScrollBar()->value() +1);
// verticalScrollBar()->setValue(horizontalScrollBar()->value()-1);
}
protected:
// void scrollContentsBy ( int dx, int dy );
void resizeEvent(QResizeEvent *event);
void wheelEvent(QWheelEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
// void paintEvent(QPaintEvent *event);
private:
DrawModule *DRAW;
double c_scale;
bool pressed;
bool net;
QPoint press_pos;
bool firstResize;
double lastStep;
QMutex *dr_mutex;
QLabel *smallNetLabel;
};
class DrawScene : public QGraphicsScene
{
Q_OBJECT
public:
DrawScene(QObject *parent = 0): QGraphicsScene(parent)
{
/// installEventFilter(this);
};
void drawNet(double startx, double endx, double starty, double endy, QColor color, const double step, const double stepY, bool net, qreal nw, qreal aw);
void setDraw(DrawModule *draw, QMutex *mutex)
{
DRAW = draw;
dr_mutex = mutex;
};
void addDrawLine(QLineF lineF, QColor color, qreal width);
void reset()
{
for (int i = 0; i < lines.count(); i++) {
removeItem(lines.at(i));
}
lines.clear();
for (int i = 0; i < texts.count(); i++) {
removeItem(texts.at(i));
}
texts.clear();
clearBuffer();
}
void upd()
{
QGraphicsScene::update();
}
void DestroyNet();
void drawOnlyAxis(double startx, double endx, double starty, double endy, qreal aw);
bool isLineAt(const QPointF &pos, qreal radius);
qreal drawText(const QString &Text, qreal widthChar, QPointF from, QColor color); //Returns offset of pen.
QRectF getRect();
int saveToFile(const QString &p_FileName);
int loadFromFile(const QString &p_FileName);
void fromBufferToScene()
{
QGraphicsItemGroup *buff = createItemGroup(itemsBuffer);
buff->setZValue(90);
addItem(buff);
clearBuffer();
}
void clearBuffer()
{
itemsBuffer.clear();
}
int buffSize()
{
return itemsBuffer.count();
}
protected:
// void resizeEvent ( QResizeEvent * event );
// bool eventFilter(QObject *object, QEvent *event);
// bool event(QEvent * event);
private:
bool isUserLine(QGraphicsItem *); //Return true if item is user item;
QList<QGraphicsLineItem *> lines;
QList<QGraphicsLineItem *> Netlines;
QList<QGraphicsLineItem *> linesDubl; //Базовый чертеж
QList<QGraphicsSimpleTextItem *> texts;
DrawModule *DRAW;
QList<QGraphicsItem *> itemsBuffer;
QMutex *dr_mutex;
};
class DrawScene;
class DrawView;
class DrawModule : public DrawModuleBase
{
Q_OBJECT
public /* methods */:
DrawModule(ExtensionSystem::KPlugin *parent);
inline void terminateEvaluation() {}
void terminateEvaluation() {}
static QList<ExtensionSystem::CommandLineParameter> acceptableCommandLineParameters();
QWidget *mainWidget() const;
QWidget *pultWidget() const;
void handleGuiReady();
bool isAutoNet() const
......@@ -195,49 +62,26 @@ public /* methods */:
{
netStepY = step;
}
double zoom()
{
return CurView->zoom();
}
QGraphicsPolygonItem *Pen() const
{
return mPen;
}
void scalePen(double factor)
{
mutex.lock();
mPen->setScale(factor);
qDebug() << "PenScale" << factor << "mPen->scale" << mPen->scale();
mutex.unlock();
}
DrawView *getCurView() const
{
return CurView;
}
static ExtensionSystem::SettingsPtr DrawSettings();
double zoom() const;
void scalePen(double factor);
QColor axisColor() const
{
return QColor(DrawSettings()->value("AxisColor", "#999900").toString());
}
void redrawPicture()
{
// CurScene->resetCashedContent();
CurScene->upd();
CurView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
CurView->forceRedraw();
CurScene->update(CurScene->sceneRect());
void redrawPicture();
CurView->viewport()->update();
CurView->setZoom(CurView->zoom() * 2);
CurView->setZoom(CurView->zoom() * 0.5);
// CurView->setViewportUpdateMode (QGraphicsView::NoViewportUpdate);
}
QString initialize(const QStringList &configurationParameters, const ExtensionSystem::CommandLine &runtimeParameters);
QMutex mutex;
public slots:
void changeGlobalState(ExtensionSystem::GlobalState old, ExtensionSystem::GlobalState current);
void loadActorData(QIODevice *source);
......@@ -267,28 +111,28 @@ public slots:
void redraw();
void updateDraw();
/* ========= CLASS PRIVATE ========= */
private:
static ExtensionSystem::SettingsPtr DrawSettings();
void createGui();
void CreatePen(void);
private:
DrawScene *CurScene;
DrawView *CurView;
QGraphicsPolygonItem *mPen;
QMutex mutex;
double netStepX, netStepY;
QColor netColor;
bool autoNet;
bool penIsDrawing;
bool firstShow;
Color penColor;
QColor penColor;
ExtensionSystem::GlobalState currentState;
DrawNavigator *navigator;
QToolButton *showToolsBut;
QDir curDir;
bool animate;
QTimer *redrawTimer;
qreal curAngle;
qreal AncX, AncY;
QPointF curPos;
};
......
#include "drawscene.h"
#include "drawmodule.h"
#include <QGraphicsItem>
#include <QMessageBox>
#include <QDebug>
#define NET_RESERVE 15
namespace ActorDraw {
DrawScene::DrawScene(DrawModule *draw, QMutex *_mutex, QObject *parent) : QGraphicsScene(parent)
{
DRAW = draw;
mutex = _mutex;
}
DrawScene::~DrawScene()
{
DRAW = 0;
mutex = 0;
}
void DrawScene::reset()
{
for (int i = 0; i < lines.count(); i++) {
removeItem(lines.at(i));
}
lines.clear();
for (int i = 0; i < texts.count(); i++) {
removeItem(texts.at(i));
}
texts.clear();
itemsBuffer.clear();
}
void DrawScene::fromBufferToScene()
{
QGraphicsItemGroup *buff = createItemGroup(itemsBuffer);
buff->setZValue(90);
addItem(buff);
itemsBuffer.clear();
}
//User lines bounding rect
QRectF DrawScene::getRect() const
{
QRectF boundRect = QRectF(QPointF(-5, -5), QPointF(5, 5)); //default rect
for (int i = 0; i < lines.count(); i++) {
if (lines.at(i)->line().dx() >= 0) {
if (lines.at(i)->line().p1().x() < boundRect.left() || (i == 0)) {
//deb = lines.at(i)->line().p1().x();
boundRect.setLeft(lines.at(i)->line().p1().x());
}
if (lines.at(i)->line().p2().x() > boundRect.right() || (i == 0)) {
boundRect.setRight(lines.at(i)->line().p2().x());
}
}
if (lines.at(i)->line().dx() < 0) {
if (lines.at(i)->line().p2().x() < boundRect.left() || (i == 0))
{
//deb = lines.at(i)->line().p1().x();
boundRect.setLeft(lines.at(i)->line().p2().x());
}
if (lines.at(i)->line().p1().x() > boundRect.right() || (i == 0)) {
//deb = lines.at(i)->line().p1().x();
boundRect.setRight(lines.at(i)->line().p1().x());
}
}
if (lines.at(i)->line().dy() >= 0) {
qDebug() << "Bott" << boundRect.bottom() << "Top:" << boundRect.top() << "line p1y:" << lines.at(i)->line().p1().y() << "line p2y:" << lines.at(i)->line().p2().y();
if (-lines.at(i)->line().p1().y() > boundRect.bottom() || (i == 0)) {
//deb = -lines.at(i)->line().p1().y();
boundRect.setBottom(-lines.at(i)->line().p1().y());
}
if (lines.at(i)->line().p2().y() > boundRect.top() || (i == 0)) {
boundRect.setTop(lines.at(i)->line().p2().y());
}
}
if (lines.at(i)->line().dy() < 0) {
qDebug() << "Bott" << boundRect.bottom() << "Top:" << boundRect.top() << "line p1y:" << lines.at(i)->line().p1().y() << "line p2y:" << lines.at(i)->line().p2().y();
if (lines.at(i)->line().p2().y() < boundRect.top()) {
boundRect.setTop(lines.at(i)->line().p2().y());
//deb = -lines.at(i)->line().p2().y();
}
if (lines.at(i)->line().p1().y() > boundRect.bottom() || (i == 0)) {
boundRect.setBottom(lines.at(i)->line().p1().y());
//deb = lines.at(i)->line().p1().y();
}
}
}
//if(boundRect.width()!=boundRect.height())
// {
// qreal size=qMax(boundRect.width(),boundRect.width());
// boundRect.setWidth(size);
// boundRect.setHeight(size);
// }
return (boundRect);
};
bool DrawScene::isUserLine(QGraphicsItem *obj) const
{
for (int i = 0; i < lines.count(); i++) {
if (lines.at(i) == obj) {
return true;
}
}
return false;
}
bool DrawScene::isLineAt(const QPointF &pos, qreal radius)
{
QGraphicsEllipseItem *testCirc = addEllipse(QRectF(pos.x() - radius, pos.y() - radius, radius * 2, radius * 2));
QList<QGraphicsItem *> colItems = collidingItems(testCirc);
removeItem(testCirc);
delete testCirc;
for (int i = 0; i < colItems.count(); i++) {
if (isUserLine(colItems.at(i))) {
return true;
}
}
return false;
}
qreal DrawScene::drawText(const QString &Text, qreal widthChar, QPointF from, QColor color)
{
QFont font("Courier", 12);
font.setPointSizeF(KUM_MULTI);
QFontMetricsF fontMetric(font);
qreal bs = fontMetric.boundingRect(Text).width();
qreal psizeF = widthChar / bs;
// font.setPointSizeF(psizeF*inc);
bs = fontMetric.boundingRect("OOOXX").width() / 5;
fontMetric = QFontMetricsF(font);
qDebug() << "Char Size:" << fontMetric.boundingRect("OOOXX").width() / 5000;
texts.append(addSimpleText(Text, font));
// texts.last()->scale(0.001,0.001);
texts.last()->setScale(psizeF);
texts.last()->setPos(from.x(), from.y() - (fontMetric.boundingRect(Text).height()*psizeF));
texts.last()->setPen(QPen(color));
texts.last()->setZValue(90);
return widthChar;
};
void DrawScene::addDrawLine(QLineF lineF, QColor color, qreal width)
{
if (lineF.length() == 0) {
return;
}
QGraphicsLineItem *line = new QGraphicsLineItem(lineF);
QPen mp = QPen(QColor(color));
mp.setWidthF(width);
mp.setCosmetic(true);
line->setPen(mp);
line->setZValue(90);
lines.append(line);
itemsBuffer.append(line);
}
void DrawScene::DestroyNet()
{
// qDebug()<<Netlines.count();
for (int i = 0; i < Netlines.count(); i++) {
delete Netlines[i];
}
Netlines.clear();
}
void DrawScene::drawOnlyAxis(double startx, double endx, double starty, double endy, qreal aw)
{
QPen axisPen = QPen(DRAW->axisColor());
axisPen.setWidthF(aw);
axisPen.setCosmetic(true);
Netlines.append(addLine(startx - qAbs(startx - endx), 0, endx + qAbs(startx - endx), 0));
Netlines.last()->setPen(axisPen);
Netlines.last()->setZValue(1);
Netlines.append(addLine(0, starty - qAbs(starty - endy), 0, endy + qAbs(startx - endx)));
Netlines.last()->setPen(axisPen);
Netlines.last()->setZValue(1);
}
void DrawScene::drawNet(
double startx, double endx, double starty, double endy,
QColor color,
double stepX, double stepY,
bool net,
qreal nw, qreal aw
) {
QColor AxisColor = DRAW->axisColor();
auto lp = QPen(color);
lp.setWidthF(nw);
lp.setCosmetic(true);
DestroyNet();
if (!net) {
drawOnlyAxis(startx, endx, starty, endy, aw);
return;
}
int lines = qRound(startx / stepX);
startx = lines * stepX;
double fx1 = startx - NET_RESERVE * stepX, fx2, fy1, fy2;
while (fx1 < endx + NET_RESERVE * stepX) {
fx1 = fx1 + stepX;
fx2 = fx1;
fy1 = starty - NET_RESERVE * stepX;
fy2 = endy + NET_RESERVE * stepX;
Netlines.append(addLine(fx1, fy1, fx2, fy2));
Netlines.last()->setZValue(0.5);
Netlines.last()->setPen(lp);
if (fx1 > 0 - 1 / DRAW->zoom() && fx1 < 0 + 1 / DRAW->zoom()) {
QPen axisPen = QPen(AxisColor);
axisPen.setWidthF(aw);
axisPen.setCosmetic(true);
// axisPen.setWidth(3/DRAW->zoom());
Netlines.last()->setPen(axisPen);
Netlines.last()->setZValue(1);
// Netlines.append(addLine(fx1+1/DRAW->zoom(), fy1 , fx2+1/DRAW->zoom(), fy2 ));
// Netlines.last()->setZValue(1);
// Netlines.last()->setPen(axisPen);
}
}
lines = starty / stepY;
starty = lines * stepY;
fy1 = starty - NET_RESERVE * stepY;
while (fy1 < endy + NET_RESERVE * stepY) {
fy1 = fy1 + stepY;
fy2 = fy1;
fx1 = startx - NET_RESERVE * stepY;
fx2 = endx + NET_RESERVE * stepY;
Netlines.append(addLine(fx1, fy1, fx2, fy2));
Netlines.last()->setZValue(0.5);
Netlines.last()->setPen(lp);
if (fy1 > 0 - 1 / DRAW->zoom() && fy1 < 0 + 1 / DRAW->zoom()) {
QPen axisPen = QPen(AxisColor);
axisPen.setWidthF(aw);
axisPen.setCosmetic(true);
Netlines.last()->setPen(axisPen);
Netlines.last()->setZValue(1);
//Netlines.append(addLine(fx1, fy1+1/DRAW->zoom() , fx2, fy2+1/DRAW->zoom() ));
//Netlines.last()->setZValue(1);
// Netlines.last()->setPen(axisPen);
}
}
}
int DrawScene::loadFromFile(const QString &p_FileName)
{
QFileInfo fi(p_FileName);
QString name = fi.fileName();
QString Title = QString::fromUtf8("Чертежник - ") + name;
// MV->setWindowTitle ( Title);
QString tmp = "";
char ctmp[200];
QString l_String;
QFile l_File(p_FileName);
QColor CurColor;
int NStrok = 0;
qreal CurrentScale = 1.0;
qreal x1 = 0, y1 = 0, x2 = 0, y2 = 0;
if (!l_File.open(QIODevice::ReadOnly)) {
QMessageBox::information(NULL, "", QString::fromUtf8("Ошибка открытия файла"), 0, 0, 0);
return 1;
}
QByteArray ttt = l_File.readLine();
l_String = QString::fromUtf8(ttt);
//QMessageBox::information( MV, "",l_String , 0,0,0);
if (l_String.isNull()) {
l_File.close();
QMessageBox::information(NULL, "", QString::fromUtf8("Ошибка чтения строки"), 0, 0, 0);
return 1;
}
l_String = l_String.simplified();
QStringList l_List = l_String.split(' ');
if (!(l_String == "%!PS-Adobe-1.0 EPSF-1.0")) {
l_File.close();
QMessageBox::information(NULL, "", QString::fromUtf8("Это не PS - файл"), 0, 0, 0);
return 1;
}
// Вторая строка %%Creator: Cherteznik
// l_String = l_File.readLine();
ttt = l_File.readLine();
l_String = QString::fromUtf8(ttt);
NStrok++;
l_String = l_String.simplified();
l_List = l_String.split(' ');
if (!(l_List[1] == "Cherteznik")) {
QMessageBox::information(NULL, "", QString::fromUtf8("Это не файл чережника"), 0, 0, 0);
l_File.close();
return 1;
}
for (int i = 0; i < 15; i++) {
// l_String = l_File.readLine();
ttt = l_File.readLine();
l_String = QString::fromUtf8(ttt);
NStrok++;
}
// koordinaty vektorov
// CurZ = 1.;
while (!l_File.atEnd()) {
//считываем цвет