]> Shamusworld >> Repos - architektonas/blobdiff - src/base/rs_image.cpp
Refactoring: Moved RS_GraphicView to GraphicView.
[architektonas] / src / base / rs_image.cpp
index bf0e1c496ca1f2fe13e912ecb88f3bfe0141db9c..a92297f2c3295d57b01947d964b72e0f3e20bb32 100644 (file)
@@ -16,7 +16,8 @@
 
 #include "rs_constructionline.h"
 #include "rs_debug.h"
-#include "rs_graphicview.h"
+#include "graphicview.h"
+#include "rs_line.h"
 #include "paintintf.h"
 
 /**
@@ -35,8 +36,8 @@ RS_Image::RS_Image(RS_EntityContainer * parent, const RS_ImageData & d):
 RS_Image::~RS_Image()
 {
        /*if (img!=NULL) {
-               delete[] img;
-       }*/
+               delete[] img;
+          }*/
 }
 
 RS_Entity * RS_Image::clone()
@@ -48,6 +49,12 @@ RS_Entity * RS_Image::clone()
        return i;
 }
 
+/**    @return RS2::EntityImage */
+/*virtual*/ RS2::EntityType RS_Image::rtti() const
+{
+       return RS2::EntityImage;
+}
+
 void RS_Image::update()
 {
        RS_DEBUG->print("RS_Image::update");
@@ -62,38 +69,154 @@ void RS_Image::update()
        RS_DEBUG->print("RS_Image::update: OK");
 
        /*
-       // number of small images:
-       nx = image.width()/100;
-       ny = image.height()/100;
-
-       // create small images:
-       img = new QImage*[nx];
-       RS_Pixmap pm;
-       int w,h;
-       for (int x = 0; x<nx; ++x) {
-               img[x] = new QImage[ny];
-               for (int y = 0; y<ny; ++y) {
-                       if (x<nx-1) {
-                               w = 100;
-                       }
-                       else {
-                               w = image.width()%100;
-                       }
-
-                       if (y<ny-1) {
-                               h = 100;
-                       }
-                       else {
-                               h = image.height()%100;
-                       }
-
-                       pm = RS_Pixmap(w, h);
-                       RS_PainterQt painter(&pm);
-                       painter.drawImage(-x*100, -y*100, image);
-                       img[x][y] = pm.convertToImage();
-               }
-       }
-       */
+          // number of small images:
+          nx = image.width()/100;
+          ny = image.height()/100;
+
+          // create small images:
+          img = new QImage*[nx];
+          RS_Pixmap pm;
+          int w,h;
+          for (int x = 0; x<nx; ++x) {
+               img[x] = new QImage[ny];
+               for (int y = 0; y<ny; ++y) {
+                       if (x<nx-1) {
+                               w = 100;
+                       }
+                       else {
+                               w = image.width()%100;
+                       }
+
+                       if (y<ny-1) {
+                               h = 100;
+                       }
+                       else {
+                               h = image.height()%100;
+                       }
+
+                       pm = RS_Pixmap(w, h);
+                       RS_PainterQt painter(&pm);
+                       painter.drawImage(-x*100, -y*100, image);
+                       img[x][y] = pm.convertToImage();
+               }
+          }
+        */
+}
+
+/** @return Copy of data that defines the image. */
+RS_ImageData RS_Image::getData() const
+{
+       return data;
+}
+
+/** @return Insertion point of the entity */
+/*virtual*/ Vector RS_Image::getInsertionPoint() const
+{
+       return data.insertionPoint;
+}
+
+/** Sets the insertion point for the image. */
+void RS_Image::setInsertionPoint(Vector ip)
+{
+       data.insertionPoint = ip;
+       calculateBorders();
+}
+
+/** @return File name of the image. */
+QString RS_Image::getFile() const
+{
+       return data.file;
+}
+
+/** Sets the file name of the image.  */
+void RS_Image::setFile(const QString & file)
+{
+       data.file = file;
+}
+
+/** @return u Vector. Points along bottom, 1 pixel long. */
+Vector RS_Image::getUVector() const
+{
+       return data.uVector;
+}
+
+/** @return v Vector. Points along left, 1 pixel long. */
+Vector RS_Image::getVVector() const
+{
+       return data.vVector;
+}
+
+/** @return Width of image in pixels. */
+int RS_Image::getWidth() const
+{
+       return (int)data.size.x;
+}
+
+/** @return Height of image in pixels. */
+int RS_Image::getHeight() const
+{
+       return (int)data.size.y;
+}
+
+/** @return Brightness. */
+int RS_Image::getBrightness() const
+{
+       return data.brightness;
+}
+
+/** @return Contrast. */
+int RS_Image::getContrast() const
+{
+       return data.contrast;
+}
+
+/** @return Fade. */
+int RS_Image::getFade() const
+{
+       return data.fade;
+}
+
+/** @return Image definition handle. */
+int RS_Image::getHandle() const
+{
+       return data.handle;
+}
+
+/** Sets the image definition handle. */
+void RS_Image::setHandle(int h)
+{
+       data.handle = h;
+}
+
+/** @return The four corners. **/
+VectorSolutions RS_Image::getCorners()
+{
+       VectorSolutions sol(4);
+
+       sol.set(0, data.insertionPoint);
+       sol.set(1,
+               data.insertionPoint + data.uVector * RS_Math::round(data.size.x));
+       sol.set(3,
+               data.insertionPoint + data.vVector * RS_Math::round(data.size.y));
+       sol.set(2, sol.get(3) + data.uVector * RS_Math::round(data.size.x));
+
+       return sol;
+}
+
+/**
+ * @return image with in graphic units.
+ */
+double RS_Image::getImageWidth()
+{
+       return data.size.x * data.uVector.magnitude();
+}
+
+/**
+ * @return image height in graphic units.
+ */
+double RS_Image::getImageHeight()
+{
+       return data.size.y * data.vVector.magnitude();
 }
 
 void RS_Image::calculateBorders()
