diff --git a/kcmkwin/kwintabbox/layoutpreview.cpp b/kcmkwin/kwintabbox/layoutpreview.cpp index 2859d673e..38371ddbd 100644 --- a/kcmkwin/kwintabbox/layoutpreview.cpp +++ b/kcmkwin/kwintabbox/layoutpreview.cpp @@ -1,267 +1,266 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2009, 2011 Martin Gräßlin 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 . *********************************************************************/ // own #include "layoutpreview.h" #include "thumbnailitem.h" #include #include #include #include #include #include #include #include #include #include #include namespace KWin { namespace TabBox { LayoutPreview::LayoutPreview(const QString &path, QObject *parent) : QObject(parent) , m_item(nullptr) { QQmlEngine *engine = new QQmlEngine(this); QQmlComponent *component = new QQmlComponent(engine, this); qmlRegisterType("org.kde.kwin", 2, 0, "ThumbnailItem"); qmlRegisterType("org.kde.kwin", 2, 0, "Switcher"); qmlRegisterType(); component->loadUrl(QUrl::fromLocalFile(path)); if (component->isError()) { qDebug() << component->errorString(); } QObject *item = component->create(); auto findSwitcher = [item]() -> SwitcherItem* { if (!item) { return nullptr; } if (SwitcherItem *i = qobject_cast(item)) { return i; } else if (QQuickWindow *w = qobject_cast(item)) { return w->contentItem()->findChild(); } return item->findChild(); }; if (SwitcherItem *switcher = findSwitcher()) { m_item = switcher; switcher->setVisible(true); } auto findWindow = [item]() -> QQuickWindow* { if (!item) { return nullptr; } if (QQuickWindow *w = qobject_cast(item)) { return w; } return item->findChild(); }; if (QQuickWindow *w = findWindow()) { w->setKeyboardGrabEnabled(true); w->setMouseGrabEnabled(true); w->installEventFilter(this); } } LayoutPreview::~LayoutPreview() { } bool LayoutPreview::eventFilter(QObject *object, QEvent *event) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast(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(object)) { if (!w->geometry().contains(static_cast(event)->globalPos())) { object->deleteLater(); deleteLater(); } } } return QObject::eventFilter(object, event); } ExampleClientModel::ExampleClientModel (QObject* parent) : QAbstractListModel (parent) { init(); } ExampleClientModel::~ExampleClientModel() { } void ExampleClientModel::init() { 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; } } QVariant ExampleClientModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } switch (role) { case Qt::DisplayRole: - case Qt::UserRole: + case CaptionRole: return m_services.at(index.row())->name(); - case Qt::UserRole+1: + case MinimizedRole: return false; - case Qt::UserRole+2: + case DesktopNameRole: return i18nc("An example Desktop Name", "Desktop 1"); - case Qt::UserRole+3: + case IconRole: return m_services.at(index.row())->icon(); - case Qt::UserRole+4: + case WindowIdRole: const auto s = m_services.at(index.row()); if (s == m_browser) { return WindowThumbnailItem::Konqueror; } else if (s == m_email) { return WindowThumbnailItem::KMail; } else if (s == m_systemSettings) { return WindowThumbnailItem::Systemsettings; } else if (s == m_fileManager) { return WindowThumbnailItem::Dolphin; } return 0; } return QVariant(); } QString ExampleClientModel::longestCaption() const { QString caption; for (const auto item : m_services) { const QString name = item->name(); if (name.size() > caption.size()) { caption = name; } } return caption; } int ExampleClientModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent) return m_services.size(); } QHash ExampleClientModel::roleNames() const { - // FIXME: Use an enum. return { - { Qt::UserRole, QByteArrayLiteral("caption") }, - { Qt::UserRole + 1, QByteArrayLiteral("minimized") }, - { Qt::UserRole + 2, QByteArrayLiteral("desktopName") }, - { Qt::UserRole + 3, QByteArrayLiteral("icon") }, - { Qt::UserRole + 4, QByteArrayLiteral("windowId") }, + { CaptionRole, QByteArrayLiteral("caption") }, + { MinimizedRole, QByteArrayLiteral("minimized") }, + { DesktopNameRole, QByteArrayLiteral("desktopName") }, + { IconRole, QByteArrayLiteral("icon") }, + { WindowIdRole, QByteArrayLiteral("windowId") }, }; } 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 { const QScreen *primaryScreen = qApp->primaryScreen(); return primaryScreen->geometry(); } 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); } } // namespace KWin } // namespace TabBox diff --git a/kcmkwin/kwintabbox/layoutpreview.h b/kcmkwin/kwintabbox/layoutpreview.h index 7f77a5b09..e87651110 100644 --- a/kcmkwin/kwintabbox/layoutpreview.h +++ b/kcmkwin/kwintabbox/layoutpreview.h @@ -1,145 +1,153 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2009, 2011 Martin Gräßlin 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 . *********************************************************************/ #ifndef KWIN_TABBOX_LAYOUTPREVIEW_H #define KWIN_TABBOX_LAYOUTPREVIEW_H #include #include #include #include namespace KWin { namespace TabBox { class SwitcherItem; class LayoutPreview : public QObject { Q_OBJECT public: explicit LayoutPreview(const QString &path, QObject *parent = nullptr); ~LayoutPreview() override; bool eventFilter(QObject *object, QEvent *event) override; private: SwitcherItem *m_item; }; class ExampleClientModel : public QAbstractListModel { Q_OBJECT public: + enum { + CaptionRole = Qt::UserRole + 1, + MinimizedRole, + DesktopNameRole, + IconRole, + WindowIdRole + }; + explicit ExampleClientModel(QObject *parent = nullptr); ~ExampleClientModel() override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; QHash roleNames() const override; Q_INVOKABLE QString longestCaption() const; private: void init(); QList m_services; KService::Ptr m_fileManager; KService::Ptr m_browser; KService::Ptr m_email; KService::Ptr m_systemSettings; }; class SwitcherItem : public QObject { Q_OBJECT Q_PROPERTY(QAbstractItemModel *model READ model NOTIFY modelChanged) Q_PROPERTY(QRect screenGeometry READ screenGeometry NOTIFY screenGeometryChanged) Q_PROPERTY(bool visible READ isVisible NOTIFY visibleChanged) Q_PROPERTY(bool allDesktops READ isAllDesktops NOTIFY allDesktopsChanged) Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged) /** * The main QML item that will be displayed in the Dialog */ Q_PROPERTY(QObject *item READ item WRITE setItem NOTIFY itemChanged) Q_CLASSINFO("DefaultProperty", "item") public: SwitcherItem(QObject *parent = nullptr); ~SwitcherItem() override; QAbstractItemModel *model() const; QRect screenGeometry() const; bool isVisible() const; bool isAllDesktops() const; int currentIndex() const; void setCurrentIndex(int index); QObject *item() const; void setItem(QObject *item); void setVisible(bool visible); void incrementIndex(); void decrementIndex(); Q_SIGNALS: void visibleChanged(); void currentIndexChanged(int index); void modelChanged(); void allDesktopsChanged(); void screenGeometryChanged(); void itemChanged(); private: QAbstractItemModel *m_model; QObject *m_item; int m_currentIndex; bool m_visible; }; inline QAbstractItemModel *SwitcherItem::model() const { return m_model; } inline bool SwitcherItem::isVisible() const { return m_visible; } inline bool SwitcherItem::isAllDesktops() const { return true; } inline int SwitcherItem::currentIndex() const { return m_currentIndex; } inline QObject *SwitcherItem::item() const { return m_item; } } // namespace TabBox } // namespace KWin #endif // KWIN_TABBOX_LAYOUTPREVIEW_H