2011-11-10 13:28:06 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
2014-01-03 15:54:07 +00:00
|
|
|
Copyright (C) 2011, 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2011-11-10 13:28:06 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "thumbnailitem.h"
|
|
|
|
// Qt
|
2018-06-05 10:52:57 +00:00
|
|
|
#include <QStandardPaths>
|
2014-01-03 13:10:17 +00:00
|
|
|
#include <QQuickWindow>
|
|
|
|
#include <QSGSimpleTextureNode>
|
2011-11-10 13:28:06 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
2014-01-03 15:54:07 +00:00
|
|
|
|
|
|
|
BrightnessSaturationShader::BrightnessSaturationShader()
|
|
|
|
: QSGMaterialShader()
|
|
|
|
, m_id_matrix(0)
|
|
|
|
, m_id_opacity(0)
|
|
|
|
, m_id_saturation(0)
|
|
|
|
, m_id_brightness(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *BrightnessSaturationShader::vertexShader() const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
"attribute highp vec4 vertex; \n"
|
|
|
|
"attribute highp vec2 texCoord; \n"
|
|
|
|
"uniform highp mat4 u_matrix; \n"
|
|
|
|
"varying highp vec2 v_coord; \n"
|
|
|
|
"void main() { \n"
|
|
|
|
" v_coord = texCoord; \n"
|
|
|
|
" gl_Position = u_matrix * vertex; \n"
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *BrightnessSaturationShader::fragmentShader() const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
"uniform sampler2D qt_Texture; \n"
|
|
|
|
"uniform lowp float u_opacity; \n"
|
|
|
|
"uniform highp float u_saturation; \n"
|
|
|
|
"uniform highp float u_brightness; \n"
|
|
|
|
"varying highp vec2 v_coord; \n"
|
|
|
|
"void main() { \n"
|
|
|
|
" lowp vec4 tex = texture2D(qt_Texture, v_coord); \n"
|
|
|
|
" if (u_saturation != 1.0) { \n"
|
|
|
|
" tex.rgb = mix(vec3(dot( vec3( 0.30, 0.59, 0.11 ), tex.rgb )), tex.rgb, u_saturation); \n"
|
|
|
|
" } \n"
|
|
|
|
" tex.rgb = tex.rgb * u_brightness; \n"
|
|
|
|
" gl_FragColor = tex * u_opacity; \n"
|
|
|
|
"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* const *BrightnessSaturationShader::attributeNames() const
|
|
|
|
{
|
|
|
|
static char const *const names[] = { "vertex", "texCoord", 0 };
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrightnessSaturationShader::updateState(const QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
|
|
|
|
{
|
|
|
|
Q_ASSERT(program()->isLinked());
|
|
|
|
if (state.isMatrixDirty())
|
|
|
|
program()->setUniformValue(m_id_matrix, state.combinedMatrix());
|
|
|
|
if (state.isOpacityDirty())
|
|
|
|
program()->setUniformValue(m_id_opacity, state.opacity());
|
|
|
|
|
|
|
|
auto *tx = static_cast<BrightnessSaturationMaterial *>(newMaterial);
|
|
|
|
auto *oldTx = static_cast<BrightnessSaturationMaterial *>(oldMaterial);
|
|
|
|
QSGTexture *t = tx->texture();
|
|
|
|
t->setFiltering(QSGTexture::Linear);
|
|
|
|
|
|
|
|
if (!oldTx || oldTx->texture()->textureId() != t->textureId())
|
|
|
|
t->bind();
|
|
|
|
else
|
|
|
|
t->updateBindOptions();
|
|
|
|
|
|
|
|
program()->setUniformValue(m_id_saturation, static_cast<float>(tx->saturation));
|
|
|
|
program()->setUniformValue(m_id_brightness, static_cast<float>(tx->brightness));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrightnessSaturationShader::initialize()
|
|
|
|
{
|
|
|
|
QSGMaterialShader::initialize();
|
|
|
|
m_id_matrix = program()->uniformLocation("u_matrix");
|
|
|
|
m_id_opacity = program()->uniformLocation("u_opacity");
|
|
|
|
m_id_saturation = program()->uniformLocation("u_saturation");
|
|
|
|
m_id_brightness = program()->uniformLocation("u_brightness");
|
|
|
|
}
|
|
|
|
|
2014-01-03 13:10:17 +00:00
|
|
|
WindowThumbnailItem::WindowThumbnailItem(QQuickItem* parent)
|
|
|
|
: QQuickItem(parent)
|
2011-11-10 13:28:06 +00:00
|
|
|
, m_wId(0)
|
|
|
|
, m_image()
|
2013-12-12 09:10:47 +00:00
|
|
|
, m_clipToItem(nullptr)
|
2014-01-03 15:54:07 +00:00
|
|
|
, m_brightness(1.0)
|
|
|
|
, m_saturation(1.0)
|
2011-11-10 13:28:06 +00:00
|
|
|
{
|
2014-01-03 13:10:17 +00:00
|
|
|
setFlag(ItemHasContents);
|
2011-11-10 13:28:06 +00:00
|
|
|
}
|
|
|
|
|
2012-03-29 13:46:53 +00:00
|
|
|
WindowThumbnailItem::~WindowThumbnailItem()
|
2011-11-10 13:28:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-03-29 13:46:53 +00:00
|
|
|
void WindowThumbnailItem::setWId(qulonglong wId)
|
2011-11-10 13:28:06 +00:00
|
|
|
{
|
|
|
|
m_wId = wId;
|
|
|
|
emit wIdChanged(wId);
|
|
|
|
findImage();
|
|
|
|
}
|
|
|
|
|
2013-12-12 09:10:47 +00:00
|
|
|
void WindowThumbnailItem::setClipTo(QQuickItem *clip)
|
|
|
|
{
|
|
|
|
if (m_clipToItem == clip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_clipToItem = clip;
|
|
|
|
emit clipToChanged();
|
|
|
|
}
|
|
|
|
|
2012-03-29 13:46:53 +00:00
|
|
|
void WindowThumbnailItem::findImage()
|
2011-11-10 13:28:06 +00:00
|
|
|
{
|
|
|
|
QString imagePath;
|
|
|
|
switch (m_wId) {
|
|
|
|
case Konqueror:
|
2013-08-03 20:34:58 +00:00
|
|
|
imagePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kwin/kcm_kwintabbox/konqueror.png");
|
2011-11-10 13:28:06 +00:00
|
|
|
break;
|
|
|
|
case Systemsettings:
|
2013-08-03 20:34:58 +00:00
|
|
|
imagePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kwin/kcm_kwintabbox/systemsettings.png");
|
2011-11-10 13:28:06 +00:00
|
|
|
break;
|
|
|
|
case KMail:
|
2013-08-03 20:34:58 +00:00
|
|
|
imagePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kwin/kcm_kwintabbox/kmail.png");
|
2011-11-10 13:28:06 +00:00
|
|
|
break;
|
|
|
|
case Dolphin:
|
2013-08-03 20:34:58 +00:00
|
|
|
imagePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kwin/kcm_kwintabbox/dolphin.png");
|
2011-11-10 13:28:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// ignore
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (imagePath.isNull()) {
|
|
|
|
m_image = QImage();
|
|
|
|
} else {
|
|
|
|
m_image = QImage(imagePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 13:10:17 +00:00
|
|
|
QSGNode *WindowThumbnailItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData)
|
2011-11-10 13:28:06 +00:00
|
|
|
{
|
2014-01-03 13:10:17 +00:00
|
|
|
Q_UNUSED(updatePaintNodeData)
|
2014-01-03 15:54:07 +00:00
|
|
|
QSGGeometryNode *node = static_cast<QSGGeometryNode*>(oldNode);
|
2014-01-03 13:10:17 +00:00
|
|
|
if (!node) {
|
2014-01-03 15:54:07 +00:00
|
|
|
node = new QSGGeometryNode();
|
|
|
|
auto *material = new BrightnessSaturationMaterial;
|
|
|
|
material->setFlag(QSGMaterial::Blending);
|
|
|
|
material->setTexture(window()->createTextureFromImage(m_image));
|
|
|
|
node->setMaterial(material);
|
|
|
|
QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4);
|
|
|
|
node->setGeometry(geometry);
|
2011-11-10 13:28:06 +00:00
|
|
|
}
|
2014-01-03 15:54:07 +00:00
|
|
|
auto *material = static_cast<BrightnessSaturationMaterial*>(node->material());
|
|
|
|
const QSize size(material->texture()->textureSize().scaled(boundingRect().size().toSize(), Qt::KeepAspectRatio));
|
2014-01-03 13:10:17 +00:00
|
|
|
const qreal x = boundingRect().x() + (boundingRect().width() - size.width())/2;
|
|
|
|
const qreal y = boundingRect().y() + (boundingRect().height() - size.height())/2;
|
2014-01-03 15:54:07 +00:00
|
|
|
QSGGeometry::updateTexturedRectGeometry(node->geometry(), QRectF(QPointF(x, y), size), QRectF(0.0, 0.0, 1.0, 1.0));
|
|
|
|
material->brightness = m_brightness;
|
|
|
|
material->saturation = m_saturation;
|
|
|
|
node->markDirty(QSGNode::DirtyGeometry | QSGNode::DirtyMaterial);
|
2014-01-03 13:10:17 +00:00
|
|
|
return node;
|
2011-11-10 13:28:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-03 15:54:07 +00:00
|
|
|
qreal WindowThumbnailItem::brightness() const
|
|
|
|
{
|
|
|
|
return m_brightness;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal WindowThumbnailItem::saturation() const
|
|
|
|
{
|
|
|
|
return m_saturation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowThumbnailItem::setBrightness(qreal brightness)
|
|
|
|
{
|
|
|
|
if (m_brightness == brightness) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_brightness = brightness;
|
|
|
|
update();
|
|
|
|
emit brightnessChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowThumbnailItem::setSaturation(qreal saturation)
|
|
|
|
{
|
|
|
|
if (m_saturation == saturation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_saturation = saturation;
|
|
|
|
update();
|
|
|
|
emit saturationChanged();
|
|
|
|
}
|
|
|
|
|
2011-11-10 13:28:06 +00:00
|
|
|
} // namespace KWin
|