@@ -101,7 +224,7 @@ void RS_Image::calculateBorders()
        resetBorders();
        VectorSolutions sol = getCorners();
 
-       for(int i=0; i<4; ++i)
+       for (int i = 0; i < 4; ++i)
        {
                minV = Vector::minimum(minV, sol.get(i));
                maxV = Vector::maximum(maxV, sol.get(i));
@@ -114,8 +237,7 @@ Vector RS_Image::getNearestEndpoint(const Vector & coord, double * dist)
        return corners.getClosest(coord, dist);
 }
 
-Vector RS_Image::getNearestPointOnEntity(const Vector & coord, bool onEntity, double * dist,
-       RS_Entity ** entity)
+Vector RS_Image::getNearestPointOnEntity(const Vector & coord, bool onEntity, double * dist, RS_Entity * * entity)
 {
        if (entity != NULL)
                *entity = this;
@@ -130,7 +252,7 @@ Vector RS_Image::getNearestPointOnEntity(const Vector & coord, bool onEntity, do
                RS_Line(NULL, RS_LineData(corners.get(3), corners.get(0)))
        };
 
-       for(int i=0; i<4; ++i)
+       for (int i = 0; i < 4; ++i)
                points.set(i, l[i].getNearestPointOnEntity(coord, onEntity));
 
        return points.getClosest(coord, dist);
@@ -141,113 +263,102 @@ Vector RS_Image::getNearestCenter(const Vector & coord, double * dist)
        VectorSolutions points(4);
        VectorSolutions corners = getCorners();
 
-       points.set(0, (corners.get(0) + corners.get(1))/2.0);
-       points.set(1, (corners.get(1) + corners.get(2))/2.0);
-       points.set(2, (corners.get(2) + corners.get(3))/2.0);
-       points.set(3, (corners.get(3) + corners.get(0))/2.0);
+       points.set(0, (corners.get(0) + corners.get(1)) / 2.0);
+       points.set(1, (corners.get(1) + corners.get(2)) / 2.0);
+       points.set(2, (corners.get(2) + corners.get(3)) / 2.0);
+       points.set(3, (corners.get(3) + corners.get(0)) / 2.0);
 
        return points.getClosest(coord, dist);
 }
 
