diff --git a/IHM_Retouche_Photo/crop.cpp b/IHM_Retouche_Photo/crop.cpp index 70728cf2de36ffd1c7e55ea95b68ce7f2b285a3a..d06de05e71233b4bc2a2dd64b5196fa36887cdf4 100644 --- a/IHM_Retouche_Photo/crop.cpp +++ b/IHM_Retouche_Photo/crop.cpp @@ -5,103 +5,95 @@ #include <QPainter> #include <QPushButton> #include "crop.h" +#include <QGraphicsSceneMouseEvent> +#include <QKeyEvent> +#include <QDebug> namespace tool { Crop::Crop(const QString & name, const QIcon & icon, bool free) : Tool(name, icon), _free(free), scene(nullptr){} - - bool cropModeEnabled = false; - - - QGraphicsRectItem* cropRectItem = nullptr; - - - - - void Crop::toggleCropMode() { +void Crop::toggleCropMode() { + if (cropModeEnabled) { + if (scene && cropRectItem) { + scene->removeItem(cropRectItem); + delete cropRectItem; + cropRectItem = nullptr; + delete scene; + scene = nullptr; + } + } else { scene = new QGraphicsScene(); - cropModeEnabled = !cropModeEnabled; - - if (cropModeEnabled) { - cropRectItem = new QGraphicsRectItem(); - cropRectItem->setPen(QPen(Qt::red, 2, Qt::DashLine)); - scene->addItem(cropRectItem); + cropRectItem = new QGraphicsRectItem(); + cropRectItem->setPen(QPen(Qt::red, 2, Qt::DashLine)); + scene->addItem(cropRectItem); + } - } else { + cropModeEnabled = !cropModeEnabled; +} - if (scene && cropRectItem) { - scene->removeItem(cropRectItem); - delete cropRectItem; - cropRectItem = nullptr; - } - } +void Crop::onMousePressed(ui::ImageArea& imageArea, ui::Selection& select, const QPoint& point, const QImage& image, const QMouseEvent& e) { + if (cropModeEnabled && cropRectItem) { + cropRectItem->setRect(point.x(), point.y(), 0, 0); } +} - void Selectionable::onMousePressed(ui::ImageArea & imageArea, ui::Selection & select, const QPoint & point, const QImage & image, const QMouseEvent & e) { - if (cropModeEnabled && cropRectItem) { - // Capture the starting point of the crop area - QPointF startPoint = point; - cropRectItem->setRect(startPoint.x(), startPoint.y(), 0, 0); - } else { - - } +void Crop::onMouseMoved(ui::ImageArea& imageArea, ui::Selection& select, const QPoint& click, const QImage& image, const QMouseEvent& e, bool hold) { + if (cropModeEnabled && cropRectItem) { + qreal width = click.x() - cropRectItem->rect().left(); + qreal height = click.y() - cropRectItem->rect().top(); + cropRectItem->setRect(cropRectItem->rect().left(), cropRectItem->rect().top(), width, height); } +} +void Crop::onMouseReleased(ui::ImageArea& imageArea, ui::Selection& select, const QPoint& click, const QImage& image, const QMouseEvent& e) { + if (cropModeEnabled && cropRectItem) { + QRectF cropRect = cropRectItem->rect(); + qDebug() << "Crop Area Dimensions: Width = " << cropRect.width() << ", Height = " << cropRect.height(); + QImage croppedImage = image.copy(cropRect.toRect()); - void Crop::onMouseMoved(ui::ImageArea &, ui::Selection &, const QPoint & click, const QImage &, const QMouseEvent &, bool) { - if (cropModeEnabled && cropRectItem) { - QPointF currentPoint = click; - qreal width = currentPoint.x() - cropRectItem->rect().left(); - qreal height = currentPoint.y() - cropRectItem->rect().top(); - cropRectItem->setRect(cropRectItem->rect().left(), cropRectItem->rect().top(), width, height); - } else { + imageArea.setImage(croppedImage); - } + toggleCropMode(); } +} +void Crop::onKeyPress(ui::ImageArea& imageArea, ui::Selection& select, const QPoint& click, const QImage& image, const QKeyEvent& e) { + // Handle key press events if needed +} - void Crop::onMouseReleased(ui::ImageArea &, ui::Selection &, const QPoint &, const QImage &, const QMouseEvent &) { - if (cropModeEnabled && cropRectItem) { - - QRectF cropRect = cropRectItem->rect(); - qDebug() << "Crop Area Dimensions: Width = " << cropRect.width() << ", Height = " << cropRect.height(); - - - } else { +void Crop::onKeyReleased(ui::ImageArea& imageArea, ui::Selection& select, const QPoint& click, const QImage& image, const QKeyEvent& e) { + // Handle key release events if needed +} - } +void Crop::pressed(ui::ImageArea& area, QImage& image, QPoint& click, ui::Selection& selection, QMouseEvent& e) { + if (image.rect().contains(click) || this->_free) { + this->onMousePressed(area, selection, click, image, e); } +} - void Crop::onKeyPress(ui::ImageArea &, ui::Selection &, const QPoint &, const QImage &, const QKeyEvent &) {} - void Crop::onKeyReleased(ui::ImageArea &, ui::Selection &, const QPoint &, const QImage &, const QKeyEvent &) {} - - void Crop::pressed(ui::ImageArea & area, QImage & image, QPoint & click, ui::Selection & selection, QMouseEvent & e) { - if(image.rect().contains(click) || this->_free) { - this->onMousePressed(area, selection, click, image, e); - } +void Crop::released(ui::ImageArea& area, QImage& image, QPoint& click, ui::Selection& selection, QMouseEvent& e) { + if (image.rect().contains(click) || this->_free) { + this->onMouseReleased(area, selection, click, image, e); } +} - void Crop::released(ui::ImageArea & area, QImage & image, QPoint & click, ui::Selection & selection, QMouseEvent & e) { - if(image.rect().contains(click) || this->_free) { - this->onMouseReleased(area, selection, click, image, e); - } +void Crop::moved(ui::ImageArea& area, QImage& image, QPoint& click, ui::Selection& selection, QMouseEvent& e, bool hold) { + if (image.rect().contains(click) || this->_free) { + this->onMouseMoved(area, selection, click, image, e, hold); } +} - void Crop::moved(ui::ImageArea & area, QImage & image, QPoint & click ,ui::Selection & selection, QMouseEvent & e, bool hold) { - if(image.rect().contains(click) || this->_free) { - this->onMouseMoved(area, selection, click, image, e, hold); - } - } +void Crop::keyPressed(ui::ImageArea& area, QImage& image, QPoint& click, ui::Selection& selection, QKeyEvent& e) { + this->onKeyPress(area, selection, click, image, e); +} - void Crop::keyPressed(ui::ImageArea & area, QImage & image, QPoint & click, ui::Selection & selection, QKeyEvent & e) { - this->onKeyPress(area, selection, click, image, e); - } +void Crop::keyReleased(ui::ImageArea& area, QImage& image, QPoint& click, ui::Selection& selection, QKeyEvent& e) { + this->onKeyReleased(area, selection, click, image, e); +} - void Crop::keyReleased(ui::ImageArea & area, QImage & image, QPoint & click, ui::Selection & selection, QKeyEvent & e) { - this->onKeyReleased(area, selection, click, image, e); - } } + diff --git a/IHM_Retouche_Photo/crop.h b/IHM_Retouche_Photo/crop.h index 8f3d2fd0cb4139a1c01433d26ad8663bba7218d4..99f1804e590702b1ef7ff87d03b099b199f184e1 100644 --- a/IHM_Retouche_Photo/crop.h +++ b/IHM_Retouche_Photo/crop.h @@ -21,35 +21,25 @@ class Crop : public Tool private: bool _free; - + bool cropModeEnabled; + QGraphicsScene* scene; + QGraphicsRectItem* cropRectItem; public: + Crop(const QString& name, const QIcon& icon, bool free); - Crop(const QString & = "NaN", const QIcon & = QIcon(":/image/oxygen/icons/16x16/ressource/image/oxygen/icons/16x16/transform-crop.png"),bool = false); - - virtual void onMousePressed(ui::ImageArea &, ui::Selection &, const QPoint &, const QImage &, const QMouseEvent &) = 0; - virtual void onMouseReleased(ui::ImageArea &, ui::Selection &, const QPoint &, const QImage &, const QMouseEvent &) = 0; - virtual void onMouseMoved(ui::ImageArea &, ui::Selection &, const QPoint &, const QImage &, const QMouseEvent &, bool) = 0; - - virtual void onKeyPress(ui::ImageArea &, ui::Selection &, const QPoint &, const QImage &, const QKeyEvent &); - virtual void onKeyReleased(ui::ImageArea &, ui::Selection &, const QPoint &, const QImage &, const QKeyEvent &); - -public slots: - virtual void pressed(ui::ImageArea &, QImage &, QPoint &, ui::Selection &, QMouseEvent &) final; - virtual void released(ui::ImageArea &, QImage &, QPoint &, ui::Selection &, QMouseEvent &) final; - virtual void moved(ui::ImageArea &, QImage &, QPoint &, ui::Selection &, QMouseEvent &, bool) final; - - virtual void keyPressed(ui::ImageArea &, QImage &, QPoint &, ui::Selection &, QKeyEvent &) final; - virtual void keyReleased(ui::ImageArea &, QImage &, QPoint &, ui::Selection &, QKeyEvent &) final; void toggleCropMode(); - -private: - tool::Selectionable *selectionable; - QPushButton *CropButton; - QGraphicsScene *scene; - QGraphicsRectItem *cropRectItem; - bool cropModeEnabled; - + void onMousePressed(ui::ImageArea& imageArea, ui::Selection& select, const QPoint& point, const QImage& image, const QMouseEvent& e) ; + void onMouseMoved(ui::ImageArea& imageArea, ui::Selection& select, const QPoint& click, const QImage& image, const QMouseEvent& e, bool hold); + void onMouseReleased(ui::ImageArea& imageArea, ui::Selection& select, const QPoint& click, const QImage& image, const QMouseEvent& e); + void onKeyPress(ui::ImageArea& imageArea, ui::Selection& select, const QPoint& click, const QImage& image, const QKeyEvent& e) ; + void onKeyReleased(ui::ImageArea& imageArea, ui::Selection& select, const QPoint& click, const QImage& image, const QKeyEvent& e) ; + + void pressed(ui::ImageArea& area, QImage& image, QPoint& click, ui::Selection& selection, QMouseEvent& e) override; + void released(ui::ImageArea& area, QImage& image, QPoint& click, ui::Selection& selection, QMouseEvent& e) override; + void moved(ui::ImageArea& area, QImage& image, QPoint& click, ui::Selection& selection, QMouseEvent& e, bool hold) override; + void keyPressed(ui::ImageArea& area, QImage& image, QPoint& click, ui::Selection& selection, QKeyEvent& e) override; + void keyReleased(ui::ImageArea& area, QImage& image, QPoint& click, ui::Selection& selection, QKeyEvent& e) override; signals: }; diff --git a/IHM_Retouche_Photo/mainwindow.cpp b/IHM_Retouche_Photo/mainwindow.cpp index 79bf277661a6968d1192542afa7938e1102463e9..16e9e37a9b1fd59aa43a064e3c3298284f85ce7d 100644 --- a/IHM_Retouche_Photo/mainwindow.cpp +++ b/IHM_Retouche_Photo/mainwindow.cpp @@ -15,6 +15,7 @@ #include <tools/hand.h> #include <tools/tool.h> + MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent), _toolRegister(), _fileselectorManager(nullptr), _viewManager(nullptr) { @@ -32,7 +33,7 @@ MainWindow::MainWindow(QWidget * parent) ColorWindow = findChild<QPushButton*>("ColorWindow"); - //crop = QSharedPointer<tool::Crop>(new tool::Crop("Crop Tool", QIcon(":/icons/crop.png"), true)); + crop = new tool::Crop("Crop Tool", QIcon(":/icons/crop.png"), true); CropButton = findChild<QPushButton*>("CropButton"); @@ -106,7 +107,7 @@ void MainWindow::openColorPicker() } void MainWindow::handleCtrlCKey() { - // Trigger the ColorWindow button when Ctrl+C is pressed + openColorPicker(); } MainWindow::~MainWindow() { diff --git a/build-IHM_Retouche_Photo-Desktop_Qt_6_6_1_MinGW_64_bit-Debug/debug/IHM_Retouche_Photo.exe b/build-IHM_Retouche_Photo-Desktop_Qt_6_6_1_MinGW_64_bit-Debug/debug/IHM_Retouche_Photo.exe index fe95008979f7aff59a96316c769ef946850a01da..e492e96d6774ee276dc683a5a47e4d7f3b9bde60 100644 Binary files a/build-IHM_Retouche_Photo-Desktop_Qt_6_6_1_MinGW_64_bit-Debug/debug/IHM_Retouche_Photo.exe and b/build-IHM_Retouche_Photo-Desktop_Qt_6_6_1_MinGW_64_bit-Debug/debug/IHM_Retouche_Photo.exe differ