turtlemodule.h 7.08 KB
Newer Older
Denis Khachko's avatar
Denis Khachko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
This file is generated, but you can safely change it
until you run "gen_actor_source.py" with "--project" flag.

Generated file is just a skeleton for module contents.
You should change it corresponding to functionality.
*/

#ifndef TURTLEMODULE_H
#define TURTLEMODULE_H

// Base class include
#include "turtlemodulebase.h"

15 16
#include <QPainter>
#include <QPainterPath>
Alexander A. Maly's avatar
Alexander A. Maly committed
17
#include <QGraphicsView>
Alexander A. Maly's avatar
Alexander A. Maly committed
18 19 20
#include <QGraphicsItem>
#include <QGraphicsSvgItem>
#include <QSvgRenderer>
Alexander A. Maly's avatar
Alexander A. Maly committed
21 22 23 24 25 26 27 28
#include <QLabel>
#include <QDebug>

class QMouseEvent;
class QWheelEvent;
class QResizeEvent;
class QToolButton;

Denis Khachko's avatar
Denis Khachko committed
29 30

class DrawNavigator;
Alexander A. Maly's avatar
Alexander A. Maly committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
class TurtlePult;

namespace ActorTurtle
{


class TurtleModule;

class TurtlePen: public QGraphicsItem
{

public:
	TurtlePen(QGraphicsItem *parent, QString svgFileName);

	void setTailColor(QColor  color)
	{
		tailColor = color;
	}

	void tailUp()
	{
		tailup = true;
	}

	void tailDown()
	{
		tailup = false;
	}

	QRectF boundingRect() const
	{
		return QRectF(-15, -30, mX, mY);
	}

	bool isTailUp() const
	{
		return tailup;
	}

	void setTurtleVis(bool vis)
	{
		turtle->setVisible(vis);
		turtle->update();

	}

	void paint(
		QPainter *painter,
		const QStyleOptionGraphicsItem *option,
		QWidget *widget
	);

	void resetPen()
	{
		mPosX = 0;
		mPosY = 0;
	}

	void movePen(qreal dx, qreal dy)
	{
		mPosX += dx;
		mPosY += dy;
	}

	QPointF penPos() const
	{
		return QPointF(mPosX, mPosY);
	}

private:
	qreal mX, mY;
	QGraphicsSvgItem *turtle;
	QPainterPath tail;
	QColor tailColor;
	QImage *turtleImage;
	bool tailup;
	qreal mPosX, mPosY;
};

class TurtleView : public QGraphicsView
{
	Q_OBJECT
public:
	TurtleView(QWidget *parent = 0)
	{
		c_scale = 1;
		pressed = false;
		press_pos = QPoint();
		firstResize = true;
		net = true;
		smallNetLabel = new QLabel(this);
		smallNetLabel->hide();
		smallNetLabel->setText(trUtf8("Слишком мелкая сетка"));
	};
	void setDraw(TurtleModule *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() +10);
		// horizontalScrollBar()->setValue(horizontalScrollBar()->value()-10);

		// 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:
	TurtleModule *DRAW;
	double c_scale;
	bool pressed;
	bool net;
	QPoint press_pos;
	bool firstResize;
	double lastStep;
	QMutex *dr_mutex;
	QLabel *smallNetLabel;

};


class TurtleScene : public QGraphicsScene
{
	Q_OBJECT
public:
	TurtleScene(QObject *parent = 0) {};
	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(TurtleModule *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();

	}
	void fromBufferToScene()
	{
		QGraphicsItemGroup *buff = createItemGroup(itemsBuffer);
		buff->setZValue(90);
		addItem(buff);
		clearBuffer();
	}
	void clearBuffer()
	{
		itemsBuffer.clear();
	}
	int buffSize()
	{
		return itemsBuffer.count();
	}
	void DestroyNet();
	void drawOnlyAxis(double startx, double endx, double starty, double endy, qreal aw);
	bool netIsShown()
	{
		return Netlines.count() > 0;
	}
	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);
protected:
	// void resizeEvent ( QResizeEvent * event );
private:
	bool isUserLine(QGraphicsItem *); //Return true if item is user item;
	QList<QGraphicsLineItem *> lines;
	QList<QGraphicsLineItem *> Netlines;
	QList<QGraphicsLineItem *> linesDubl; //Базовый чертеж
	QList<QGraphicsSimpleTextItem *> texts;
	TurtleModule *DRAW;
	QMutex *dr_mutex;
	QList<QGraphicsItem *> itemsBuffer;
};