-
-
-Vector RS_Image::getNearestMiddle(const Vector& coord,
-                                     double* dist) {
-    return getNearestCenter(coord, dist);
+Vector RS_Image::getNearestMiddle(const Vector & coord, double * dist)
+{
+       return getNearestCenter(coord, dist);
 }
 
+Vector RS_Image::getNearestDist(double distance, const Vector & coord, double * dist)
+{
+       VectorSolutions corners = getCorners();
+       VectorSolutions points(4);
 
+       RS_Line l[] =
+       {
+               RS_Line(NULL, RS_LineData(corners.get(0), corners.get(1))),
+               RS_Line(NULL, RS_LineData(corners.get(1), corners.get(2))),
+               RS_Line(NULL, RS_LineData(corners.get(2), corners.get(3))),
+               RS_Line(NULL, RS_LineData(corners.get(3), corners.get(0)))
+       };
 
-Vector RS_Image::getNearestDist(double distance,
-                                   const Vector& coord,
-                                   double* dist) {
-
-    VectorSolutions corners = getCorners();
-    VectorSolutions points(4);
-
-    RS_Line l[] =
-        {
-            RS_Line(NULL, RS_LineData(corners.get(0), corners.get(1))),
-            RS_Line(NULL, RS_LineData(corners.get(1), corners.get(2))),
-            RS_Line(NULL, RS_LineData(corners.get(2), corners.get(3))),
-            RS_Line(NULL, RS_LineData(corners.get(3), corners.get(0)))
-        };
-
-    for (int i=0; i<4; ++i) {
-        points.set(i, l[i].getNearestDist(distance, coord, dist));
-    }
+       for (int i = 0; i < 4; ++i)
+               points.set(i, l[i].getNearestDist(distance, coord, dist));
 
-    return points.getClosest(coord, dist);
+       return points.getClosest(coord, dist);
 }
 
+double RS_Image::getDistanceToPoint(const Vector & coord, RS_Entity * * entity, RS2::ResolveLevel /*level*/,        double /*solidDist*/)
+{
+       if (entity != NULL)
+               *entity = this;
 
+       VectorSolutions corners = getCorners();
+       double dist;
+       double minDist = RS_MAXDOUBLE;
 
-double RS_Image::getDistanceToPoint(const Vector& coord,
-                                    RS_Entity** entity,
-                                    RS2::ResolveLevel /*level*/,
-                                                                       double /*solidDist*/) {
-    if (entity!=NULL) {
-        *entity = this;
-    }
-
-    VectorSolutions corners = getCorners();
-    double dist;
-    double minDist = RS_MAXDOUBLE;
+       RS_Line l[] =
+       {
+               RS_Line(NULL, RS_LineData(corners.get(0), corners.get(1))),
+               RS_Line(NULL, RS_LineData(corners.get(1), corners.get(2))),
+               RS_Line(NULL, RS_LineData(corners.get(2), corners.get(3))),
+               RS_Line(NULL, RS_LineData(corners.get(3), corners.get(0)))
+       };
 
-    RS_Line l[] =
-        {
-            RS_Line(NULL, RS_LineData(corners.get(0), corners.get(1))),
-            RS_Line(NULL, RS_LineData(corners.get(1), corners.get(2))),
-            RS_Line(NULL, RS_LineData(corners.get(2), corners.get(3))),
-            RS_Line(NULL, RS_LineData(corners.get(3), corners.get(0)))
-        };
+       for (int i = 0; i < 4; ++i)
+       {
+               dist = l[i].getDistanceToPoint(coord, NULL);
 
-    for (int i=0; i<4; ++i) {
-        dist = l[i].getDistanceToPoint(coord, NULL);
-        if (dist<minDist) {
-            minDist = dist;
-        }
-    }
+               if (dist < minDist)
+                       minDist = dist;
+       }
 
-    return minDist;
+       return minDist;
 }
 
-
-
-void RS_Image::move(Vector offset) {
-    data.insertionPoint.move(offset);
-    calculateBorders();
+/*virtual*/ double RS_Image::getLength()
+{
+       return -1.0;
 }
 
-
-
-void RS_Image::rotate(Vector center, double angle) {
-    data.insertionPoint.rotate(center, angle);
-    data.uVector.rotate(angle);
-    data.vVector.rotate(angle);
-    calculateBorders();
+void RS_Image::move(Vector offset)
+{
+       data.insertionPoint.move(offset);
+       calculateBorders();
 }
 
-
-
-void RS_Image::scale(Vector center, Vector factor) {
-    data.insertionPoint.scale(center, factor);
-    data.uVector.scale(factor);
-    data.vVector.scale(factor);
-    calculateBorders();
+void RS_Image::rotate(Vector center, double angle)
+{
+       data.insertionPoint.rotate(center, angle);
+       data.uVector.rotate(angle);
+       data.vVector.rotate(angle);
+       calculateBorders();
 }
 
+void RS_Image::scale(Vector center, Vector factor)
+{
+       data.insertionPoint.scale(center, factor);
+       data.uVector.scale(factor);
+       data.vVector.scale(factor);
+       calculateBorders();
+}
 
-
-void RS_Image::mirror(Vector axisPoint1, Vector axisPoint2) {
-    data.insertionPoint.mirror(axisPoint1, axisPoint2);
-    data.uVector.mirror(Vector(0.0,0.0), axisPoint2-axisPoint1);
-    data.vVector.mirror(Vector(0.0,0.0), axisPoint2-axisPoint1);
-    calculateBorders();
+void RS_Image::mirror(Vector axisPoint1, Vector axisPoint2)
+{
+       data.insertionPoint.mirror(axisPoint1, axisPoint2);
+       data.uVector.mirror(Vector(0.0, 0.0), axisPoint2 - axisPoint1);
+       data.vVector.mirror(Vector(0.0, 0.0), axisPoint2 - axisPoint1);
+       calculateBorders();
 }
 
-//void RS_Image::draw(RS_Painter* painter, RS_GraphicView* view, double /*patternOffset*/) {
-void RS_Image::draw(PaintInterface * painter, RS_GraphicView * view, double /*patternOffset*/)
+void RS_Image::draw(PaintInterface * painter, GraphicView * view, double /*patternOffset*/)
 {
        if (painter == NULL || view == NULL || img.isNull())
                return;
@@ -264,42 +375,34 @@ void RS_Image::draw(PaintInterface * painter, RS_GraphicView * view, double /*pa
        int height = view->getHeight();
 
        Vector scale = Vector(view->toGuiDX(data.uVector.magnitude()),
-               view->toGuiDY(data.vVector.magnitude()));
+                       view->toGuiDY(data.vVector.magnitude()));
        double angle = data.uVector.angle();
 
        int startX, stopX, startY, stopY;
 
        if (data.uVector.x > 1.0e-6 && data.vVector.y > 1.0e-6)
        {
-               startX = (int)((view->toGraphX(ox)-data.insertionPoint.x) / data.uVector.x) - 1;
+               startX = (int)((view->toGraphX(ox) - data.insertionPoint.x) / data.uVector.x) - 1;
 
                if (startX < 0)
-               {
                        startX = 0;
-               }
 
-               stopX = (int)((view->toGraphX(width)-data.insertionPoint.x) / data.uVector.x) + 1;
+               stopX = (int)((view->toGraphX(width) - data.insertionPoint.x) / data.uVector.x) + 1;
 
                if (stopX > (int)data.size.x)
-               {
                        stopX = (int)data.size.x;
-               }
 
-               startY = -(int)((view->toGraphY(oy) - (data.insertionPoint.y+getImageHeight())) /
-                       data.vVector.y) - 1;
+               startY = -(int)((view->toGraphY(oy) - (data.insertionPoint.y + getImageHeight()))
+                               / data.vVector.y) - 1;
 
-               if (startY<0)
-               {
+               if (startY < 0)
                        startY = 0;
-               }
 
-               stopY = -(int)((view->toGraphY(height) - (data.insertionPoint.y+getImageHeight())) /
-                       data.vVector.y) + 1;
+               stopY = -(int)((view->toGraphY(height) - (data.insertionPoint.y + getImageHeight()))
+                              / data.vVector.y) + 1;
 
-               if (stopY>(int)data.size.y)
-               {
+               if (stopY > (int)data.size.y)
                        stopY = (int)data.size.y;
-               }
        }
        else
        {
@@ -310,7 +413,7 @@ void RS_Image::draw(PaintInterface * painter, RS_GraphicView * view, double /*pa
        }
 
        painter->drawImg(img, view->toGui(data.insertionPoint), angle, scale,
-               startX, startY, stopX-startX, stopY-startY);
+               startX, startY, stopX - startX, stopY - startY);
 
        if (isSelected())
        {
@@ -331,4 +434,3 @@ std::ostream & operator<<(std::ostream & os, const RS_Image & i)
        os << " Image: " << i.getData() << "\n";
        return os;
 }
-