12#include <KApplicationTrader>
13#include <KConfigGroup>
14#include <KDesktopFile>
15#include <KLocalizedString>
16#include <QApplication>
21#include <QStandardPaths>
32 QQmlEngine *engine =
new QQmlEngine(
this);
33 QQmlComponent *component =
new QQmlComponent(engine,
this);
34 qmlRegisterType<WindowThumbnailItem>(
"org.kde.kwin", 3, 0,
"WindowThumbnail");
35 qmlRegisterType<SwitcherItem>(
"org.kde.kwin", 3, 0,
"TabBoxSwitcher");
36 qmlRegisterType<DesktopBackground>(
"org.kde.kwin", 3, 0,
"DesktopBackground");
37 qmlRegisterAnonymousType<QAbstractItemModel>(
"org.kde.kwin", 3);
38 component->loadUrl(QUrl::fromLocalFile(path));
39 if (component->isError()) {
40 qDebug() << component->errorString();
42 QObject *item = component->create();
47 if (
SwitcherItem *i = qobject_cast<SwitcherItem *>(item)) {
49 }
else if (QQuickWindow *w = qobject_cast<QQuickWindow *>(item)) {
56 static_cast<ExampleClientModel *
>(switcher->model())->showDesktopThumbnail(showDesktopThumbnail);
57 switcher->setVisible(
true);
59 auto findWindow = [item]() -> QQuickWindow * {
63 if (QQuickWindow *w = qobject_cast<QQuickWindow *>(item)) {
66 return item->findChild<QQuickWindow *>();
68 if (QQuickWindow *w = findWindow()) {
69 w->setKeyboardGrabEnabled(
true);
70 w->installEventFilter(
this);
80 if (event->type() == QEvent::KeyPress) {
81 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(event);
82 if (keyEvent->key() == Qt::Key_Escape || keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Space) {
83 object->deleteLater();
86 if (m_item && keyEvent->key() == Qt::Key_Tab) {
89 if (m_item && keyEvent->key() == Qt::Key_Backtab) {
92 }
else if (event->type() == QEvent::FocusOut) {
93 object->deleteLater();
96 return QObject::eventFilter(
object, event);
100 : QAbstractListModel(parent)
109void ExampleClientModel::init()
111 if (
const auto s = KApplicationTrader::preferredService(QStringLiteral(
"inode/directory"))) {
114 if (
const auto s = KApplicationTrader::preferredService(QStringLiteral(
"text/html"))) {
117 if (
const auto s = KApplicationTrader::preferredService(QStringLiteral(
"message/rfc822"))) {
120 if (
const auto s = KService::serviceByDesktopName(QStringLiteral(
"kdesystemsettings"))) {
128 const int desktopIndex = m_thumbnails.indexOf(desktopThumbnail);
129 if (showDesktop == (desktopIndex >= 0)) {
133 Q_EMIT beginResetModel();
135 m_thumbnails << desktopThumbnail;
137 m_thumbnails.removeAt(desktopIndex);
139 Q_EMIT endResetModel();
144 if (!index.isValid() || index.row() >=
rowCount()) {
148 const ThumbnailInfo &item = m_thumbnails.at(index.row());
151 case Qt::DisplayRole:
157 return i18nc(
"An example Desktop Name",
"Desktop 1");
171 for (
const auto &item : m_thumbnails) {
172 if (item.caption.size() > caption.size()) {
173 caption = item.caption;
181 return m_thumbnails.size();
190 {
IconRole, QByteArrayLiteral(
"icon")},
226 if (m_currentIndex == index) {
229 m_currentIndex = index;
235 const QScreen *primaryScreen = qApp->primaryScreen();
236 return primaryScreen->geometry();
246 int index = m_currentIndex - 1;
248 index = m_model->rowCount() - 1;
258 connect(
this, &QQuickItem::windowChanged,
this, &DesktopBackground::stretchToScreen);
262void DesktopBackground::stretchToScreen()
264 const QQuickWindow *w = window();
268 const QScreen *screen = w->screen();
272 setImplicitSize(screen->size().width(), screen->size().height());
278#include "moc_layoutpreview.cpp"
DesktopBackground(QQuickItem *parent=nullptr)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
ExampleClientModel(QObject *parent=nullptr)
void showDesktopThumbnail(bool showDesktop)
Q_INVOKABLE QString longestCaption() const
~ExampleClientModel() override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QHash< int, QByteArray > roleNames() const override
bool eventFilter(QObject *object, QEvent *event) override
LayoutPreview(const QString &path, bool showDesktopThumbnail=false, QObject *parent=nullptr)
~LayoutPreview() override
void setCurrentIndex(int index)
void currentIndexChanged(int index)
void setVisible(bool visible)
void setItem(QObject *item)
SwitcherItem(QObject *parent=nullptr)
void setWId(qulonglong wId)