<commit_before>/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library 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 Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: <EMAIL>                             *
******************************************************************************/
#ifndef SOFA_IMAGE_IMAGEPLANEWIDGET_H
#define SOFA_IMAGE_IMAGEPLANEWIDGET_H

#include "initImage.h"
#include <sofa/gui/qt/DataWidget.h>
#include <sofa/gui/qt/SimpleDataWidget.h>

#ifdef SOFA_QT4
#include <QLabel>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QImage>
#include <QSlider>
#include <QString>
#include <QDoubleSpinBox>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QtGui>
#else
#include <qlabel.h>
#include <qvboxlayout.h>
#include <qgridlayout.h>
#include <qimage.h>
#include <qlayout.h>
#include <qslider.h>
#include <qstring.h>
#include <qdoublespinbox.h>
#include <qgraphicsView.h>
#include <qgraphicsscene.h>
#include <qtgui.h>
#endif

#include "ImageTypes.h"
#include <sofa/core/objectmodel/BaseData.h>
#include <sofa/core/objectmodel/Data.h>
#include <sofa/helper/rmath.h>
#include <sofa/helper/vector.h>


#if !defined(INFINITY)
#define INFINITY 9.0e10
#endif


namespace sofa
{
namespace gui
{
namespace qt
{

using namespace helper;

//-----------------------------------------------------------------------------------------------//
//	image widget -> draw image and handle mouse events
//-----------------------------------------------------------------------------------------------//

class ImagePlaneGraphScene : public QGraphicsScene
{
    Q_OBJECT

public:
    ImagePlaneGraphScene(QImage* im,QObject *parent=0) : QGraphicsScene(parent)	, drawrectangle(false),image(im) 	{ this->setSceneRect(0,0,image->width(),image->height()); }
    QPointF P1;
    QPointF P2;
    bool drawrectangle;

private:
    const QImage *image;
    void drawBackground(QPainter *painter, const QRectF &rect)
    {
        QGraphicsScene::drawBackground(painter,rect);
        if(image) painter->drawImage(this->sceneRect(),*image);
    }

    void drawForeground(QPainter *painter, const QRectF &rect)
    {
        QGraphicsScene::drawForeground(painter,rect);

        if(drawrectangle)
        {
            QRectF r(P1,P2);
            painter->setPen ( Qt::NoPen);
            painter->setBrush(Qt::SolidPattern);
            painter->setBrush(Qt::cyan);
            painter->setOpacity (0.1);
            painter->drawRect(r);

            painter->setPen ( QPen ( Qt::darkCyan, 0.5, Qt::DotLine ) );
            painter->setBrush(Qt::NoBrush);
            painter->setOpacity (1);
            painter->drawRect(r);
        }
        else
        {
            QRectF r(round(P1.x()),round(P1.y()),1,1);
            painter->setPen ( Qt::NoPen);
            painter->setBrush(Qt::SolidPattern);
            painter->setBrush(Qt::green);
            painter->setOpacity (0.5);
            painter->drawRect(r);
        }
    }

};


class ImagePlaneGraphWidget : public QGraphicsView
{
    Q_OBJECT

public slots:
    void Render ()  { if(this->scene) this->scene->update(); }
    void setRoiHorizontal(const qreal left, const qreal width)	{roi.setLeft(left); roi.setWidth(width);	this->fitInView(); 	}
    void setRoiVertical(const qreal top, const qreal height)	{roi.setTop(top);	roi.setHeight(height);	this->fitInView(); 	}
    void setCursorX(const qreal v)	{scene->P1.setX(v); Render(); 	}
    void setCursorY(const qreal v)	{scene->P1.setY(v); Render(); 	}
    void togglemodels(int i) { this->visumodels=i?true:false; draw(); }
    void setScrollBarPolicies(bool on)  { if(on) { this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); 	this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); }	else   { this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);	this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);} }

signals:
    void wheelevent (int delta); // -> change index on slider
    void roiHorizontalChanged(const qreal left, const qreal width); // to synchronize different views (roi)
    void roiVerticalChanged(const qreal top,const qreal height);	// to synchronize different views (roi)
    void cursorChangedX(const qreal);		// to synchronize different views (cursor)
    void cursorChangedY(const qreal);		// to synchronize different views (cursor)
    void cursorChangedZ(const qreal);		// to synchronize different views (cursor)
    void roiResized();		// to synchronize different views (scrollBars on/off)

    void sliceModified();	// when the slice index is modified using slider or wheel -> set widget dirty
    void onPlane(const unsigned int,const Vector3&,const Vector3&,const QString&);  // when the mouse is on image -> update info

public:
    ImagePlaneGraphWidget(QWidget *parent )	: QGraphicsView(parent),scene(NULL)   {	 this->setMouseTracking(true); }

    virtual QSize 	sizeHint () const { return QSize(10000,10000); }  // for vertical expanding

    virtual void fromOption(const unsigned int i)=0;					// slice index change -> redraw
    virtual void fromGraph(const QPointF &pt,const bool clicked)=0;		// mouse move -> get image info
    virtual void draw()=0;

    bool isRoiResized() {return RoiResized;}

