src/toolsets/viewtoolset.cpp

Fri, 03 Mar 2017 23:23:28 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 03 Mar 2017 23:23:28 +0200
changeset 1180
2005e4147ad6
parent 1171
430ffa371d2a
child 1192
317f4ce38f3f
child 1251
e75cc5bff076
permissions
-rw-r--r--

Grid::snap now snaps points.

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2017 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QFileDialog>
#include <QInputDialog>
#include <QMessageBox>
#include "../mainwindow.h"
#include "../lddocument.h"
#include "../miscallenous.h"
#include "../canvas.h"
#include "../primitives.h"
#include "../colors.h"
#include "../glcompiler.h"
#include "../documentmanager.h"
#include "viewtoolset.h"

ViewToolset::ViewToolset (MainWindow *parent) :
	Toolset (parent) {}

void ViewToolset::selectAll()
{
	for (LDObject* obj : currentDocument()->objects())
		currentDocument()->addToSelection(obj);
}

void ViewToolset::selectByColor()
{
	if (selectedObjects().isEmpty())
		return;

	QSet<LDColor> colors;

	for (LDObject* obj : selectedObjects())
	{
		if (obj->isColored())
			colors << obj->color();
	}

	currentDocument()->clearSelection();

	for (LDObject* obj : currentDocument()->objects())
	{
		if (colors.contains (obj->color()))
			currentDocument()->addToSelection(obj);
	}
}

void ViewToolset::selectByType()
{
	if (selectedObjects().isEmpty())
		return;

	QSet<LDObjectType> types;
	QSet<QString> subfilenames;

	for (LDObject* obj : selectedObjects())
	{
		types << obj->type();

		if (obj->type() == LDObjectType::SubfileReference)
			subfilenames << static_cast<LDSubfileReference*> (obj)->fileInfo()->name();
	}

	currentDocument()->clearSelection();

	for (LDObject* obj : currentDocument()->objects())
	{
		LDObjectType type = obj->type();

		if (not types.contains (type))
			continue;

		// For subfiles, type check is not enough, we check the name of the document as well.
		if (type == LDObjectType::SubfileReference
			and not subfilenames.contains (static_cast<LDSubfileReference*> (obj)->fileInfo()->name()))
		{
			continue;
		}

		currentDocument()->addToSelection(obj);
	}
}

void ViewToolset::resetView()
{
	m_window->renderer()->resetAngles();
	m_window->renderer()->update();
}

void ViewToolset::screenshot()
{
	const char* imageFormats = "PNG images (*.png);;JPG images (*.jpg);;BMP images (*.bmp);;"
	    "PPM images (*.ppm);;X11 Bitmaps (*.xbm);;X11 Pixmaps (*.xpm);;All Files (*.*)";
	QImage image = m_window->renderer()->screenCapture();
	QString root = Basename (currentDocument()->name());

	if (root.right (4) == ".dat")
		root.chop (4);

	QString defaultname = (not root.isEmpty()) ? format ("%1.png", root) : "";
	QString filename = QFileDialog::getSaveFileName (m_window, "Save Screencap", defaultname, imageFormats);

	if (not filename.isEmpty() and not image.save (filename))
	{
		QString message = format(tr("Couldn't open %1 for writing to save screen capture: %2"), filename, strerror(errno));
		QMessageBox::critical(m_window, tr("Error"), message);
	}
}

void ViewToolset::axes()
{
	m_config->toggleDrawAxes();
	m_window->updateActions();
	m_window->renderer()->update();
}

void ViewToolset::visibilityToggle()
{
	for (LDObject* obj : selectedObjects())
		obj->setHidden (not obj->isHidden());
}

void ViewToolset::visibilityHide()
{
	for (LDObject* obj : selectedObjects())
		obj->setHidden (true);
}

void ViewToolset::visibilityReveal()
{
	for (LDObject* obj : selectedObjects())
		obj->setHidden (false);
}

void ViewToolset::wireframe()
{
	m_config->toggleDrawWireframe();
	m_window->renderer()->update();
}

void ViewToolset::drawAngles()
{
	m_config->toggleDrawAngles();
	m_window->renderer()->update();
}

void ViewToolset::setDrawDepth()
{
	if (m_window->renderer()->camera() == Camera::Free)
		return;

	bool ok;
	double depth = QInputDialog::getDouble(m_window, "Set Draw Depth",
	                                       format("Depth value for %1:", m_window->renderer()->currentCamera().name()),
	                                       m_window->renderer()->getDepthValue(), -10000.0f, 10000.0f, 3, &ok);

	if (ok)
		m_window->renderer()->setDepthValue (depth);
}

#if 0
// This is a test to draw a dummy axle. Meant to be used as a primitive gallery,
// but I can't figure how to generate these pictures properly. Multi-threading
// these is an immense pain.
void ViewToolset::testpic()
{
	LDDocument* file = getFile ("axle.dat");
	setlocale (LC_ALL, "C");

	if (not file)
	{
		critical ("couldn't load axle.dat");
		return;
	}

	int w, h;

	GLRenderer* rend = new GLRenderer;
	rend->resize (64, 64);
	rend->setAttribute (Qt::WA_DontShowOnScreen);
	rend->show();
	rend->setFile (file);
	rend->setDrawOnly (true);
	rend->compileAllObjects();
	rend->initGLData();
	rend->drawGLScene();

	uchar* imgdata = rend->screencap (w, h);
	QImage img = imageFromScreencap (imgdata, w, h);

	if (img.isNull())
	{
		critical ("Failed to create the image!\n");
	}
	else
	{
		QLabel* label = new QLabel;
		QDialog* dlg = new QDialog;
		label->setPixmap (QPixmap::fromImage (img));
		QVBoxLayout* layout = new QVBoxLayout (dlg);
		layout->addWidget (label);
		dlg->exec();
	}

	delete[] imgdata;
	rend->deleteLater();
}
#endif

void ViewToolset::bfcView()
{
	m_config->toggleBfcRedGreenView();

	if (m_config->bfcRedGreenView())
		m_config->setRandomColors (false);

	m_window->updateActions();
	m_window->renderer()->update();
}

void ViewToolset::jumpTo()
{
	bool ok;
	int defaultValue = (countof(selectedObjects()) == 1) ? (*selectedObjects().begin())->lineNumber() : 0;
	int index = QInputDialog::getInt (nullptr, "Go to line", "Go to line:", defaultValue, 1, currentDocument()->size(), 1, &ok);

	if (ok)
	{
		LDObject *object = currentDocument()->getObject(index - 1);

		if (object)
		{
			currentDocument()->clearSelection();
			currentDocument()->addToSelection(object);
			m_window->updateSelection();
		}
	}
}

void ViewToolset::randomColors()
{
	m_config->toggleRandomColors();

	if (m_config->randomColors())
		m_config->setBfcRedGreenView (false);

	m_window->updateActions();
	m_window->renderer()->update();
}

void ViewToolset::drawSurfaces()
{
	m_config->toggleDrawSurfaces();
	m_window->updateActions();
}

void ViewToolset::drawEdgeLines()
{
	m_config->toggleDrawEdgeLines();
	m_window->updateActions();
}

void ViewToolset::drawConditionalLines()
{
	m_config->toggleDrawConditionalLines();
	m_window->updateActions();
}

void ViewToolset::lighting()
{
	m_config->toggleLighting();
	m_window->updateActions();
}

mercurial