Commit b839987f authored by Riccardo Degli Esposti's avatar Riccardo Degli Esposti 🍌 Committed by Tomaz Canabrava
Browse files

Added wallpaper scaling

parent 23b84ae3
......@@ -187,7 +187,7 @@ ColorScheme::ColorScheme()
, _colorRandomization(false)
, _wallpaper(nullptr)
{
setWallpaper(QString());
setWallpaper(QString(), ColorSchemeWallpaper::Tile);
}
ColorScheme::ColorScheme(const ColorScheme &other)
......@@ -464,7 +464,8 @@ void ColorScheme::read(const KConfig &config)
_description = i18n(schemeDescription.toUtf8().constData());
setOpacity(configGroup.readEntry("Opacity", 1.0));
_blur = configGroup.readEntry("Blur", false);
setWallpaper(configGroup.readEntry("Wallpaper", QString()));
setWallpaper(configGroup.readEntry("Wallpaper", QString()),
static_cast<ColorSchemeWallpaper::FillStyle>(configGroup.readEntry("FillStyle", 0)));
_colorRandomization = configGroup.readEntry(EnableColorRandomizationKey, false);
for (int i = 0; i < TABLE_COLORS; i++) {
......@@ -525,6 +526,7 @@ void ColorScheme::write(KConfig &config) const
configGroup.writeEntry("Opacity", _opacity);
configGroup.writeEntry("Blur", _blur);
configGroup.writeEntry("Wallpaper", _wallpaper->path());
configGroup.writeEntry("FillStyle", (int) _wallpaper->style());
configGroup.writeEntry(EnableColorRandomizationKey, _colorRandomization);
for (int i = 0; i < TABLE_COLORS; i++) {
......@@ -572,9 +574,9 @@ void ColorScheme::writeColorEntry(KConfig &config, int index) const
checkAndMaybeSaveValue(RandomLightnessRangeKey, random.lightness);
}
void ColorScheme::setWallpaper(const QString &path)
void ColorScheme::setWallpaper(const QString &path, ColorSchemeWallpaper::FillStyle style)
{
_wallpaper = new ColorSchemeWallpaper(path);
_wallpaper = new ColorSchemeWallpaper(path, style);
}
ColorSchemeWallpaper::Ptr ColorScheme::wallpaper() const
......
......@@ -133,7 +133,7 @@ public:
*/
bool blur() const;
void setWallpaper(const QString &path);
void setWallpaper(const QString &path, ColorSchemeWallpaper::FillStyle style);
ColorSchemeWallpaper::Ptr wallpaper() const;
......
......@@ -187,12 +187,12 @@ void ColorSchemeEditor::selectWallpaper()
void ColorSchemeEditor::wallpaperPathChanged(const QString &path)
{
if (path.isEmpty()) {
_colors->setWallpaper(path);
_colors->setWallpaper(path, _colors->wallpaper()->style());
} else {
QFileInfo i(path);
if (i.exists() && i.isFile() && i.isReadable()) {
_colors->setWallpaper(path);
_colors->setWallpaper(path, _colors->wallpaper()->style());
}
}
}
......
......@@ -17,9 +17,10 @@
using namespace Konsole;
ColorSchemeWallpaper::ColorSchemeWallpaper(const QString &path)
ColorSchemeWallpaper::ColorSchemeWallpaper(const QString &path, ColorSchemeWallpaper::FillStyle style)
: _path(path)
, _picture(nullptr)
, _style(style)
{
}
......@@ -53,7 +54,12 @@ bool ColorSchemeWallpaper::draw(QPainter &painter, const QRect rect, qreal opaci
}
if (qFuzzyCompare(qreal(1.0), opacity)) {
painter.drawTiledPixmap(rect, *_picture, rect.topLeft());
if (_style == Tile) {
painter.drawTiledPixmap(rect, *_picture, rect.topLeft());
} else {
QRectF srcRect = ScaledRect(painter.viewport().size(), _picture->size(), rect);
painter.drawPixmap(rect, *_picture, srcRect);
}
return true;
}
......@@ -61,8 +67,16 @@ bool ColorSchemeWallpaper::draw(QPainter &painter, const QRect rect, qreal opaci
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.fillRect(rect, QColor(0, 0, 0, 0));
painter.setOpacity(opacity);
painter.drawTiledPixmap(rect, *_picture, rect.topLeft());
if (_style == Tile) {
painter.drawTiledPixmap(rect, *_picture, rect.topLeft());
} else {
QRectF srcRect = ScaledRect(painter.viewport().size(), _picture->size(), rect);
painter.drawPixmap(rect, *_picture, srcRect);
}
painter.restore();
return true;
}
......@@ -70,3 +84,42 @@ QString ColorSchemeWallpaper::path() const
{
return _path;
}
ColorSchemeWallpaper::FillStyle ColorSchemeWallpaper::style() const
{
return _style;
}
QRectF ColorSchemeWallpaper::ScaledRect(const QSize viewportSize, const QSize pictureSize, const QRect rect)
{
QRectF scaledRect = QRectF();
QSize scaledSize = pictureSize.scaled(viewportSize, RatioMode());
double scaleX = pictureSize.width() / static_cast<double>(scaledSize.width());
double scaleY = pictureSize.height() / static_cast<double>(scaledSize.height());
double offsetX = (scaledSize.width() - viewportSize.width()) / 2.0;
double offsetY = (scaledSize.height() - viewportSize.height()) / 2.0;
scaledRect.setX((rect.x() + offsetX) * scaleX);
scaledRect.setY((rect.y() + offsetY) * scaleY);
scaledRect.setWidth(rect.width() * scaleX);
scaledRect.setHeight(rect.height() * scaleY);
return scaledRect;
}
Qt::AspectRatioMode ColorSchemeWallpaper::RatioMode()
{
switch (_style) {
case Center:
return Qt::KeepAspectRatioByExpanding;
case Adapt:
return Qt::KeepAspectRatio;
case Tile:
case Stretch:
default:
return Qt::IgnoreAspectRatio;
}
}
......@@ -30,9 +30,17 @@ namespace Konsole
class ColorSchemeWallpaper : public QSharedData
{
public:
enum FillStyle
{
Tile = 0,
Stretch,
Center,
Adapt
};
typedef QExplicitlySharedDataPointer<ColorSchemeWallpaper> Ptr;
explicit ColorSchemeWallpaper(const QString &path);
explicit ColorSchemeWallpaper(const QString &path, FillStyle style);
~ColorSchemeWallpaper();
void load();
......@@ -44,11 +52,17 @@ public:
QString path() const;
FillStyle style() const;
private:
Q_DISABLE_COPY(ColorSchemeWallpaper)
QString _path;
std::unique_ptr<QPixmap> _picture;
FillStyle _style;
QRectF ScaledRect(const QSize viewportSize, const QSize pictureSize, const QRect rect);
Qt::AspectRatioMode RatioMode();
};
}
......
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