diff --git a/src/plugins/qtsupport/screenshotcropper.cpp b/src/plugins/qtsupport/screenshotcropper.cpp index 4997be926f0..920a6aa9bc1 100644 --- a/src/plugins/qtsupport/screenshotcropper.cpp +++ b/src/plugins/qtsupport/screenshotcropper.cpp @@ -58,9 +58,8 @@ static inline QString fileNameForPath(const QString &path) static QRect cropRectForAreaOfInterest(const QSize &imageSize, const QSize &cropSize, const QRect &areaOfInterest) { QRect result; - const qreal cropSizeToAreaSizeFactor = qMin(cropSize.width() / qreal(areaOfInterest.width()), - cropSize.height() / qreal(areaOfInterest.height())); - if (cropSizeToAreaSizeFactor >= 1) { + if (areaOfInterest.width() <= cropSize.width() + && areaOfInterest.height() <= cropSize.height()) { const QPoint areaOfInterestCenter = areaOfInterest.center(); const int cropX = qBound(0, areaOfInterestCenter.x() - cropSize.width() / 2, @@ -72,19 +71,28 @@ static QRect cropRectForAreaOfInterest(const QSize &imageSize, const QSize &crop const int cropHeight = qMin(imageSize.height(), cropSize.height()); result = QRect(cropX, cropY, cropWidth, cropHeight); } else { - QSize resultSize = cropSize.expandedTo(areaOfInterest.size()); + QSize resultSize = cropSize.scaled(areaOfInterest.width(), areaOfInterest.height(), + Qt::KeepAspectRatioByExpanding); result = QRect(QPoint(), resultSize); + result.moveCenter(areaOfInterest.center()); } return result; } -QImage ScreenshotCropper::croppedImage(const QImage &sourceImage, const QString &filePath, const QSize &cropSize) +} // namespace Internal + +namespace ScreenshotCropper { + +QImage croppedImage(const QImage &sourceImage, const QString &filePath, const QSize &cropSize, + const QRect &areaOfInterest) { - const QRect areaOfInterest = welcomeScreenAreas()->areas.value(fileNameForPath(filePath)); + const QRect area = areaOfInterest.isValid() ? areaOfInterest : + Internal::welcomeScreenAreas()->areas.value(Internal::fileNameForPath(filePath)); QImage result; - if (areaOfInterest.isValid()) { - const QRect cropRect = cropRectForAreaOfInterest(sourceImage.size(), cropSize, areaOfInterest); + if (area.isValid()) { + const QRect cropRect = Internal::cropRectForAreaOfInterest(sourceImage.size(), + cropSize, areaOfInterest); const QSize cropRectSize = cropRect.size(); result = sourceImage.copy(cropRect); if (cropRectSize.width() <= cropSize.width() && cropRectSize.height() <= cropSize.height()) @@ -115,7 +123,7 @@ static const QString xmlAttributeY = QLatin1String("y"); static const QString xmlAttributeWidth = QLatin1String("width"); static const QString xmlAttributeHeight = QLatin1String("height"); -QMap ScreenshotCropper::loadAreasOfInterest(const QString &areasXmlFile) +QMap loadAreasOfInterest(const QString &areasXmlFile) { QMap areasOfInterest; QFile xmlFile(areasXmlFile); @@ -146,7 +154,7 @@ QMap ScreenshotCropper::loadAreasOfInterest(const QString &areas return areasOfInterest; } -bool ScreenshotCropper::saveAreasOfInterest(const QString &areasXmlFile, QMap &areas) +bool saveAreasOfInterest(const QString &areasXmlFile, QMap &areas) { QFile file(areasXmlFile); if (!file.open(QIODevice::WriteOnly)) @@ -169,5 +177,5 @@ bool ScreenshotCropper::saveAreasOfInterest(const QString &areasXmlFile, QMap #include -#include + +QT_BEGIN_NAMESPACE +class QImage; +class QSize; +QT_END_NAMESPACE namespace QtSupport { -namespace Internal { +namespace ScreenshotCropper { -class ScreenshotCropper -{ -public: - static QImage croppedImage(const QImage &sourceImage, const QString &filePath, const QSize &cropSize); - static QMap loadAreasOfInterest(const QString &areasXmlFile); - static bool saveAreasOfInterest(const QString &areasXmlFile, QMap &areas); -}; +QTSUPPORT_EXPORT QImage croppedImage(const QImage &sourceImage, const QString &filePath, + const QSize &cropSize, const QRect &areaOfInterest = {}); +QTSUPPORT_EXPORT QMap loadAreasOfInterest(const QString &areasXmlFile); +QTSUPPORT_EXPORT bool saveAreasOfInterest(const QString &areasXmlFile, + QMap &areas); -} // namespace Internal +} // ScreenshotCropper } // namespace QtSupport diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index 2b99c0bcf53..79878f18d3a 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -42,7 +42,7 @@ add_subdirectory(qtc-askpass) add_subdirectory(qtcreatorcrashhandler) # add_subdirectory(qtcreatorwidgets) ## qbs does not build this either add_subdirectory(qtpromaker) -# add_subdirectory(screenshotcropper) ## qbs does not build this either +add_subdirectory(screenshotcropper) add_subdirectory(sdktool) add_subdirectory(valgrindfake) add_subdirectory(wininterrupt) ## windows only diff --git a/src/tools/screenshotcropper/CMakeLists.txt b/src/tools/screenshotcropper/CMakeLists.txt index c33a1311c83..1eb807dc909 100644 --- a/src/tools/screenshotcropper/CMakeLists.txt +++ b/src/tools/screenshotcropper/CMakeLists.txt @@ -1,22 +1,9 @@ -# -# Copyright (C) YourCompany. All rights reserved. -# -# GENERATED BY CPP-DEPENDENCIES - do not edit, your changes will be lost -# If you must edit, remove these two lines to avoid regeneration - -project(src.tools.screenshotcropper) - - -add_library(${PROJECT_NAME} STATIC - cropimageview.cpp - cropimageview.h - main.cpp - screenshotcropperwindow.cpp - screenshotcropperwindow.h +add_qtc_executable(screenshotcropper + SKIP_INSTALL + DEPENDS Qt5::Widgets Utils Core QtSupport + SOURCES + main.cpp + cropimageview.cpp + cropimageview.h + screenshotcropperwindow.h screenshotcropperwindow.cpp screenshotcropperwindow.ui ) - -target_link_libraries(${PROJECT_NAME} - PRIVATE - src.plugins.qtsupport -) - diff --git a/src/tools/screenshotcropper/cropimageview.cpp b/src/tools/screenshotcropper/cropimageview.cpp index 5beadd1842d..6dadb04c014 100644 --- a/src/tools/screenshotcropper/cropimageview.cpp +++ b/src/tools/screenshotcropper/cropimageview.cpp @@ -24,6 +24,7 @@ ****************************************************************************/ #include "cropimageview.h" + #include #include @@ -34,19 +35,18 @@ CropImageView::CropImageView(QWidget *parent) void CropImageView::mousePressEvent(QMouseEvent *event) { - setArea(QRect(event->pos(), m_area.bottomRight())); - update(); + m_initialPoint = event->pos(); + setEndPoint(m_initialPoint); } void CropImageView::mouseMoveEvent(QMouseEvent *event) { - setArea(QRect(m_area.topLeft(), event->pos())); - update(); + setEndPoint(event->pos()); } void CropImageView::mouseReleaseEvent(QMouseEvent *event) { - mouseMoveEvent(event); + setEndPoint(event->pos()); } void CropImageView::setImage(const QImage &image) @@ -63,6 +63,11 @@ void CropImageView::setArea(const QRect &area) update(); } +void CropImageView::setEndPoint(const QPoint &point) +{ + setArea(QRect(m_initialPoint, point)); +} + void CropImageView::paintEvent(QPaintEvent *event) { Q_UNUSED(event) diff --git a/src/tools/screenshotcropper/cropimageview.h b/src/tools/screenshotcropper/cropimageview.h index c5d3ae026d0..cefccd92abd 100644 --- a/src/tools/screenshotcropper/cropimageview.h +++ b/src/tools/screenshotcropper/cropimageview.h @@ -41,6 +41,7 @@ public: void setImage(const QImage &image); void setArea(const QRect &area); + void setEndPoint(const QPoint &point); signals: void cropAreaChanged(const QRect &area); @@ -48,4 +49,5 @@ signals: private: QImage m_image; QRect m_area; + QPoint m_initialPoint; }; diff --git a/src/tools/screenshotcropper/main.cpp b/src/tools/screenshotcropper/main.cpp index bc87ef41431..b287aa035d7 100644 --- a/src/tools/screenshotcropper/main.cpp +++ b/src/tools/screenshotcropper/main.cpp @@ -29,8 +29,6 @@ #include "screenshotcropperwindow.h" -using namespace QtSupport::Internal; - const QString settingsKeyAreasXmlFile = QLatin1String("areasXmlFile"); const QString settingsKeyImagesFolder = QLatin1String("imagesFolder"); diff --git a/src/tools/screenshotcropper/screenshotcropperwindow.cpp b/src/tools/screenshotcropper/screenshotcropperwindow.cpp index 224bf3f33a4..f4f825edd96 100644 --- a/src/tools/screenshotcropper/screenshotcropperwindow.cpp +++ b/src/tools/screenshotcropper/screenshotcropperwindow.cpp @@ -25,11 +25,13 @@ #include "screenshotcropperwindow.h" #include "ui_screenshotcropperwindow.h" + +#include +#include + #include #include -using namespace QtSupport::Internal; - ScreenShotCropperWindow::ScreenShotCropperWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::ScreenShotCropperWindow) @@ -51,7 +53,7 @@ void ScreenShotCropperWindow::loadData(const QString &areasXmlFile, const QStrin typedef QMap::ConstIterator StringRectConstIt; m_areasOfInterestFile = areasXmlFile; - m_areasOfInterest = ScreenshotCropper::loadAreasOfInterest(m_areasOfInterestFile); + m_areasOfInterest = QtSupport::ScreenshotCropper::loadAreasOfInterest(m_areasOfInterestFile); m_imagesFolder = imagesFolder; const StringRectConstIt cend = m_areasOfInterest.constEnd(); for (StringRectConstIt it = m_areasOfInterest.constBegin(); it != cend; ++it) @@ -70,12 +72,25 @@ void ScreenShotCropperWindow::setArea(const QRect &area) const QListWidgetItem *item = ui->m_filenamesList->currentItem(); if (!item) return; + + if (!area.isValid()) { + ui->m_previewLabel->setPixmap({}); + return; + } + const QString currentFile = item->text(); m_areasOfInterest.insert(currentFile, area); + const QImage img(m_imagesFolder + QLatin1Char('/') + currentFile); + const QPixmap cropped = QPixmap::fromImage( + QtSupport::ScreenshotCropper::croppedImage(img, currentFile, + Core::ListModel::defaultImageSize, + area)); + ui->m_previewLabel->setPixmap(cropped); } void ScreenShotCropperWindow::saveData() { - if (!ScreenshotCropper::saveAreasOfInterest(m_areasOfInterestFile, m_areasOfInterest)) + if (!QtSupport::ScreenshotCropper::saveAreasOfInterest(m_areasOfInterestFile, + m_areasOfInterest)) qFatal("Cannot write %s", qPrintable(m_areasOfInterestFile)); } diff --git a/src/tools/screenshotcropper/screenshotcropperwindow.h b/src/tools/screenshotcropper/screenshotcropperwindow.h index 5fc5085960c..19f9a9fcf67 100644 --- a/src/tools/screenshotcropper/screenshotcropperwindow.h +++ b/src/tools/screenshotcropper/screenshotcropperwindow.h @@ -25,11 +25,9 @@ #pragma once -#include - #include - -using namespace QtSupport::Internal; +#include +#include namespace Ui { class ScreenShotCropperWindow; } diff --git a/src/tools/screenshotcropper/screenshotcropperwindow.ui b/src/tools/screenshotcropper/screenshotcropperwindow.ui index 9d50284c63a..a298497a9be 100644 --- a/src/tools/screenshotcropper/screenshotcropperwindow.ui +++ b/src/tools/screenshotcropper/screenshotcropperwindow.ui @@ -15,6 +15,13 @@ + + + + QDialogButtonBox::Close|QDialogButtonBox::Save + + + @@ -43,29 +50,39 @@ 0 0 - 398 - 435 + 386 + 438 0 - + 0 - + + + + 1 + 0 + + + - - - - QDialogButtonBox::Close|QDialogButtonBox::Save + + + + QFrame::Box + + +