2011-10-30 15:07:14 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2011 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
|
|
|
// own
|
|
|
|
#include "declarative.h"
|
|
|
|
#include "tabboxhandler.h"
|
|
|
|
#include "clientmodel.h"
|
|
|
|
// Qt
|
|
|
|
#include <QtDeclarative/qdeclarative.h>
|
|
|
|
#include <QtDeclarative/QDeclarativeContext>
|
|
|
|
#include <QtDeclarative/QDeclarativeEngine>
|
|
|
|
#include <QtGui/QGraphicsObject>
|
|
|
|
#include <QtGui/QResizeEvent>
|
2011-12-15 16:47:23 +00:00
|
|
|
#include <QX11Info>
|
|
|
|
|
2011-10-30 15:07:14 +00:00
|
|
|
// include KDE
|
|
|
|
#include <KDE/KDebug>
|
|
|
|
#include <KDE/KIconEffect>
|
|
|
|
#include <KDE/KIconLoader>
|
2012-02-12 14:15:54 +00:00
|
|
|
#include <KDE/KServiceTypeTrader>
|
2011-10-30 15:07:14 +00:00
|
|
|
#include <KDE/KStandardDirs>
|
|
|
|
#include <KDE/Plasma/FrameSvg>
|
|
|
|
#include <KDE/Plasma/Theme>
|
|
|
|
#include <KDE/Plasma/WindowEffects>
|
|
|
|
#include <kdeclarative.h>
|
|
|
|
#include <kephal/screens.h>
|
2011-11-10 13:28:06 +00:00
|
|
|
// KWin
|
|
|
|
#include "thumbnailitem.h"
|
2011-12-01 07:48:18 +00:00
|
|
|
#include <kwindowsystem.h>
|
2011-10-30 15:07:14 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
namespace TabBox
|
|
|
|
{
|
|
|
|
|
|
|
|
ImageProvider::ImageProvider(QAbstractItemModel *model)
|
|
|
|
: QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap)
|
|
|
|
, m_model(model)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap ImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
QStringList parts = id.split('/');
|
|
|
|
const int row = parts.first().toInt(&ok);
|
|
|
|
if (!ok) {
|
|
|
|
return QDeclarativeImageProvider::requestPixmap(id, size, requestedSize);
|
|
|
|
}
|
|
|
|
const QModelIndex index = m_model->index(row, 0);
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return QDeclarativeImageProvider::requestPixmap(id, size, requestedSize);
|
|
|
|
}
|
2011-11-10 20:23:14 +00:00
|
|
|
if (index.model()->data(index, ClientModel::EmptyRole).toBool()) {
|
|
|
|
return QDeclarativeImageProvider::requestPixmap(id, size, requestedSize);
|
|
|
|
}
|
2011-10-30 15:07:14 +00:00
|
|
|
TabBoxClient* client = static_cast< TabBoxClient* >(index.model()->data(index, ClientModel::ClientRole).value<void *>());
|
2011-11-10 20:23:14 +00:00
|
|
|
if (!client) {
|
|
|
|
return QDeclarativeImageProvider::requestPixmap(id, size, requestedSize);
|
|
|
|
}
|
2011-10-30 15:07:14 +00:00
|
|
|
|
|
|
|
QSize s(32, 32);
|
|
|
|
if (requestedSize.isValid()) {
|
|
|
|
s = requestedSize;
|
|
|
|
}
|
|
|
|
*size = s;
|
|
|
|
QPixmap icon = client->icon(s);
|
2011-11-12 21:39:01 +00:00
|
|
|
if (s.width() > icon.width() || s.height() > icon.height()) {
|
|
|
|
// icon is smaller than what we requested - QML would scale it which looks bad
|
|
|
|
QPixmap temp(s);
|
|
|
|
temp.fill(Qt::transparent);
|
|
|
|
QPainter p(&temp);
|
|
|
|
p.drawPixmap(s.width()/2 - icon.width()/2, s.height()/2 - icon.height()/2, icon);
|
|
|
|
icon = temp;
|
|
|
|
}
|
2011-10-30 15:07:14 +00:00
|
|
|
if (parts.size() > 2) {
|
|
|
|
KIconEffect *effect = KIconLoader::global()->iconEffect();
|
|
|
|
KIconLoader::States state = KIconLoader::DefaultState;
|
|
|
|
if (parts.at(2) == QLatin1String("selected")) {
|
|
|
|
state = KIconLoader::ActiveState;
|
|
|
|
} else if (parts.at(2) == QLatin1String("disabled")) {
|
|
|
|
state = KIconLoader::DisabledState;
|
|
|
|
}
|
|
|
|
icon = effect->apply(icon, KIconLoader::Desktop, state);
|
|
|
|
}
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
2012-01-14 10:24:42 +00:00
|
|
|
DeclarativeView::DeclarativeView(QAbstractItemModel *model, TabBoxConfig::TabBoxMode mode, QWidget *parent)
|
2011-10-30 15:07:14 +00:00
|
|
|
: QDeclarativeView(parent)
|
|
|
|
, m_model(model)
|
2012-01-14 10:24:42 +00:00
|
|
|
, m_mode(mode)
|
2011-10-30 15:07:14 +00:00
|
|
|
, m_currentScreenGeometry()
|
|
|
|
, m_frame(new Plasma::FrameSvg(this))
|
|
|
|
, m_currentLayout()
|
2011-12-01 07:48:18 +00:00
|
|
|
, m_cachedWidth(0)
|
|
|
|
, m_cachedHeight(0)
|
2011-10-30 15:07:14 +00:00
|
|
|
{
|
|
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
setWindowFlags(Qt::X11BypassWindowManagerHint);
|
2011-12-01 07:48:18 +00:00
|
|
|
if (tabBox->embedded()) {
|
|
|
|
setResizeMode(QDeclarativeView::SizeRootObjectToView);
|
|
|
|
} else {
|
|
|
|
setResizeMode(QDeclarativeView::SizeViewToRootObject);
|
|
|
|
}
|
2011-10-30 15:07:14 +00:00
|
|
|
QPalette pal = palette();
|
|
|
|
pal.setColor(backgroundRole(), Qt::transparent);
|
|
|
|
setPalette(pal);
|
|
|
|
foreach (const QString &importPath, KGlobal::dirs()->findDirs("module", "imports")) {
|
|
|
|
engine()->addImportPath(importPath);
|
|
|
|
}
|
|
|
|
engine()->addImageProvider(QLatin1String("client"), new ImageProvider(model));
|
|
|
|
KDeclarative kdeclarative;
|
|
|
|
kdeclarative.setDeclarativeEngine(engine());
|
|
|
|
kdeclarative.initialize();
|
|
|
|
kdeclarative.setupBindings();
|
2011-11-10 13:28:06 +00:00
|
|
|
qmlRegisterType<ThumbnailItem>("org.kde.kwin", 0, 1, "ThumbnailItem");
|
|
|
|
rootContext()->setContextProperty("viewId", static_cast<qulonglong>(winId()));
|
2012-01-14 10:24:42 +00:00
|
|
|
if (m_mode == TabBoxConfig::ClientTabBox) {
|
|
|
|
rootContext()->setContextProperty("clientModel", model);
|
|
|
|
} else if (m_mode == TabBoxConfig::DesktopTabBox) {
|
|
|
|
rootContext()->setContextProperty("clientModel", model);
|
|
|
|
}
|
2011-11-04 19:08:23 +00:00
|
|
|
setSource(QUrl(KStandardDirs::locate("data", "kwin/tabbox/tabbox.qml")));
|
2011-10-30 15:07:14 +00:00
|
|
|
|
|
|
|
// FrameSvg
|
|
|
|
m_frame->setImagePath("dialogs/background");
|
|
|
|
m_frame->setCacheAllRenderedFrames(true);
|
|
|
|
m_frame->setEnabledBorders(Plasma::FrameSvg::AllBorders);
|
|
|
|
|
|
|
|
connect(tabBox, SIGNAL(configChanged()), SLOT(updateQmlSource()));
|
2012-01-14 10:24:42 +00:00
|
|
|
if (m_mode == TabBoxConfig::ClientTabBox) {
|
|
|
|
connect(tabBox, SIGNAL(embeddedChanged(bool)), SLOT(slotEmbeddedChanged(bool)));
|
|
|
|
}
|
2011-10-30 15:07:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarativeView::showEvent(QShowEvent *event)
|
|
|
|
{
|
2011-12-01 07:48:18 +00:00
|
|
|
if (tabBox->embedded()) {
|
|
|
|
connect(KWindowSystem::self(), SIGNAL(windowChanged(WId,uint)), SLOT(slotWindowChanged(WId, uint)));
|
|
|
|
}
|
2011-11-04 19:08:23 +00:00
|
|
|
updateQmlSource();
|
2011-10-30 15:07:14 +00:00
|
|
|
m_currentScreenGeometry = Kephal::ScreenUtils::screenGeometry(tabBox->activeScreen());
|
|
|
|
rootObject()->setProperty("screenWidth", m_currentScreenGeometry.width());
|
|
|
|
rootObject()->setProperty("screenHeight", m_currentScreenGeometry.height());
|
|
|
|
rootObject()->setProperty("allDesktops", tabBox->config().tabBoxMode() == TabBoxConfig::ClientTabBox &&
|
|
|
|
((tabBox->config().clientListMode() == TabBoxConfig::AllDesktopsClientList) ||
|
|
|
|
(tabBox->config().clientListMode() == TabBoxConfig::AllDesktopsApplicationList)));
|
2012-01-14 10:24:42 +00:00
|
|
|
if (ClientModel *clientModel = qobject_cast<ClientModel*>(m_model)) {
|
|
|
|
rootObject()->setProperty("longestCaption", clientModel->longestCaption());
|
|
|
|
}
|
2011-10-30 15:07:14 +00:00
|
|
|
|
|
|
|
if (QObject *item = rootObject()->findChild<QObject*>("listView")) {
|
|
|
|
item->setProperty("currentIndex", tabBox->first().row());
|
2011-10-31 13:34:37 +00:00
|
|
|
connect(item, SIGNAL(currentIndexChanged(int)), SLOT(currentIndexChanged(int)));
|
2011-10-30 15:07:14 +00:00
|
|
|
}
|
|
|
|
slotUpdateGeometry();
|
|
|
|
QGraphicsView::showEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarativeView::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
m_frame->resizeFrame(event->size());
|
2011-12-01 07:48:18 +00:00
|
|
|
if (Plasma::Theme::defaultTheme()->windowTranslucencyEnabled() && !tabBox->embedded()) {
|
2011-10-30 15:07:14 +00:00
|
|
|
// blur background
|
|
|
|
Plasma::WindowEffects::enableBlurBehind(winId(), true, m_frame->mask());
|
|
|
|
Plasma::WindowEffects::overrideShadow(winId(), true);
|
2011-12-01 07:48:18 +00:00
|
|
|
} else if (tabBox->embedded()) {
|
|
|
|
Plasma::WindowEffects::enableBlurBehind(winId(), false);
|
2011-10-30 15:07:14 +00:00
|
|
|
} else {
|
|
|
|
// do not trim to mask with compositing enabled, otherwise shadows are cropped
|
|
|
|
setMask(m_frame->mask());
|
|
|
|
}
|
|
|
|
QDeclarativeView::resizeEvent(event);
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:48:18 +00:00
|
|
|
void DeclarativeView::hideEvent(QHideEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::hideEvent(event);
|
|
|
|
if (tabBox->embedded()) {
|
|
|
|
disconnect(KWindowSystem::self(), SIGNAL(windowChanged(WId,uint)), this, SLOT(slotWindowChanged(WId,uint)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-15 16:47:23 +00:00
|
|
|
bool DeclarativeView::x11Event(XEvent *e)
|
|
|
|
{
|
|
|
|
if (tabBox->embedded() &&
|
|
|
|
(e->type == ButtonPress || e->type == ButtonRelease || e->type == MotionNotify)) {
|
|
|
|
XEvent ev;
|
|
|
|
|
|
|
|
memcpy(&ev, e, sizeof(ev));
|
|
|
|
if (e->type == ButtonPress || e->type == ButtonRelease) {
|
|
|
|
ev.xbutton.x += m_relativePos.x();
|
|
|
|
ev.xbutton.y += m_relativePos.y();
|
|
|
|
ev.xbutton.window = tabBox->embedded();
|
|
|
|
} else if (e->type == MotionNotify) {
|
|
|
|
ev.xmotion.x += m_relativePos.x();
|
|
|
|
ev.xmotion.y += m_relativePos.y();
|
|
|
|
ev.xmotion.window = tabBox->embedded();
|
|
|
|
}
|
|
|
|
|
|
|
|
XSendEvent( QX11Info::display(), tabBox->embedded(), False, NoEventMask, &ev );
|
|
|
|
}
|
|
|
|
return QDeclarativeView::x11Event(e);
|
|
|
|
}
|
|
|
|
|
2011-10-30 15:07:14 +00:00
|
|
|
void DeclarativeView::slotUpdateGeometry()
|
|
|
|
{
|
2011-12-01 07:48:18 +00:00
|
|
|
const WId embeddedId = tabBox->embedded();
|
|
|
|
if (embeddedId != 0) {
|
|
|
|
const KWindowInfo info = KWindowSystem::windowInfo(embeddedId, NET::WMGeometry);
|
|
|
|
const Qt::Alignment alignment = tabBox->embeddedAlignment();
|
|
|
|
const QPoint offset = tabBox->embeddedOffset();
|
|
|
|
int x = info.geometry().left();
|
|
|
|
int y = info.geometry().top();
|
|
|
|
int width = tabBox->embeddedSize().width();
|
|
|
|
int height = tabBox->embeddedSize().height();
|
|
|
|
if (alignment.testFlag(Qt::AlignLeft) || alignment.testFlag(Qt::AlignHCenter)) {
|
|
|
|
x += offset.x();
|
|
|
|
}
|
|
|
|
if (alignment.testFlag(Qt::AlignRight)) {
|
|
|
|
x = x + info.geometry().width() - offset.x() - width;
|
|
|
|
}
|
|
|
|
if (alignment.testFlag(Qt::AlignHCenter)) {
|
|
|
|
width = info.geometry().width() - 2 * offset.x();
|
|
|
|
}
|
|
|
|
if (alignment.testFlag(Qt::AlignTop) || alignment.testFlag(Qt::AlignVCenter)) {
|
|
|
|
y += offset.y();
|
|
|
|
}
|
|
|
|
if (alignment.testFlag(Qt::AlignBottom)) {
|
|
|
|
y = y + info.geometry().height() - offset.y() - height;
|
|
|
|
}
|
|
|
|
if (alignment.testFlag(Qt::AlignVCenter)) {
|
|
|
|
height = info.geometry().height() - 2 * offset.y();
|
|
|
|
}
|
|
|
|
setGeometry(QRect(x, y, width, height));
|
2011-12-15 16:47:23 +00:00
|
|
|
|
|
|
|
m_relativePos = QPoint(info.geometry().x(), info.geometry().x());
|
2011-12-01 07:48:18 +00:00
|
|
|
} else {
|
|
|
|
const int width = rootObject()->property("width").toInt();
|
|
|
|
const int height = rootObject()->property("height").toInt();
|
|
|
|
setGeometry(m_currentScreenGeometry.x() + static_cast<qreal>(m_currentScreenGeometry.width()) * 0.5 - static_cast<qreal>(width) * 0.5,
|
|
|
|
m_currentScreenGeometry.y() + static_cast<qreal>(m_currentScreenGeometry.height()) * 0.5 - static_cast<qreal>(height) * 0.5,
|
|
|
|
width, height);
|
2011-12-15 16:47:23 +00:00
|
|
|
m_relativePos = pos();
|
2011-12-01 07:48:18 +00:00
|
|
|
}
|
2011-10-30 15:07:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarativeView::setCurrentIndex(const QModelIndex &index)
|
|
|
|
{
|
2012-01-14 10:24:42 +00:00
|
|
|
if (tabBox->config().tabBoxMode() != m_mode) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-30 15:07:14 +00:00
|
|
|
if (QObject *item = rootObject()->findChild<QObject*>("listView")) {
|
|
|
|
item->setProperty("currentIndex", index.row());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 13:34:37 +00:00
|
|
|
void DeclarativeView::currentIndexChanged(int row)
|
|
|
|
{
|
|
|
|
tabBox->setCurrentIndex(m_model->index(row, 0));
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:48:18 +00:00
|
|
|
void DeclarativeView::updateQmlSource(bool force)
|
2011-10-30 15:07:14 +00:00
|
|
|
{
|
2012-01-14 10:24:42 +00:00
|
|
|
if (tabBox->config().tabBoxMode() != m_mode) {
|
|
|
|
return;
|
|
|
|
}
|
2011-12-01 07:48:18 +00:00
|
|
|
if (!force && tabBox->config().layoutName() == m_currentLayout) {
|
2011-10-30 15:07:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-01-14 10:24:42 +00:00
|
|
|
if (m_mode == TabBoxConfig::DesktopTabBox) {
|
2012-02-12 14:15:54 +00:00
|
|
|
m_currentLayout = tabBox->config().layoutName();
|
|
|
|
const QString file = KStandardDirs::locate("data", "kwin/tabbox/desktop.qml");
|
|
|
|
rootObject()->setProperty("source", QUrl(file));
|
|
|
|
return;
|
2012-01-14 10:24:42 +00:00
|
|
|
}
|
2012-02-12 14:15:54 +00:00
|
|
|
QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(tabBox->config().layoutName());
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("KWin/WindowSwitcher", constraint);
|
|
|
|
if (offers.isEmpty()) {
|
|
|
|
// load default
|
|
|
|
constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg("informative");
|
|
|
|
offers = KServiceTypeTrader::self()->query("KWin/WindowSwitcher", constraint);
|
|
|
|
if (offers.isEmpty()) {
|
|
|
|
kDebug(1212) << "could not find default window switcher layout";
|
|
|
|
return;
|
2012-01-14 10:24:42 +00:00
|
|
|
}
|
2011-10-30 15:07:14 +00:00
|
|
|
}
|
2012-02-12 14:15:54 +00:00
|
|
|
m_currentLayout = tabBox->config().layoutName();
|
|
|
|
KService::Ptr service = offers.first();
|
|
|
|
const QString pluginName = service->property("X-KDE-PluginInfo-Name").toString();
|
|
|
|
if (service->property("X-Plasma-API").toString() != "declarativeappletscript") {
|
|
|
|
kDebug(1212) << "Window Switcher Layout is no declarativeappletscript";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const QString scriptName = service->property("X-Plasma-MainScript").toString();
|
|
|
|
const QString file = KStandardDirs::locate("data", "kwin/tabbox/" + pluginName + "/contents/" + scriptName);
|
|
|
|
if (file.isNull()) {
|
|
|
|
kDebug(1212) << "Could not find QML file for window switcher";
|
|
|
|
return;
|
|
|
|
}
|
2011-10-30 15:07:14 +00:00
|
|
|
rootObject()->setProperty("source", QUrl(file));
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:48:18 +00:00
|
|
|
void DeclarativeView::slotEmbeddedChanged(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled) {
|
|
|
|
// cache the width
|
|
|
|
setResizeMode(QDeclarativeView::SizeRootObjectToView);
|
|
|
|
m_cachedWidth = rootObject()->property("width").toInt();
|
|
|
|
m_cachedHeight = rootObject()->property("height").toInt();
|
|
|
|
} else {
|
|
|
|
setResizeMode(QDeclarativeView::SizeViewToRootObject);
|
|
|
|
if (m_cachedWidth != 0 && m_cachedHeight != 0) {
|
|
|
|
rootObject()->setProperty("width", m_cachedWidth);
|
|
|
|
rootObject()->setProperty("height", m_cachedHeight);
|
|
|
|
}
|
|
|
|
updateQmlSource(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarativeView::slotWindowChanged(WId wId, unsigned int properties)
|
|
|
|
{
|
|
|
|
if (wId != tabBox->embedded()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (properties & NET::WMGeometry) {
|
|
|
|
slotUpdateGeometry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-30 15:07:14 +00:00
|
|
|
} // namespace TabBox
|
|
|
|
} // namespace KWin
|