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"
|
2011-10-31 07:41:07 +00:00
|
|
|
#include <QtDeclarative/qdeclarative.h>
|
|
|
|
#include <QtDeclarative/QDeclarativeContext>
|
|
|
|
#include <QtDeclarative/QDeclarativeEngine>
|
2013-02-26 08:00:51 +00:00
|
|
|
#include <QGraphicsObject>
|
2013-08-03 20:34:58 +00:00
|
|
|
#include <QtCore/QStandardPaths>
|
2011-10-31 07:41:07 +00:00
|
|
|
#include <kdeclarative.h>
|
2012-02-12 14:15:54 +00:00
|
|
|
#include <KDE/KConfigGroup>
|
2011-10-31 07:41:07 +00:00
|
|
|
#include <KDE/KDesktopFile>
|
|
|
|
#include <KDE/KGlobal>
|
|
|
|
#include <KDE/KIcon>
|
|
|
|
#include <KDE/KIconEffect>
|
|
|
|
#include <KDE/KIconLoader>
|
|
|
|
#include <KDE/KService>
|
2009-09-13 11:36:45 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
namespace TabBox
|
|
|
|
{
|
|
|
|
|
2012-04-09 16:59:26 +00:00
|
|
|
LayoutPreview::LayoutPreview(QWidget* parent)
|
2011-10-31 07:41:07 +00:00
|
|
|
: QDeclarativeView(parent)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-09 16:59:26 +00:00
|
|
|
setAutoFillBackground(false);
|
2011-10-31 07:41:07 +00:00
|
|
|
QPalette pal = palette();
|
|
|
|
pal.setColor(backgroundRole(), Qt::transparent);
|
|
|
|
setPalette(pal);
|
2012-04-09 16:59:26 +00:00
|
|
|
setMinimumSize(QSize(480, 300));
|
2011-10-31 07:41:07 +00:00
|
|
|
setResizeMode(QDeclarativeView::SizeRootObjectToView);
|
|
|
|
foreach (const QString &importPath, KGlobal::dirs()->findDirs("module", "imports")) {
|
|
|
|
engine()->addImportPath(importPath);
|
|
|
|
}
|
2013-08-03 20:34:58 +00:00
|
|
|
foreach (const QString &importPath, QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "kwin/tabbox", QStandardPaths::LocateDirectoty)) {
|
2011-10-31 07:41:07 +00:00
|
|
|
engine()->addImportPath(importPath);
|
|
|
|
}
|
|
|
|
ExampleClientModel *model = new ExampleClientModel(this);
|
|
|
|
engine()->addImageProvider(QLatin1String("client"), new TabBoxImageProvider(model));
|
|
|
|
KDeclarative kdeclarative;
|
|
|
|
kdeclarative.setDeclarativeEngine(engine());
|
|
|
|
kdeclarative.initialize();
|
|
|
|
kdeclarative.setupBindings();
|
2012-03-29 13:46:53 +00:00
|
|
|
qmlRegisterType<WindowThumbnailItem>("org.kde.kwin", 0, 1, "ThumbnailItem");
|
2011-10-31 07:41:07 +00:00
|
|
|
rootContext()->setContextProperty("clientModel", model);
|
2012-04-09 16:59:26 +00:00
|
|
|
rootContext()->setContextProperty("sourcePath", QString());
|
|
|
|
rootContext()->setContextProperty("name", QString());
|
2013-08-03 20:34:58 +00:00
|
|
|
setSource(QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kwin/kcm_kwintabbox/main.qml"));
|
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
|
|
|
|
2012-04-09 16:59:26 +00:00
|
|
|
void LayoutPreview::setLayout(const QString &path, const QString &name)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-09 16:59:26 +00:00
|
|
|
rootContext()->setContextProperty("sourcePath", path);
|
|
|
|
rootContext()->setContextProperty("name", name);
|
2011-10-31 07:41:07 +00:00
|
|
|
}
|
2009-09-13 11:36:45 +00:00
|
|
|
|
2011-10-31 07:41:07 +00:00
|
|
|
TabBoxImageProvider::TabBoxImageProvider(QAbstractListModel* model)
|
|
|
|
: QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap)
|
|
|
|
, m_model(model)
|
|
|
|
{
|
|
|
|
}
|
2009-09-13 11:36:45 +00:00
|
|
|
|
2011-10-31 07:41:07 +00:00
|
|
|
QPixmap TabBoxImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
QStringList parts = id.split('/');
|
|
|
|
const int index = parts.first().toInt(&ok);
|
|
|
|
if (!ok) {
|
|
|
|
return QDeclarativeImageProvider::requestPixmap(id, size, requestedSize);
|
|
|
|
}
|
|
|
|
QSize s(32, 32);
|
|
|
|
if (requestedSize.isValid()) {
|
|
|
|
s = requestedSize;
|
|
|
|
}
|
|
|
|
*size = s;
|
|
|
|
QPixmap icon(KIcon(m_model->data(m_model->index(index), Qt::UserRole+3).toString()).pixmap(s));
|
|
|
|
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;
|
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";
|
|
|
|
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
|
|
|
{
|
2011-10-31 07:41:07 +00:00
|
|
|
QList<QString> applications;
|
|
|
|
applications << "konqbrowser" << "KMail2" << "systemsettings" << "dolphin";
|
|
|
|
|
|
|
|
foreach (const QString& application, applications) {
|
|
|
|
KService::Ptr service = KService::serviceByStorageId("kde4-" + application + ".desktop");
|
|
|
|
if (service) {
|
|
|
|
m_nameList << service->entryPath();
|
|
|
|
}
|
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:
|
|
|
|
return KDesktopFile(m_nameList.at(index.row())).readName();
|
|
|
|
case Qt::UserRole+1:
|
|
|
|
return false;
|
|
|
|
case Qt::UserRole+2:
|
|
|
|
return i18nc("An example Desktop Name", "Desktop 1");
|
|
|
|
case Qt::UserRole+3:
|
|
|
|
return KDesktopFile(m_nameList.at(index.row())).readIcon();
|
2011-11-10 13:28:06 +00:00
|
|
|
case Qt::UserRole+4:
|
|
|
|
const QString desktopFile = KDesktopFile(m_nameList.at(index.row())).fileName().split('/').last();
|
|
|
|
if (desktopFile == "konqbrowser.desktop") {
|
2012-03-29 13:46:53 +00:00
|
|
|
return WindowThumbnailItem::Konqueror;
|
2011-11-10 13:28:06 +00:00
|
|
|
} else if (desktopFile == "KMail2.desktop") {
|
2012-03-29 13:46:53 +00:00
|
|
|
return WindowThumbnailItem::KMail;
|
2011-11-10 13:28:06 +00:00
|
|
|
} else if (desktopFile == "systemsettings.desktop") {
|
2012-03-29 13:46:53 +00:00
|
|
|
return WindowThumbnailItem::Systemsettings;
|
2011-11-10 13:28:06 +00:00
|
|
|
} else if (desktopFile == "dolphin.desktop") {
|
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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
return m_nameList.size();
|
|
|
|
}
|
|
|
|
|
2009-09-13 11:36:45 +00:00
|
|
|
} // namespace KWin
|
|
|
|
} // namespace TabBox
|
|
|
|
|