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 <iostream>
#include <GL/gl.h>
#include <cstdlib>
#include <math.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <QMimeData>
#include <QTabWidget>
#include <iostream>
#include <sstream>
#include <string>
OE_display::OE_display(QWidget* parent, Qt::WindowFlags f) : QOpenGLWidget(parent, f), zoom(1.0), pan(false), width(0), height(0)
{
}
//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();
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();
}
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())
if (!qurl.isLocalFile())
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")))
uris.push_back(url);
}
}
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)
{
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
std::string fileLower = file;
std::transform(fileLower.begin(), fileLower.end(), fileLower.begin(), ::tolower);
OE_document* newDoc = nullptr;
if (endsWith(fileLower, ".oe"))
{
printf("Importing oe file from %s\n", file.c_str());
newDoc = new OE_document();
newDoc->loadFromFile(file);
}
else if (endsWith(fileLower, ".pes"))
{
printf("Importing pes file from %s\n", file.c_str());
newDoc = new OE_document();
newDoc->loadFromPES(file);
}
else if (endsWith(fileLower, ".svg"))
{
printf("Importing svg file from %s\n", file.c_str());
newDoc = OE_document::newFromSvg(file);
}
if (newDoc)
{
OE_document* oldDoc = document;
document = newDoc;
if (oldDoc)
delete oldDoc;
if (controller)
{
controller->setDocument(newDoc);
controller->initNewDocument();
}
setDocument(newDoc);
showAll();
QTabWidget* myTab = dynamic_cast<QTabWidget*>(this->parent()->parent()->parent());
if (myTab)
{
int idx = 0; // TODO load correct tab index
myTab->setTabText(idx, QString::fromStdString(file.substr(file.rfind('/')+1)));
myTab->setTabToolTip(idx, QString::fromStdString(file));
}
}
}
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();
Loading full blame...