QElectroTech  0.70
diagrameventaddimage.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "diagrameventaddimage.h"
20 #include "diagram.h"
21 #include "diagramimageitem.h"
22 #include "diagramcommands.h"
23 
30  DiagramEventInterface(diagram),
31  m_image (nullptr),
32  m_is_added (false)
33 {
34  openDialog();
35 }
36 
41 {
42  if (m_running || m_abort)
43  {
45  delete m_image;
46  }
47 
48  foreach (QGraphicsView *view, m_diagram->views())
49  view->setContextMenuPolicy((Qt::DefaultContextMenu));
50 }
51 
57 void DiagramEventAddImage::mousePressEvent(QGraphicsSceneMouseEvent *event)
58 {
59  if (m_image && event -> button() == Qt::LeftButton)
60  {
61  QPointF pos = event->scenePos();
62  pos.rx() -= m_image->boundingRect().width()/2;
63  pos.ry() -= m_image->boundingRect().height()/2;
64  m_diagram -> undoStack().push (new AddItemCommand<DiagramImageItem *>(m_image, m_diagram, pos));
65 
66  for (QGraphicsView *view : m_diagram->views()) {
67  view->setContextMenuPolicy((Qt::DefaultContextMenu));
68  }
69 
70  m_running = false;
71  emit finish();
72  event->setAccepted(true);
73  }
74  else if (m_image && event -> button() == Qt::RightButton)
75  {
76  m_image->setRotation(m_image->rotation() + 90);
77  event->setAccepted(true);
78  }
79 }
80 
86 void DiagramEventAddImage::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
87 {
88  if (!m_image || event->buttons() != Qt::NoButton) {
89  return;
90  };
91 
92  QPointF pos = event->scenePos();
93 
94  if (!m_is_added)
95  {
96  for (QGraphicsView *view : m_diagram->views()) {
97  view->setContextMenuPolicy((Qt::NoContextMenu));
98  }
99 
101  m_is_added = true;
102  }
103 
104  m_image->setPos(pos - m_image->boundingRect().center());
105  event->setAccepted(true);
106 }
107 
114 void DiagramEventAddImage::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
115  event->setAccepted(true);
116 }
117 
123 void DiagramEventAddImage::wheelEvent(QGraphicsSceneWheelEvent *event)
124 {
125  if (!m_is_added || !m_image || event -> modifiers() != Qt::CTRL) {
126  return;
127  }
128 
129  qreal scaling = m_image->scale();
130  event->delta() > 1? scaling += 0.01 : scaling -= 0.01;
131  if (scaling>0.01 && scaling <= 2) {
132  m_image->setScale(scaling);
133  }
134 
135  event->setAccepted(true);
136 }
137 
143 {
144  if (!m_image) return true;
145  return false;
146 }
147 
153 {
154  if (m_diagram -> isReadOnly()) return;
155 
156  //Open dialog for select image
157  QString pathPictures = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
158  QString fileName = QFileDialog::getOpenFileName(m_diagram->views().isEmpty()? nullptr : m_diagram->views().first(), QObject::tr("Selectionner une image..."), pathPictures, QObject::tr("Image Files (*.png *.jpg *.jpeg *.bmp *.svg)"));
159 
160  if (fileName.isEmpty()) return;
161 
162  QImage image(fileName);
163  if(image.isNull())
164  {
165  QMessageBox::critical(m_diagram->views().isEmpty()? nullptr : m_diagram->views().first(), QObject::tr("Erreur"), QObject::tr("Impossible de charger l'image."));
166  return;
167  }
168 
169  m_image = new DiagramImageItem (QPixmap::fromImage(image));
170  m_running = true;
171 }
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
DiagramEventAddImage::mouseMoveEvent Action when mouse move.
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
DiagramEventAddImage::mousePressEvent Action when mouse is pressed.
~DiagramEventAddImage() override
DiagramEventAddImage::~DiagramEventAddImage.
void openDialog()
DiagramEventAddImage::openDialog Open dialog for select the image to add.
DiagramEventAddImage(Diagram *diagram)
DiagramEventAddImage::DiagramEventAddImage Default constructor.
QRectF boundingRect() const override
DiagramImageItem::boundingRect the outer bounds of the item as a rectangle, if no pixmap are set...
The AddItemCommand class This command add an item in a diagram The item to add is template...
bool isNull() const
DiagramEventAddImage::isNull.
virtual void removeItem(QGraphicsItem *item)
Diagram::removeItem Réimplemented from QGraphicsScene::removeItem(QGraphicsItem *item) Do some specif...
Definition: diagram.cpp:1133
bool isEmpty() const
Definition: diagram.cpp:574
QIcon tr
Definition: qeticons.cpp:204
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
DiagramEventAddImage::mouseDoubleClickEvent This method is only use to overwrite double click...
DiagramImageItem * m_image
The DiagramEventInterface class isRunning() return true if action is running (do something). By default return false.
void wheelEvent(QGraphicsSceneWheelEvent *event) override
DiagramEventAddImage::wheelEvent Action when mouse wheel is rotate.
virtual void addItem(QGraphicsItem *item)
Diagram::addItem Réimplemented from QGraphicsScene::addItem(QGraphicsItem *item) Do some specific ope...
Definition: diagram.cpp:1108
virtual void setPos(const QPointF &p)
QetGraphicsItem::setPos set the position of the item to p.
QMessageBox::StandardButton critical(QWidget *, const QString &, const QString &, QMessageBox::StandardButtons=QMessageBox::Ok, QMessageBox::StandardButton=QMessageBox::NoButton)