diff options
| author | Bent Bisballe Nyeng <deva@aasimon.org> | 2018-12-29 16:36:53 +0100 | 
|---|---|---|
| committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2018-12-29 16:36:53 +0100 | 
| commit | 58183c47c2b3235f8f56dc6492bb41b775dd6f26 (patch) | |
| tree | 568a2ea0b7fd451a36637c44d61618b20792668b | |
| parent | 9f33eb49ccb7fa8fb80f00865819beb499e70025 (diff) | |
Added image export functionality.
| -rw-r--r-- | src/mainwindow.cc | 27 | ||||
| -rw-r--r-- | src/mainwindow.h | 1 | ||||
| -rw-r--r-- | src/outputwindow.cc | 121 | ||||
| -rw-r--r-- | src/outputwindow.h | 4 | 
4 files changed, 113 insertions, 40 deletions
| diff --git a/src/mainwindow.cc b/src/mainwindow.cc index e52b9f3..298c811 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -117,6 +117,11 @@ MainWindow::MainWindow(QString p)  		connect(act, SIGNAL(triggered()), this, SLOT(reset()));  	} +	{ +		QAction *act = toolbar->addAction("Image"); +		connect(act, SIGNAL(triggered()), this, SLOT(image())); +	} +  	loadSettings();  	statusBar()->showMessage(tr("Ready")); @@ -356,6 +361,28 @@ void MainWindow::reset()  	out->reset();  } +void MainWindow::image() +{ +	QString filename +		= QFileDialog::getSaveFileName(this, tr("Save Kaiman File"), +		                               "", +		                               tr("Image Files (*.png)")); + +	if(filename == "") +	{ +		// User clicked cancel +		return; +	} + +	if(filename.right(4) != ".png") +	{ +		filename += ".png"; +	} + +	auto image = out->acquire(true); +	image.save(filename); +} +  void MainWindow::programChanged()  {  	dirty = true; diff --git a/src/mainwindow.h b/src/mainwindow.h index 5943301..2be4731 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -57,6 +57,7 @@ private slots:  	void stop();  	void start();  	void reset(); +	void image();  	void programChanged(); diff --git a/src/outputwindow.cc b/src/outputwindow.cc index b7b1a51..8c4cb7b 100644 --- a/src/outputwindow.cc +++ b/src/outputwindow.cc @@ -48,16 +48,6 @@ OutputWindow::OutputWindow()  	reset();  } -double OutputWindow::coordX() -{ -	return x; -} - -double OutputWindow::coordY() -{ -	return y; -} -  void OutputWindow::stopScript()  {  	stop = true; @@ -103,6 +93,50 @@ void OutputWindow::setColour(int r, int g, int b, int a)  	sem.acquire();  } +double OutputWindow::coordX() +{ +	return x; +} + +double OutputWindow::coordY() +{ +	return y; +} + +QImage OutputWindow::acquire(bool includePen) +{ +	double scale = 2.0; + +	// Minimum picture size is 32x32 +	double minx = -16; +	double miny = -16; +	double maxx = 16; +	double maxy = 16; +	for(const auto& line : lines) +	{ +		maxx = std::max(line.line.p1().x() * scale, maxx); +		maxx = std::max(line.line.p2().x() * scale, maxx); +		maxy = std::max(line.line.p1().y() * scale, maxy); +		maxy = std::max(line.line.p2().y() * scale, maxy); + +		minx = std::min(line.line.p1().x() * scale, minx); +		minx = std::min(line.line.p2().x() * scale, minx); +		miny = std::min(line.line.p1().y() * scale, miny); +		miny = std::min(line.line.p2().y() * scale, miny); +	} + +	int border = 100 * scale; +	int width = maxx - minx + border*2; +	int height = maxy - miny + border*2; + +	QImage img({width, height}, QImage::Format_ARGB32); +	img.fill(QColor(0,0,0,0)); +	QPainter painter(&img); +	paint(painter, {-minx + border, -miny + border}, includePen, scale); + +	return img; +} +  void OutputWindow::timeout()  {  	//x++; @@ -112,12 +146,28 @@ void OutputWindow::timeout()  	timer.start(25);  } +void OutputWindow::reset() +{ +	speed = 50; +	penfile = "gfx/kaiman.png"; +	loadpen = true; + +	lines.clear(); +	current_points.clear(); +	x = 0; +	y = 0; +	r = 0; +	colour = QColor(150, 0, 0, 150); +	stop = false; +	while(sem.tryAcquire()) +	{ +	} +}  void OutputWindow::wheelEvent(QWheelEvent* event)  {  	double delta = event->delta() / 120.0 * scale;  	scale += delta / 10.0; -	printf("%f\n", scale);  	if(scale < 0.3)  	{  		scale = 0.3; @@ -127,8 +177,14 @@ void OutputWindow::wheelEvent(QWheelEvent* event)  void OutputWindow::paintEvent(QPaintEvent *)  {  	//sem.acquire(); -	QPainter p(this); +	QPainter painter(this); +	paint(painter, {width() / 2.0, height() / 2.0}, true, scale); +	sem.release(); +} +void OutputWindow::paint(QPainter& p, const QPointF& offset, +                         bool includePen, double scale) +{  	if(loadpen)  	{  		QPixmap img(penfile); @@ -136,16 +192,21 @@ void OutputWindow::paintEvent(QPaintEvent *)  		loadpen = false;  	} -	QTransform t; -	t.rotate(-r + 90); -	t.scale((100.0 / kaiman.width() * scale), -	        (100.0 / kaiman.width() * scale) ); -	QImage img = kaiman.transformed(t, Qt::SmoothTransformation); -	p.drawImage((x * scale) - img.width()/2 + width() / 2, -	            (y * scale) - img.height()/2 + height() / 2, img); +	if(includePen) +	{ +		QTransform t; +		t.rotate(-r + 90); +		auto m = std::max(kaiman.width(),kaiman.height()); +		t.scale((100.0 / m * scale), +		        (100.0 / m * scale) ); +		QImage img = kaiman.transformed(t, Qt::SmoothTransformation); +		p.drawImage((x * scale) - img.width()/2 + offset.x(), +		            (y * scale) - img.height()/2 + offset.y(), +		            img); +	}  	QTransform gt; -	gt.translate(width() / 2, height() / 2); +	gt.translate(offset.x(), offset.y());  	gt.scale(scale, scale);  	p.setTransform(gt); @@ -190,26 +251,6 @@ void OutputWindow::paintEvent(QPaintEvent *)  		QPointF p2 = current_points[i + 1];  		p.drawLine(QLineF(p1, p2));  	} - -	sem.release(); -} - -void OutputWindow::reset() -{ -	speed = 50; -	penfile = "gfx/kaiman.png"; -	loadpen = true; - -	lines.clear(); -	current_points.clear(); -	x = 0;//(width() / 2) / scale; -	y = 0;//(height() / 2) / scale; -	r = 0; -	colour = QColor(150, 0, 0, 150); -	stop = false; -	while(sem.tryAcquire()) -	{ -	}  }  static inline int sign(int x) diff --git a/src/outputwindow.h b/src/outputwindow.h index fc63a44..bb7ef7b 100644 --- a/src/outputwindow.h +++ b/src/outputwindow.h @@ -58,6 +58,8 @@ public:  	double coordX();  	double coordY(); +	QImage acquire(bool includePen); +  public slots:  	void timeout();  	void reset(); @@ -67,6 +69,8 @@ protected:  	void paintEvent(QPaintEvent* event);  private: +	void paint(QPainter& p, const QPointF& offset, bool includePen, double scale); +  	class ColLine  	{  	public: | 
