Image can be edited in the dock widget

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3944 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2015-05-08 17:49:29 +00:00
parent 7ef8cbc0db
commit 6618887ed6
10 changed files with 457 additions and 83 deletions

View File

@@ -16,8 +16,8 @@
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "diagramimageitem.h"
#include "diagramcommands.h"
#include "diagram.h"
#include "imagepropertiesdialog.h"
/**
* @brief DiagramImageItem::DiagramImageItem
@@ -27,10 +27,7 @@
DiagramImageItem::DiagramImageItem(QetGraphicsItem *parent_item):
QetGraphicsItem(parent_item)
{
setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
#if QT_VERSION >= 0x040600
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
#endif
setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemSendsGeometryChanges);
}
/**
@@ -44,10 +41,7 @@ DiagramImageItem::DiagramImageItem(const QPixmap &pixmap, QetGraphicsItem *paren
pixmap_(pixmap)
{
setTransformOriginPoint(boundingRect().center());
setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
#if QT_VERSION >= 0x040600
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
#endif
setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemSendsGeometryChanges);
}
/**
@@ -85,76 +79,14 @@ void DiagramImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *
}
/**
* @brief DiagramImageItem::setScale
* @param scale the value of @scale must be betwen 1 and 200
* @brief DiagramImageItem::editProperty
* Open the approriate dialog to edit this image
*/
void DiagramImageItem::PreviewScale(int scale) {
if (scale >= 1 && scale <= 200) {
qreal new_scale = scale;
new_scale /= 100;
setScale(new_scale);
}
}
/**
* @brief Edit the image with ....
*/
void DiagramImageItem::editProperty() {
void DiagramImageItem::editProperty()
{
if (diagram() -> isReadOnly()) return;
//the range for scale image and divisor factor
int min_range = 1;
int max_range = 200;
int factor_range = 100;
//the dialog
QDialog property_dialog(diagram()->views().at(0));
property_dialog.setWindowTitle(tr("Éditer les propriétés d'une image", "window title"));
//the main layout
QVBoxLayout dialog_layout(&property_dialog);
//GroupBox for resizer image
QGroupBox resize_groupe(tr("Dimension de l'image", "image size"));
dialog_layout.addWidget(&resize_groupe);
QHBoxLayout resize_layout(&resize_groupe);
//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
resize_layout.addWidget(&slider);
resize_layout.addWidget(&spin_box);
//check box for disable move
QCheckBox cb(tr("Verrouiller la position"), &property_dialog);
cb.setChecked(!is_movable_);
dialog_layout.addWidget(&cb);
//dialog button, box
QDialogButtonBox dbb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
dialog_layout.addWidget(&dbb);
connect(&dbb, SIGNAL(accepted()), &property_dialog, SLOT(accept()));
connect(&dbb, SIGNAL(rejected()), &property_dialog, SLOT(reject()));
//dialog is accepted...
if (property_dialog.exec() == QDialog::Accepted) {
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 ItemResizerCommand(this, scale_, new_scale, tr("une image")));
}
//...or not
else setScale(scale_);
return;
ImagePropertiesDialog dialog(this, QApplication::activeWindow());
dialog.exec();
}
/**

View File

@@ -60,11 +60,6 @@ class DiagramImageItem : public QetGraphicsItem {
protected:
virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
signals:
private slots:
void PreviewScale(int);
protected:
QPixmap pixmap_;
};

View File

@@ -37,6 +37,10 @@ class QetGraphicsItem : public QGraphicsObject
virtual void setPos (qreal x, qreal y);
virtual void rotateBy (const qreal &);
virtual void applyRotation (const qreal &);
virtual bool isMovable () const {return is_movable_;}
virtual void setMovable (bool movable) { is_movable_ = movable;}
virtual void editProperty () {}
virtual QString name ()const {return QString("");}

View File

@@ -19,6 +19,8 @@
#include "elementpropertieswidget.h"
#include "diagram.h"
#include "element.h"
#include "diagramimageitem.h"
#include "imagepropertieswidget.h"
/**
* @brief DiagramPropertiesEditorDockWidget::DiagramPropertiesEditorDockWidget
@@ -69,11 +71,15 @@ void DiagramPropertiesEditorDockWidget::selectionChanged()
{
if (!m_diagram) return;
clear();
if (m_diagram->selectedItems().size() == 1)
{
QGraphicsItem *item = m_diagram->selectedItems().first();
if (Element *elmt = dynamic_cast<Element*>(item))
addEditor(new ElementPropertiesWidget(elmt, this));
else if (DiagramImageItem *image = dynamic_cast<DiagramImageItem *>(item))
addEditor(new ImagePropertiesWidget(image, this));
}
}

View File

@@ -0,0 +1,29 @@
#include "imagepropertiesdialog.h"
#include "ui_imagepropertiesdialog.h"
#include "imagepropertieswidget.h"
#include "diagramimageitem.h"
ImagePropertiesDialog::ImagePropertiesDialog(DiagramImageItem *image, QWidget *parent) :
QDialog(parent),
ui(new Ui::ImagePropertiesDialog)
{
ui->setupUi(this);
m_editor = new ImagePropertiesWidget(image, this);
ui->verticalLayout->insertWidget(0, m_editor);
}
ImagePropertiesDialog::~ImagePropertiesDialog() {
delete ui;
}
void ImagePropertiesDialog::setImageItem(DiagramImageItem *image) {
m_editor->setImageItem(image);
}
void ImagePropertiesDialog::on_buttonBox_accepted() {
m_editor->apply();
}
void ImagePropertiesDialog::on_buttonBox_rejected() {
m_editor->reset();
}

View File

@@ -0,0 +1,31 @@
#ifndef IMAGEPROPERTIESDIALOG_H
#define IMAGEPROPERTIESDIALOG_H
#include <QDialog>
class ImagePropertiesWidget;
class DiagramImageItem;
namespace Ui {
class ImagePropertiesDialog;
}
class ImagePropertiesDialog : public QDialog
{
Q_OBJECT
public:
explicit ImagePropertiesDialog(DiagramImageItem *image = nullptr, QWidget *parent = 0);
~ImagePropertiesDialog();
void setImageItem (DiagramImageItem *image);
private slots:
void on_buttonBox_accepted();
void on_buttonBox_rejected();
private:
Ui::ImagePropertiesDialog *ui;
ImagePropertiesWidget *m_editor;
};
#endif // IMAGEPROPERTIESDIALOG_H

View File

@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ImagePropertiesDialog</class>
<widget class="QDialog" name="ImagePropertiesDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>194</width>
<height>52</height>
</rect>
</property>
<property name="windowTitle">
<string>Éditer les propriétés de image</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>ImagePropertiesDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>ImagePropertiesDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -0,0 +1,137 @@
/*
Copyright 2006-2015 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 "imagepropertieswidget.h"
#include "ui_imagepropertieswidget.h"
#include "diagramimageitem.h"
#include "diagramcommands.h"
#include <QUndoCommand>
/**
* @brief ImagePropertiesWidget::ImagePropertiesWidget
* Constructor
* @param image : image to edit properties
* @param parent : parent widget
*/
ImagePropertiesWidget::ImagePropertiesWidget(DiagramImageItem *image, QWidget *parent) :
PropertiesEditorWidget(parent),
ui(new Ui::ImagePropertiesWidget),
m_image(nullptr)
{
ui->setupUi(this);
this->setDisabled(true);
setImageItem(image);
}
/**
* @brief ImagePropertiesWidget::~ImagePropertiesWidget
* Destructor
*/
ImagePropertiesWidget::~ImagePropertiesWidget() {
delete ui;
}
/**
* @brief ImagePropertiesWidget::setImageItem
* Set the image to edit properties
* @param image : image to edit
*/
void ImagePropertiesWidget::setImageItem(DiagramImageItem *image)
{
if(!image) return;
this->setEnabled(true);
if (m_image == image) return;
if (m_image)
disconnect(m_image, SIGNAL(destroyed()), this, SLOT(imageWasDeleted()));
m_image = image;
connect(m_image, SIGNAL(destroyed()), this, SLOT(imageWasDeleted()));
m_movable = image->isMovable();
m_scale = m_image->scale();
updateUi();
}
/**
* @brief ImagePropertiesWidget::apply
* Apply the change
*/
void ImagePropertiesWidget::apply()
{
if(!m_image) return;
if (m_image->diagram())
m_image->diagram()->undoStack().push(associatedUndo());
m_scale = m_image->scale();
}
/**
* @brief ImagePropertiesWidget::reset
* Reset the change
*/
void ImagePropertiesWidget::reset()
{
if(!m_image) return;
m_image->setScale(m_scale);
m_image->setMovable(m_movable);
updateUi();
}
/**
* @brief ImagePropertiesWidget::associatedUndo
* @return the change in an undo command (ItemResizerCommand)
*/
QUndoCommand* ImagePropertiesWidget::associatedUndo()
{
qreal value = ui->m_scale_slider->value();
value /= 100;
return new ItemResizerCommand(m_image, m_scale, value, tr("une image"));
}
/**
* @brief ImagePropertiesWidget::updateUi
* Udpdate the ui, notably when the image to edit change
*/
void ImagePropertiesWidget::updateUi()
{
ui->m_scale_slider->setValue(m_scale * 100);
ui->m_lock_pos_cb->setChecked(!m_movable);
}
/**
* @brief ImagePropertiesWidget::on_m_scale_slider_valueChanged
* Update the size of image when move slider.
* @param value
*/
void ImagePropertiesWidget::on_m_scale_slider_valueChanged(int value)
{
qreal scale = value;
m_image->setScale(scale / 100);
}
void ImagePropertiesWidget::imageWasDeleted() {
m_image = nullptr;
}
/**
* @brief ImagePropertiesWidget::on_m_lock_pos_cb_clicked
* Set movable or not the image according to corresponding check box
*/
void ImagePropertiesWidget::on_m_lock_pos_cb_clicked() {
m_image->setMovable(!ui->m_lock_pos_cb->isChecked());
}

