2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2009-09-13 11:36:45 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2009, 2011 Martin Gräßlin <mgraesslin@kde.org>
|
2009-09-13 11:36:45 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2009-09-13 11:36:45 +00:00
|
|
|
// own
|
2012-04-09 16:59:26 +00:00
|
|
|
#include "layoutpreview.h"
|
2011-11-10 13:28:06 +00:00
|
|
|
#include "thumbnailitem.h"
|
2013-12-12 10:13:11 +00:00
|
|
|
#include <QApplication>
|
2014-01-03 10:24:23 +00:00
|
|
|
#include <QDebug>
|
2013-12-12 10:13:11 +00:00
|
|
|
#include <QQmlEngine>
|
2013-10-01 05:15:11 +00:00
|
|
|
#include <QQmlContext>
|
2019-07-10 13:26:49 +00:00
|
|
|
#include <QScreen>
|
2018-06-05 10:52:57 +00:00
|
|
|
#include <QStandardPaths>
|
2014-03-17 15:24:10 +00:00
|
|
|
#include <KConfigGroup>
|
|
|
|
#include <KDesktopFile>
|
|
|
|
#include <KLocalizedString>
|
2020-08-11 22:24:23 +00:00
|
|
|
#include <KApplicationTrader>
|
2009-09-13 11:36:45 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
namespace TabBox
|
|
|
|
{
|
|
|
|
|
2013-12-12 10:13:11 +00:00
|
|
|
LayoutPreview::LayoutPreview(const QString &path, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_item(nullptr)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2013-12-12 10:13:11 +00:00
|
|
|
QQmlEngine *engine = new QQmlEngine(this);
|
|
|
|
QQmlComponent *component = new QQmlComponent(engine, this);
|
|
|
|
qmlRegisterType<WindowThumbnailItem>("org.kde.kwin", 2, 0, "ThumbnailItem");
|
|
|
|
qmlRegisterType<SwitcherItem>("org.kde.kwin", 2, 0, "Switcher");
|
2013-12-16 10:48:41 +00:00
|
|
|
qmlRegisterType<QAbstractItemModel>();
|
2013-12-12 10:13:11 +00:00
|
|
|
component->loadUrl(QUrl::fromLocalFile(path));
|
2014-01-03 10:24:23 +00:00
|
|
|
if (component->isError()) {
|
|
|
|
qDebug() << component->errorString();
|
|
|
|
}
|
2013-12-12 10:13:11 +00:00
|
|
|
QObject *item = component->create();
|
|
|
|
auto findSwitcher = [item]() -> SwitcherItem* {
|
|
|
|
if (!item) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (SwitcherItem *i = qobject_cast<SwitcherItem*>(item)) {
|
|
|
|
return i;
|
|
|
|
} else if (QQuickWindow *w = qobject_cast<QQuickWindow*>(item)) {
|
|
|
|
return w->contentItem()->findChild<SwitcherItem*>();
|
|
|
|
}
|
|
|
|
return item->findChild<SwitcherItem*>();
|
|
|
|
};
|
|
|
|
if (SwitcherItem *switcher = findSwitcher()) {
|
|
|
|
m_item = switcher;
|
|
|
|
switcher->setVisible(true);
|
|
|
|
}
|
|
|
|
auto findWindow = [item]() -> QQuickWindow* {
|
|
|
|
if (!item) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (QQuickWindow *w = qobject_cast<QQuickWindow*>(item)) {
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
return item->findChild<QQuickWindow*>();
|
|
|
|
};
|
|
|
|
if (QQuickWindow *w = findWindow()) {
|
|
|
|
w->setKeyboardGrabEnabled(true);
|
|
|
|
w->setMouseGrabEnabled(true);
|
|
|
|
w->installEventFilter(this);
|
2011-10-31 07:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-13 11:36:45 +00:00
|
|
|
|
2012-04-09 16:59:26 +00:00
|
|
|
LayoutPreview::~LayoutPreview()
|
2011-10-31 07:41:07 +00:00
|
|
|
{
|
|
|
|
}
|
2009-09-13 11:36:45 +00:00
|
|
|
|
2013-12-12 10:13:11 +00:00
|
|
|
bool LayoutPreview::eventFilter(QObject *object, QEvent *event)
|
2011-10-31 07:41:07 +00:00
|
|
|
{
|
2013-12-12 10:13:11 +00:00
|
|
|
if (event->type() == QEvent::KeyPress) {
|
2015-03-25 07:18:12 +00:00
|
|
|
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
|
2013-12-12 10:13:11 +00:00
|
|
|
if (keyEvent->key() == Qt::Key_Escape ||
|
|
|
|
keyEvent->key() == Qt::Key_Return ||
|
|
|
|
keyEvent->key() == Qt::Key_Enter ||
|
|
|
|
keyEvent->key() == Qt::Key_Space) {
|
|
|
|
object->deleteLater();
|
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
if (m_item && keyEvent->key() == Qt::Key_Tab) {
|
|
|
|
m_item->incrementIndex();
|
|
|
|
}
|
|
|
|
if (m_item && keyEvent->key() == Qt::Key_Backtab) {
|
|
|
|
m_item->decrementIndex();
|
|
|
|
}
|
|
|
|
} else if (event->type() == QEvent::MouseButtonPress) {
|
|
|
|
if (QWindow *w = qobject_cast<QWindow*>(object)) {
|
|
|
|
if (!w->geometry().contains(static_cast<QMouseEvent*>(event)->globalPos())) {
|
|
|
|
object->deleteLater();
|
|
|
|
deleteLater();
|
|
|
|
}
|
2011-10-31 07:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-12 10:13:11 +00:00
|
|
|
return QObject::eventFilter(object, event);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-09-13 11:36:45 +00:00
|
|
|
|
2011-10-31 07:41:07 +00:00
|
|
|
ExampleClientModel::ExampleClientModel (QObject* parent)
|
|
|
|
: QAbstractListModel (parent)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-10-31 07:41:07 +00:00
|
|
|
init();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-09-13 11:36:45 +00:00
|
|
|
|
2011-10-31 07:41:07 +00:00
|
|
|
ExampleClientModel::~ExampleClientModel()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
}
|
2009-09-13 11:36:45 +00:00
|
|
|
|
2011-10-31 07:41:07 +00:00
|
|
|
void ExampleClientModel::init()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2020-08-11 22:24:23 +00:00
|
|
|
if (const auto s = KApplicationTrader::preferredService(QStringLiteral("inode/directory"))) {
|
2015-01-20 14:16:38 +00:00
|
|
|
m_services << s;
|
|
|
|
m_fileManager = s;
|
|
|
|
}
|
2020-08-11 22:24:23 +00:00
|
|
|
if (const auto s = KApplicationTrader::preferredService(QStringLiteral("text/html"))) {
|
2015-01-20 14:16:38 +00:00
|
|
|
m_services << s;
|
|
|
|
m_browser = s;
|
|
|
|
}
|
2020-08-11 22:24:23 +00:00
|
|
|
if (const auto s = KApplicationTrader::preferredService(QStringLiteral("message/rfc822"))) {
|
2015-01-20 14:16:38 +00:00
|
|
|
m_services << s;
|
|
|
|
m_email = s;
|
|
|
|
}
|
|
|
|
if (const auto s = KService::serviceByDesktopName(QStringLiteral("kdesystemsettings"))) {
|
|
|
|
m_services << s;
|
|
|
|
m_systemSettings = s;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-10-31 07:41:07 +00:00
|
|
|
}
|
2009-09-13 11:36:45 +00:00
|
|
|
|
2011-10-31 07:41:07 +00:00
|
|
|
QVariant ExampleClientModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
2019-11-22 10:39:01 +00:00
|
|
|
case CaptionRole:
|
2015-01-20 14:16:38 +00:00
|
|
|
return m_services.at(index.row())->name();
|
2019-11-22 10:39:01 +00:00
|
|
|
case MinimizedRole:
|
2011-10-31 07:41:07 +00:00
|
|
|
return false;
|
2019-11-22 10:39:01 +00:00
|
|
|
case DesktopNameRole:
|
2011-10-31 07:41:07 +00:00
|
|
|
return i18nc("An example Desktop Name", "Desktop 1");
|
2019-11-22 10:39:01 +00:00
|
|
|
case IconRole:
|
2015-01-20 14:16:38 +00:00
|
|
|
return m_services.at(index.row())->icon();
|
2019-11-22 10:39:01 +00:00
|
|
|
case WindowIdRole:
|
2015-01-20 14:16:38 +00:00
|
|
|
const auto s = m_services.at(index.row());
|
|
|
|
if (s == m_browser) {
|
2012-03-29 13:46:53 +00:00
|
|
|
return WindowThumbnailItem::Konqueror;
|
2015-01-20 14:16:38 +00:00
|
|
|
} else if (s == m_email) {
|
2012-03-29 13:46:53 +00:00
|
|
|
return WindowThumbnailItem::KMail;
|
2015-01-20 14:16:38 +00:00
|
|
|
} else if (s == m_systemSettings) {
|
2012-03-29 13:46:53 +00:00
|
|
|
return WindowThumbnailItem::Systemsettings;
|
2015-01-20 14:16:38 +00:00
|
|
|
} else if (s == m_fileManager) {
|
2012-03-29 13:46:53 +00:00
|
|
|
return WindowThumbnailItem::Dolphin;
|
2011-11-10 13:28:06 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2011-10-31 07:41:07 +00:00
|
|
|
}
|
|
|
|
return QVariant();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
|
2013-12-22 11:01:05 +00:00
|
|
|
QString ExampleClientModel::longestCaption() const
|
|
|
|
{
|
|
|
|
QString caption;
|
2015-01-20 14:16:38 +00:00
|
|
|
for (const auto item : m_services) {
|
|
|
|
const QString name = item->name();
|
2013-12-22 11:01:05 +00:00
|
|
|
if (name.size() > caption.size()) {
|
|
|
|
caption = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return caption;
|
|
|
|
}
|
|
|
|
|
2011-10-31 07:41:07 +00:00
|
|
|
int ExampleClientModel::rowCount(const QModelIndex &parent) const
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-10-31 07:41:07 +00:00
|
|
|
Q_UNUSED(parent)
|
2015-01-20 14:16:38 +00:00
|
|
|
return m_services.size();
|
2011-10-31 07:41:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 21:56:00 +00:00
|
|
|
QHash<int, QByteArray> ExampleClientModel::roleNames() const
|
|
|
|
{
|
|
|
|
return {
|
2019-11-22 10:39:01 +00:00
|
|
|
{ CaptionRole, QByteArrayLiteral("caption") },
|
|
|
|
{ MinimizedRole, QByteArrayLiteral("minimized") },
|
|
|
|
{ DesktopNameRole, QByteArrayLiteral("desktopName") },
|
|
|
|
{ IconRole, QByteArrayLiteral("icon") },
|
|
|
|
{ WindowIdRole, QByteArrayLiteral("windowId") },
|
2019-07-09 21:56:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-12-12 10:13:11 +00:00
|
|
|
SwitcherItem::SwitcherItem(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_model(new ExampleClientModel(this))
|
|
|
|
, m_item(nullptr)
|
|
|
|
, m_currentIndex(0)
|
|
|
|
, m_visible(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SwitcherItem::~SwitcherItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitcherItem::setVisible(bool visible)
|
|
|
|
{
|
|
|
|
if (m_visible == visible) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_visible = visible;
|
|
|
|
emit visibleChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitcherItem::setItem(QObject *item)
|
|
|
|
{
|
|
|
|
m_item = item;
|
|
|
|
emit itemChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitcherItem::setCurrentIndex(int index)
|
|
|
|
{
|
|
|
|
if (m_currentIndex == index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_currentIndex = index;
|
|
|
|
emit currentIndexChanged(m_currentIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect SwitcherItem::screenGeometry() const
|
|
|
|
{
|
2019-07-10 13:26:49 +00:00
|
|
|
const QScreen *primaryScreen = qApp->primaryScreen();
|
|
|
|
return primaryScreen->geometry();
|
2013-12-12 10:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SwitcherItem::incrementIndex()
|
|
|
|
{
|
|
|
|
setCurrentIndex((m_currentIndex + 1) % m_model->rowCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitcherItem::decrementIndex()
|
|
|
|
{
|
|
|
|
int index = m_currentIndex -1;
|
|
|
|
if (index < 0) {
|
|
|
|
index = m_model->rowCount() -1;
|
|
|
|
}
|
|
|
|
setCurrentIndex(index);
|
|
|
|
}
|
|
|
|
|
2009-09-13 11:36:45 +00:00
|
|
|
} // namespace KWin
|
|
|
|
} // namespace TabBox
|
|
|
|
|