2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2007 by Dan Meltzer <hydrogen@notyetimplemented.com>
|
|
|
|
* Copyright (C) 2008 by Alexis Ménard <darktears31@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, 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 Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "windowpreview_p.h"
|
|
|
|
|
2009-02-20 23:12:59 +01:00
|
|
|
#include <QPainter>
|
2009-03-20 11:27:31 +01:00
|
|
|
#include <QVarLengthArray>
|
2009-06-26 08:40:15 +02:00
|
|
|
#include <QMouseEvent>
|
2009-02-20 23:12:59 +01:00
|
|
|
|
2008-11-04 03:39:56 +01:00
|
|
|
#include <kwindowsystem.h>
|
2009-02-20 23:12:59 +01:00
|
|
|
#include <kdebug.h>
|
|
|
|
|
|
|
|
#include <plasma/framesvg.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
#include <QX11Info>
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <fixx11h.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Plasma {
|
|
|
|
|
|
|
|
bool WindowPreview::previewsAvailable() // static
|
|
|
|
{
|
|
|
|
if (!KWindowSystem::compositingActive()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
// hackish way to find out if KWin has the effect enabled,
|
|
|
|
// TODO provide proper support
|
|
|
|
Display *dpy = QX11Info::display();
|
|
|
|
Atom atom = XInternAtom(dpy, "_KDE_WINDOW_PREVIEW", False);
|
|
|
|
int cnt;
|
|
|
|
Atom *list = XListProperties(dpy, DefaultRootWindow(dpy), &cnt);
|
|
|
|
if (list != NULL) {
|
|
|
|
bool ret = (qFind(list, list + cnt, atom) != list + cnt);
|
|
|
|
XFree(list);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowPreview::WindowPreview(QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
2009-02-20 23:12:59 +01:00
|
|
|
m_background = new Plasma::FrameSvg(this);
|
|
|
|
m_background->setImagePath("widgets/frame");
|
|
|
|
m_background->setElementPrefix("raised");
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-03-19 23:15:29 +01:00
|
|
|
void WindowPreview::setWindowIds(const QList<WId> wids)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
if (!previewsAvailable()) {
|
2009-02-20 23:12:59 +01:00
|
|
|
setMinimumSize(0,0);
|
|
|
|
setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
|
2009-03-19 23:15:29 +01:00
|
|
|
ids.clear();
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-03-19 23:15:29 +01:00
|
|
|
ids = wids;
|
|
|
|
readWindowSizes();
|
2009-02-20 23:12:59 +01:00
|
|
|
QSize s(sizeHint());
|
|
|
|
if (s.isValid()) {
|
|
|
|
setFixedSize(sizeHint());
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-03-19 23:15:29 +01:00
|
|
|
QList<WId> WindowPreview::windowIds() const
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2009-03-19 23:15:29 +01:00
|
|
|
return ids;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize WindowPreview::sizeHint() const
|
|
|
|
{
|
2009-03-19 23:15:29 +01:00
|
|
|
if (ids.size() == 0) {
|
2008-11-04 00:08:39 +01:00
|
|
|
return QSize();
|
|
|
|
}
|
2009-03-19 23:15:29 +01:00
|
|
|
if (!windowSizes.size() == 0) {
|
|
|
|
readWindowSizes();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-03-19 23:15:29 +01:00
|
|
|
|
|
|
|
int maxHeight = 0;
|
|
|
|
int totalWidth = 0;
|
|
|
|
foreach (QSize s, windowSizes) {
|
|
|
|
if (s.height() > maxHeight) {
|
|
|
|
maxHeight = s.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
totalWidth += s.width();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize s(totalWidth, maxHeight);
|
|
|
|
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
m_background->getMargins(left, top, right, bottom);
|
|
|
|
|
2009-03-30 05:45:30 +02:00
|
|
|
s.scale(WINDOW_WIDTH*windowSizes.size(), WINDOW_HEIGHT, Qt::KeepAspectRatio);
|
2009-03-19 23:15:29 +01:00
|
|
|
|
2009-03-30 05:45:30 +02:00
|
|
|
s = s + QSize(left+right+WINDOW_MARGIN*(windowSizes.size()-1), top+bottom);
|
2009-02-20 23:12:59 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-03-19 23:15:29 +01:00
|
|
|
void WindowPreview::readWindowSizes() const
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2009-03-19 23:15:29 +01:00
|
|
|
windowSizes.clear();
|
|
|
|
foreach (WId id, ids) {
|
2008-11-04 00:08:39 +01:00
|
|
|
#ifdef Q_WS_X11
|
2009-03-19 23:15:29 +01:00
|
|
|
if (id > 0) {
|
|
|
|
KWindowInfo info = KWindowSystem::windowInfo(id, NET::WMGeometry|NET::WMFrameExtents);
|
|
|
|
windowSizes.append(info.frameGeometry().size());
|
|
|
|
} else {
|
|
|
|
windowSizes.append(QSize());
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
#else
|
2009-03-19 23:15:29 +01:00
|
|
|
windowSizes.append(QSize());
|
2008-11-04 00:08:39 +01:00
|
|
|
#endif
|
2009-03-19 23:15:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WindowPreview::isEmpty() const
|
|
|
|
{
|
|
|
|
foreach (WId id, ids) {
|
|
|
|
if (id != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-04-23 18:38:38 +02:00
|
|
|
|
2009-03-19 23:15:29 +01:00
|
|
|
return true;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowPreview::setInfo()
|
|
|
|
{
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
Display *dpy = QX11Info::display();
|
|
|
|
Atom atom = XInternAtom(dpy, "_KDE_WINDOW_PREVIEW", False);
|
2009-03-19 23:15:29 +01:00
|
|
|
if (isEmpty()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
XDeleteProperty(dpy, parentWidget()->winId(), atom);
|
|
|
|
return;
|
|
|
|
}
|
2009-05-01 18:28:10 +02:00
|
|
|
|
2009-03-19 23:15:29 +01:00
|
|
|
if (windowSizes.size() == 0) {
|
|
|
|
readWindowSizes();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-05-01 18:28:10 +02:00
|
|
|
|
2009-03-19 23:15:29 +01:00
|
|
|
if (windowSizes.size() == 0) {
|
2008-11-04 00:08:39 +01:00
|
|
|
XDeleteProperty(dpy, parentWidget()->winId(), atom);
|
|
|
|
return;
|
|
|
|
}
|
2009-05-01 18:28:10 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
Q_ASSERT(parentWidget()->isWindow()); // parent must be toplevel
|
2009-02-20 23:12:59 +01:00
|
|
|
|
|
|
|
QSize thumbnailSize = sizeHint();
|
|
|
|
thumbnailSize.scale(size(), Qt::KeepAspectRatio);
|
|
|
|
m_background->resizeFrame(thumbnailSize);
|
|
|
|
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
m_background->getMargins(left, top, right, bottom);
|
|
|
|
QRect thumbnailRect = geometry().adjusted(left, top, -right, -bottom);
|
|
|
|
|
2009-03-19 23:15:29 +01:00
|
|
|
const int numWindows = ids.size();
|
|
|
|
|
2009-04-27 22:16:22 +02:00
|
|
|
m_thumbnailRects.clear();
|
2009-03-19 23:15:29 +01:00
|
|
|
int x = thumbnailRect.x();
|
|
|
|
|
|
|
|
foreach (QSize s, windowSizes) {
|
2009-04-27 22:16:22 +02:00
|
|
|
s.scale((qreal)(thumbnailRect.width()-WINDOW_MARGIN*(numWindows-1))/numWindows, thumbnailRect.height(), Qt::KeepAspectRatio);
|
2009-03-19 23:15:29 +01:00
|
|
|
int y = thumbnailRect.y() + (thumbnailRect.height() - s.height())/2;
|
2009-04-27 22:16:22 +02:00
|
|
|
m_thumbnailRects.append(QRect(QPoint(x,y), s));
|
|
|
|
x += s.width() + WINDOW_MARGIN;
|
2009-03-19 23:15:29 +01:00
|
|
|
}
|
|
|
|
|
2009-05-01 18:28:10 +02:00
|
|
|
QVarLengthArray<long, 1024> data(1 + (6 * numWindows));
|
2009-03-19 23:15:29 +01:00
|
|
|
data[0] = numWindows;
|
|
|
|
|
2009-05-01 18:28:10 +02:00
|
|
|
for (int i = 0; i < numWindows; ++i) {
|
|
|
|
const int start = (i * 6) + 1;
|
2009-04-27 22:16:22 +02:00
|
|
|
const QRect thumbnailRect = m_thumbnailRects[i];
|
2009-03-19 23:15:29 +01:00
|
|
|
|
|
|
|
data[start] = 5;
|
|
|
|
data[start+1] = ids[i];
|
|
|
|
data[start+2] = thumbnailRect.x();
|
|
|
|
data[start+3] = thumbnailRect.y();
|
|
|
|
data[start+4] = thumbnailRect.width();
|
|
|
|
data[start+5] = thumbnailRect.height();
|
|
|
|
}
|
2009-02-20 23:12:59 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
XChangeProperty(dpy, parentWidget()->winId(), atom, atom, 32, PropModeReplace,
|
2009-03-20 11:27:31 +01:00
|
|
|
reinterpret_cast<unsigned char *>(data.data()), data.size());
|
2008-11-04 00:08:39 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-20 23:12:59 +01:00
|
|
|
void WindowPreview::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e)
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
QPainter painter(this);
|
2009-04-27 22:16:22 +02:00
|
|
|
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
m_background->getMargins(left, top, right, bottom);
|
|
|
|
|
|
|
|
foreach (QRect r, m_thumbnailRects) {
|
2009-06-26 08:40:15 +02:00
|
|
|
//kWarning()<<r;
|
2009-04-27 22:16:22 +02:00
|
|
|
m_background->resizeFrame(r.size()+QSize(left+right, top+bottom));
|
|
|
|
m_background->paintFrame(&painter, r.topLeft()-pos()-QPoint(left,top));
|
|
|
|
}
|
2009-02-20 23:12:59 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-26 08:40:15 +02:00
|
|
|
void WindowPreview::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QPoint p = event->pos();
|
|
|
|
WId wid = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < m_thumbnailRects.size(); ++i) {
|
|
|
|
if (m_thumbnailRects[i].contains(p)) {
|
|
|
|
wid = ids[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wid) {
|
|
|
|
emit windowPreviewClicked(wid, event->buttons(), event->modifiers(), event->globalPos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "windowpreview_p.moc"
|