protected:
    void resizeEvent ( QResizeEvent* /*resizeevent*/)  { this->fitInView();	}

    void fitInView() // modified version of original fitinview (no margin)
    {
        QRectF unity = this->matrix().mapRect(QRectF(0, 0, 1, 1));
        scale(1 / unity.width(), 1 / unity.height());
        QRectF sceneRect = this->matrix().mapRect(roi);<commit_msg>r11779/sofa-dev : FIX: small edits to allow Image to compile in VS2008<commit_after>/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library 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 Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: <EMAIL>                             *
******************************************************************************/
#ifndef SOFA_IMAGE_IMAGEPLANEWIDGET_H
#define SOFA_IMAGE_IMAGEPLANEWIDGET_H

#include "initImage.h"
#include <sofa/gui/qt/DataWidget.h>
#include <sofa/gui/qt/SimpleDataWidget.h>

#ifdef SOFA_QT4
#include <QLabel>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QImage>
#include <QSlider>
#include <QString>
#include <QDoubleSpinBox>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QtGui>
#else
#include <qlabel.h>
#include <qvboxlayout.h>
#include <qgridlayout.h>
#include <qimage.h>
#include <qlayout.h>
#include <qslider.h>
#include <qstring.h>
#include <qdoublespinbox.h>
#include <qgraphicsView.h>
#include <qgraphicsscene.h>
#include <qtgui.h>
#endif

#include "ImageTypes.h"
#include <sofa/core/objectmodel/BaseData.h>
#include <sofa/core/objectmodel/Data.h>
#include <sofa/helper/rmath.h>
#include <sofa/helper/vector.h>


#if !defined(INFINITY)
#define INFINITY 9.0e10
#endif


namespace sofa
{
namespace gui
{
namespace qt
{

using namespace helper;

//-----------------------------------------------------------------------------------------------//
//	image widget -> draw image and handle mouse events
//-----------------------------------------------------------------------------------------------//

class ImagePlaneGraphScene : public QGraphicsScene
{
    Q_OBJECT

public:
    ImagePlaneGraphScene(QImage* im,QObject *parent=0) : QGraphicsScene(parent)	, drawrectangle(false),image(im) 	{ this->setSceneRect(0,0,image->width(),image->height()); }
    QPointF P1;
    QPointF P2;
    bool drawrectangle;

private:
    const QImage *image;
    void drawBackground(QPainter *painter, const QRectF &rect)
    {
        QGraphicsScene::drawBackground(painter,rect);
        if(image) painter->drawImage(this->sceneRect(),*image);
    }

    void drawForeground(QPainter *painter, const QRectF &rect)
    {
        QGraphicsScene::drawForeground(painter,rect);

        if(drawrectangle)
        {
            QRectF r(P1,P2);
            painter->setPen ( Qt::NoPen);
            painter->setBrush(Qt::SolidPattern);
            painter->setBrush(Qt::cyan);
            painter->setOpacity (0.1);
            painter->drawRect(r);

            painter->setPen ( QPen ( Qt::darkCyan, 0.5, Qt::DotLine ) );
            painter->setBrush(Qt::NoBrush);
            painter->setOpacity (1);
            painter->drawRect(r);
        }
        else
        {
            QRectF r(round(P1.x()),round(P1.y()),1,1);
            painter->setPen ( Qt::NoPen);
            painter->setBrush(Qt::SolidPattern);
            painter->setBrush(Qt::green);
            painter->setOpacity (0.5);
            painter->drawRect(r);
        }
    }

};


class ImagePlaneGraphWidget : public QGraphicsView
{
    Q_OBJECT

public slots:
    void Render ()  { if(this->scene) this->scene->update(); }
    void setRoiHorizontal(const qreal left, const qreal width)	{roi.setLeft(left); roi.setWidth(width);	this->fitInView(); 	}
    void setRoiVertical(const qreal top, const qreal height)	{roi.setTop(top);	roi.setHeight(height);	this->fitInView(); 	}
    void setCursorX(const qreal v)	{scene->P1.setX(v); Render(); 	}
    void setCursorY(const qreal v)	{scene->P1.setY(v); Render(); 	}
    void togglemodels(int i) { this->visumodels=i?true:false; draw(); }
    void setScrollBarPolicies(bool on)  { if(on) { this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); 	this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); }	else   { this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);	this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);} }

signals:
    void wheelevent (int delta); // -> change index on slider
    void roiHorizontalChanged(const qreal left, const qreal width); // to synchronize different views (roi)
    void roiVerticalChanged(const qreal top,const qreal height);	// to synchronize different views (roi)
    void cursorChangedX(const qreal);		// to synchronize different views (cursor)
    void cursorChangedY(const qreal);		// to synchronize different views (cursor)
    void cursorChangedZ(const qreal);		// to synchronize different views (cursor)
    void roiResized();		// to synchronize different views (scrollBars on/off)

    void sliceModified();	// when the slice index is modified using slider or wheel -> set widget dirty
    void onPlane(const unsigned int,const sofa::defaulttype::Vec3d&,const sofa::defaulttype::Vec3d&,const QString&);  // when the mouse is on image -> update info

public:
    ImagePlaneGraphWidget(QWidget *parent )	: QGraphicsView(parent),scene(NULL)   {	 this->setMouseTracking(true); }

    virtual QSize 	sizeHint () const { return QSize(10000,10000); }  // for vertical expanding

    virtual void fromOption(const unsigned int i)=0;					// slice index change -> redraw
    virtual void fromGraph(const QPointF &pt,const bool clicked)=0;		// mouse move -> get image info
    virtual void draw()=0;

    bool isRoiResized() {return RoiResized;}

protected:
    void resizeEvent ( QResizeEvent* /*resizeevent*/)  { this->fitInView();	}

    void fitInView() // modified version of original fitinview (no margin)
    {
        QRectF unity = this->matrix().mapRect(QRectF(0, 0, 1, 1));
        scale(1 / unity.width(), 1 / unity.height());
        QRectF sceneRect = this->matrix().mapRect(roi);
{'fraction_non_alphanumeric': 0.16464723395416464, 'fraction_numerical': 0.010316416257010316, 'mean_word_length': 2.3747663551401867, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7049830', 'n_tokens_mistral': 4147, 'n_tokens_neox': 4015, 'n_words': 1057}