Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef PLANE_VIEWER_H
Toshihiro Shimizu 890ddd
#define PLANE_VIEWER_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
#include "traster.h"
Toshihiro Shimizu 890ddd
#include "timage.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Toshihiro Shimizu 890ddd
#include <qglwidget></qglwidget>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TOONZQT_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//  Forward declarations
Toshihiro Shimizu 890ddd
class TRasterImageP;
Toshihiro Shimizu 890ddd
class TToonzImageP;
Toshihiro Shimizu 890ddd
class TVectorImageP;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  \brief    The PlaneViewer class implements a basic OpenGL widget showing a plane in a 
Toshihiro Shimizu 890ddd
            standard top-down view, and supports image drawing from the Toonz images API.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  \details  A PlaneViewer instance is designed to view bidimensional objects layed-out
Toshihiro Shimizu 890ddd
            on a plane, providing standard mouse interaction functions and some built-in
Toshihiro Shimizu 890ddd
            keyboard shortcuts to provide view navigation.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
            The class implements the necessary methods to draw objects on the plane - 
Toshihiro Shimizu 890ddd
            prominently, functions to push both world and widget references for standard
Toshihiro Shimizu 890ddd
            OpenGL drawing, conversions between world and widget coordinates, and
Toshihiro Shimizu 890ddd
            efficient image-drawing functions for all Toonz image types.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI PlaneViewer : public QGLWidget
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	PlaneViewer(QWidget *parent = 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Background functions
Toshihiro Shimizu 890ddd
	void setBgColor(const TPixel32 &color1, const TPixel32 &color2);
Toshihiro Shimizu 890ddd
	void getBgColor(TPixel32 &color1, TPixel32 &color2) const;
Toshihiro Shimizu 890ddd
	void setChessSize(double size);
Toshihiro Shimizu 890ddd
	void drawBackground();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Image drawing functions
Toshihiro Shimizu 890ddd
	void draw(TRasterP ras, double dpiX, double dpiY, TPalette *palette = 0);
Toshihiro Shimizu 890ddd
	void draw(TRasterImageP ri);
Toshihiro Shimizu 890ddd
	void draw(TToonzImageP ti);
Toshihiro Shimizu 890ddd
	void draw(TVectorImageP vi);
Toshihiro Shimizu 890ddd
	void draw(TImageP img);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// World-Widget conversion functions
Toshihiro Shimizu 890ddd
	TPointD winToWorld(int x, int y) { return m_aff.inv() * TPointD(x, y); }
Toshihiro Shimizu 890ddd
	TPointD worldToWin(double x, double y) { return m_aff * TPointD(x, y); }
Toshihiro Shimizu 890ddd
	TPoint qtWinToWin(int x, int y) { return TPoint(x, height() - y); }
Toshihiro Shimizu 890ddd
	TPointD qtWinToWorld(int x, int y) { return m_aff.inv() * TPointD(x, height() - y); }
Toshihiro Shimizu 890ddd
	TPoint winToQtWin(int x, int y) { return qtWinToWin(x, y); }
Toshihiro Shimizu 890ddd
	TPointD worldToQtWin(double x, double y)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPointD res(worldToWin(x, y));
Toshihiro Shimizu 890ddd
		return TPointD(res.x, height() - res.y);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// World-Widget OpenGL references
Toshihiro Shimizu 890ddd
	void pushGLWorldCoordinates();
Toshihiro Shimizu 890ddd
	void pushGLWinCoordinates();
Toshihiro Shimizu 890ddd
	void popGLCoordinates();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// View functions
Toshihiro Shimizu 890ddd
	TAffine &viewAff() { return m_aff; }
Toshihiro Shimizu 890ddd
	const TAffine &viewAff() const { return m_aff; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void resetView();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void zoomIn();
Toshihiro Shimizu 890ddd
	void zoomOut();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setViewPos(double x, double y);
Toshihiro Shimizu 890ddd
	void setViewZoom(double x, double y, double zoom);
Toshihiro Shimizu 890ddd
	void setViewZoom(double zoom) { setViewZoom(0.5 * width(), 0.5 * height(), zoom); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void moveView(double dx, double dy) { setViewPos(m_aff.a13 + dx, m_aff.a23 + dy); }
Toshihiro Shimizu 890ddd
	void zoomView(double x, double y, double delta) { setViewZoom(x, y, m_aff.a11 * delta); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setZoomRange(double zoomMin, double zoomMax);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Auxiliary functions
Toshihiro Shimizu 890ddd
	TRaster32P rasterBuffer();
Toshihiro Shimizu 890ddd
	void flushRasterBuffer();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Toshihiro Shimizu 890ddd
	int m_xpos, m_ypos; //!< Mouse position on mouse operations.
Toshihiro Shimizu 890ddd
	TAffine m_aff;		//!< Affine transform from world to widget coords.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	float m_bgColorF[6]; //!< Widget bg color cast in the [0, 1] interval.
Toshihiro Shimizu 890ddd
	double m_chessSize;  //!< Size of the chessboard squares (default is 40).
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRaster32P m_rasterBuffer; //!< Auxiliary buffer used to draw on the widget directly.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double m_zoomRange[2]; //!< Viewport zoom range (default: [-1024, 1024]).
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Toshihiro Shimizu 890ddd
	virtual void mouseMoveEvent(QMouseEvent *event);
Toshihiro Shimizu 890ddd
	virtual void mousePressEvent(QMouseEvent *event);
Toshihiro Shimizu 890ddd
	virtual void wheelEvent(QWheelEvent *event);
Toshihiro Shimizu 890ddd
	virtual void keyPressEvent(QKeyEvent *event);
Toshihiro Shimizu 890ddd
	virtual void resizeEvent(QResizeEvent *event);
Toshihiro Shimizu 890ddd
	virtual void hideEvent(QHideEvent *event);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void initMatrix();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	GLdouble m_matrix[16];
Toshihiro Shimizu 890ddd
	bool m_firstResize;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif //PLANE_VIEWER_H