QetShapeItem: improve code.

Other improvement related to the refactoring of QetShapeItem


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3134 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2014-06-14 16:04:34 +00:00
parent 22db245e6b
commit f5ca3114d1
14 changed files with 508 additions and 488 deletions

View File

@@ -20,6 +20,7 @@
#include <QTextStream> #include <QTextStream>
#include <QMessageBox> #include <QMessageBox>
#include <QString> #include <QString>
#include "exportdialog.h"
const double Createdxf::sheetWidth = 4000; 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*/ /* 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()) { if (!fileName.isEmpty()) {
QFile file(fileName); 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<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 */ /* 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()) { if (!fileName.isEmpty()) {
QFile file(fileName); 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 */ /* draw arc in dx format */
void Createdxf::drawArc(QString fileName,double x,double y,double rad,double startAngle,double endAngle,int color) void Createdxf::drawArc(QString fileName,double x,double y,double rad,double startAngle,double endAngle,int color)
{ {

View File

@@ -34,11 +34,19 @@ class Createdxf
static void drawCircle(QString,double,double,double,int); 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 drawArc(QString,double x,double y,double rad,double startAngle,double endAngle,int color);
static void drawDonut(QString,double,double,double,int); 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 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, 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);
bool leftAlign = false, float scale = 0);
static const double sheetWidth; static const double sheetWidth;
static const double sheetHeight; static const double sheetHeight;

View File

