imagecropperdialog.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef IMAGECROPPER_H
  2. #define IMAGECROPPER_H
  3. #include <QWidget>
  4. #include <QDialog>
  5. #include <QPainter>
  6. #include <QLabel>
  7. #include <QPixmap>
  8. #include <QString>
  9. #include <QMessageBox>
  10. #include <QHBoxLayout>
  11. #include <QVBoxLayout>
  12. #include <QPushButton>
  13. #include "imagecropperlabel.h"
  14. /*******************************************************
  15. * Loacl private class, which do image-cropping
  16. * Used in class ImageCropper
  17. *******************************************************/
  18. class ImageCropperDialogPrivate : public QDialog {
  19. Q_OBJECT
  20. public:
  21. ImageCropperDialogPrivate(const QPixmap& imageIn, QPixmap& outputImage,
  22. int windowWidth, int windowHeight,
  23. CropperShape shape, QSize cropperSize = QSize()) :
  24. QDialog(nullptr), outputImage(outputImage)
  25. {
  26. this->setAttribute(Qt::WA_DeleteOnClose, true);
  27. this->setWindowTitle("Image Cropper");
  28. this->setMouseTracking(true);
  29. this->setModal(true);
  30. imageLabel = new ImageCropperLabel(windowWidth, windowHeight, this);
  31. imageLabel->setCropper(shape, cropperSize);
  32. imageLabel->setOutputShape(OutputShape::RECT);
  33. imageLabel->setOriginalImage(imageIn);
  34. imageLabel->enableOpacity(true);
  35. QHBoxLayout* btnLayout = new QHBoxLayout();
  36. btnOk = new QPushButton("OK", this);
  37. btnCancel = new QPushButton("Cancel", this);
  38. btnLayout->addStretch();
  39. btnLayout->addWidget(btnOk);
  40. btnLayout->addWidget(btnCancel);
  41. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  42. mainLayout->addWidget(imageLabel);
  43. mainLayout->addLayout(btnLayout);
  44. connect(btnOk, &QPushButton::clicked, this, [this](){
  45. this->outputImage = this->imageLabel->getCroppedImage();
  46. this->close();
  47. });
  48. connect(btnCancel, &QPushButton::clicked, this, [this](){
  49. this->outputImage = QPixmap();
  50. this->close();
  51. });
  52. }
  53. private:
  54. ImageCropperLabel* imageLabel;
  55. QPushButton* btnOk;
  56. QPushButton* btnCancel;
  57. QPixmap& outputImage;
  58. };
  59. /*******************************************************************
  60. * class ImageCropperDialog
  61. * create a instane of class ImageCropperDialogPrivate
  62. * and get cropped image from the instance(after closing)
  63. ********************************************************************/
  64. class ImageCropperDialog : QObject {
  65. public:
  66. static QPixmap getCroppedImage(const QString& filename,int windowWidth, int windowHeight,
  67. CropperShape cropperShape, QSize crooperSize = QSize())
  68. {
  69. QPixmap inputImage;
  70. QPixmap outputImage;
  71. if (!inputImage.load(filename)) {
  72. QMessageBox::critical(nullptr, "Error", "Load image failed!", QMessageBox::Ok);
  73. return outputImage;
  74. }
  75. ImageCropperDialogPrivate* imageCropperDo =
  76. new ImageCropperDialogPrivate(inputImage, outputImage,
  77. windowWidth, windowHeight,
  78. cropperShape, crooperSize);
  79. imageCropperDo->exec();
  80. return outputImage;
  81. }
  82. };
  83. #endif // IMAGECROPPER_H