[qet] [3134] QetShapeItem: improve code. |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/qet Archives
]
Revision: 3134
Author: blacksun
Date: 2014-06-14 18:04:34 +0200 (Sat, 14 Jun 2014)
Log Message:
-----------
QetShapeItem: improve code.
Other improvement related to the refactoring of QetShapeItem
Modified Paths:
--------------
trunk/sources/createdxf.cpp
trunk/sources/createdxf.h
trunk/sources/diagram.cpp
trunk/sources/diagram.h
trunk/sources/diagramcommands.cpp
trunk/sources/diagramcommands.h
trunk/sources/diagramview.cpp
trunk/sources/diagramview.h
trunk/sources/exportdialog.cpp
trunk/sources/exportdialog.h
trunk/sources/qetgraphicsitem/diagramimageitem.cpp
trunk/sources/qetgraphicsitem/qetgraphicsitem.cpp
trunk/sources/qetgraphicsitem/qetshapeitem.cpp
trunk/sources/qetgraphicsitem/qetshapeitem.h
Modified: trunk/sources/createdxf.cpp
===================================================================
--- trunk/sources/createdxf.cpp 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/createdxf.cpp 2014-06-14 16:04:34 UTC (rev 3134)
@@ -20,6 +20,7 @@
#include <QTextStream>
#include <QMessageBox>
#include <QString>
+#include "exportdialog.h"
const double Createdxf::sheetWidth = 4000;
@@ -286,7 +287,7 @@
/* 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 @@
}
}
+/**
+ * @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<qreal,qreal> > 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<qreal, qreal> newPart(start,span);
+ arc_parts_vector.push_back(newPart);
+ start = i;
+ }
+ }
+ if (start != i) {
+ span = i - start;
+ QPair<qreal, qreal> 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<qreal, qreal> newPart(start,span);
+ arc_parts_vector.push_back(newPart);
+ start = i;
+ }
+ }
+ if (start != i) {
+ span = i - start;
+ QPair<qreal, qreal> newPart(start,span);
+ arc_parts_vector.push_back(newPart);
+ }
+ }
+
+ for (int i = 0; i < arc_parts_vector.size(); i++) {
+
+ QPair<qreal,qreal> 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 @@
}
}
+/**
+ * @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)
{
Modified: trunk/sources/createdxf.h
===================================================================
--- trunk/sources/createdxf.h 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/createdxf.h 2014-06-14 16:04:34 UTC (rev 3134)
@@ -34,11 +34,19 @@
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;
Modified: trunk/sources/diagram.cpp
===================================================================
--- trunk/sources/diagram.cpp 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/diagram.cpp 2014-06-14 16:04:34 UTC (rev 3134)
@@ -418,9 +418,7 @@
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);
}
@@ -1107,6 +1105,20 @@
}
/**
+ * @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
*/
Modified: trunk/sources/diagram.h
===================================================================
--- trunk/sources/diagram.h 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/diagram.h 2014-06-14 16:04:34 UTC (rev 3134)
@@ -156,6 +156,7 @@
void setBorderOptions(BorderOptions);
BorderOptions borderOptions();
DiagramPosition convertPosition(const QPointF &);
+ static QPointF snapToGrid(const QPointF &p);
bool drawTerminals() const;
void setDrawTerminals(bool);
Modified: trunk/sources/diagramcommands.cpp
===================================================================
--- trunk/sources/diagramcommands.cpp 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/diagramcommands.cpp 2014-06-14 16:04:34 UTC (rev 3134)
@@ -497,10 +497,10 @@
// 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 @@
}
/**
- * @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<new_size) setText(QObject::tr("Agrandire une image \340 %1 %").arg(new_size*100));
- else setText(QObject::tr("R\351duire une image \340 %1 %").arg(new_size*100));
- image_ -> setScale(new_size);
+ if (old_size<new_size) setText(QObject::tr("Agrandire %1 \340 %2 %").arg(m_text).arg(new_size*100));
+ else setText(QObject::tr("R\351duire %1 \340 %2 %").arg(m_text).arg(new_size*100));
+ m_qgi -> setScale(new_size);
+ QUndoCommand::redo();
}
@@ -1212,7 +1217,9 @@
old_style (old_),
new_style (new_),
diagram(shape->diagram())
-{}
+{
+ setText(QObject::tr("Changer le style d'une shape"));
+}
/**
* @brief ChangeShapeStyleCommand::~ChangeShapeStyleCommand destructor
@@ -1237,50 +1244,7 @@
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
* @param elmt1 element to Link
Modified: trunk/sources/diagramcommands.h
===================================================================
--- trunk/sources/diagramcommands.h 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/diagramcommands.h 2014-06-14 16:04:34 UTC (rev 3134)
@@ -598,11 +598,11 @@
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 @@
//attributes
private:
- DiagramImageItem *image_;
+ QetGraphicsItem *m_qgi;
qreal old_size, new_size;
Diagram *diagram;
+ QString m_text;
};
@@ -635,24 +636,6 @@
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
Modified: trunk/sources/diagramview.cpp
===================================================================
--- trunk/sources/diagramview.cpp 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/diagramview.cpp 2014-06-14 16:04:34 UTC (rev 3134)
@@ -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 @@
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 @@
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 @@
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;
}
Modified: trunk/sources/diagramview.h
===================================================================
--- trunk/sources/diagramview.h 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/diagramview.h 2014-06-14 16:04:34 UTC (rev 3134)
@@ -65,7 +65,7 @@
QPoint next_position_;
QPointF center_view_;
QImage image_to_add_;
- QetShapeItem *newItem;
+ QetShapeItem *newShapeItem;
QPointF rubber_band_origin;
// methods
Modified: trunk/sources/exportdialog.cpp
===================================================================
--- trunk/sources/exportdialog.cpp 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/exportdialog.cpp 2014-06-14 16:04:34 UTC (rev 3134)
@@ -421,7 +421,8 @@
QList<DiagramImageItem *> list_images;
QList<QLineF *> list_lines;
QList<QRectF *> list_rectangles;
- QList<QRectF *> list_ellipses;
+ //QList<QRectF *> list_ellipses;
+ QList <QetShapeItem *> list_shapes;
DiagramFolioList *ptr;
if (ptr = dynamic_cast<DiagramFolioList *>(diagram)) {
@@ -482,46 +483,13 @@
} else if (DiagramImageItem *dii = qgraphicsitem_cast<DiagramImageItem *>(qgi)) {
list_images << dii;
} else if (QetShapeItem *dii = qgraphicsitem_cast<QetShapeItem *>(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);
- }
+ foreach (QetShapeItem *qsi, list_shapes) qsi->toDXF(file_path);
- //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);
- }
-
//Draw elements
foreach(Element *elmt, list_elements) {
@@ -647,7 +615,7 @@
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 @@
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<qreal,qreal> > 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<qreal, qreal> newPart(start,span);
- arc_parts_vector.push_back(newPart);
- start = i;
- }
- }
- if (start != i) {
- span = i - start;
- QPair<qreal, qreal> 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<qreal, qreal> newPart(start,span);
- arc_parts_vector.push_back(newPart);
- start = i;
- }
- }
- if (start != i) {
- span = i - start;
- QPair<qreal, qreal> newPart(start,span);
- arc_parts_vector.push_back(newPart);
- }
- }
-
- for (int i = 0; i < arc_parts_vector.size(); i++) {
-
- QPair<qreal,qreal> 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)
{
Modified: trunk/sources/exportdialog.h
===================================================================
--- trunk/sources/exportdialog.h 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/exportdialog.h 2014-06-14 16:04:34 UTC (rev 3134)
@@ -41,6 +41,7 @@
// methods
public:
int diagramsToExportCount() const;
+ static QPointF rotation_transformed(qreal, qreal, qreal, qreal, qreal);
private:
class ExportDiagramLine {
@@ -94,8 +95,6 @@
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);
Modified: trunk/sources/qetgraphicsitem/diagramimageitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/diagramimageitem.cpp 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/qetgraphicsitem/diagramimageitem.cpp 2014-06-14 16:04:34 UTC (rev 3134)
@@ -147,7 +147,7 @@
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_);
Modified: trunk/sources/qetgraphicsitem/qetgraphicsitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/qetgraphicsitem.cpp 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/qetgraphicsitem/qetgraphicsitem.cpp 2014-06-14 16:04:34 UTC (rev 3134)
@@ -50,11 +50,7 @@
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);
}
Modified: trunk/sources/qetgraphicsitem/qetshapeitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/qetshapeitem.cpp 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/qetgraphicsitem/qetshapeitem.cpp 2014-06-14 16:04:34 UTC (rev 3134)
@@ -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 <http://www.gnu.org/licenses/>.
+*/
#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);
- }
+ 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());
- 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);
- }
-
+ 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 @@
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,24 +260,34 @@
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");
+ //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);
- scale_layout.addWidget(&scale_label);
- scale_layout.addWidget(&scale_lineedit);
-
//dialog button, box
QDialogButtonBox dbb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
dialog_layout.addWidget(&dbb);
@@ -260,11 +299,13 @@
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_);
}
Modified: trunk/sources/qetgraphicsitem/qetshapeitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/qetshapeitem.h 2014-06-13 07:26:33 UTC (rev 3133)
+++ trunk/sources/qetgraphicsitem/qetshapeitem.h 2014-06-14 16:04:34 UTC (rev 3134)
@@ -1,3 +1,20 @@
+/*
+ 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 <http://www.gnu.org/licenses/>.
+*/
#ifndef QETSHAPEITEM_H
#define QETSHAPEITEM_H
@@ -3,63 +20,58 @@
#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
- };
+ enum { Type = UserType + 1008 };
- QetShapeItem(QPointF, QPointF = QPointF(0,0), ShapeType = Line, bool lineAngle = false, QGraphicsItem *parent = 0);
+ QetShapeItem(QPointF, QPointF = QPointF(0,0), ShapeType = Line, QGraphicsItem *parent = 0);
virtual ~QetShapeItem();
- // attributes
- public:
- enum { Type = UserType + 1008 };
-
- // methods
- public:
/**
- 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 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;
- void scale(double factor);
+ QPainterPath shape() const;
+ protected:
+ virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
+
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;
+ void changeGraphicsItem (const ShapeType &newtype);
- protected:
- void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
- virtual void mousePressEvent(QGraphicsSceneMouseEvent *e);
- virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
- QPainterPath shape() const;
+ private slots:
+ void previewScale(int factor);
+ ///ATTRIBUTES
+ private:
+ ShapeType m_shapeType;
+ Qt::PenStyle m_shapeStyle;
+ QPointF m_P1, m_P2;
};
-
#endif // QETSHAPEITEM_H