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>
|
2009-10-09 16:40:03 +02:00
|
|
|
#include <plasma/windoweffects.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 {
|
|
|
|
|
|
|
|
WindowPreview::WindowPreview(QWidget *parent)
|
2009-12-24 15:54:48 +01:00
|
|
|
: QWidget(parent),
|
|
|
|
m_highlightWindows(false)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2009-02-20 23:12:59 +01:00
|
|
|
m_background = new Plasma::FrameSvg(this);
|
|
|
|
m_background->setImagePath("widgets/frame");
|
|
|
|
m_background->setElementPrefix("raised");
|
2009-11-11 16:28:40 +01:00
|
|
|
setMouseTracking(true);
|
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
|
|
|
{
|
2009-10-09 16:40:03 +02:00
|
|
|
if (!WindowEffects::isEffectAvailable(WindowEffects::WindowPreview)) {
|
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-08-02 21:12:20 +02:00
|
|
|
|
|
|
|
//FIXME: need to get rid of this 4 window maximum by using a smarter layout
|
|
|
|
if (wids.count() < 5) {
|
|
|
|
ids = wids;
|
|
|
|
} else {
|
|
|
|
ids = wids.mid(0, 4);
|
|
|
|
}
|
|
|
|
|
2009-10-09 16:40:03 +02:00
|
|
|
windowSizes = WindowEffects::windowSizes(ids);
|
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-08-02 21:12:20 +02:00
|
|
|
|
2009-03-19 23:15:29 +01:00
|
|
|
if (!windowSizes.size() == 0) {
|
2009-10-09 16:40:03 +02:00
|
|
|
windowSizes = WindowEffects::windowSizes(ids);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-03-19 23:15:29 +01:00
|
|
|
|
|
|
|
int maxHeight = 0;
|
|
|
|
int totalWidth = 0;
|
2009-08-02 21:12:20 +02:00
|
|
|
|
2009-09-24 16:24:46 +02:00
|
|
|
foreach (const QSize &s, windowSizes) {
|
2009-03-19 23:15:29 +01:00
|
|
|
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);
|
|
|
|
s = s + QSize(left+right+WINDOW_MARGIN*(windowSizes.size()-1), top+bottom);
|
2008-11-04 00:08:39 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-12-24 15:54:48 +01:00
|
|
|
|
|
|
|
void WindowPreview::setHighlightWindows(const bool highlight)
|
|
|
|
{
|
|
|
|
m_highlightWindows = highlight;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WindowPreview::highlightWindows() const
|
|
|
|
{
|
|
|
|
return m_highlightWindows;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void WindowPreview::setInfo()
|
|
|
|
{
|
2011-07-11 21:46:34 +02:00
|
|
|
QWidget *w = parentWidget();
|
2009-03-19 23:15:29 +01:00
|
|
|
if (isEmpty()) {
|
2011-07-11 21:46:34 +02:00
|
|
|
WindowEffects::showWindowThumbnails(w->winId());
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-05-01 18:28:10 +02:00
|
|
|
|
2009-03-19 23:15:29 +01:00
|
|
|
if (windowSizes.size() == 0) {
|
2009-10-09 16:40:03 +02:00
|
|
|
windowSizes = WindowEffects::windowSizes(ids);
|
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) {
|
2011-07-11 21:46:34 +02:00
|
|
|
WindowEffects::showWindowThumbnails(w->winId());
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-05-01 18:28:10 +02:00
|
|
|
|
2011-07-11 21:46:34 +02:00
|
|
|
Q_ASSERT(w->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);
|
2011-07-11 21:46:34 +02:00
|
|
|
const QRect thumbnailRect(QPoint(left, top), size() - QSize(left + right, top + bottom));
|
2009-03-19 23:15:29 +01:00
|
|
|
const int numWindows = ids.size();
|
2011-07-11 21:46:34 +02:00
|
|
|
const qreal thumbWidth = (thumbnailRect.width() - WINDOW_MARGIN*(numWindows - 1)) / numWindows;
|
2009-03-19 23:15:29 +01:00
|
|
|
|
2011-07-11 21:46:34 +02:00
|
|
|
// we paint in parent coords, but accept events in local coords
|
|
|
|
QList<QRect> inParentCoords;
|
2009-04-27 22:16:22 +02:00
|
|
|
m_thumbnailRects.clear();
|
2009-03-19 23:15:29 +01:00
|
|
|
|
2011-07-11 21:46:34 +02:00
|
|
|
int x = thumbnailRect.x();
|
2009-03-19 23:15:29 +01:00
|
|
|
foreach (QSize s, windowSizes) {
|
2011-07-11 21:46:34 +02:00
|
|
|
s.scale(thumbWidth, thumbnailRect.height(), Qt::KeepAspectRatio);
|
2009-03-19 23:15:29 +01:00
|
|
|
int y = thumbnailRect.y() + (thumbnailRect.height() - s.height())/2;
|
2011-07-11 21:46:34 +02:00
|
|
|
m_thumbnailRects.append(QRect(QPoint(x, y), s));
|
|
|
|
inParentCoords.append(QRect(mapToParent(QPoint(x, y)), s));
|
2009-04-27 22:16:22 +02:00
|
|
|
x += s.width() + WINDOW_MARGIN;
|
2009-03-19 23:15:29 +01:00
|
|
|
}
|
|
|
|
|
2011-07-11 21:46:34 +02:00
|
|
|
WindowEffects::showWindowThumbnails(w->winId(), ids, inParentCoords);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-02-20 23:12:59 +01:00
|
|
|
void WindowPreview::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e)
|
2009-10-09 16:40:03 +02:00
|
|
|
|
2009-02-20 23:12:59 +01:00
|
|
|
QPainter painter(this);
|
2009-04-27 22:16:22 +02:00
|
|
|
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
m_background->getMargins(left, top, right, bottom);
|
2011-07-11 21:46:34 +02:00
|
|
|
const QSize delta(left + right, top + bottom);
|
|
|
|
const QPoint topLeft(left, top);
|
2009-04-27 22:16:22 +02:00
|
|
|
|
2009-09-24 16:24:46 +02:00
|
|
|
foreach (const QRect &r, m_thumbnailRects) {
|
2009-06-26 08:40:15 +02:00
|
|
|
//kWarning()<<r;
|
2011-07-11 21:46:34 +02:00
|
|
|
m_background->resizeFrame(r.size() + delta);
|
|
|
|
m_background->paintFrame(&painter, r.topLeft() - topLeft);
|
2009-04-27 22:16:22 +02:00
|
|
|
}
|
2009-02-20 23:12:59 +01:00
|
|
|
}
|
|
|
|
|
2009-06-26 08:40:15 +02:00
|
|
|
void WindowPreview::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_thumbnailRects.size(); ++i) {
|
2011-07-11 21:46:34 +02:00
|
|
|
if (m_thumbnailRects[i].contains(event->pos())) {
|
|
|
|
emit windowPreviewClicked(ids[i], event->buttons(), event->modifiers(), event->globalPos());
|
|
|
|
return;
|
2009-06-26 08:40:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-11 16:28:40 +01:00
|
|
|
void WindowPreview::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
2009-12-24 15:54:48 +01:00
|
|
|
if (!m_highlightWindows) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-11 21:46:34 +02:00
|
|
|
for (int i = 0; i < m_thumbnailRects.size(); ++i) {
|
|
|
|
if (m_thumbnailRects[i].contains(event->pos())) {
|
|
|
|
WindowEffects::highlightWindows(effectiveWinId(), QList<WId>() << effectiveWinId() << ids[i]);
|
2009-11-11 16:28:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-07-11 21:46:34 +02:00
|
|
|
|
2009-11-11 16:28:40 +01:00
|
|
|
WindowEffects::highlightWindows(effectiveWinId(), QList<WId>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowPreview::leaveEvent(QEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
2009-12-24 15:54:48 +01:00
|
|
|
if (!m_highlightWindows) {
|
|
|
|
return;
|
|
|
|
}
|
2009-11-11 16:28:40 +01:00
|
|
|
WindowEffects::highlightWindows(effectiveWinId(), QList<WId>());
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "windowpreview_p.moc"
|