Commit 4ab95e7e authored by Alex Nemeth's avatar Alex Nemeth Committed by Henrik Fehlauer

Let window size depend on the screenshot aspect ratio

Summary:
Now the window width size changes with the aspect ratio of the captured image.
Maximum width is 1000 pixels for this automatic resize.

Test Plan: {F5697651}

Reviewers: ngraham, #spectacle, rkflx

Reviewed By: ngraham, #spectacle, rkflx

Subscribers: rkflx, ngraham

Tags: #spectacle

Differential Revision: https://phabricator.kde.org/D10377
parent f8ab96be
......@@ -25,7 +25,7 @@ KSImageWidget::KSImageWidget(QWidget *parent):
{
mDSEffect = new QGraphicsDropShadowEffect(this);
mDSEffect->setBlurRadius(5);
mDSEffect->setBlurRadius(SpectacleImage::SHADOW_RADIUS);
mDSEffect->setOffset(0);
mDSEffect->setColor(QColor(Qt::black));
......
......@@ -31,10 +31,15 @@
#include <KLocalizedString>
namespace SpectacleImage {
static const int SHADOW_RADIUS = 5;
}
class KSImageWidget : public QLabel
{
Q_OBJECT
public:
explicit KSImageWidget(QWidget *parent = 0);
......
......@@ -46,6 +46,10 @@
#include "ExportManager.h"
#include "SpectacleConfig.h"
static const int DEFAULT_WINDOW_HEIGHT = 420;
static const int DEFAULT_WINDOW_WIDTH = 840;
static const int MAXIMUM_WINDOW_WIDTH = 1000;
KSMainWindow::KSMainWindow(bool onClickAvailable, QWidget *parent) :
QDialog(parent),
mKSWidget(new KSWidget),
......@@ -186,12 +190,28 @@ void KSMainWindow::init()
if (!mOnClickAvailable) {
mKSWidget->disableOnClick();
}
resize(QSize(840, 420).expandedTo(minimumSize()));
resize(QSize(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT).expandedTo(minimumSize()));
// done with the init
}
int KSMainWindow::windowWidth(const QPixmap &pixmap) const
{
// Calculates what the width of the window should be for the captured image to perfectly fit
// the area reserved for the image, with the height already set.
const float pixmapAspectRatio = (float)pixmap.width() / pixmap.height();
const int imageHeight = mKSWidget->height() - 2 * layout()->spacing();
const int imageWidth = pixmapAspectRatio * imageHeight;
int alignedWindowWidth = qMin(mKSWidget->imagePaddingWidth() + imageWidth, MAXIMUM_WINDOW_WIDTH);
alignedWindowWidth += layout()->contentsMargins().left() + layout()->contentsMargins().right();
alignedWindowWidth += 2; // margins is removing 1 - 1 pixel for some reason
return alignedWindowWidth;
}
void KSMainWindow::buildSaveMenu()
{
// first clear the menu
......@@ -260,6 +280,8 @@ void KSMainWindow::setScreenshotAndShow(const QPixmap &pixmap)
setWindowModified(true);
show();
resize(QSize(windowWidth(pixmap), DEFAULT_WINDOW_HEIGHT));
}
void KSMainWindow::showPrintDialog()
......
......@@ -53,6 +53,7 @@ class KSMainWindow : public QDialog
void save();
void saveAs();
void saveAndExit();
int windowWidth(const QPixmap &pixmap) const;
public slots:
......
......@@ -142,13 +142,13 @@ KSWidget::KSWidget(QWidget *parent) :
mRightLayout->addLayout(mContentOptionsForm);
mRightLayout->addStretch(10);
mRightLayout->addWidget(mTakeScreenshotButton, 1, Qt::AlignHCenter);
mRightLayout->setContentsMargins(20, 0, 0, 10);
mRightLayout->setContentsMargins(10, 0, 0, 10);
mMainLayout = new QGridLayout(this);
mMainLayout->addWidget(mImageWidget, 0, 0, 1, 1);
mMainLayout->addLayout(mRightLayout, 0, 1, 1, 1);
mMainLayout->setColumnMinimumWidth(0, 400);
mMainLayout->setColumnMinimumWidth(1, 400);
mMainLayout->setColumnMinimumWidth(0, 320);
mMainLayout->setColumnMinimumWidth(1, 320);
// and read in the saved checkbox states and capture mode indices
......@@ -162,6 +162,22 @@ KSWidget::KSWidget(QWidget *parent) :
// done
}
int KSWidget::imagePaddingWidth() const
{
int rightLayoutLeft = 0;
int rightLayoutRight = 0;
int mainLayoutRight = 0;
mRightLayout->getContentsMargins(&rightLayoutLeft, nullptr, &rightLayoutRight, nullptr);
mMainLayout->getContentsMargins(nullptr, nullptr, &mainLayoutRight, nullptr);
int paddingWidth = (rightLayoutLeft + rightLayoutRight + mainLayoutRight);
paddingWidth += mRightLayout->contentsRect().width();
paddingWidth += 2 * SpectacleImage::SHADOW_RADIUS; // image drop shadow
return paddingWidth;
}
// public slots
void KSWidget::setScreenshotPixmap(const QPixmap &pixmap)
......
......@@ -42,9 +42,11 @@ class KSWidget : public QWidget
Q_OBJECT
public:
explicit KSWidget(QWidget *parent = 0);
int imagePaddingWidth() const;
signals:
void dragInitiated();
......@@ -62,7 +64,7 @@ class KSWidget : public QWidget
void captureModeChanged(int index);
private:
QGridLayout *mMainLayout;
QHBoxLayout *mDelayLayout;
QVBoxLayout *mRightLayout;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment