diff --git a/sources/createdxf.cpp b/sources/createdxf.cpp index a6c2fd840..f904448a5 100644 --- a/sources/createdxf.cpp +++ b/sources/createdxf.cpp @@ -20,6 +20,7 @@ #include #include #include +#include "exportdialog.h" const double Createdxf::sheetWidth = 4000; @@ -286,7 +287,7 @@ void Createdxf::drawCircle (QString fileName, double radius, double x, double y, /* draw line in DXF Format*/ -void Createdxf::drawLine (QString fileName, double x1, double y1, double x2, double y2, int colour) +void Createdxf::drawLine (const QString &fileName, double x1, double y1, double x2, double y2,const int &colour) { if (!fileName.isEmpty()) { QFile file(fileName); @@ -322,8 +323,161 @@ void Createdxf::drawLine (QString fileName, double x1, double y1, double x2, dou } } +/** + * @brief Createdxf::drawLine + * Conveniance function to draw line + * @param filepath + * @param line + * @param colorcode + */ +void Createdxf::drawLine(const QString &filepath, const QLineF &line, const int &colorcode) { + drawLine(filepath, line.p1().x() * xScale, + sheetHeight - (line.p1().y() * yScale), + line.p2().x() * xScale, + sheetHeight - (line.p2().y() * yScale), + colorcode); +} + +void Createdxf::drawArcEllipse(const QString &file_path, qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal spanAngle, qreal hotspot_x, qreal hotspot_y, qreal rotation_angle, const int &colorcode) { + // vector of parts of arc (stored as a pair of startAngle and spanAngle) for each quadrant. + QVector< QPair > arc_parts_vector; + + if (spanAngle > 0) { + qreal start = startAngle; + qreal span; + int i; + for ( i = startAngle; i < startAngle+spanAngle; i++ ) { + int absolute_theta = (i > 0) ? i : -i; + if (absolute_theta == 0 || absolute_theta == 90 || + absolute_theta == 180 || absolute_theta == 270 || + absolute_theta == 360) { + span = i - start; + QPair newPart(start,span); + arc_parts_vector.push_back(newPart); + start = i; + } + } + if (start != i) { + span = i - start; + QPair newPart(start,span); + arc_parts_vector.push_back(newPart); + } + } else { + qreal start = startAngle; + qreal span; + int i; + for ( i = startAngle; i > startAngle+spanAngle; i-- ) { + int absolute_theta = (i > 0) ? i : -i; + if (absolute_theta == 0 || absolute_theta == 90 || + absolute_theta == 180 || absolute_theta == 270 || + absolute_theta == 360) { + span = i - start; + QPair newPart(start,span); + arc_parts_vector.push_back(newPart); + start = i; + } + } + if (start != i) { + span = i - start; + QPair newPart(start,span); + arc_parts_vector.push_back(newPart); + } + } + + for (int i = 0; i < arc_parts_vector.size(); i++) { + + QPair arc = arc_parts_vector[i]; + if (arc.second == 0) + continue; + qreal arc_startAngle = arc.first * 3.142/180; + qreal arc_spanAngle = arc.second * 3.142/180; + + qreal a = w/2; + qreal b = h/2; + + qreal x1 = x + w/2 + a*cos(arc_startAngle); + qreal y1 = y - h/2 + b*sin(arc_startAngle); + qreal x2 = x + w/2 + a*cos(arc_startAngle + arc_spanAngle); + qreal y2 = y - h/2 + b*sin(arc_startAngle + arc_spanAngle); + + + qreal mid_ellipse_x = x + w/2 + a*cos(arc_startAngle + arc_spanAngle/2); + qreal mid_ellipse_y = y - h/2 + b*sin(arc_startAngle + arc_spanAngle/2); + qreal mid_line_x = (x1+x2)/2; + qreal mid_line_y = (y1+y2)/2; + + qreal x3 = (mid_ellipse_x + mid_line_x)/2; + qreal y3 = (mid_ellipse_y + mid_line_y)/2; + + // find circumcenter of points (x1,y1), (x3,y3) and (x2,y2) + qreal a1 = 2*x2 - 2*x1; + qreal b1 = 2*y2 - 2*y1; + qreal c1 = x1*x1 + y1*y1 - x2*x2 - y2*y2; + + qreal a2 = 2*x3 - 2*x1; + qreal b2 = 2*y3 - 2*y1; + qreal c2 = x1*x1 + y1*y1 - x3*x3 - y3*y3; + + qreal center_x = (b1*c2 - b2*c1) / (a1*b2 - a2*b1); + qreal center_y = (a1*c2 - a2*c1) / (b1*a2 - b2*a1); + + qreal radius = sqrt( (x1-center_x)*(x1-center_x) + (y1-center_y)*(y1-center_y) ); + + if ( x1 > center_x && y1 > center_y ) + arc_startAngle = asin( (y1 - center_y) / radius ); + else if ( x1 > center_x && y1 < center_y ) + arc_startAngle = 3.142*2 - asin( (center_y - y1) / radius ); + else if ( x1 < center_x && y1 < center_y ) + arc_startAngle = 3.142 + asin( (center_y - y1) / radius ); + else + arc_startAngle = 3.142 - asin( (y1 - center_y) / radius ); + + qreal arc_endAngle; + + if ( x2 > center_x && y2 > center_y ) + arc_endAngle = asin( (y2 - center_y) / radius ); + else if ( x2 > center_x && y2 < center_y ) + arc_endAngle = 3.142*2 - asin( (center_y - y2) / radius ); + else if ( x2 < center_x && y2 < center_y ) + arc_endAngle = 3.142 + asin( (center_y - y2) / radius ); + else + arc_endAngle = 3.142 - asin( (y2 - center_y) / radius ); + + if (arc_endAngle < arc_startAngle) { + qreal temp = arc_startAngle; + arc_startAngle = arc_endAngle; + arc_endAngle = temp; + } + + QPointF transformed_point = ExportDialog::rotation_transformed(center_x, center_y, hotspot_x, hotspot_y, rotation_angle); + center_x = transformed_point.x(); + center_y = transformed_point.y(); + arc_endAngle *= 180/3.142; + arc_startAngle *= 180/3.142; + arc_endAngle -= rotation_angle; + arc_startAngle -= rotation_angle; + + drawArc(file_path, center_x, center_y, radius, arc_startAngle, arc_endAngle, colorcode); + } +} + +/** + * @brief Createdxf::drawEllipse + * Conveniance function for draw ellipse + * @param filepath + * @param rect + * @param colorcode + */ +void Createdxf::drawEllipse(const QString &filepath, const QRectF &rect, const int &colorcode) { + drawArcEllipse(filepath, rect.topLeft().x() * xScale, + sheetHeight - (rect.topLeft().y() * yScale), + rect.width() * xScale, + rect.height() * yScale, + 0, 360, 0, 0, 0, colorcode); +} + /* draw rectangle in dxf format */ -void Createdxf::drawRectangle (QString fileName, double x1, double y1, double width, double height, int colour) +void Createdxf::drawRectangle (const QString &fileName, double x1, double y1, double width, double height, const int &colour) { if (!fileName.isEmpty()) { QFile file(fileName); @@ -413,6 +567,21 @@ void Createdxf::drawRectangle (QString fileName, double x1, double y1, double wi } } +/** + * @brief Createdxf::drawRectangle + * Conveniance function for draw rectangle + * @param filepath + * @param rect + * @param color + */ +void Createdxf::drawRectangle(const QString &filepath, const QRectF &rect, const int &colorcode) { + drawRectangle(filepath, rect.bottomLeft().x() * xScale, + sheetHeight - (rect.bottomLeft().y() * yScale), + rect.width() * xScale, + rect.height() * yScale, + colorcode); +} + /* draw arc in dx format */ void Createdxf::drawArc(QString fileName,double x,double y,double rad,double startAngle,double endAngle,int color) { diff --git a/sources/createdxf.h b/sources/createdxf.h index b9fd2a2b3..b6a49627a 100644 --- a/sources/createdxf.h +++ b/sources/createdxf.h @@ -34,11 +34,19 @@ class Createdxf static void drawCircle(QString,double,double,double,int); static void drawArc(QString,double x,double y,double rad,double startAngle,double endAngle,int color); static void drawDonut(QString,double,double,double,int); - static void drawRectangle(QString,double,double,double,double,int); - static void drawLine(QString,double,double,double,double,int); + + static void drawArcEllipse (const QString &file_path, qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal spanAngle, qreal hotspot_x, qreal hotspot_y, qreal rotation_angle, const int &colorcode); + + static void drawEllipse (const QString &filepath, const QRectF &rect, const int &colorcode); + + static void drawRectangle(const QString &filepath,double,double,double,double,const int &colorcode); + static void drawRectangle(const QString &filepath, const QRectF &rect, const int &colorcode); + + static void drawLine(const QString &filapath,double,double,double,double, const int &clorcode); + static void drawLine(const QString &filepath, const QLineF &line,const int &colorcode); + static void drawText(QString,QString,double,double,double,double,int); - static void drawTextAligned(QString fileName, QString text,double x, double y, double height, double rotation, double oblique,int hAlign, int vAlign, double xAlign, int colour, - bool leftAlign = false, float scale = 0); + static void drawTextAligned(QString fileName, QString text,double x, double y, double height, double rotation, double oblique,int hAlign, int vAlign, double xAlign, int colour, bool leftAlign = false, float scale = 0); static const double sheetWidth; static const double sheetHeight; diff --git a/sources/diagram.cpp b/sources/diagram.cpp index 7446c324e..053ecace3 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -418,9 +418,7 @@ QDomDocument Diagram::toXml(bool whole_content) { if (!list_shapes.isEmpty()) { QDomElement shapes = document.createElement("shapes"); foreach (QetShapeItem *dii, list_shapes) { - dii ->setWritingXml(true); shapes.appendChild(dii -> toXml(document)); - dii ->setWritingXml(false); } racine.appendChild(shapes); } @@ -1106,6 +1104,20 @@ DiagramPosition Diagram::convertPosition(const QPointF &pos) { return(diagram_position); } +/** + * @brief Diagram::snapToGrid + * Return a nearest snap point of p + * @param p point to find the nearest snaped point + * @return + */ +QPointF Diagram::snapToGrid(const QPointF &p) { + // arrondit l'abscisse a 10 px pres + int p_x = qRound(p.x() / (Diagram::xGrid * 1.0)) * Diagram::xGrid; + // arrondit l'ordonnee a 10 px pres + int p_y = qRound(p.y() / (Diagram::yGrid * 1.0)) * Diagram::yGrid; + return (QPointF(p_x, p_y)); +} + /** Definit s'il faut afficher ou non les bornes @param dt true pour afficher les bornes, false sinon diff --git a/sources/diagram.h b/sources/diagram.h index 8cc08a322..2bdf34cb7 100644 --- a/sources/diagram.h +++ b/sources/diagram.h @@ -156,6 +156,7 @@ class Diagram : public QGraphicsScene { void setBorderOptions(BorderOptions); BorderOptions borderOptions(); DiagramPosition convertPosition(const QPointF &); + static QPointF snapToGrid(const QPointF &p); bool drawTerminals() const; void setDrawTerminals(bool); diff --git a/sources/diagramcommands.cpp b/sources/diagramcommands.cpp index 8037dd31b..439d9c735 100644 --- a/sources/diagramcommands.cpp +++ b/sources/diagramcommands.cpp @@ -497,10 +497,10 @@ void MoveElementsCommand::move(const QPointF &actual_movement) { // deplace les shapes foreach (QetShapeItem *dsi, content_to_move.shapes) { dsi -> setPos(dsi -> pos() + actual_movement); - QRectF rec = dsi -> boundingRect(); + /*QRectF rec = dsi -> boundingRect(); rec.translate(actual_movement); dsi -> setBoundingRect(rec); - dsi -> setPos(dsi -> pos() - actual_movement); + dsi -> setPos(dsi -> pos() - actual_movement);*/ } } @@ -1161,41 +1161,46 @@ void ChangeSeveralConductorsPropertiesCommand::redo() { } /** - * @brief ImageResizerCommand::ImageResizerCommand Constructor - * @param image - * @param old_ old size of image - * @param new_ new size of image - * @param parent undocommand parent + * @brief ItemResizerCommand::ItemResizerCommand + * Change the size of @qgi + * @param qgi item to resize + * @param old_ old size + * @param new_ new size + * @param text text to display + * @param parent undo parent */ -ImageResizerCommand::ImageResizerCommand (DiagramImageItem *image, qreal &old_, qreal &new_, QUndoCommand *parent): +ItemResizerCommand::ItemResizerCommand (QetGraphicsItem *qgi, qreal &old_, qreal &new_, const QString &text, QUndoCommand *parent): QUndoCommand(parent), - image_(image), - old_size (old_), - new_size (new_), - diagram(image->diagram()) + m_qgi ( qgi ), + old_size ( old_ ), + new_size ( new_ ), + diagram ( qgi->diagram() ), + m_text ( text ) {} /** - * @brief ImageResizerCommand::~ImageResizerCommand destructor + * @brief ItemResizerCommand::~ItemResizerCommand */ -ImageResizerCommand::~ImageResizerCommand() {} +ItemResizerCommand::~ItemResizerCommand() {} /** - * @brief ImageResizerCommand::undo set the old size + * @brief ItemResizerCommand::undo */ -void ImageResizerCommand::undo() { +void ItemResizerCommand::undo() { diagram -> showMe(); - image_ -> setScale(old_size); + m_qgi -> setScale(old_size); + QUndoCommand::undo(); } /** - * @brief ImageResizerCommand::redo set the new size + * @brief ItemResizerCommand::redo */ -void ImageResizerCommand::redo() { +void ItemResizerCommand::redo() { diagram -> showMe(); - if (old_size setScale(new_size); + if (old_size setScale(new_size); + QUndoCommand::redo(); } @@ -1212,7 +1217,9 @@ ChangeShapeStyleCommand::ChangeShapeStyleCommand(QetShapeItem *shape, Qt::PenSty old_style (old_), new_style (new_), diagram(shape->diagram()) -{} +{ + setText(QObject::tr("Changer le style d'une shape")); +} /** * @brief ChangeShapeStyleCommand::~ChangeShapeStyleCommand destructor @@ -1237,49 +1244,6 @@ void ChangeShapeStyleCommand::redo() { QUndoCommand::redo(); } - - -/** - * @brief ChangeShapeScaleCommand::ChangeShapeScaleCommand Constructor - * @param shape - * @param scale_factor - * @param parent undocommand parent - */ -ChangeShapeScaleCommand::ChangeShapeScaleCommand(QetShapeItem *shape, double scale_factor, QUndoCommand *parent): - QUndoCommand(parent), - shape_(shape), - factor (scale_factor), - diagram(shape->diagram()) -{} - -/** - * @brief ChangeShapeScaleCommand::~ChangeShapeScaleCommand destructor - */ -ChangeShapeScaleCommand::~ChangeShapeScaleCommand() {} - -/** - * @brief ChangeShapeScaleCommand::undo set the old size - */ -void ChangeShapeScaleCommand::undo() { - diagram -> removeItem(shape_); - shape_ -> scale(1/factor); - diagram -> addItem(shape_); - diagram -> showMe(); - QUndoCommand::undo(); -} - -/** - * @brief ChangeShapeScaleCommand::redo set the new size - */ -void ChangeShapeScaleCommand::redo() { - diagram -> removeItem(shape_); - shape_ -> scale(factor); - diagram -> addItem(shape_); - diagram -> showMe(); - QUndoCommand::redo(); -} - - /** * @brief LinkElementsCommand::LinkElementsCommand *Constructor diff --git a/sources/diagramcommands.h b/sources/diagramcommands.h index b6a42e5f0..e4fb6147d 100644 --- a/sources/diagramcommands.h +++ b/sources/diagramcommands.h @@ -598,11 +598,11 @@ class ChangeSeveralConductorsPropertiesCommand : public QUndoCommand { Diagram *diagram; }; -class ImageResizerCommand : public QUndoCommand { +class ItemResizerCommand : public QUndoCommand { //constructor and destructor public: - ImageResizerCommand (DiagramImageItem *image, qreal &old_, qreal &new_, QUndoCommand *parent = 0); - virtual ~ImageResizerCommand(); + ItemResizerCommand (QetGraphicsItem *qgi, qreal &old_, qreal &new_,const QString &text, QUndoCommand *parent = 0); + virtual ~ItemResizerCommand(); //methods public: @@ -611,9 +611,10 @@ class ImageResizerCommand : public QUndoCommand { //attributes private: - DiagramImageItem *image_; + QetGraphicsItem *m_qgi; qreal old_size, new_size; Diagram *diagram; + QString m_text; }; @@ -635,24 +636,6 @@ class ChangeShapeStyleCommand : public QUndoCommand { Diagram *diagram; }; -class ChangeShapeScaleCommand : public QUndoCommand { - //constructor and destructor - public: - ChangeShapeScaleCommand (QetShapeItem *shape, double scale_factor, QUndoCommand *parent = 0); - virtual ~ChangeShapeScaleCommand(); - - //methods - public: - virtual void undo(); - virtual void redo(); - - //attributes - private: - QetShapeItem *shape_; - double factor; - Diagram *diagram; -}; - class LinkElementsCommand : public QUndoCommand { public: // constructor destructor diff --git a/sources/diagramview.cpp b/sources/diagramview.cpp index db2eb732a..14c5820ea 100644 --- a/sources/diagramview.cpp +++ b/sources/diagramview.cpp @@ -50,7 +50,7 @@ @param diagram Schema a afficher ; si diagram vaut 0, un nouveau Diagram est utilise @param parent Le QWidget parent de cette vue de schema */ -DiagramView::DiagramView(Diagram *diagram, QWidget *parent) : QGraphicsView(parent), newItem(0){ +DiagramView::DiagramView(Diagram *diagram, QWidget *parent) : QGraphicsView(parent), newShapeItem(nullptr){ setAttribute(Qt::WA_DeleteOnClose, true); setInteractive(true); current_behavior = noAction; @@ -462,16 +462,16 @@ void DiagramView::mousePressEvent(QMouseEvent *e) { current_behavior = noAction; break; case addingLine: - newItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Line, false); - scene -> addItem(newItem); + newShapeItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Line); + scene -> addItem(newShapeItem); break; case addingRectangle: - newItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Rectangle); - scene -> addItem(newItem); + newShapeItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Rectangle); + scene -> addItem(newShapeItem); break; case addingEllipse: - newItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Ellipse); - scene -> addItem(newItem); + newShapeItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Ellipse); + scene -> addItem(newShapeItem); break; case dragView: current_behavior = noAction; @@ -504,12 +504,7 @@ void DiagramView::mouseMoveEvent(QMouseEvent *e) { return; } else if (e -> buttons() == Qt::LeftButton && current_behavior & addingShape) { - QRectF rec = QRectF(rubber_band_origin, mapToScene(e->pos())).normalized(); - scene ->removeItem(newItem); - newItem -> setBoundingRect(rec); - if (current_behavior == addingLine) - newItem -> setLineAngle(rubber_band_origin != rec.topLeft() && rubber_band_origin != rec.bottomRight()); - scene ->addItem(newItem); + newShapeItem->setP2(mapToScene(e->pos())); } else QGraphicsView::mouseMoveEvent(e); } @@ -524,10 +519,8 @@ void DiagramView::mouseReleaseEvent(QMouseEvent *e) { return; } else if (current_behavior & addingShape) { - newItem -> setFullyBuilt(true); // place it to the good position with an undo command - scene -> undoStack().push(new AddShapeCommand(scene, newItem, rubber_band_origin)); - adjustSceneRect(); + scene -> undoStack().push(new AddShapeCommand(scene, newShapeItem, rubber_band_origin)); emit(itemAdded()); current_behavior = noAction; } diff --git a/sources/diagramview.h b/sources/diagramview.h index 791bfefdc..ef339299a 100644 --- a/sources/diagramview.h +++ b/sources/diagramview.h @@ -65,7 +65,7 @@ class DiagramView : public QGraphicsView { QPoint next_position_; QPointF center_view_; QImage image_to_add_; - QetShapeItem *newItem; + QetShapeItem *newShapeItem; QPointF rubber_band_origin; // methods diff --git a/sources/exportdialog.cpp b/sources/exportdialog.cpp index 5d2cc9c1f..7d0d94ece 100644 --- a/sources/exportdialog.cpp +++ b/sources/exportdialog.cpp @@ -421,7 +421,8 @@ void ExportDialog::generateDxf(Diagram *diagram, int width, int height, bool kee QList list_images; QList list_lines; QList list_rectangles; - QList list_ellipses; + //QList list_ellipses; + QList list_shapes; DiagramFolioList *ptr; if (ptr = dynamic_cast(diagram)) { @@ -482,45 +483,12 @@ void ExportDialog::generateDxf(Diagram *diagram, int width, int height, bool kee } else if (DiagramImageItem *dii = qgraphicsitem_cast(qgi)) { list_images << dii; } else if (QetShapeItem *dii = qgraphicsitem_cast(qgi)) { - if (dii -> getType() == QetShapeItem::Line && dii -> getLine()) { - list_lines << dii -> getLine(); - } else if (dii -> getType() == QetShapeItem::Rectangle && dii -> getRectangle()) { - list_rectangles << dii -> getRectangle(); - } else if (dii -> getEllipse()){ - list_ellipses << dii -> getEllipse(); - } + list_shapes << dii; } } } - //draw lines - foreach(QLineF *line, list_lines) { - qreal x1 = (line -> p1().x()) * Createdxf::xScale; - qreal y1 = Createdxf::sheetHeight - (line -> p1().y()) * Createdxf::yScale; - qreal x2 = (line -> p2().x()) * Createdxf::xScale; - qreal y2 = Createdxf::sheetHeight - (line -> p2().y()) * Createdxf::yScale; - Createdxf::drawLine(file_path, x1, y1, x2, y2, 0); - } - - //draw rectangles - foreach(QRectF *rect, list_rectangles) { - qreal x1 = (rect -> bottomLeft().x()) * Createdxf::xScale; - qreal y1 = Createdxf::sheetHeight - (rect -> bottomLeft().y()) * Createdxf::yScale; - qreal w = rect -> width() * Createdxf::xScale; - qreal h = rect -> height() * Createdxf::yScale; - Createdxf::drawRectangle(file_path, x1, y1, w, h, 0); - } - - //draw independent ellipses - foreach(QRectF *rect, list_ellipses) { - qreal x1 = (rect -> topLeft().x()) * Createdxf::xScale; - qreal y1 = Createdxf::sheetHeight - (rect -> topLeft().y()) * Createdxf::yScale; - qreal w = rect -> width() * Createdxf::xScale; - qreal h = rect -> height() * Createdxf::yScale; - qreal startAngle = 0; - qreal spanAngle = 360; - drawDxfArcEllipse(file_path, x1, y1, w, h, startAngle, spanAngle, 0, 0, 0); - } + foreach (QetShapeItem *qsi, list_shapes) qsi->toDXF(file_path); //Draw elements foreach(Element *elmt, list_elements) { @@ -647,7 +615,7 @@ void ExportDialog::generateDxf(Diagram *diagram, int width, int height, bool kee qreal h = arc -> at(3) * Createdxf::yScale; qreal startAngle = arc -> at(4); qreal spanAngle = arc -> at(5); - drawDxfArcEllipse(file_path, x, y, w, h, startAngle, spanAngle, hotspot_x, hotspot_y, rotation_angle); + Createdxf::drawArcEllipse(file_path, x, y, w, h, startAngle, spanAngle, hotspot_x, hotspot_y, rotation_angle, 0); } } @@ -719,132 +687,6 @@ void ExportDialog::generateDxf(Diagram *diagram, int width, int height, bool kee Createdxf::dxfEnd(file_path); } -// approximate the ellipse to parts of circles. -void ExportDialog::drawDxfArcEllipse(QString file_path, qreal x, qreal y, qreal w, qreal h, qreal startAngle, - qreal spanAngle, qreal hotspot_x, qreal hotspot_y, qreal rotation_angle) { - - // vector of parts of arc (stored as a pair of startAngle and spanAngle) for each quadrant. - QVector< QPair > arc_parts_vector; - - if (spanAngle > 0) { - qreal start = startAngle; - qreal span; - int i; - for ( i = startAngle; i < startAngle+spanAngle; i++ ) { - int absolute_theta = (i > 0) ? i : -i; - if (absolute_theta == 0 || absolute_theta == 90 || - absolute_theta == 180 || absolute_theta == 270 || - absolute_theta == 360) { - span = i - start; - QPair newPart(start,span); - arc_parts_vector.push_back(newPart); - start = i; - } - } - if (start != i) { - span = i - start; - QPair newPart(start,span); - arc_parts_vector.push_back(newPart); - } - } else { - qreal start = startAngle; - qreal span; - int i; - for ( i = startAngle; i > startAngle+spanAngle; i-- ) { - int absolute_theta = (i > 0) ? i : -i; - if (absolute_theta == 0 || absolute_theta == 90 || - absolute_theta == 180 || absolute_theta == 270 || - absolute_theta == 360) { - span = i - start; - QPair newPart(start,span); - arc_parts_vector.push_back(newPart); - start = i; - } - } - if (start != i) { - span = i - start; - QPair newPart(start,span); - arc_parts_vector.push_back(newPart); - } - } - - for (int i = 0; i < arc_parts_vector.size(); i++) { - - QPair arc = arc_parts_vector[i]; - if (arc.second == 0) - continue; - qreal arc_startAngle = arc.first * 3.142/180; - qreal arc_spanAngle = arc.second * 3.142/180; - - qreal a = w/2; - qreal b = h/2; - - qreal x1 = x + w/2 + a*cos(arc_startAngle); - qreal y1 = y - h/2 + b*sin(arc_startAngle); - qreal x2 = x + w/2 + a*cos(arc_startAngle + arc_spanAngle); - qreal y2 = y - h/2 + b*sin(arc_startAngle + arc_spanAngle); - - - qreal mid_ellipse_x = x + w/2 + a*cos(arc_startAngle + arc_spanAngle/2); - qreal mid_ellipse_y = y - h/2 + b*sin(arc_startAngle + arc_spanAngle/2); - qreal mid_line_x = (x1+x2)/2; - qreal mid_line_y = (y1+y2)/2; - - qreal x3 = (mid_ellipse_x + mid_line_x)/2; - qreal y3 = (mid_ellipse_y + mid_line_y)/2; - - // find circumcenter of points (x1,y1), (x3,y3) and (x2,y2) - qreal a1 = 2*x2 - 2*x1; - qreal b1 = 2*y2 - 2*y1; - qreal c1 = x1*x1 + y1*y1 - x2*x2 - y2*y2; - - qreal a2 = 2*x3 - 2*x1; - qreal b2 = 2*y3 - 2*y1; - qreal c2 = x1*x1 + y1*y1 - x3*x3 - y3*y3; - - qreal center_x = (b1*c2 - b2*c1) / (a1*b2 - a2*b1); - qreal center_y = (a1*c2 - a2*c1) / (b1*a2 - b2*a1); - - qreal radius = sqrt( (x1-center_x)*(x1-center_x) + (y1-center_y)*(y1-center_y) ); - - if ( x1 > center_x && y1 > center_y ) - arc_startAngle = asin( (y1 - center_y) / radius ); - else if ( x1 > center_x && y1 < center_y ) - arc_startAngle = 3.142*2 - asin( (center_y - y1) / radius ); - else if ( x1 < center_x && y1 < center_y ) - arc_startAngle = 3.142 + asin( (center_y - y1) / radius ); - else - arc_startAngle = 3.142 - asin( (y1 - center_y) / radius ); - - qreal arc_endAngle; - - if ( x2 > center_x && y2 > center_y ) - arc_endAngle = asin( (y2 - center_y) / radius ); - else if ( x2 > center_x && y2 < center_y ) - arc_endAngle = 3.142*2 - asin( (center_y - y2) / radius ); - else if ( x2 < center_x && y2 < center_y ) - arc_endAngle = 3.142 + asin( (center_y - y2) / radius ); - else - arc_endAngle = 3.142 - asin( (y2 - center_y) / radius ); - - if (arc_endAngle < arc_startAngle) { - qreal temp = arc_startAngle; - arc_startAngle = arc_endAngle; - arc_endAngle = temp; - } - - QPointF transformed_point = rotation_transformed(center_x, center_y, hotspot_x, hotspot_y, rotation_angle); - center_x = transformed_point.x(); - center_y = transformed_point.y(); - arc_endAngle *= 180/3.142; - arc_startAngle *= 180/3.142; - arc_endAngle -= rotation_angle; - arc_startAngle -= rotation_angle; - - Createdxf::drawArc(file_path, center_x, center_y, radius, arc_startAngle, arc_endAngle, 0); - } -} - void ExportDialog::fillRow(QString file_path, const QRectF &row_rect, QString author, QString title, QString folio, QString date) { diff --git a/sources/exportdialog.h b/sources/exportdialog.h index db49b53e5..fcea79163 100644 --- a/sources/exportdialog.h +++ b/sources/exportdialog.h @@ -41,6 +41,7 @@ class ExportDialog : public QDialog { // methods public: int diagramsToExportCount() const; + static QPointF rotation_transformed(qreal, qreal, qreal, qreal, qreal); private: class ExportDiagramLine { @@ -94,8 +95,6 @@ class ExportDialog : public QDialog { void exportDiagram(ExportDiagramLine *); qreal diagramRatio(Diagram *); QSize diagramSize(Diagram *); - QPointF rotation_transformed(qreal, qreal, qreal, qreal, qreal); - void drawDxfArcEllipse(QString, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal); public slots: void slot_correctWidth(int); diff --git a/sources/qetgraphicsitem/diagramimageitem.cpp b/sources/qetgraphicsitem/diagramimageitem.cpp index 80345ef58..2aa080cae 100644 --- a/sources/qetgraphicsitem/diagramimageitem.cpp +++ b/sources/qetgraphicsitem/diagramimageitem.cpp @@ -147,7 +147,7 @@ void DiagramImageItem::editProperty() { cb.isChecked() ? is_movable_=false : is_movable_=true; qreal new_scale = slider.value(); new_scale /= factor_range; - if (scale_ != new_scale) diagram()->undoStack().push(new ImageResizerCommand(this, scale_, new_scale)); + if (scale_ != new_scale) diagram()->undoStack().push(new ItemResizerCommand(this, scale_, new_scale, tr("une image"))); } //...or not else setScale(scale_); diff --git a/sources/qetgraphicsitem/qetgraphicsitem.cpp b/sources/qetgraphicsitem/qetgraphicsitem.cpp index 5dde9b5ee..bb2a97fe3 100644 --- a/sources/qetgraphicsitem/qetgraphicsitem.cpp +++ b/sources/qetgraphicsitem/qetgraphicsitem.cpp @@ -50,11 +50,7 @@ Diagram* QetGraphicsItem::diagram() const{ void QetGraphicsItem::setPos(const QPointF &p) { if (p == pos() || !is_movable_) return; if (scene() && snap_to_grid_) { - // arrondit l'abscisse a 10 px pres - int p_x = qRound(p.x() / (Diagram::xGrid * 1.0)) * Diagram::xGrid; - // arrondit l'ordonnee a 10 px pres - int p_y = qRound(p.y() / (Diagram::yGrid * 1.0)) * Diagram::yGrid; - QGraphicsItem::setPos(p_x, p_y); + QGraphicsItem::setPos(Diagram::snapToGrid(p)); emit positionChange(pos()); } else QGraphicsItem::setPos(p); } diff --git a/sources/qetgraphicsitem/qetshapeitem.cpp b/sources/qetgraphicsitem/qetshapeitem.cpp index 2d7063d54..a5c241a4f 100644 --- a/sources/qetgraphicsitem/qetshapeitem.cpp +++ b/sources/qetgraphicsitem/qetshapeitem.cpp @@ -1,217 +1,246 @@ +/* + Copyright 2006-2014 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech 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 2 of the License, or + (at your option) any later version. + + QElectroTech 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 QElectroTech. If not, see . +*/ #include "qetshapeitem.h" #include "diagramcommands.h" +#include "createdxf.h" +#include "diagram.h" -QetShapeItem::QetShapeItem(QPointF p1, QPointF p2, ShapeType type, bool lineAngle,QGraphicsItem *parent) : +/** + * @brief QetShapeItem::QetShapeItem + * Constructor of shape item. point 1 and 2 must be in scene coordinate + * @param p1 first point + * @param p2 second point + * @param type type of item (line, rectangle, ellipse) + * @param parent parent item + */ +QetShapeItem::QetShapeItem(QPointF p1, QPointF p2, ShapeType type, QGraphicsItem *parent) : QetGraphicsItem(parent), - _shapeStyle(Qt::DashLine), - _lineAngle(lineAngle), - _isFullyBuilt(false), - _writingXml(false) + m_shapeType(type), + m_shapeStyle(Qt::DashLine), + m_P1 (Diagram::snapToGrid(p1)), + m_P2 (Diagram::snapToGrid(p2)) + { - _shapeType = type; - _boundingRect = QRectF(p1, p2); + setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); } QetShapeItem::~QetShapeItem() { } +/** + * @brief QetShapeItem::setStyle + * Set the new style of pen for thi item + * @param newStyle + */ void QetShapeItem::setStyle(Qt::PenStyle newStyle) { - _shapeStyle = newStyle; + m_shapeStyle = newStyle; update(); } -void QetShapeItem::scale(double factor) -{ - QRectF bounding_rect = boundingRect(); - bounding_rect.setWidth(bounding_rect.width() * factor); - bounding_rect.setHeight(bounding_rect.height() * factor); - setBoundingRect(bounding_rect); - update(); -} - -void QetShapeItem::setFullyBuilt(bool isBuilt) -{ - _isFullyBuilt = isBuilt; - setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); -} - -QLineF *QetShapeItem::getLine() -{ - QRectF rect = boundingRect(); - QLineF *line = 0; - if (_shapeType == Line) { - if (_lineAngle) - line = new QLineF(rect.topRight(), rect.bottomLeft()); - else - line = new QLineF(rect.topLeft(), rect.bottomRight()); +/** + * @brief QetShapeItem::scale + * Preview the scale of this item to factor + * @param factor + */ +void QetShapeItem::previewScale(int factor) { + setTransformOriginPoint(boundingRect().center()); + if (factor >= 1 && factor <= 200) { + qreal new_scale = factor; + new_scale /= 100; + setScale(new_scale); } - return line; } -QRectF *QetShapeItem::getRectangle() -{ - QRectF rect = boundingRect(); - QRectF *rec = 0; - if (_shapeType == Rectangle) - rec = new QRectF(rect); - return rec; +/** + * @brief QetShapeItem::setP2 + * Set the second point of this item + * @param P2 + */ +void QetShapeItem::setP2(QPointF P2) { + P2 = Diagram::snapToGrid(P2); + if (P2 == m_P2) return; + prepareGeometryChange(); + m_P2 = P2; + setTransformOriginPoint(boundingRect().center()); } -QRectF *QetShapeItem::getEllipse() -{ - QRectF rect = boundingRect(); - QRectF *rec = 0; - if (_shapeType == Ellipse) - rec = new QRectF(rect); - return rec; +/** + * @brief QetShapeItem::boundingRect + * @return the bounding rect of this item + */ +QRectF QetShapeItem::boundingRect() const { + QRectF b(m_P1, m_P2); + return b.normalized(); } -QRectF QetShapeItem::boundingRect() const -{ - return _boundingRect; -} - -QPainterPath QetShapeItem::shape() const -{ +/** + * @brief QetShapeItem::shape + * @return the shape of this item + */ +QPainterPath QetShapeItem::shape() const { QPainterPath path; QPainterPathStroker pps; - QRectF rect = boundingRect(); - switch (_shapeType) { + switch (m_shapeType) { case Line: - if (_lineAngle) { - path.moveTo(rect.topRight()); - path.lineTo(rect.bottomLeft()); - } else { - path.moveTo(rect.topLeft()); - path.lineTo(rect.bottomRight()); - } - //use @pps for grab line with bigger outerline - //more usefull + path.moveTo(m_P1); + path.lineTo(m_P2); pps.setWidth(10); - path= pps.createStroke(path); + path = pps.createStroke(path); break; case Rectangle: - path = QetGraphicsItem::shape(); + path.addRect(boundingRect()); break; case Ellipse: - path.addEllipse(rect); + path.addEllipse(boundingRect()); break; default: - path = QetGraphicsItem::shape(); + Q_ASSERT(false); + break; } return path; } +/** + * @brief QetShapeItem::changeGraphicsItem + * Change the curent type of this item to newtype + * @param newtype + */ +void QetShapeItem::changeGraphicsItem(const ShapeType &newtype) { + if (newtype == m_shapeType) return; + prepareGeometryChange(); + m_shapeType = newtype; +} + +/** + * @brief QetShapeItem::paint + * Paint this item + * @param painter + * @param option + * @param widget + */ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - if (!_writingXml) { - painter -> setRenderHint(QPainter::Antialiasing, false); - QRectF rec = boundingRect(); - QPen pen(Qt::black); + Q_UNUSED(option); Q_UNUSED(widget); -#ifdef Q_WS_WIN - pen.setWidthF(1); -#endif + QPen pen; + pen.setStyle(m_shapeStyle); + if (isSelected()) pen.setColor(Qt::red); + painter->setPen(pen); - if (isSelected()) - pen.setColor(Qt::red); - pen.setStyle(_shapeStyle); - painter->setPen(pen); - switch(_shapeType) { - case Line: - if (_lineAngle) - painter -> drawLine(rec.topRight(), rec.bottomLeft()); - else - painter -> drawLine(rec.topLeft(), rec.bottomRight()); - break; - case Rectangle: - painter -> drawRect(rec); - break; - default: //(case Ellipse:) - painter ->drawEllipse(rec); - } + switch (m_shapeType) { + case Line: + painter->drawLine(QLineF(m_P1, m_P2)); + break; + case Rectangle: + painter->drawRect(boundingRect()); + break; + case Ellipse: + painter->drawEllipse(boundingRect()); + break; } } -void QetShapeItem::mousePressEvent(QGraphicsSceneMouseEvent *e) -{ - _origMousePress = mapToScene(e -> pos()); - first_move_ = true; - if (e -> modifiers() & Qt::ControlModifier) { - setSelected(!isSelected()); - } - QGraphicsItem::mousePressEvent(e); -} +/** + * @brief QetShapeItem::fromXml + * Build this item from the xml description + * @param e element where is stored this item + * @return true if load success + */ +bool QetShapeItem::fromXml(const QDomElement &e) { + if (e.tagName() != "shape") return (false); -void QetShapeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) -{ - if (diagram()) diagram() -> endMoveElements(); - QPointF newCoord = mapToScene(e -> pos()); - if (newCoord != _origMousePress) { - //translate bounding rectangle - QRectF rec = boundingRect(); - rec.translate(newCoord - _origMousePress); - setBoundingRect(rec); - setPos(pos() - newCoord + _origMousePress); - } - - if (!(e -> modifiers() & Qt::ControlModifier)) QGraphicsItem::mouseReleaseEvent(e); -} - - -bool QetShapeItem::fromXml(const QDomElement &e) -{ - if (!_writingXml) { - if (e.tagName() != "shape") return (false); - - _shapeType = QetShapeItem::ShapeType(e.attribute("type","0").toInt()); - _shapeStyle = Qt::PenStyle(e.attribute("style","0").toInt()); - _lineAngle = e.attribute("lineAngle","0").toInt(); - qreal x = e.attribute("x","0").toDouble(); - qreal y = e.attribute("y","0").toDouble(); - qreal w = e.attribute("w","0").toDouble(); - qreal h = e.attribute("h","0").toDouble(); - setBoundingRect(QRectF(x, y, w, h)); - setFullyBuilt(true); - } + m_shapeStyle = Qt::PenStyle(e.attribute("style","0").toInt()); + m_P1.setX(e.attribute("x1", 0).toDouble()); + m_P1.setY(e.attribute("y1", 0).toDouble()); + m_P2.setX(e.attribute("x2", 0).toDouble()); + m_P2.setY(e.attribute("y2", 0).toDouble()); + changeGraphicsItem(QetShapeItem::ShapeType(e.attribute("type","0").toInt())); return (true); } - +/** + * @brief QetShapeItem::toXml + * Save this item to xml element + * @param document parent document xml + * @return element xml where is write this item + */ QDomElement QetShapeItem::toXml(QDomDocument &document) const { QDomElement result = document.createElement("shape"); - QRectF rec = boundingRect(); //write some attribute - result.setAttribute("type", QString::number(_shapeType)); - result.setAttribute("x", QString::number(rec.topLeft().x())); - result.setAttribute("y", QString::number(rec.topLeft().y())); - result.setAttribute("w", QString::number(rec.width())); - result.setAttribute("h", QString::number(rec.height())); - result.setAttribute("lineAngle", QString::number(_lineAngle)); - result.setAttribute("style", QString::number(_shapeStyle)); + result.setAttribute("type", QString::number(m_shapeType)); + result.setAttribute("style", QString::number(m_shapeStyle)); + result.setAttribute("x1", mapToScene(m_P1).x()); + result.setAttribute("y1", mapToScene(m_P1).y()); + result.setAttribute("x2", mapToScene(m_P2).x()); + result.setAttribute("y2", mapToScene(m_P2).y()); return(result); } +/** + * @brief QetShapeItem::toDXF + * Draw this element to the dxf document + * @param filepath file path of the the dxf document + * @return true if draw success + */ +bool QetShapeItem::toDXF(const QString &filepath) { + switch (m_shapeType) { + case Line: + Createdxf::drawLine(filepath, QLineF(mapToScene(m_P1), mapToScene(m_P2)), 0); + return true; + break; + case Rectangle: + Createdxf::drawRectangle(filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), 0); + return true; + break; + case Ellipse: + Createdxf::drawEllipse(filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), 0); + return true; + break; + default: + return false; + break; + } +} + +/** + * @brief QetShapeItem::editProperty + * Edit the property of this item + */ void QetShapeItem::editProperty() { if (diagram() -> isReadOnly()) return; //the dialog QDialog property_dialog(diagram()->views().at(0)); - property_dialog.setWindowTitle(tr("\311diter les propri\351t\351s d'une liaison, Zone ", "window title")); + property_dialog.setWindowTitle(tr("\311diter les propri\351t\351s d'une shape, Zone ", "window title")); //the main layout QVBoxLayout dialog_layout(&property_dialog); //GroupBox for resizer image - QGroupBox restyle_groupe(QObject::tr("Type de ligne", "shape style")); + QGroupBox restyle_groupe(QObject::tr("Type de trait", "shape style")); dialog_layout.addWidget(&restyle_groupe); QHBoxLayout restyle_layout(&restyle_groupe); @@ -223,7 +252,7 @@ void QetShapeItem::editProperty() style_combo.addItem(QObject::tr("Traits points points")); // The items have been added in order accordance with Qt::PenStyle. - style_combo.setCurrentIndex(int(_shapeStyle) - 1); + style_combo.setCurrentIndex(int(m_shapeStyle) - 1); restyle_layout.addWidget(&style_combo); @@ -231,23 +260,33 @@ void QetShapeItem::editProperty() QCheckBox cb(tr("Verrouiller la position"), &property_dialog); cb.setChecked(!is_movable_); dialog_layout.addWidget(&cb); - cb.setVisible(false); //GroupBox for Scaling QGroupBox scale_groupe(QObject::tr("\311chelle", "shape scale")); dialog_layout.addWidget(&scale_groupe); QHBoxLayout scale_layout(&scale_groupe); - QLabel scale_label(&property_dialog); - scale_label.setText(tr("Facteur d'\351chelle")); + int min_range = 1; + int max_range = 200; + int factor_range = 100; - QLineEdit scale_lineedit(&property_dialog); - QDoubleValidator scale_val(0.0,1000,3, &property_dialog); - scale_lineedit.setValidator(&scale_val); - scale_lineedit.setText("1.0"); - - scale_layout.addWidget(&scale_label); - scale_layout.addWidget(&scale_lineedit); + //slider + QSlider slider(Qt::Horizontal, &property_dialog); + slider.setRange(min_range, max_range); + qreal scale_= scale(); + slider.setValue(scale_*factor_range); + //spinbox + QSpinBox spin_box(&property_dialog); + spin_box.setRange(min_range, max_range); + spin_box.setValue(scale_*factor_range); + spin_box.setSuffix(" %"); + //synchro slider with spinbox + connect(&slider, SIGNAL(valueChanged(int)), &spin_box, SLOT(setValue(int))); + connect(&slider, SIGNAL(valueChanged(int)), this, SLOT(previewScale(int))); + connect(&spin_box, SIGNAL(valueChanged(int)), &slider, SLOT(setValue(int))); + //add slider and spinbox to layout + scale_layout.addWidget(&slider); + scale_layout.addWidget(&spin_box); //dialog button, box QDialogButtonBox dbb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); @@ -260,11 +299,13 @@ void QetShapeItem::editProperty() cb.isChecked() ? is_movable_=false : is_movable_=true; Qt::PenStyle new_style = Qt::PenStyle(style_combo.currentIndex() + 1); - if (new_style != _shapeStyle) - diagram()->undoStack().push(new ChangeShapeStyleCommand(this, _shapeStyle, new_style)); - double scale_factor = scale_lineedit.text().toDouble(); - if (scale_factor != 1 && scale_factor > 0 && scale_factor < 1000 ) - diagram()->undoStack().push(new ChangeShapeScaleCommand(this, scale_factor)); + if (new_style != m_shapeStyle) diagram()->undoStack().push(new ChangeShapeStyleCommand(this, m_shapeStyle, new_style)); + + qreal scale_factor = slider.value(); + scale_factor /= factor_range; + if (scale_ != scale_factor) diagram()->undoStack().push(new ItemResizerCommand(this, scale_, scale_factor, tr("une shape"))); + return; } - return; + //...or not + setScale(scale_); } diff --git a/sources/qetgraphicsitem/qetshapeitem.h b/sources/qetgraphicsitem/qetshapeitem.h index b0eb836da..29b62da86 100644 --- a/sources/qetgraphicsitem/qetshapeitem.h +++ b/sources/qetgraphicsitem/qetshapeitem.h @@ -1,66 +1,78 @@ +/* + Copyright 2006-2014 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech 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 2 of the License, or + (at your option) any later version. + + QElectroTech 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 QElectroTech. If not, see . +*/ #ifndef QETSHAPEITEM_H #define QETSHAPEITEM_H #include "qetgraphicsitem.h" +/** + * @brief The QetShapeItem class + * this class is used to draw a basic shape (line, rectangle, ellipse) + * into a diagram, that can be saved to .qet file. + */ class QetShapeItem : public QetGraphicsItem { + Q_OBJECT + public: + Q_ENUMS(ShapeType) + enum ShapeType {Line =0, + Rectangle =1, + Ellipse =2}; - enum ShapeType { - Line = 0, - Rectangle, - Ellipse - }; - - QetShapeItem(QPointF, QPointF = QPointF(0,0), ShapeType = Line, bool lineAngle = false, QGraphicsItem *parent = 0); - virtual ~QetShapeItem(); - - // attributes - public: enum { Type = UserType + 1008 }; - // methods - public: + QetShapeItem(QPointF, QPointF = QPointF(0,0), ShapeType = Line, QGraphicsItem *parent = 0); + virtual ~QetShapeItem(); + /** - Enable the use of qgraphicsitem_cast to safely cast a QGraphicsItem into a - QetShapeItem + Enable the use of qgraphicsitem_cast to safely cast a QGraphicsItem into a QetShapeItem @return the QGraphicsItem type */ virtual int type() const { return Type; } + ///METHODS void setStyle(Qt::PenStyle); - Qt::PenStyle getStyle() const { return _shapeStyle; } - ShapeType getType() const { return _shapeType; } - void setBoundingRect(QRectF rec) { _boundingRect = rec; } - void setLineAngle(bool lineAngle) { _lineAngle = lineAngle; } - void setFullyBuilt(bool isBuilt); - QLineF *getLine(); - QRectF *getRectangle(); - QRectF *getEllipse(); - virtual bool fromXml(const QDomElement &); - virtual QDomElement toXml(QDomDocument &document) const; - void setWritingXml(bool writing) { _writingXml = writing; } - virtual void editProperty(); - QRectF boundingRect() const; - void scale(double factor); - private: - ShapeType _shapeType; - Qt::PenStyle _shapeStyle; - QRectF _boundingRect; - bool _lineAngle; // false if line from topleft corner to bottomright corner - // and true if line from topright corner to bottomleft corner - bool _isFullyBuilt; - QPointF _origMousePress; - bool _writingXml; + virtual bool fromXml (const QDomElement &); + virtual QDomElement toXml (QDomDocument &document) const; + virtual bool toDXF (const QString &filepath); + + virtual void editProperty(); + + void setP2(QPointF P2); + + QRectF boundingRect() const; + QPainterPath shape() const; protected: - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); - virtual void mousePressEvent(QGraphicsSceneMouseEvent *e); - virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *e); - QPainterPath shape() const; + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + private: + void changeGraphicsItem (const ShapeType &newtype); + + private slots: + void previewScale(int factor); + + ///ATTRIBUTES + private: + ShapeType m_shapeType; + Qt::PenStyle m_shapeStyle; + QPointF m_P1, m_P2; }; - #endif // QETSHAPEITEM_H