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()
|
|
|
|
{
|
2009-03-19 23:15:29 +01:00
|
|
|
if (isEmpty()) {
|
2009-10-09 16:40:03 +02:00
|
|
|
WindowEffects::showWindowThumbnails(parentWidget()->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) {
|
2009-10-09 16:40:03 +02:00
|
|
|
WindowEffects::showWindowThumbnails(parentWidget()->winId());
|
2008-11-04 00:08:39 +01:00
|
|
|
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-10-09 16:40:03 +02:00
|
|
|
WindowEffects::showWindowThumbnails(parentWidget()->winId(), ids, m_thumbnailRects);
|
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);
|
|
|
|
|
2009-09-24 16:24:46 +02:00
|
|
|
foreach (const 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-10-09 16:40:03 +02:00
|
|
|
|
2009-02-20 23:12:59 +01:00
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-11-11 16:28:40 +01:00
|
|
|
int i = 0;
|
|
|
|
foreach (QRect rect, m_thumbnailRects) {
|
|
|
|
if (rect.contains(event->pos())) {
|
|
|
|
WindowEffects::highlightWindows(effectiveWinId(), QList<WId>()<<effectiveWinId()<<ids[i]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
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"
|