kwin/kcmkwin/kwindecoration/preview.cpp

631 lines
15 KiB
C++
Raw Normal View History

/*
*
* Copyright (c) 2003 Lubos Lunak <l.lunak@kde.org>
* Copyright (c) 2013 Martin Gräßlin <mgraesslin@kde.org>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "preview.h"
#include <KLocalizedString>
#include <kconfig.h>
#include <QApplication>
#include <QPainter>
#include <QMouseEvent>
#include <kdecorationfactory.h>
static const int SMALL_OFFSET = 10;
static const int LARGE_OFFSET = 40;
PreviewItem::PreviewItem(QQuickItem *parent)
: QQuickPaintedItem(parent)
, m_plugins(new KDecorationPreviewPlugins(KSharedConfig::openConfig(QStringLiteral("kwinrc"))))
, m_activeBridge(new KDecorationPreviewBridge(this, true))
, m_inactiveBridge(new KDecorationPreviewBridge(this, false))
, m_activeDecoration(nullptr)
, m_inactiveDecoration(nullptr)
, m_activeEntered(nullptr)
, m_inactiveEntered(nullptr)
{
setFlag(ItemHasContents, true);
setAcceptHoverEvents(true);
connect(this, &PreviewItem::libraryChanged, this, &PreviewItem::loadDecorationPlugin);
}
PreviewItem::~PreviewItem()
{
delete m_activeDecoration;
delete m_inactiveDecoration;
}
void PreviewItem::setLibraryName(const QString &library)
{
if (library == m_libraryName || library.isEmpty()) {
return;
}
m_libraryName = library;
emit libraryChanged();
}
void PreviewItem::loadDecorationPlugin()
{
const bool loaded = m_plugins->loadPlugin(m_libraryName);
if (!loaded) {
return;
}
m_plugins->destroyPreviousPlugin();
connect(m_plugins->factory(), &KDecorationFactory::recreateDecorations,
this, &PreviewItem::recreateDecorations, Qt::QueuedConnection);
recreateDecorations();
}
void PreviewItem::recreateDecorations()
{
delete m_activeDecoration;
delete m_inactiveDecoration;
m_activeEntered = nullptr;
m_inactiveEntered = nullptr;
m_activeDecoration = m_plugins->createDecoration(m_activeBridge.data());
m_inactiveDecoration = m_plugins->createDecoration(m_inactiveBridge.data());
if (m_activeDecoration) {
m_activeDecoration->init();
if (m_activeDecoration->widget()) {
m_activeDecoration->widget()->installEventFilter(this);
}
}
if (m_inactiveDecoration) {
m_inactiveDecoration->init();
if (m_inactiveDecoration->widget()) {
m_inactiveDecoration->widget()->installEventFilter(this);
}
}
updatePreview();
}
void PreviewItem::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry)
{
QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
updatePreview();
}
void PreviewItem::updatePreview()
{
if (width() == 0 && height() == 0) {
return;
}
if (!m_activeDecoration && !m_inactiveDecoration) {
return;
}
const QSize size(width() - 50, height() - 50);
updateSize(size, m_activeDecoration, m_activeBuffer);
updateSize(size, m_inactiveDecoration, m_inactiveBuffer);
render(&m_activeBuffer, m_activeDecoration);
render(&m_inactiveBuffer, m_inactiveDecoration);
update();
}
void PreviewItem::updateSize(const QSize &baseSize, KDecoration *decoration, QImage &buffer)
{
if (!decoration) {
return;
}
int left, right, top, bottom;
left = right = top = bottom = 0;
decoration->padding(left, right, top, bottom);
const QSize size = baseSize + QSize(left + right, top + bottom);
if (decoration->geometry().size() != size) {
decoration->resize(size);
}
if (buffer.isNull() || buffer.size() != size) {
buffer = QImage(size, QImage::Format_ARGB32_Premultiplied);
}
}
void PreviewItem::render(QImage* image, KDecoration* decoration)
{
image->fill(Qt::transparent);
decoration->render(image, QRegion());
}
void PreviewItem::paint(QPainter *painter)
{
int paddingLeft, paddingRigth, paddingTop, paddingBottom;
paddingLeft = paddingRigth = paddingTop = paddingBottom = 0;
if (m_inactiveDecoration) {
m_inactiveDecoration->padding(paddingLeft, paddingRigth, paddingTop, paddingBottom);
}
painter->drawImage(LARGE_OFFSET - paddingLeft, SMALL_OFFSET - paddingTop, m_inactiveBuffer);
paddingLeft = paddingRigth = paddingTop = paddingBottom = 0;
if (m_activeDecoration) {
m_activeDecoration->padding(paddingLeft, paddingRigth, paddingTop, paddingBottom);
}
painter->drawImage(SMALL_OFFSET - paddingLeft, LARGE_OFFSET - paddingTop, m_activeBuffer);
}
void PreviewItem::hoverMoveEvent(QHoverEvent* event)
{
QQuickItem::hoverMoveEvent(event);
const int w = width() - LARGE_OFFSET - SMALL_OFFSET;
const int h = height() - LARGE_OFFSET - SMALL_OFFSET;
forwardMoveEvent(event, QRect(SMALL_OFFSET, LARGE_OFFSET, w, h), m_activeDecoration, &m_activeEntered);
forwardMoveEvent(event, QRect(LARGE_OFFSET, SMALL_OFFSET, w, h), m_inactiveDecoration, &m_inactiveEntered);
}
void PreviewItem::forwardMoveEvent(QHoverEvent *event, const QRect &geo, KDecoration *deco, QWidget **entered)
{
auto leaveEvent = [](QWidget **widget) {
if (!(*widget)) {
return;
}
// send old one a leave event
QEvent leave(QEvent::Leave);
QApplication::sendEvent(*widget, &leave);
*widget = nullptr;
};
if (geo.contains(event->pos()) && deco && deco->widget()) {
int paddingLeft, paddingRigth, paddingTop, paddingBottom;
paddingLeft = paddingRigth = paddingTop = paddingBottom = 0;
deco->padding(paddingLeft, paddingRigth, paddingTop, paddingBottom);
const QPoint widgetPos = event->pos() - geo.topLeft() + QPoint(paddingLeft, paddingTop);
if (QWidget *widget = deco->widget()->childAt(widgetPos)) {
if (widget != *entered) {
leaveEvent(entered);
// send enter event
*entered = widget;
QEnterEvent enter(widgetPos - widget->geometry().topLeft(), widgetPos, event->pos());
QApplication::sendEvent(*entered, &enter);
}
} else {
leaveEvent(entered);
}
} else {
leaveEvent(entered);
}
}
void PreviewItem::updateDecoration(KDecorationPreviewBridge *bridge)
{
if (bridge == m_activeBridge.data()) {
render(&m_activeBuffer, m_activeDecoration);
} else if (bridge == m_inactiveBridge.data()) {
render(&m_inactiveBuffer, m_inactiveDecoration);
}
update();
}
2011-01-30 14:34:42 +00:00
KDecorationPreview::KDecorationPreview(QWidget* parent)
: QWidget(parent)
{
bridge[Active] = new KDecorationPreviewBridge(this, true);
bridge[Inactive] = new KDecorationPreviewBridge(this, false);
deco[Active] = deco[Inactive] = NULL;
2011-01-30 14:34:42 +00:00
setMinimumSize(100, 100);
}
KDecorationPreview::~KDecorationPreview()
2011-01-30 14:34:42 +00:00
{
for (int i = 0; i < NumWindows; i++) {
delete deco[i];
delete bridge[i];
}
2011-01-30 14:34:42 +00:00
}
void KDecorationPreview::disablePreview()
2011-01-30 14:34:42 +00:00
{
delete deco[Active];
delete deco[Inactive];
deco[Active] = deco[Inactive] = NULL;
2011-01-30 14:34:42 +00:00
}
KDecorationFactory *KDecorationPreview::factory() const
{
return deco[Active] ? deco[Active]->factory() : 0;
}
2011-01-30 14:34:42 +00:00
QRect KDecorationPreview::windowGeometry(bool active) const
{
QWidget *widget = active ? deco[Active]->widget() : deco[Inactive]->widget();
return widget->geometry();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QRegion KDecorationPreview::unobscuredRegion(bool active, const QRegion& r) const
{
Q_UNUSED(active)
return r;
}
2011-01-30 14:34:42 +00:00
KDecorationPreviewBridge::KDecorationPreviewBridge(KDecorationPreview* p, bool a)
2014-02-03 10:37:04 +00:00
: preview(p)
, m_previewItem(nullptr)
, active(a)
{
}
KDecorationPreviewBridge::KDecorationPreviewBridge(PreviewItem *p, bool a)
: preview(nullptr)
, m_previewItem(p)
2014-02-03 10:37:04 +00:00
, active(a)
2011-01-30 14:34:42 +00:00
{
}
Qt::WindowFlags KDecorationPreviewBridge::initialWFlags() const
2011-01-30 14:34:42 +00:00
{
return 0;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isActive() const
2011-01-30 14:34:42 +00:00
{
return active;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isCloseable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isMaximizable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
KDecoration::MaximizeMode KDecorationPreviewBridge::maximizeMode() const
2011-01-30 14:34:42 +00:00
{
return KDecoration::MaximizeRestore;
2011-01-30 14:34:42 +00:00
}
KDecoration::QuickTileMode KDecorationPreviewBridge::quickTileMode() const
{
return KDecoration::QuickTileNone;
}
bool KDecorationPreviewBridge::isMinimizable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::providesContextHelp() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
int KDecorationPreviewBridge::desktop() const
2011-01-30 14:34:42 +00:00
{
return 1;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isOnAllDesktopsAvailable() const
{
return true;
}
bool KDecorationPreviewBridge::isModal() const
2011-01-30 14:34:42 +00:00
{
return false;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isShadeable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isShade() const
2011-01-30 14:34:42 +00:00
{
return false;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isSetShade() const
2011-01-30 14:34:42 +00:00
{
return false;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::keepAbove() const
2011-01-30 14:34:42 +00:00
{
return false;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::keepBelow() const
2011-01-30 14:34:42 +00:00
{
return false;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isMovable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isResizable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
NET::WindowType KDecorationPreviewBridge::windowType(unsigned long) const
{
return NET::Normal;
2011-01-30 14:34:42 +00:00
}
QIcon KDecorationPreviewBridge::icon() const
2011-01-30 14:34:42 +00:00
{
2013-10-09 06:38:00 +00:00
return QIcon::fromTheme(QStringLiteral("xorg"));
2011-01-30 14:34:42 +00:00
}
QString KDecorationPreviewBridge::caption() const
2011-01-30 14:34:42 +00:00
{
return active ? i18n("Active Window") : i18n("Inactive Window");
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::processMousePressEvent(QMouseEvent*)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::showWindowMenu(const QRect &)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::showWindowMenu(const QPoint &)
{
}
void KDecorationPreviewBridge::showApplicationMenu(const QPoint &)
{
}
bool KDecorationPreviewBridge::menuAvailable() const
{
return false;
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::performWindowOperation(WindowOperation)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::setMask(const QRegion& reg, int mode)
{
Q_UNUSED(reg)
Q_UNUSED(mode)
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isPreview() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
QRect KDecorationPreviewBridge::geometry() const
2011-01-30 14:34:42 +00:00
{
if (preview) {
return preview->windowGeometry(active);
}
return QRect();
2011-01-30 14:34:42 +00:00
}
QRect KDecorationPreviewBridge::iconGeometry() const
2011-01-30 14:34:42 +00:00
{
return QRect();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QRegion KDecorationPreviewBridge::unobscuredRegion(const QRegion& r) const
{
return preview->unobscuredRegion(active, r);
}
WId KDecorationPreviewBridge::windowId() const
2011-01-30 14:34:42 +00:00
{
return 0; // no decorated window
2011-01-30 14:34:42 +00:00
}
void KDecorationPreviewBridge::closeWindow()
2011-01-30 14:34:42 +00:00
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::maximize(MaximizeMode)
{
}
void KDecorationPreviewBridge::minimize()
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::showContextHelp()
2011-01-30 14:34:42 +00:00
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::setDesktop(int)
{
}
void KDecorationPreviewBridge::titlebarDblClickOperation()
2011-01-30 14:34:42 +00:00
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::titlebarMouseWheelOperation(int)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::setShade(bool)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::setKeepAbove(bool)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::setKeepBelow(bool)
{
}
int KDecorationPreviewBridge::currentDesktop() const
2011-01-30 14:34:42 +00:00
{
return 1;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::grabXServer(bool)
{
}
bool KDecorationPreviewBridge::compositingActive() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
QRect KDecorationPreviewBridge::transparentRect() const
2011-01-30 14:34:42 +00:00
{
return QRect();
2011-01-30 14:34:42 +00:00
}
// Window tabbing
int KDecorationPreviewBridge::tabCount() const
2011-01-30 14:34:42 +00:00
{
return 1;
}
QString KDecorationPreviewBridge::caption(int) const
{
return active ? "Active Window" : "Inactive Window";
2011-01-30 14:34:42 +00:00
}
QIcon KDecorationPreviewBridge::icon(int) const
2011-01-30 14:34:42 +00:00
{
return icon();
2011-01-30 14:34:42 +00:00
}
long KDecorationPreviewBridge::tabId(int) const
2011-01-30 14:34:42 +00:00
{
return 0;
2011-01-30 14:34:42 +00:00
}
long KDecorationPreviewBridge::currentTabId() const
2011-01-30 14:34:42 +00:00
{
return 0;
2011-01-30 14:34:42 +00:00
}
void KDecorationPreviewBridge::setCurrentTab(long)
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::tab_A_before_B(long, long)
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::tab_A_behind_B(long, long)
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::untab(long, const QRect&)
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::closeTab(long)
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::closeTabGroup()
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::showWindowMenu(const QPoint &, long)
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::update(const QRegion&)
{
if (m_previewItem) {
// call update
m_previewItem->updateDecoration(this);
}
}
2011-01-30 14:34:42 +00:00
KDecoration::WindowOperation KDecorationPreviewBridge::buttonToWindowOperation(Qt::MouseButtons)
{
return KDecoration::NoOp;
2011-01-30 14:34:42 +00:00
}
QPalette KDecorationPreviewBridge::palette() const
{
return QApplication::palette();
}
KDecorationPreviewOptions::KDecorationPreviewOptions()
2011-01-30 14:34:42 +00:00
{
customBorderSize = BordersCount; // invalid
customButtonsChanged = false; // invalid
customButtons = true;
customTitleButtonsLeft.clear(); // invalid
customTitleButtonsRight.clear(); // invalid
updateSettings();
2011-01-30 14:34:42 +00:00
}
KDecorationPreviewOptions::~KDecorationPreviewOptions()
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewOptions::updateSettings()
2011-01-30 14:34:42 +00:00
{
KConfig cfg("kwinrc");
KDecorationOptions::updateSettings(&cfg);
// set custom border size/buttons
if (customBorderSize != BordersCount)
2011-01-30 14:34:42 +00:00
setBorderSize(customBorderSize);
if (customButtonsChanged)
2011-01-30 14:34:42 +00:00
setCustomButtonPositions(customButtons);
if (customButtons) {
if (!customTitleButtonsLeft.isEmpty())
2011-01-30 14:34:42 +00:00
setTitleButtonsLeft(customTitleButtonsLeft);
if (!customTitleButtonsRight.isEmpty())
2011-01-30 14:34:42 +00:00
setTitleButtonsRight(customTitleButtonsRight);
} else {
2011-01-30 14:34:42 +00:00
setTitleButtonsLeft(KDecorationOptions::defaultTitleButtonsLeft());
setTitleButtonsRight(KDecorationOptions::defaultTitleButtonsRight());
}
2011-01-30 14:34:42 +00:00
}
void KDecorationPreviewOptions::setCustomBorderSize(BorderSize size)
2011-01-30 14:34:42 +00:00
{
customBorderSize = size;
updateSettings();
2011-01-30 14:34:42 +00:00
}
void KDecorationPreviewOptions::setCustomTitleButtonsEnabled(bool enabled)
{
customButtonsChanged = true;
customButtons = enabled;
updateSettings();
}
void KDecorationPreviewOptions::setCustomTitleButtons(const QList<DecorationButton> &left, const QList<DecorationButton> &right)
2011-01-30 14:34:42 +00:00
{
customTitleButtonsLeft = left;
customTitleButtonsRight = right;
updateSettings();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
bool KDecorationPreviewPlugins::provides(Requirement)
{
return false;
2011-01-30 14:34:42 +00:00
}
#include "preview.moc"