QetShapeItem: Add Scale option with UNDO/REDO

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3078 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
abhishekm71
2014-05-25 18:33:06 +00:00
parent 92b8d4babf
commit 215e268467
4 changed files with 89 additions and 3 deletions

View File

@@ -1239,6 +1239,47 @@ void ChangeShapeStyleCommand::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

@@ -635,6 +635,24 @@ 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

@@ -23,6 +23,15 @@ void QetShapeItem::setStyle(Qt::PenStyle newStyle)
update(); 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) void QetShapeItem::setFullyBuilt(bool isBuilt)
{ {
_isFullyBuilt = isBuilt; _isFullyBuilt = isBuilt;
@@ -224,6 +233,22 @@ void QetShapeItem::editProperty()
dialog_layout.addWidget(&cb); dialog_layout.addWidget(&cb);
cb.setVisible(false); cb.setVisible(false);
//GroupBox for Scaling
QGroupBox scale_groupe(QObject::tr("Scale", "shape scale"));
dialog_layout.addWidget(&scale_groupe);
QHBoxLayout scale_layout(&scale_groupe);
QLabel scale_label(&property_dialog);
scale_label.setText(tr("Scale Factor"));
QLineEdit scale_lineedit(&property_dialog);
QDoubleValidator scale_val(0.0,1000,3, &property_dialog);
scale_lineedit.setValidator(&scale_val);
scale_lineedit.setText("1.0");
scale_layout.addWidget(&scale_label);
scale_layout.addWidget(&scale_lineedit);
//dialog button, box //dialog button, box
QDialogButtonBox dbb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); QDialogButtonBox dbb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
dialog_layout.addWidget(&dbb); dialog_layout.addWidget(&dbb);
@@ -237,6 +262,9 @@ void QetShapeItem::editProperty()
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 != _shapeStyle)
diagram()->undoStack().push(new ChangeShapeStyleCommand(this, _shapeStyle, new_style)); 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));
} }
return; return;
} }

View File

@@ -43,6 +43,7 @@ class QetShapeItem : public QetGraphicsItem
void setWritingXml(bool writing) { _writingXml = writing; } void setWritingXml(bool writing) { _writingXml = writing; }
virtual void editProperty(); virtual void editProperty();
QRectF boundingRect() const; QRectF boundingRect() const;
void scale(double factor);
private: private:
ShapeType _shapeType; ShapeType _shapeType;
@@ -51,10 +52,8 @@ class QetShapeItem : public QetGraphicsItem
bool _lineAngle; // false if line from topleft corner to bottomright corner bool _lineAngle; // false if line from topleft corner to bottomright corner
// and true if line from topright corner to bottomleft corner // and true if line from topright corner to bottomleft corner
bool _isFullyBuilt; bool _isFullyBuilt;
QPointF _lineP1;
QPointF _lineP2;
QPointF _origMousePress; QPointF _origMousePress;
bool _writingXml; bool _writingXml;
protected: protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);