kwin/kcmkwin/kwindecoration/preview.cpp

530 lines
12 KiB
C++
Raw Normal View History

/*
*
* Copyright (c) 2003 Lubos Lunak <l.lunak@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 <kapplication.h>
#include <klocale.h>
#include <kconfig.h>
#include <kglobal.h>
#include <QLabel>
#include <QStyle>
#include <QPainter>
#include <QMouseEvent>
#include <QVector>
#include <kicon.h>
#include <kdecorationfactory.h>
2011-01-30 14:34:42 +00:00
KDecorationPreview::KDecorationPreview(QWidget* parent)
: QWidget(parent)
{
options = new KDecorationPreviewOptions;
2011-01-30 14:34:42 +00:00
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
delete options;
}
2011-01-30 14:34:42 +00:00
bool KDecorationPreview::recreateDecoration(KDecorationPlugins* plugins)
{
for (int i = 0; i < NumWindows; i++) {
delete deco[i]; // deletes also window
2011-01-30 14:34:42 +00:00
deco[i] = plugins->createDecoration(bridge[i]);
deco[i]->init();
2011-01-30 14:34:42 +00:00
}
m_activeMask = QRegion();
m_inactiveMask = QRegion();
2011-01-30 14:34:42 +00:00
if (deco[Active] == NULL || deco[Inactive] == NULL) {
return false;
2011-01-30 14:34:42 +00:00
}
return true;
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;
}
QPixmap KDecorationPreview::preview()
2011-01-30 14:34:42 +00:00
{
QPixmap pixmap(size());
pixmap.fill(Qt::transparent);
if (!deco[Active] || !deco[Inactive])
return pixmap;
int titleBarHeight, leftBorder, rightBorder, xoffset,
dummy1, dummy2, dummy3;
// don't have more than one reference to the same dummy variable in one borders() call.
2011-01-30 14:34:42 +00:00
deco[Active]->borders(dummy1, dummy2, titleBarHeight, dummy3);
deco[Inactive]->borders(leftBorder, rightBorder, dummy1, dummy2);
2011-01-30 14:34:42 +00:00
titleBarHeight = qMin(int(titleBarHeight * .9), 30);
xoffset = qMin(qMax(10, QApplication::isRightToLeft()
? leftBorder : rightBorder), 30);
QPainter p;
p.begin(&pixmap);
const QSize size(width() - xoffset - 20, height() - titleBarHeight - 20);
render(&p, deco[Inactive], size, QPoint(10 + xoffset, 10), m_inactiveMask);
render(&p, deco[Active], size, QPoint(10, 10 + titleBarHeight), m_activeMask);
p.end();
return pixmap;
2011-01-30 14:34:42 +00:00
}
void KDecorationPreview::render(QPainter *painter, KDecoration *decoration, const QSize &recommendedSize, const QPoint &offset, const QRegion &mask) const
2011-01-30 14:34:42 +00:00
{
QWidget *w = decoration->widget();
QSize size = QSize(recommendedSize)
.expandedTo(decoration->minimumSize());
int padLeft, padRight, padTop, padBottom;
padLeft = padRight = padTop = padBottom = 0;
bool useMask = true;
if (KDecorationUnstable *unstable = qobject_cast<KDecorationUnstable *>(decoration)) {
unstable->padding(padLeft, padRight, padTop, padBottom);
size.setWidth(size.width() + padLeft + padRight);
size.setHeight(size.height() + padTop + padBottom);
if (padLeft || padRight || padTop || padBottom) {
useMask = false;
}
}
decoration->resize(size);
// why an if-else block instead of (useMask ? mask : QRegion())?
// For what reason ever it completely breaks if the mask is copied.
if (useMask) {
w->render(painter, offset + QPoint(-padLeft, - padTop), mask,
QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask);
2011-01-30 14:34:42 +00:00
} else {
w->render(painter, offset + QPoint(-padLeft, - padTop), QRegion(),
QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask);
}
2011-01-30 14:34:42 +00:00
}
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
}
void KDecorationPreview::setTempBorderSize(KDecorationPlugins* plugin, KDecorationDefines::BorderSize size)
2011-01-30 14:34:42 +00:00
{
options->setCustomBorderSize(size);
2011-01-30 14:34:42 +00:00
if (plugin->factory()->reset(KDecorationDefines::SettingBorder)) {
// can't handle the change, recreate decorations then
recreateDecoration(plugin);
}
2011-01-30 14:34:42 +00:00
}
void KDecorationPreview::setTempButtons(KDecorationPlugins* plugin, bool customEnabled, const QString &left, const QString &right)
2011-01-30 14:34:42 +00:00
{
options->setCustomTitleButtonsEnabled(customEnabled);
options->setCustomTitleButtons(left, right);
2011-01-30 14:34:42 +00:00
if (plugin->factory()->reset(KDecorationDefines::SettingButtons)) {
// can't handle the change, recreate decorations then
recreateDecoration(plugin);
}
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;
}
void KDecorationPreview::setMask(const QRegion &region, bool active)
{
if (active) {
m_activeMask = region;
} else {
m_inactiveMask = region;
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
KDecorationPreviewBridge::KDecorationPreviewBridge(KDecorationPreview* p, bool a)
: preview(p), active(a)
{
}
QWidget* KDecorationPreviewBridge::initialParentWidget() const
2011-01-30 14:34:42 +00:00
{
return preview;
2011-01-30 14:34:42 +00:00
}
Qt::WFlags 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
}
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::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
{
return KIcon("xorg");
}
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(mode)
preview->setMask(reg, active);
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
{
return preview->windowGeometry(active);
}
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
{
}
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
}
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
{
}
unsigned long KDecorationPreviewOptions::updateSettings()
2011-01-30 14:34:42 +00:00
{
KConfig cfg("kwinrc");
unsigned long changed = 0;
2011-01-30 14:34:42 +00:00
changed |= 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) {
2011-01-30 14:34:42 +00:00
if (!customTitleButtonsLeft.isNull())
setTitleButtonsLeft(customTitleButtonsLeft);
if (!customTitleButtonsRight.isNull())
setTitleButtonsRight(customTitleButtonsRight);
} else {
2011-01-30 14:34:42 +00:00
setTitleButtonsLeft(KDecorationOptions::defaultTitleButtonsLeft());
setTitleButtonsRight(KDecorationOptions::defaultTitleButtonsRight());
}
return changed;
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 QString &left, const QString &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"