@@ -418,9 +418,7 @@ QDomDocument Diagram::toXml(bool whole_content) {
if (!list_shapes.isEmpty()) { if (!list_shapes.isEmpty()) {
QDomElement shapes = document.createElement("shapes"); QDomElement shapes = document.createElement("shapes");
foreach (QetShapeItem *dii, list_shapes) { foreach (QetShapeItem *dii, list_shapes) {
dii ->setWritingXml(true);
shapes.appendChild(dii -> toXml(document)); shapes.appendChild(dii -> toXml(document));
dii ->setWritingXml(false);
} }
racine.appendChild(shapes); racine.appendChild(shapes);
} }
@@ -1106,6 +1104,20 @@ DiagramPosition Diagram::convertPosition(const QPointF &pos) {
return(diagram_position); 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 Definit s'il faut afficher ou non les bornes
@param dt true pour afficher les bornes, false sinon @param dt true pour afficher les bornes, false sinon

View File

@@ -156,6 +156,7 @@ class Diagram : public QGraphicsScene {
void setBorderOptions(BorderOptions); void setBorderOptions(BorderOptions);
BorderOptions borderOptions(); BorderOptions borderOptions();
DiagramPosition convertPosition(const QPointF &); DiagramPosition convertPosition(const QPointF &);
static QPointF snapToGrid(const QPointF &p);
bool drawTerminals() const; bool drawTerminals() const;
void setDrawTerminals(bool); void setDrawTerminals(bool);

View File

@@ -497,10 +497,10 @@ void MoveElementsCommand::move(const QPointF &actual_movement) {
// deplace les shapes // deplace les shapes
foreach (QetShapeItem *dsi, content_to_move.shapes) { foreach (QetShapeItem *dsi, content_to_move.shapes) {
dsi -> setPos(dsi -> pos() + actual_movement); dsi -> setPos(dsi -> pos() + actual_movement);
QRectF rec = dsi -> boundingRect(); /*QRectF rec = dsi -> boundingRect();
rec.translate(actual_movement); rec.translate(actual_movement);
dsi -> setBoundingRect(rec); 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 * @brief ItemResizerCommand::ItemResizerCommand
* @param image * Change the size of @qgi
* @param old_ old size of image * @param qgi item to resize
* @param new_ new size of image * @param old_ old size
* @param parent undocommand parent * @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), QUndoCommand(parent),
image_(image), m_qgi ( qgi ),
old_size (old_), old_size ( old_ ),
new_size (new_), new_size ( new_ ),
diagram(image->diagram()) 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(); 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(); diagram -> showMe();
if (old_size<new_size) setText(QObject::tr("Agrandire une image \340 %1 %").arg(new_size*100)); 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 une image \340 %1 %").arg(new_size*100)); else setText(QObject::tr("R\351duire %1 \340 %2 %").arg(m_text).arg(new_size*100));
image_ -> setScale(new_size); m_qgi -> setScale(new_size);
QUndoCommand::redo();
} }
@@ -1212,7 +1217,9 @@ ChangeShapeStyleCommand::ChangeShapeStyleCommand(QetShapeItem *shape, Qt::PenSty
old_style (old_), old_style (old_),
new_style (new_), new_style (new_),
diagram(shape->diagram()) diagram(shape->diagram())
{} {
setText(QObject::tr("Changer le style d'une shape"));
}
/** /**
* @brief ChangeShapeStyleCommand::~ChangeShapeStyleCommand destructor * @brief ChangeShapeStyleCommand::~ChangeShapeStyleCommand destructor
@@ -1237,49 +1244,6 @@ void ChangeShapeStyleCommand::redo() {
QUndoCommand::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 * @brief LinkElementsCommand::LinkElementsCommand
*Constructor *Constructor

View File

@@ -598,11 +598,11 @@ class ChangeSeveralConductorsPropertiesCommand : public QUndoCommand {
Diagram *diagram; Diagram *diagram;
}; };
class ImageResizerCommand : public QUndoCommand { class ItemResizerCommand : public QUndoCommand {
//constructor and destructor //constructor and destructor
public: public:
ImageResizerCommand (DiagramImageItem *image, qreal &old_, qreal &new_, QUndoCommand *parent = 0); ItemResizerCommand (QetGraphicsItem *qgi, qreal &old_, qreal &new_,const QString &text, QUndoCommand *parent = 0);
virtual ~ImageResizerCommand(); virtual ~ItemResizerCommand();
//methods //methods
public: public:
@@ -611,9 +611,10 @@ class ImageResizerCommand : public QUndoCommand {
//attributes //attributes
private: private:
DiagramImageItem *image_; QetGraphicsItem *m_qgi;
qreal old_size, new_size; qreal old_size, new_size;
Diagram *diagram; Diagram *diagram;
QString m_text;
}; };
@@ -635,24 +636,6 @@ class ChangeShapeStyleCommand : public QUndoCommand {
Diagram *diagram; 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 { class LinkElementsCommand : public QUndoCommand {
public: public:
// constructor destructor // constructor destructor

View File

@@ -50,7 +50,7 @@
@param diagram Schema a afficher ; si diagram vaut 0, un nouveau Diagram est utilise @param diagram Schema a afficher ; si diagram vaut 0, un nouveau Diagram est utilise
@param parent Le QWidget parent de cette vue de schema @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); setAttribute(Qt::WA_DeleteOnClose, true);
setInteractive(true); setInteractive(true);
current_behavior = noAction; current_behavior = noAction;
@@ -462,16 +462,16 @@ void DiagramView::mousePressEvent(QMouseEvent *e) {
current_behavior = noAction; current_behavior = noAction;
break; break;
case addingLine: case addingLine:
newItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Line, false); newShapeItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Line);
scene -> addItem(newItem); scene -> addItem(newShapeItem);
break; break;
case addingRectangle: case addingRectangle:
newItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Rectangle); newShapeItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Rectangle);
scene -> addItem(newItem); scene -> addItem(newShapeItem);
break; break;
case addingEllipse: case addingEllipse:
newItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Ellipse); newShapeItem = new QetShapeItem(rubber_band_origin, rubber_band_origin, QetShapeItem::Ellipse);
scene -> addItem(newItem); scene -> addItem(newShapeItem);
break; break;
case dragView: case dragView:
current_behavior = noAction; current_behavior = noAction;
@@ -504,12 +504,7 @@ void DiagramView::mouseMoveEvent(QMouseEvent *e) {
return; return;
} }
else if (e -> buttons() == Qt::LeftButton && current_behavior & addingShape) { else if (e -> buttons() == Qt::LeftButton && current_behavior & addingShape) {
QRectF rec = QRectF(rubber_band_origin, mapToScene(e->pos())).normalized(); newShapeItem->setP2(mapToScene(e->pos()));
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);
} }
else QGraphicsView::mouseMoveEvent(e); else QGraphicsView::mouseMoveEvent(e);
} }
@@ -524,10 +519,8 @@ void DiagramView::mouseReleaseEvent(QMouseEvent *e) {
return; return;
} }
else if (current_behavior & addingShape) { else if (current_behavior & addingShape) {
newItem -> setFullyBuilt(true);
// place it to the good position with an undo command // place it to the good position with an undo command
scene -> undoStack().push(new AddShapeCommand(scene, newItem, rubber_band_origin)); scene -> undoStack().push(new AddShapeCommand(scene, newShapeItem, rubber_band_origin));
adjustSceneRect();
emit(itemAdded()); emit(itemAdded());
current_behavior = noAction; current_behavior = noAction;
} }

View File

@@ -65,7 +65,7 @@ class DiagramView : public QGraphicsView {
QPoint next_position_; QPoint next_position_;
QPointF center_view_; QPointF center_view_;
QImage image_to_add_; QImage image_to_add_;
QetShapeItem *newItem; QetShapeItem *newShapeItem;
QPointF rubber_band_origin; QPointF rubber_band_origin;
// methods // methods

View File

@@ -421,7 +421,8 @@ void ExportDialog::generateDxf(Diagram *diagram, int width, int height, bool kee
QList<DiagramImageItem *> list_images; QList<DiagramImageItem *> list_images;
QList<QLineF *> list_lines; QList<QLineF *> list_lines;
QList<QRectF *> list_rectangles; QList<QRectF *> list_rectangles;
QList<QRectF *> list_ellipses; //QList<QRectF *> list_ellipses;
QList <QetShapeItem *> list_shapes;
DiagramFolioList *ptr; DiagramFolioList *ptr;
if (ptr = dynamic_cast<DiagramFolioList *>(diagram)) { if (ptr = dynamic_cast<DiagramFolioList *>(diagram)) {
@@ -482,45 +483,12 @@ void ExportDialog::generateDxf(Diagram *diagram, int width, int height, bool kee
} else if (DiagramImageItem *dii = qgraphicsitem_cast<DiagramImageItem *>(qgi)) { } else if (DiagramImageItem *dii = qgraphicsitem_cast<DiagramImageItem *>(qgi)) {
list_images << dii; list_images << dii;
} else if (QetShapeItem *dii = qgraphicsitem_cast<QetShapeItem *>(qgi)) { } else if (QetShapeItem *dii = qgraphicsitem_cast<QetShapeItem *>(qgi)) {
if (dii -> getType() == QetShapeItem::Line && dii -> getLine()) { list_shapes << dii;
list_lines << dii -> getLine();
} else if (dii -> getType() == QetShapeItem::Rectangle && dii -> getRectangle()) {
list_rectangles << dii -> getRectangle();
} else if (dii -> getEllipse()){
list_ellipses << dii -> getEllipse();
}
} }
} }
} }
//draw lines foreach (QetShapeItem *qsi, list_shapes) qsi->toDXF(file_path);
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);
}
//Draw elements //Draw elements
foreach(Element *elmt, list_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 h = arc -> at(3) * Createdxf::yScale;
qreal startAngle = arc -> at(4); qreal startAngle = arc -> at(4);
qreal spanAngle = arc -> at(5); 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); 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, void ExportDialog::fillRow(QString file_path, const QRectF &row_rect, QString author, QString title,
QString folio, QString date) QString folio, QString date)
{ {

View File

@@ -41,6 +41,7 @@ class ExportDialog : public QDialog {
// methods // methods
public: public:
int diagramsToExportCount() const; int diagramsToExportCount() const;
static QPointF rotation_transformed(qreal, qreal, qreal, qreal, qreal);
private: private:
class ExportDiagramLine { class ExportDiagramLine {
@@ -94,8 +95,6 @@ class ExportDialog : public QDialog {
void exportDiagram(ExportDiagramLine *); void exportDiagram(ExportDiagramLine *);
qreal diagramRatio(Diagram *); qreal diagramRatio(Diagram *);
QSize diagramSize(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: public slots:
void slot_correctWidth(int); void slot_correctWidth(int);

View File

@@ -147,7 +147,7 @@ void DiagramImageItem::editProperty() {
cb.isChecked() ? is_movable_=false : is_movable_=true; cb.isChecked() ? is_movable_=false : is_movable_=true;
qreal new_scale = slider.value(); qreal new_scale = slider.value();
new_scale /= factor_range; 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 //...or not
else setScale(scale_); else setScale(scale_);

View File

@@ -50,11 +50,7 @@ Diagram* QetGraphicsItem::diagram() const{
void QetGraphicsItem::setPos(const QPointF &p) { void QetGraphicsItem::setPos(const QPointF &p) {
if (p == pos() || !is_movable_) return; if (p == pos() || !is_movable_) return;
if (scene() && snap_to_grid_) { if (scene() && snap_to_grid_) {
// arrondit l'abscisse a 10 px pres QGraphicsItem::setPos(Diagram::snapToGrid(p));
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);
emit positionChange(pos()); emit positionChange(pos());
} else QGraphicsItem::setPos(p); } else QGraphicsItem::setPos(p);
} }

View File

@@ -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 "qetshapeitem.h"
#include "diagramcommands.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), QetGraphicsItem(parent),
_shapeStyle(Qt::DashLine), m_shapeType(type),
_lineAngle(lineAngle), m_shapeStyle(Qt::DashLine),
_isFullyBuilt(false), m_P1 (Diagram::snapToGrid(p1)),
_writingXml(false) m_P2 (Diagram::snapToGrid(p2))
{ {
_shapeType = type; setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
_boundingRect = QRectF(p1, p2);
} }
QetShapeItem::~QetShapeItem() QetShapeItem::~QetShapeItem()
{ {
} }
/**
* @brief QetShapeItem::setStyle
* Set the new style of pen for thi item
* @param newStyle
*/
void QetShapeItem::setStyle(Qt::PenStyle newStyle) void QetShapeItem::setStyle(Qt::PenStyle newStyle)
{ {
_shapeStyle = newStyle; m_shapeStyle = newStyle;
update(); update();
} }
void QetShapeItem::scale(double factor) /**
{ * @brief QetShapeItem::scale
QRectF bounding_rect = boundingRect(); * Preview the scale of this item to factor
bounding_rect.setWidth(bounding_rect.width() * factor); * @param factor
bounding_rect.setHeight(bounding_rect.height() * factor); */
setBoundingRect(bounding_rect); void QetShapeItem::previewScale(int factor) {
update(); setTransformOriginPoint(boundingRect().center());
} if (factor >= 1 && factor <= 200) {
qreal new_scale = factor;
void QetShapeItem::setFullyBuilt(bool isBuilt) new_scale /= 100;
{ setScale(new_scale);
_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());
} }
return line;
} }
QRectF *QetShapeItem::getRectangle() /**
{ * @brief QetShapeItem::setP2
QRectF rect = boundingRect(); * Set the second point of this item
QRectF *rec = 0; * @param P2
if (_shapeType == Rectangle) */
rec = new QRectF(rect); void QetShapeItem::setP2(QPointF P2) {
return rec; P2 = Diagram::snapToGrid(P2);
if (P2 == m_P2) return;
prepareGeometryChange();
m_P2 = P2;
setTransformOriginPoint(boundingRect().center());
} }
QRectF *QetShapeItem::getEllipse() /**
{ * @brief QetShapeItem::boundingRect
QRectF rect = boundingRect(); * @return the bounding rect of this item
QRectF *rec = 0; */
if (_shapeType == Ellipse) QRectF QetShapeItem::boundingRect() const {
rec = new QRectF(rect); QRectF b(m_P1, m_P2);
return rec; return b.normalized();
} }
QRectF QetShapeItem::boundingRect() const /**
{ * @brief QetShapeItem::shape
return _boundingRect; * @return the shape of this item
} */
QPainterPath QetShapeItem::shape() const {
QPainterPath QetShapeItem::shape() const
{
QPainterPath path; QPainterPath path;
QPainterPathStroker pps; QPainterPathStroker pps;
QRectF rect = boundingRect();
switch (_shapeType) { switch (m_shapeType) {
case Line: case Line:
if (_lineAngle) { path.moveTo(m_P1);
path.moveTo(rect.topRight()); path.lineTo(m_P2);
path.lineTo(rect.bottomLeft());
} else {
path.moveTo(rect.topLeft());
path.lineTo(rect.bottomRight());
}
//use @pps for grab line with bigger outerline
//more usefull
pps.setWidth(10); pps.setWidth(10);
path= pps.createStroke(path); path = pps.createStroke(path);
break; break;
case Rectangle: case Rectangle:
path = QetGraphicsItem::shape(); path.addRect(boundingRect());
break; break;
case Ellipse: case Ellipse:
path.addEllipse(rect); path.addEllipse(boundingRect());
break; break;
default: default:
path = QetGraphicsItem::shape(); Q_ASSERT(false);
break;
} }
return path; 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) void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{ {
if (!_writingXml) { Q_UNUSED(option); Q_UNUSED(widget);
painter -> setRenderHint(QPainter::Antialiasing, false);
QRectF rec = boundingRect();
QPen pen(Qt::black);
#ifdef Q_WS_WIN QPen pen;
pen.setWidthF(1); pen.setStyle(m_shapeStyle);
#endif if (isSelected()) pen.setColor(Qt::red);
painter->setPen(pen);
if (isSelected()) switch (m_shapeType) {
pen.setColor(Qt::red); case Line:
pen.setStyle(_shapeStyle); painter->drawLine(QLineF(m_P1, m_P2));
painter->setPen(pen); break;
switch(_shapeType) { case Rectangle:
case Line: painter->drawRect(boundingRect());
if (_lineAngle) break;
painter -> drawLine(rec.topRight(), rec.bottomLeft()); case Ellipse:
else painter->drawEllipse(boundingRect());
painter -> drawLine(rec.topLeft(), rec.bottomRight()); break;
break;
case Rectangle:
painter -> drawRect(rec);
break;
default: //(case Ellipse:)
painter ->drawEllipse(rec);
}
} }
} }
void QetShapeItem::mousePressEvent(QGraphicsSceneMouseEvent *e) /**
{ * @brief QetShapeItem::fromXml
_origMousePress = mapToScene(e -> pos()); * Build this item from the xml description
first_move_ = true; * @param e element where is stored this item
if (e -> modifiers() & Qt::ControlModifier) { * @return true if load success
setSelected(!isSelected()); */
} bool QetShapeItem::fromXml(const QDomElement &e) {
QGraphicsItem::mousePressEvent(e); if (e.tagName() != "shape") return (false);
}
void QetShapeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) m_shapeStyle = Qt::PenStyle(e.attribute("style","0").toInt());
{ m_P1.setX(e.attribute("x1", 0).toDouble());
if (diagram()) diagram() -> endMoveElements(); m_P1.setY(e.attribute("y1", 0).toDouble());
QPointF newCoord = mapToScene(e -> pos()); m_P2.setX(e.attribute("x2", 0).toDouble());
if (newCoord != _origMousePress) { m_P2.setY(e.attribute("y2", 0).toDouble());
//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);
}
changeGraphicsItem(QetShapeItem::ShapeType(e.attribute("type","0").toInt()));
return (true); 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 QetShapeItem::toXml(QDomDocument &document) const {
QDomElement result = document.createElement("shape"); QDomElement result = document.createElement("shape");
QRectF rec = boundingRect();
//write some attribute //write some attribute
result.setAttribute("type", QString::number(_shapeType)); result.setAttribute("type", QString::number(m_shapeType));
result.setAttribute("x", QString::number(rec.topLeft().x())); result.setAttribute("style", QString::number(m_shapeStyle));
result.setAttribute("y", QString::number(rec.topLeft().y())); result.setAttribute("x1", mapToScene(m_P1).x());
result.setAttribute("w", QString::number(rec.width())); result.setAttribute("y1", mapToScene(m_P1).y());
result.setAttribute("h", QString::number(rec.height())); result.setAttribute("x2", mapToScene(m_P2).x());
result.setAttribute("lineAngle", QString::number(_lineAngle)); result.setAttribute("y2", mapToScene(m_P2).y());
result.setAttribute("style", QString::number(_shapeStyle));
return(result); 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() void QetShapeItem::editProperty()
{ {
if (diagram() -> isReadOnly()) return; if (diagram() -> isReadOnly()) return;
//the dialog //the dialog
QDialog property_dialog(diagram()->views().at(0)); 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 //the main layout
QVBoxLayout dialog_layout(&property_dialog); QVBoxLayout dialog_layout(&property_dialog);
//GroupBox for resizer image //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); dialog_layout.addWidget(&restyle_groupe);
QHBoxLayout restyle_layout(&restyle_groupe); QHBoxLayout restyle_layout(&restyle_groupe);
@@ -223,7 +252,7 @@ void QetShapeItem::editProperty()
style_combo.addItem(QObject::tr("Traits points points")); style_combo.addItem(QObject::tr("Traits points points"));
// The items have been added in order accordance with Qt::PenStyle. // 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); restyle_layout.addWidget(&style_combo);
@@ -231,23 +260,33 @@ void QetShapeItem::editProperty()
QCheckBox cb(tr("Verrouiller la position"), &property_dialog); QCheckBox cb(tr("Verrouiller la position"), &property_dialog);
cb.setChecked(!is_movable_); cb.setChecked(!is_movable_);
dialog_layout.addWidget(&cb); dialog_layout.addWidget(&cb);
cb.setVisible(false);
//GroupBox for Scaling //GroupBox for Scaling
QGroupBox scale_groupe(QObject::tr("\311chelle", "shape scale")); QGroupBox scale_groupe(QObject::tr("\311chelle", "shape scale"));
dialog_layout.addWidget(&scale_groupe); dialog_layout.addWidget(&scale_groupe);
QHBoxLayout scale_layout(&scale_groupe); QHBoxLayout scale_layout(&scale_groupe);
QLabel scale_label(&property_dialog); int min_range = 1;
scale_label.setText(tr("Facteur d'\351chelle")); int max_range = 200;
int factor_range = 100;
QLineEdit scale_lineedit(&property_dialog); //slider
QDoubleValidator scale_val(0.0,1000,3, &property_dialog); QSlider slider(Qt::Horizontal, &property_dialog);
scale_lineedit.setValidator(&scale_val); slider.setRange(min_range, max_range);
scale_lineedit.setText("1.0"); qreal scale_= scale();
slider.setValue(scale_*factor_range);
scale_layout.addWidget(&scale_label); //spinbox
scale_layout.addWidget(&scale_lineedit); 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 //dialog button, box
QDialogButtonBox dbb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); QDialogButtonBox dbb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
@@ -260,11 +299,13 @@ void QetShapeItem::editProperty()
cb.isChecked() ? is_movable_=false : is_movable_=true; cb.isChecked() ? is_movable_=false : is_movable_=true;
Qt::PenStyle new_style = Qt::PenStyle(style_combo.currentIndex() + 1); Qt::PenStyle new_style = Qt::PenStyle(style_combo.currentIndex() + 1);
if (new_style != _shapeStyle) if (new_style != m_shapeStyle) diagram()->undoStack().push(new ChangeShapeStyleCommand(this, m_shapeStyle, new_style));
diagram()->undoStack().push(new ChangeShapeStyleCommand(this, _shapeStyle, new_style));
double scale_factor = scale_lineedit.text().toDouble(); qreal scale_factor = slider.value();
if (scale_factor != 1 && scale_factor > 0 && scale_factor < 1000 ) scale_factor /= factor_range;
diagram()->undoStack().push(new ChangeShapeScaleCommand(this, scale_factor)); if (scale_ != scale_factor) diagram()->undoStack().push(new ItemResizerCommand(this, scale_, scale_factor, tr("une shape")));
return;
} }
return; //...or not
setScale(scale_);
} }

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef QETSHAPEITEM_H #ifndef QETSHAPEITEM_H
#define QETSHAPEITEM_H #define QETSHAPEITEM_H
#include "qetgraphicsitem.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 class QetShapeItem : public QetGraphicsItem
{ {
Q_OBJECT
public: 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 }; enum { Type = UserType + 1008 };
// methods QetShapeItem(QPointF, QPointF = QPointF(0,0), ShapeType = Line, QGraphicsItem *parent = 0);
public: virtual ~QetShapeItem();
/** /**
Enable the use of qgraphicsitem_cast to safely cast a QGraphicsItem into a Enable the use of qgraphicsitem_cast to safely cast a QGraphicsItem into a QetShapeItem
QetShapeItem
@return the QGraphicsItem type @return the QGraphicsItem type
*/ */
virtual int type() const { return Type; } virtual int type() const { return Type; }
///METHODS
void setStyle(Qt::PenStyle); 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: virtual bool fromXml (const QDomElement &);
ShapeType _shapeType; virtual QDomElement toXml (QDomDocument &document) const;
Qt::PenStyle _shapeStyle; virtual bool toDXF (const QString &filepath);
QRectF _boundingRect;
bool _lineAngle; // false if line from topleft corner to bottomright corner virtual void editProperty();
// and true if line from topright corner to bottomleft corner
bool _isFullyBuilt; void setP2(QPointF P2);
QPointF _origMousePress;
bool _writingXml; QRectF boundingRect() const;
QPainterPath shape() const;
protected: protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
virtual void mousePressEvent(QGraphicsSceneMouseEvent *e);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
QPainterPath shape() const;
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 #endif // QETSHAPEITEM_H