Salome HOME
compilation on Linux
[modules/hydro.git] / src / HYDROData / test_HYDROData_OperationsFactory.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <test_HYDROData_OperationsFactory.h>
24
25 #include <HYDROData_Document.h>
26 #include <HYDROData_Image.h>
27 #include <HYDROData_PolylineXY.h>
28 #include <HYDROData_OperationsFactory.h>
29
30 #include <ImageComposer_CropOperator.h>
31
32 #include <gp_XY.hxx>
33
34 #include <QPainter>
35
36 void test_HYDROData_OperationsFactory::testCreate()
37 {
38   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document(1);
39   
40   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
41   CPPUNIT_ASSERT(aFactory);
42   Handle(HYDROData_Image) anImage = aFactory->CreateImage(aDoc, NULL);
43   CPPUNIT_ASSERT(!anImage.IsNull());
44   CPPUNIT_ASSERT(anImage->Image().isNull());
45   
46   aDoc->Close();
47 }
48
49 static QImage TestImage() {
50   QImage aPic(50, 40, QImage::Format_RGB32);
51   aPic.fill(Qt::white);
52   QPainter aPainter(&aPic);
53   aPainter.drawEllipse(6, 7, 38, 30);
54   aPainter.drawLine(0, 40, 10, 0);
55   aPainter.drawLine(10, 0, 25, 35);
56   aPainter.drawLine(25, 35, 40, 0);
57   aPainter.drawLine(40, 0, 50, 40);
58   return aPic;
59 }
60
61 void test_HYDROData_OperationsFactory::testCrop()
62 {
63   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
64   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
65   
66   // prepare the original image and crop-path
67   Handle(HYDROData_Image) anOriImage = 
68     Handle(HYDROData_Image)::DownCast( aDoc->CreateObject( KIND_IMAGE ) );
69
70   QImage aTestImage = TestImage();
71   anOriImage->SetImage( aTestImage );
72
73   Handle(HYDROData_PolylineXY) aCropPolyline = 
74     Handle(HYDROData_PolylineXY)::DownCast( aDoc->CreateObject( KIND_POLYLINEXY ) );
75
76   aCropPolyline->AddSection( "", HYDROData_PolylineXY::SECTION_POLYLINE, true );
77
78   aCropPolyline->AddPoint( 0, HYDROData_PolylineXY::Point( 25, 0  ) );
79   aCropPolyline->AddPoint( 0, HYDROData_PolylineXY::Point( 0,  20 ) );
80   aCropPolyline->AddPoint( 0, HYDROData_PolylineXY::Point( 25, 40 ) );
81   aCropPolyline->AddPoint( 0, HYDROData_PolylineXY::Point( 50, 20 ) );
82
83   // prepare Composer Operation
84   ImageComposer_Operator* aCropOp = 
85     aFactory->Operator( ImageComposer_CropOperator::Type() );
86   
87   CPPUNIT_ASSERT( aCropOp );
88
89   aCropOp->setArgs( Qt::red );
90
91   // create crop - image 
92   Handle(HYDROData_Image) aCropImage = aFactory->CreateImage( aDoc, aCropOp );
93   CPPUNIT_ASSERT( !aCropImage.IsNull() );
94
95   aCropImage->AppendReference( anOriImage );
96   aCropImage->AppendReference( aCropPolyline );
97   aCropImage->Update();
98
99   // check crop operation was performed
100   CPPUNIT_ASSERT( !aCropImage->Image().isNull() );
101   CPPUNIT_ASSERT( aCropImage->Image() != aTestImage );
102   
103   aDoc->Close();
104 }