Skip to content
OE_display.cpp 38.8 KiB
Newer Older
3dsman's avatar
3dsman committed
* This file is part of project OpenEmbroidery. It's copyrighted by
* the contributors recorded in the version control history of the file.
* Original project location https://code.electrolab.fr/openEmbroidery/openEmbroidery_software
*
* SPDX-License-Identifier: CECILL-2.1
* License-Filename: Licence_CeCILL_V2.1-en.txt
*/

#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
#include <QMimeData>
#include <QTabWidget>
#include <iostream>
#include <sstream>
#include <string>
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();
}

raoul's avatar
raoul committed

static bool endsWith(const std::string& str, const std::string& suffix)
{
	return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
}

static std::vector<std::string> getAllowedFiles(QDropEvent *event)
{
	std::vector<std::string> uris;
	if (event->mimeData()->hasFormat("text/uri-list"))
	{
		for (const QUrl& qurl : event->mimeData()->urls())
raoul's avatar
raoul committed
		{
raoul's avatar
raoul committed
				continue;
			std::string url = qurl.toLocalFile().toStdString();
			std::string urlLower = url;
			std::transform(urlLower.begin(), urlLower.end(), urlLower.begin(), ::tolower);
			if (!(endsWith(urlLower, ".oe") || endsWith(urlLower, ".svg") || endsWith(urlLower, ".pes")))
raoul's avatar
raoul committed
				continue;
raoul's avatar
raoul committed
		}
	}
	return uris;
}

void OE_display::dragEnterEvent(QDragEnterEvent *event)
{
	std::vector<std::string> files = getAllowedFiles(event);
	if (files.size() != 1)
	{
		event->ignore();
		return;
	}
	event->setDropAction(Qt::MoveAction);
	event->accept();
}

void OE_display::dropEvent(QDropEvent *event)
{
	event->acceptProposedAction();
	loadFile(getAllowedFiles(event).front());
}
void OE_display::loadFile(std::string file)
{
raoul's avatar
raoul committed
	std::string fileLower = file;
	std::transform(fileLower.begin(), fileLower.end(), fileLower.begin(), ::tolower);

	OE_document* newDoc = nullptr;
	printf("Importing file from %s\n", file.c_str());
	newDoc = new OE_document();
	if (!newDoc->loadFromFile(file))
raoul's avatar
raoul committed
	{
		delete newDoc;
		newDoc = nullptr;
raoul's avatar
raoul committed
	}

	if (newDoc)
	{
		OE_document* oldDoc = document;
		document = newDoc;
		if (oldDoc)
			delete oldDoc;

		if (controller)
		{
			controller->setDocument(newDoc);
			controller->initNewDocument();
		}

		setDocument(newDoc);
		showAll();
		QTabWidget* tabWidget = dynamic_cast<QTabWidget*>(this->parent()->parent()->parent());
		int idx = tabWidget->indexOf(dynamic_cast<QWidget*>(this->parent()));
		tabWidget->setTabText(idx, QString::fromStdString(file.substr(file.rfind('/')+1)));
		tabWidget->setTabToolTip(idx, QString::fromStdString(file));
raoul's avatar
raoul committed
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)
{
Loading full blame...