View File

@@ -0,0 +1,63 @@
/*
Copyright 2006-2015 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 IMAGEPROPERTIESWIDGET_H
#define IMAGEPROPERTIESWIDGET_H
#include <QWidget>
#include "PropertiesEditor/propertieseditorwidget.h"
class DiagramImageItem;
namespace Ui {
class ImagePropertiesWidget;
}
/**
* @brief The ImagePropertiesWidget class
* This class provide a widget to edit the propertie of a DiagramImageItem
*/
class ImagePropertiesWidget : public PropertiesEditorWidget
{
Q_OBJECT
public:
explicit ImagePropertiesWidget(DiagramImageItem *image = nullptr, QWidget *parent = 0);
~ImagePropertiesWidget();
void setImageItem (DiagramImageItem *image);
void apply();
void reset();
QUndoCommand* associatedUndo();
private:
void updateUi();
private slots:
void on_m_scale_slider_valueChanged(int value);
void imageWasDeleted();
void on_m_lock_pos_cb_clicked();
private:
Ui::ImagePropertiesWidget *ui;
DiagramImageItem *m_image;
bool m_movable;
qreal m_scale;
};
#endif // IMAGEPROPERTIESWIDGET_H

View File

@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ImagePropertiesWidget</class>
<widget class="QWidget" name="ImagePropertiesWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>231</width>
<height>94</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item row="2" column="0">
<widget class="QCheckBox" name="m_lock_pos_cb">
<property name="text">
<string>Verrouiller la position</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Dimension de l'image</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QSlider" name="m_scale_slider">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>200</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="m_scale_sb">
<property name="suffix">
<string>%</string>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>200</number>
</property>
</widget>
</item>
<item row="3" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>m_scale_slider</sender>
<signal>valueChanged(int)</signal>
<receiver>m_scale_sb</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>81</x>
<y>40</y>
</hint>
<hint type="destinationlabel">
<x>190</x>
<y>40</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_scale_sb</sender>
<signal>valueChanged(int)</signal>
<receiver>m_scale_slider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>190</x>
<y>40</y>
</hint>
<hint type="destinationlabel">
<x>81</x>
<y>40</y>
</hint>
</hints>
</connection>
</connections>
</ui>