class TurtleModule : public TurtleModuleBase
Denis Khachko's avatar
Denis Khachko committed
241
{
Alexander A. Maly's avatar
Alexander A. Maly committed
242
	Q_OBJECT
Denis Khachko's avatar
Denis Khachko committed
243
public /* methods */:
Alexander A. Maly's avatar
Alexander A. Maly committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	TurtleModule(ExtensionSystem::KPlugin *parent);
	static QList<ExtensionSystem::CommandLineParameter> acceptableCommandLineParameters();
	QWidget *mainWidget() const;
	QWidget *pultWidget() const;
	void handleGuiReady();
	QString initialize(const QStringList &configurationParameters, const ExtensionSystem::CommandLine &runtimeParameters);
	void scalePen(double factor)
	{
		// mutex.lock();
		mPen->setScale(factor);
		//  mTurt->setScale(factor);
		qDebug() << "PenScale" << factor << "mPen->scale" << mPen->scale();
		//  mutex.unlock();
	}
	bool isAutoNet() const
	{
		return autoNet;
	}
	void setAutoNet(bool state)
	{
		autoNet = state;
	}
	double NetStepX() const
	{
		return netStepX;
	}
	void setNetStepX(double step)
	{
		netStepX = step;
	}
	double NetStepY() const
	{
		return netStepY;
	}
	void setNetStepY(double step)
	{
		netStepY = step;
	}
	double zoom() const
	{
		return CurView->zoom();
	}

	TurtlePen *Pen() const
	{
		return mPen;
	}
	static ExtensionSystem::SettingsPtr TurtleSettings();
	QColor axisColor() const
	{
		return QColor(TurtleSettings()->value("AxisColor", "#996600").toString());
	}
	TurtleView *getCurView() const
	{
		return CurView;
	}
	void updateTurtle();
public slots:
	void changeGlobalState(ExtensionSystem::GlobalState old, ExtensionSystem::GlobalState current);
	void loadActorData(QIODevice *source);
	void reloadSettings(ExtensionSystem::SettingsPtr settings, const QStringList &keys);
	void reset();
	inline void terminateEvaluation() {}
	void setAnimationEnabled(bool enabled);
	void runTailUp();
	void runTailDown();
	void runForward(const qreal dist);
	void runBack(const qreal dist);
	void runLeft(const qreal angle);
	void runRight(const qreal angle);
	void runSetPenColor(const Color &color);

	void drawNet();
	void autoNetChange(bool value);
	void netStepChange(double value);
	void zoomIn();
	void zoomOut();
	void zoomNorm();
	void zoomFullDraw();

	void showNavigator(bool state);

	void showTurtleSlt(bool state);
	void redraw();
Denis Khachko's avatar
Denis Khachko committed
328 329

private:
Alexander A. Maly's avatar
Alexander A. Maly committed
330 331
	void createGui();
	void CreatePen(void);
Denis Khachko's avatar
Denis Khachko committed
332

Alexander A. Maly's avatar
Alexander A. Maly committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	bool animation;
	QMutex mutex;

	TurtleScene *CurScene;
	TurtleView *CurView;

	TurtlePen *mPen;
	QGraphicsSvgItem *mTurt;
	double netStepX, netStepY;
	QColor netColor;
	bool autoNet;
	bool penIsDrawing;
	Color penColor;
	ExtensionSystem::GlobalState currentState;
	DrawNavigator *navigator;
	QToolButton *showToolsBut, *showTurtleBut;
	QDir curDir;
	bool animate;
	qreal curAngle;
	qreal AncX, AncY;
	QTimer *redrawTimer;
	TurtlePult *pult;
Denis Khachko's avatar
Denis Khachko committed
355
};
Alexander A. Maly's avatar
Alexander A. Maly committed
356

Denis Khachko's avatar
Denis Khachko committed
357 358 359 360

} // namespace ActorTurtle

#endif // TURTLEMODULE_H