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) ...@@ -4,11 +4,15 @@ project(ActorDraw)
kumir2_use_qt(Core Gui Xml Svg) kumir2_use_qt(Core Gui Xml Svg)
set(SOURCES set(SOURCES
drawview.cpp
drawmodule.cpp drawmodule.cpp
drawscene.cpp
) )
set(MOC_HEADERS set(MOC_HEADERS
drawmodule.h drawmodule.h
drawscene.h
drawview.h
) )
......
This diff is collapsed.
...@@ -11,164 +11,31 @@ You should change it corresponding to functionality. ...@@ -11,164 +11,31 @@ You should change it corresponding to functionality.
// Base class include // Base class include
#include "drawmodulebase.h" #include "drawmodulebase.h"
#include <QDebug> #include <QColor>
#include <QLabel> #include <QPointF>
#include <QGraphicsView> class QTimer;
#include <QGraphicsLineItem>
class QToolButton; class QToolButton;
class QGraphicsPolygonItem;
class DrawNavigator; class DrawNavigator;
namespace ActorDraw namespace ActorDraw
{ {
class DrawModule; class DrawScene;
class DrawView;
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 DrawModule : public DrawModuleBase class DrawModule : public DrawModuleBase
{ {
Q_OBJECT Q_OBJECT
public /* methods */: public /* methods */:
DrawModule(ExtensionSystem::KPlugin *parent); DrawModule(ExtensionSystem::KPlugin *parent);
inline void terminateEvaluation() {}
void terminateEvaluation() {}
static QList<ExtensionSystem::CommandLineParameter> acceptableCommandLineParameters(); static QList<ExtensionSystem::CommandLineParameter> acceptableCommandLineParameters();
QWidget *mainWidget() const; QWidget *mainWidget() const;
QWidget *pultWidget() const; QWidget *pultWidget() const;
void handleGuiReady(); void handleGuiReady();
bool isAutoNet() const bool isAutoNet() const
...@@ -195,49 +62,26 @@ public /* methods */: ...@@ -195,49 +62,26 @@ public /* methods */:
{ {
netStepY = step; netStepY = step;
} }
double zoom()
{
return CurView->zoom();
}
QGraphicsPolygonItem *Pen() const QGraphicsPolygonItem *Pen() const
{ {
return mPen; return mPen;
} }
void scalePen(double factor)
{ double zoom() const;
mutex.lock();
mPen->setScale(factor); void scalePen(double factor);
qDebug() << "PenScale" << factor << "mPen->scale" << mPen->scale();
mutex.unlock();
}
DrawView *getCurView() const
{
return CurView;
}
static ExtensionSystem::SettingsPtr DrawSettings();
QColor axisColor() const QColor axisColor() const
{ {
return QColor(DrawSettings()->value("AxisColor", "#999900").toString()); 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); QString initialize(const QStringList &configurationParameters, const ExtensionSystem::CommandLine &runtimeParameters);
QMutex mutex;
public slots: public slots:
void changeGlobalState(ExtensionSystem::GlobalState old, ExtensionSystem::GlobalState current); void changeGlobalState(ExtensionSystem::GlobalState old, ExtensionSystem::GlobalState current);
void loadActorData(QIODevice *source); void loadActorData(QIODevice *source);
...@@ -267,28 +111,28 @@ public slots: ...@@ -267,28 +111,28 @@ public slots:
void redraw(); void redraw();
void updateDraw(); void updateDraw();
/* ========= CLASS PRIVATE ========= */
private: private:
static ExtensionSystem::SettingsPtr DrawSettings();
void createGui(); void createGui();
void CreatePen(void); void CreatePen(void);
private:
DrawScene *CurScene; DrawScene *CurScene;
DrawView *CurView; DrawView *CurView;
QGraphicsPolygonItem *mPen; QGraphicsPolygonItem *mPen;
QMutex mutex;
double netStepX, netStepY; double netStepX, netStepY;
QColor netColor; QColor netColor;
bool autoNet; bool autoNet;
bool penIsDrawing; bool penIsDrawing;
bool firstShow; bool firstShow;
Color penColor; QColor penColor;
ExtensionSystem::GlobalState currentState; ExtensionSystem::GlobalState currentState;
DrawNavigator *navigator; DrawNavigator *navigator;
QToolButton *showToolsBut; QToolButton *showToolsBut;
QDir curDir; QDir curDir;
bool animate; bool animate;
QTimer *redrawTimer; QTimer *redrawTimer;
qreal curAngle;
qreal AncX, AncY;
QPointF curPos; 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);