Skip to content
OE_display.cpp 35.4 KiB
Newer Older
/*
 * Copyright (c) 2015 Tricoire Sebastien 3dsman@free.fr
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 *
 */

#include "OE_display.h"
#include "OE_utils.h"

#include "OE_controller.h"

raoul's avatar
raoul committed
#include <qevent.h>

#include <GL/gl.h>
#include <cstdlib>
#include <math.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
raoul's avatar
raoul committed

raoul's avatar
raoul committed
OE_display::OE_display(QWidget* parent, Qt::WindowFlags f) : QOpenGLWidget(parent, f), zoom(1.0), pan(false), width(0), height(0)
{
}
raoul's avatar
raoul committed
OE_display::~OE_display()
raoul's avatar
raoul committed
void OE_display::mouseMoveEvent(QMouseEvent* event)
raoul's avatar
raoul committed
	//DEBUG printf("MouseEvent pos %d, %d\n", event->pos().x(), event->pos().y()); fflush(stdout);
	event->accept();
	bool redraw = mouse_Pos(event->pos().x(), event->pos().y());
	if (redraw)
		update();
	//TODO ? repaint();
raoul's avatar
raoul committed
void OE_display::mousePressEvent(QMouseEvent* event)
{
	event->accept();
	bool redraw = mouse_Button(event);
	if (redraw)
		update();
}

void OE_display::mouseReleaseEvent(QMouseEvent* event)
{
	event->accept();
	bool redraw = mouse_Button(event);
	if (redraw)
		update();
}

void OE_display::wheelEvent(QWheelEvent* event)
{
	event->accept();
	OE_display::scroll(0, event->delta());
	update();
}

void OE_display::initializeGL()
raoul's avatar
raoul committed
	initializeOpenGLFunctions();
	glEnable(GL_POINT_SMOOTH);
	glEnable(GL_LINE_SMOOTH);
}

void OE_display::resizeGL(int w, int h)
{
	resize(w, h);
	update();
}

void OE_display::paintGL()
{
	draw();
}

/** \brief draw the document on screen
 *
 * \return true if all is ok
 *
 */

raoul's avatar
raoul committed
bool OE_display::setDocument(OE_document* document)
raoul's avatar
raoul committed
/*	if (document)
raoul's avatar
raoul committed
		std::list<OE_pointcurve*>::iterator curve = document->curves.begin();

		while (curve != document->curves.end())
			(*curve)->refresh(zoom/2);
			curve++;
bool OE_display::setController(OE_controller* controller)
	this->controller = controller;
	return true;
}

bool OE_display::setDisplayStyle(OE_displayStyle* style)
{
	this->style = style;
	return true;
}

bool OE_display::refreshAll()
raoul's avatar
raoul committed
	if (document)
	{
		std::list<OE_pointcurve*>::iterator curve = document->curves.begin();

		while (curve != document->curves.end())
		{
			(*curve)->refresh(zoom/2, !changeDpi);
		std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();

		while (stitch != document->stitchs.end())
		{
			(*stitch)->refresh(zoom/2, !changeDpi);

		if (controller) controller->generateInstructions();

		changeDpi = false;
		return true;
	}
	return false;
}

/** \brief draw the move gizmo
 *
 * \return true if all is ok
 *
 */
bool OE_display::drawSelectionTools(bool select)
{
	vector_2d min = selectionBounds.getMin();
	vector_2d max = selectionBounds.getMax();

	float crossSize = zoom*style->moveCrossSize;
	float boxOffset = zoom*style->moveCrossSize;

	min.x -= boxOffset;
	min.y -= boxOffset;
	max.x += boxOffset;
	max.y += boxOffset;

	if (selectionBounds.init)
	{
		if (select)
		{
raoul's avatar
raoul committed
			glTranslatef(min.x, min.y, 0);
			glPushName(0); //id of the move tool

			glBegin(GL_QUADS);
				style->selectionBoundColor.gl();
raoul's avatar
raoul committed
				//glColor4d(1, 0, 0, 1);
				glVertex2f(crossSize*1.0f, 0);
				glVertex2f(0, crossSize*1.0f);
				glVertex2f(-crossSize*1.0f, 0);
				glVertex2f(0, -crossSize*1.0f);
			glEnd();

			glPopName();
			glPushName(1); //id of the scale tool

			glBegin(GL_QUADS);

raoul's avatar
raoul committed
			glVertex2f(-crossSize*0.5f, -crossSize*0.5f);
			glVertex2f(-crossSize*0.5f, -crossSize*1.5f);
			glVertex2f(-crossSize*1.5f, -crossSize*1.5f);
Loading full blame...