2008-09-07 23:33:02 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 by Montel Laurent <montel@kde.org>
|
|
|
|
*
|
2008-09-08 19:32:25 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2008-09-07 23:33:02 +02:00
|
|
|
*
|
2008-09-08 19:32:25 +02:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2008-09-07 23:33:02 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-09-08 19:32:25 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2008-09-07 23:33:02 +02:00
|
|
|
*
|
2008-09-08 19:32:25 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor,
|
2008-09-07 23:33:02 +02:00
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2008-07-17 11:30:57 +02:00
|
|
|
|
|
|
|
#include "popupapplet.h"
|
|
|
|
|
|
|
|
#include <QGraphicsProxyWidget>
|
|
|
|
#include <QGraphicsLinearLayout>
|
|
|
|
#include <QVBoxLayout>
|
2008-07-28 01:23:57 +02:00
|
|
|
#include <QTimer>
|
2008-07-17 11:30:57 +02:00
|
|
|
|
|
|
|
#include <KIcon>
|
|
|
|
#include <KIconLoader>
|
2008-09-04 19:09:34 +02:00
|
|
|
#include <KWindowSystem>
|
2008-07-17 11:30:57 +02:00
|
|
|
|
|
|
|
#include <plasma/dialog.h>
|
2008-08-06 22:41:10 +02:00
|
|
|
#include <plasma/corona.h>
|
2008-08-25 15:55:26 +02:00
|
|
|
#include <plasma/containment.h>
|
2008-08-26 01:16:18 +02:00
|
|
|
#include <plasma/extender.h>
|
2008-07-17 11:30:57 +02:00
|
|
|
#include <plasma/widgets/icon.h>
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class PopupAppletPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PopupAppletPrivate(PopupApplet *applet)
|
|
|
|
: q(applet),
|
|
|
|
icon(0),
|
|
|
|
dialog(0),
|
|
|
|
layout(0),
|
2008-07-28 01:23:57 +02:00
|
|
|
proxy(0),
|
|
|
|
savedAspectRatio(Plasma::InvalidAspectRatioMode),
|
2008-08-27 12:45:43 +02:00
|
|
|
timer(0)
|
2008-07-17 11:30:57 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~PopupAppletPrivate()
|
|
|
|
{
|
|
|
|
if (proxy) {
|
|
|
|
proxy->setWidget(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete dialog;
|
|
|
|
delete icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void togglePopup();
|
2008-07-28 01:23:57 +02:00
|
|
|
void hideTimedPopup();
|
2008-08-28 16:32:48 +02:00
|
|
|
void dialogSizeChanged();
|
2008-09-01 12:45:33 +02:00
|
|
|
void dialogStatusChanged(bool status);
|
2008-07-17 11:30:57 +02:00
|
|
|
|
|
|
|
PopupApplet *q;
|
|
|
|
Plasma::Icon *icon;
|
|
|
|
Plasma::Dialog *dialog;
|
|
|
|
QGraphicsLinearLayout *layout;
|
|
|
|
QGraphicsProxyWidget *proxy;
|
2008-07-28 01:23:57 +02:00
|
|
|
Plasma::AspectRatioMode savedAspectRatio;
|
|
|
|
QTimer *timer;
|
2008-07-17 11:30:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
PopupApplet::PopupApplet(QObject *parent, const QVariantList &args)
|
|
|
|
: Plasma::Applet(parent, args),
|
|
|
|
d(new PopupAppletPrivate(this))
|
|
|
|
{
|
|
|
|
int iconSize = IconSize(KIconLoader::Desktop);
|
|
|
|
resize(iconSize, iconSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
PopupApplet::~PopupApplet()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopupApplet::setIcon(const QIcon &icon)
|
|
|
|
{
|
|
|
|
if (!d->icon) {
|
|
|
|
d->icon = new Plasma::Icon(icon, QString(), this);
|
|
|
|
} else {
|
|
|
|
d->icon->setIcon(icon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopupApplet::setIcon(const QString &iconName)
|
|
|
|
{
|
|
|
|
if (!d->icon) {
|
|
|
|
d->icon = new Plasma::Icon(KIcon(iconName), QString(), this);
|
|
|
|
} else {
|
|
|
|
d->icon->setIcon(iconName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon PopupApplet::icon() const
|
|
|
|
{
|
|
|
|
return d->icon->icon();
|
|
|
|
}
|
|
|
|
|
2008-07-28 01:23:57 +02:00
|
|
|
QWidget *PopupApplet::widget()
|
|
|
|
{
|
|
|
|
return 0;
|
2008-07-28 08:59:20 +02:00
|
|
|
}
|
2008-07-28 01:23:57 +02:00
|
|
|
|
|
|
|
QGraphicsWidget *PopupApplet::graphicsWidget()
|
|
|
|
{
|
|
|
|
return 0;
|
2008-07-28 08:59:20 +02:00
|
|
|
}
|
2008-07-28 01:23:57 +02:00
|
|
|
|
2008-07-17 11:30:57 +02:00
|
|
|
void PopupApplet::constraintsEvent(Plasma::Constraints constraints)
|
|
|
|
{
|
|
|
|
if (constraints & Plasma::StartupCompletedConstraint) {
|
|
|
|
if (!d->icon) {
|
|
|
|
d->icon = new Plasma::Icon(KIcon("icons"), QString(), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
d->layout = new QGraphicsLinearLayout(this);
|
|
|
|
d->layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
d->layout->setSpacing(0);
|
|
|
|
d->layout->setOrientation(Qt::Horizontal);
|
2008-09-05 18:24:32 +02:00
|
|
|
setAspectRatioMode(Plasma::ConstrainedSquare);
|
2008-07-17 11:30:57 +02:00
|
|
|
setLayout(d->layout);
|
2008-07-28 01:23:57 +02:00
|
|
|
|
2008-07-17 11:30:57 +02:00
|
|
|
connect(d->icon, SIGNAL(clicked()), this, SLOT(togglePopup()));
|
|
|
|
}
|
|
|
|
|
2008-08-25 15:55:26 +02:00
|
|
|
//since we call this function when an extender's geometry gets updated, we want to avoid doing
|
|
|
|
//anything if the StartupCompletedConstraint hasn't been called yet.
|
|
|
|
if (!d->layout) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-17 11:30:57 +02:00
|
|
|
if (constraints & Plasma::FormFactorConstraint) {
|
|
|
|
d->layout->removeAt(0);
|
2008-07-28 01:23:57 +02:00
|
|
|
|
2008-08-27 12:45:43 +02:00
|
|
|
switch (formFactor()) {
|
2008-07-17 11:30:57 +02:00
|
|
|
case Plasma::Planar:
|
2008-07-26 17:05:16 +02:00
|
|
|
case Plasma::MediaCenter: {
|
2008-08-11 20:15:37 +02:00
|
|
|
if (d->icon) {
|
|
|
|
d->icon->hide();
|
|
|
|
}
|
|
|
|
|
2008-07-28 01:23:57 +02:00
|
|
|
if (d->savedAspectRatio != Plasma::InvalidAspectRatioMode) {
|
|
|
|
setAspectRatioMode(d->savedAspectRatio);
|
|
|
|
}
|
2008-07-17 11:30:57 +02:00
|
|
|
|
2008-07-28 01:23:57 +02:00
|
|
|
if (d->dialog) {
|
|
|
|
if (d->dialog->layout() && widget()) {
|
|
|
|
//we dont want to delete Widget inside the dialog layout
|
|
|
|
d->dialog->layout()->removeWidget(widget());
|
|
|
|
}
|
2008-07-17 11:30:57 +02:00
|
|
|
|
2008-07-28 01:23:57 +02:00
|
|
|
delete d->dialog;
|
|
|
|
d->dialog = 0;
|
2008-07-17 11:30:57 +02:00
|
|
|
}
|
|
|
|
|
2008-07-26 17:05:16 +02:00
|
|
|
//get the margins
|
|
|
|
QSizeF marginSize = size() - contentsRect().size();
|
|
|
|
|
2008-09-07 22:49:10 +02:00
|
|
|
QGraphicsWidget *gWidget = graphicsWidget();
|
|
|
|
if (gWidget) {
|
|
|
|
d->layout->addItem(gWidget);
|
|
|
|
setMinimumSize(gWidget->minimumSize() + marginSize);
|
|
|
|
gWidget->installEventFilter(this);
|
|
|
|
} else {
|
2008-07-28 01:23:57 +02:00
|
|
|
if (!d->proxy) {
|
|
|
|
d->proxy = new QGraphicsProxyWidget(this);
|
|
|
|
d->proxy->setWidget(widget());
|
|
|
|
d->proxy->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->layout->addItem(d->proxy);
|
|
|
|
setMinimumSize(widget() ? widget()->minimumSize() + marginSize : QSizeF(300, 200));
|
|
|
|
}
|
|
|
|
|
2008-07-17 11:30:57 +02:00
|
|
|
break;
|
2008-07-26 17:05:16 +02:00
|
|
|
}
|
2008-07-17 11:30:57 +02:00
|
|
|
case Plasma::Horizontal:
|
|
|
|
case Plasma::Vertical:
|
2008-07-28 01:23:57 +02:00
|
|
|
//save the aspect ratio mode in case we drag'n drop in the Desktop later
|
|
|
|
d->savedAspectRatio = aspectRatioMode();
|
|
|
|
setAspectRatioMode(Plasma::ConstrainedSquare);
|
2008-07-17 11:30:57 +02:00
|
|
|
|
2008-08-11 20:15:37 +02:00
|
|
|
if (d->icon) {
|
|
|
|
d->icon->show();
|
|
|
|
}
|
|
|
|
|
2008-07-17 11:30:57 +02:00
|
|
|
if (d->proxy) {
|
|
|
|
d->proxy->setWidget(0); // prevent it from deleting our widget!
|
|
|
|
delete d->proxy;
|
|
|
|
d->proxy = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!d->dialog) {
|
2008-09-06 22:04:59 +02:00
|
|
|
d->dialog = new Plasma::Dialog();
|
2008-09-04 19:09:34 +02:00
|
|
|
|
|
|
|
//no longer use Qt::Popup since that seems to cause a lot of problem when you drag
|
|
|
|
//stuff out of your Dialog (extenders). Monitor WindowDeactivate events so we can
|
|
|
|
//emulate the same kind of behavior as Qt::Popup (close when you click somewhere
|
|
|
|
//else.
|
|
|
|
d->dialog->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
|
|
|
|
KWindowSystem::setState(d->dialog->winId(), NET::SkipTaskbar | NET::SkipPager);
|
|
|
|
d->dialog->installEventFilter(this);
|
|
|
|
|
2008-08-28 16:32:48 +02:00
|
|
|
connect(d->dialog, SIGNAL(dialogResized()), this, SLOT(dialogSizeChanged()));
|
2008-09-01 12:45:33 +02:00
|
|
|
connect(d->dialog, SIGNAL(dialogVisible(bool)), this , SLOT(dialogStatusChanged(bool)));
|
2008-09-07 22:49:10 +02:00
|
|
|
QGraphicsWidget *gWidget = graphicsWidget();
|
|
|
|
if (gWidget) {
|
|
|
|
Corona *corona = qobject_cast<Corona *>(gWidget->scene());
|
2008-08-06 22:41:10 +02:00
|
|
|
|
|
|
|
//could that cast ever fail??
|
|
|
|
if (corona) {
|
2008-09-07 22:49:10 +02:00
|
|
|
corona->addOffscreenWidget(gWidget);
|
|
|
|
graphicsWidget()->resize(gWidget->preferredSize());
|
|
|
|
graphicsWidget()->setMinimumSize(gWidget->preferredSize());
|
|
|
|
d->dialog->setGraphicsWidget(gWidget);
|
2008-08-06 22:41:10 +02:00
|
|
|
}
|
2008-07-28 01:23:57 +02:00
|
|
|
} else {
|
2008-08-06 22:41:10 +02:00
|
|
|
QVBoxLayout *l_layout = new QVBoxLayout(d->dialog);
|
|
|
|
l_layout->setSpacing(0);
|
|
|
|
l_layout->setMargin(0);
|
2008-07-28 01:23:57 +02:00
|
|
|
l_layout->addWidget(widget());
|
|
|
|
}
|
2008-07-17 11:30:57 +02:00
|
|
|
}
|
|
|
|
|
2008-07-28 01:23:57 +02:00
|
|
|
d->dialog->adjustSize();
|
2008-07-17 11:30:57 +02:00
|
|
|
d->layout->addItem(d->icon);
|
2008-08-25 15:55:26 +02:00
|
|
|
|
2008-07-17 11:30:57 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-29 21:00:10 +02:00
|
|
|
bool PopupApplet::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
2008-09-04 19:09:34 +02:00
|
|
|
if (watched == d->dialog && (event->type() == QEvent::WindowDeactivate)) {
|
|
|
|
hidePopup();
|
|
|
|
}
|
|
|
|
|
2008-08-29 21:00:10 +02:00
|
|
|
if (watched == graphicsWidget() && (event->type() == QEvent::GraphicsSceneResize)) {
|
|
|
|
//sizes are recalculated in the constraintsevent so let's just call that.
|
|
|
|
constraintsEvent(Plasma::FormFactorConstraint);
|
|
|
|
|
|
|
|
//resize vertically if necesarry.
|
|
|
|
if (formFactor() == Plasma::MediaCenter || formFactor() == Plasma::Planar) {
|
|
|
|
resize(QSizeF(size().width(), minimumHeight()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Applet::eventFilter(watched, event);
|
|
|
|
}
|
|
|
|
|
2008-09-05 22:44:43 +02:00
|
|
|
QVariant PopupApplet::itemChange(GraphicsItemChange change, const QVariant &value)
|
|
|
|
{
|
|
|
|
if (change == QGraphicsItem::ItemParentHasChanged && d->dialog) {
|
|
|
|
d->dialog->setParent(view());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Applet::itemChange(change, value);
|
|
|
|
}
|
|
|
|
|
2008-07-28 01:23:57 +02:00
|
|
|
void PopupApplet::showPopup(uint popupDuration)
|
2008-07-17 11:30:57 +02:00
|
|
|
{
|
|
|
|
if (d->dialog && (formFactor() == Horizontal || formFactor() == Vertical)) {
|
2008-08-26 01:16:18 +02:00
|
|
|
d->dialog->move(popupPosition(d->dialog->size()));
|
2008-07-17 11:30:57 +02:00
|
|
|
d->dialog->show();
|
2008-09-04 19:09:34 +02:00
|
|
|
KWindowSystem::setState(d->dialog->winId(), NET::SkipTaskbar | NET::SkipPager);
|
2008-07-28 01:23:57 +02:00
|
|
|
|
|
|
|
if (d->timer) {
|
|
|
|
d->timer->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (popupDuration > 0) {
|
|
|
|
if (!d->timer) {
|
|
|
|
d->timer = new QTimer(this);
|
|
|
|
connect(d->timer, SIGNAL(timeout()), this, SLOT(hideTimedPopup()));
|
|
|
|
}
|
|
|
|
|
|
|
|
d->timer->start(popupDuration);
|
|
|
|
}
|
2008-07-17 11:30:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopupApplet::hidePopup()
|
|
|
|
{
|
|
|
|
if (d->dialog && (formFactor() == Horizontal || formFactor() == Vertical)) {
|
|
|
|
d->dialog->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-28 20:30:50 +02:00
|
|
|
void PopupApplet::popupEvent(bool)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-07-17 11:30:57 +02:00
|
|
|
void PopupAppletPrivate::togglePopup()
|
|
|
|
{
|
2008-09-07 22:49:10 +02:00
|
|
|
if (dialog) {
|
|
|
|
if (timer) {
|
|
|
|
timer->stop();
|
|
|
|
}
|
2008-09-02 16:54:58 +02:00
|
|
|
|
2008-09-07 22:49:10 +02:00
|
|
|
dialog->move(q->popupPosition(dialog->size()));
|
2008-09-04 19:09:34 +02:00
|
|
|
|
2008-09-07 22:49:10 +02:00
|
|
|
if (dialog->isVisible()) {
|
|
|
|
dialog->hide();
|
|
|
|
} else {
|
|
|
|
dialog->show();
|
|
|
|
KWindowSystem::setState(dialog->winId(), NET::SkipTaskbar | NET::SkipPager);
|
|
|
|
}
|
2008-09-02 16:54:58 +02:00
|
|
|
|
2008-09-07 22:49:10 +02:00
|
|
|
dialog->clearFocus();
|
2008-07-17 11:30:57 +02:00
|
|
|
}
|
2008-07-28 01:23:57 +02:00
|
|
|
}
|
2008-07-17 11:30:57 +02:00
|
|
|
|
2008-07-28 01:23:57 +02:00
|
|
|
void PopupAppletPrivate::hideTimedPopup()
|
|
|
|
{
|
|
|
|
timer->stop();
|
|
|
|
q->hidePopup();
|
2008-07-17 11:30:57 +02:00
|
|
|
}
|
|
|
|
|
2008-08-28 16:32:48 +02:00
|
|
|
void PopupAppletPrivate::dialogSizeChanged()
|
|
|
|
{
|
|
|
|
//Reposition the dialog.
|
|
|
|
if (dialog) {
|
|
|
|
dialog->updateGeometry();
|
|
|
|
dialog->move(q->popupPosition(dialog->size()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-01 12:45:33 +02:00
|
|
|
void PopupAppletPrivate::dialogStatusChanged(bool status)
|
|
|
|
{
|
|
|
|
q->popupEvent(status);
|
|
|
|
}
|
|
|
|
|
2008-07-17 11:30:57 +02:00
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "popupapplet.moc"
|
|
|
|
|