2009-09-13 11:36:45 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
2011-10-31 07:41:07 +00:00
|
|
|
Copyright (C) 2009, 2011 Martin Gräßlin <mgraesslin@kde.org>
|
2009-09-13 11:36:45 +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/>.
|
|
|
|
*********************************************************************/
|
|
|
|
// 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 <QDesktopWidget>
|
|
|
|
#include <QQmlEngine>
|
2013-10-01 05:15:11 +00:00
|
|
|
#include <QQmlContext>
|
2013-08-03 20:34:58 +00:00
|
|
|
#include <QtCore/QStandardPaths>
|
2014-03-17 15:24:10 +00:00
|
|
|
#include <KConfigGroup>
|
|
|
|
#include <KDesktopFile>
|
|
|
|
#include <KLocalizedString>
|
2015-01-20 14:16:38 +00:00
|
|
|
#include <KMimeTypeTrader>
|
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) {
|
|
|
|
QKeyEvent *keyEvent = dynamic_cast<QKeyEvent*>(event);
|
|
|
|
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
|
|
|
QHash<int, QByteArray> roles;
|
|
|
|
roles[Qt::UserRole] = "caption";
|
|
|
|
roles[Qt::UserRole+1] = "minimized";
|
2013-12-12 10:26:57 +00:00
|
|
|
roles[Qt::UserRole + 3] = "icon";
|
2011-10-31 07:41:07 +00:00
|
|
|
roles[Qt::UserRole+2] = "desktopName";
|
2011-11-10 13:28:06 +00:00
|
|
|
roles[Qt::UserRole+4] = "windowId";
|
2011-10-31 07:41:07 +00:00
|
|
|
setRoleNames(roles);
|
|
|
|
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
|
|
|
{
|
2015-01-20 14:16:38 +00:00
|
|
|
if (const auto s = KMimeTypeTrader::self()->preferredService(QStringLiteral("inode/directory"))) {
|
|
|
|
m_services << s;
|
|
|
|
m_fileManager = s;
|
|
|
|
}
|
|
|
|
if (const auto s = KMimeTypeTrader::self()->preferredService(QStringLiteral("text/html"))) {
|
|
|
|
m_services << s;
|
|
|
|
m_browser = s;
|
|
|
|
}
|
|
|
|
if (const auto s = KMimeTypeTrader::self()->preferredService(QStringLiteral("message/rfc822"))) {
|
|
|
|
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:
|
|
|
|
case Qt::UserRole:
|
2015-01-20 14:16:38 +00:00
|
|
|
return m_services.at(index.row())->name();
|
2011-10-31 07:41:07 +00:00
|
|
|
case Qt::UserRole+1:
|
|
|
|
return false;
|
|
|
|
case Qt::UserRole+2:
|
|
|
|
return i18nc("An example Desktop Name", "Desktop 1");
|
|
|
|
case Qt::UserRole+3:
|
2015-01-20 14:16:38 +00:00
|
|
|
return m_services.at(index.row())->icon();
|
2011-11-10 13:28:06 +00:00
|
|
|
case Qt::UserRole+4:
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return qApp->desktop()->screenGeometry(qApp->desktop()->primaryScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|