1 /***************************************************************************
2 * copyright : (C) 2007 Leo Franchi <lfranchi@gmail.com> *
3 **************************************************************************/
5 /***************************************************************************
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 ***************************************************************************/
14 #include "ColumnApplet.h"
16 #include "ContextScene.h"
20 #include "plasma/layouts/layoutanimator.h"
21 #include "plasma/phase.h"
23 #include <KAuthorized>
27 #include <QGraphicsScene>
33 ColumnApplet::ColumnApplet( QObject
*parent
, const QVariantList
&args
)
34 : Context::Containment( parent
, args
)
36 , m_defaultColumnSize( 450 )
40 setContainmentType( CustomContainment
);
42 m_columns
= new ContextLayout( this );
43 m_columns
->setColumnWidth( m_defaultColumnSize
);
45 m_background
= new Svg( "widgets/amarok-wallpaper", this );
46 m_logo
= new Svg( "widgets/amarok-logo", this );
48 m_width
= 300; // TODO hardcoding for now, do we want this configurable?
49 m_aspectRatio
= (qreal
)m_logo
->size().height() / (qreal
)m_logo
->size().width();
50 m_logo
->resize( (int)m_width
, (int)( m_width
* m_aspectRatio
) );
52 connect( this, SIGNAL( appletAdded( Plasma::Applet
* ) ), this, SLOT( addApplet( Plasma::Applet
* ) ) );
55 m_appletBrowserAction
= new QAction(i18n("Add applet"), this);
56 connect(m_appletBrowserAction
, SIGNAL(triggered(bool)), this, SLOT(launchAppletBrowser()));
57 // set up default context menu actions
58 m_actions
= new QList
<QAction
*>();
59 m_actions
->append( m_appletBrowserAction
);
61 m_appletBrowser
= new Plasma::AppletBrowser( this );
62 m_appletBrowser
->setApplication( "amarok" );
63 m_appletBrowser
->hide();
66 void ColumnApplet::saveToConfig( KConfig
& conf
)
69 debug() << "number of m_columns:" << m_columns
->count();
70 for( int i
= 0; i
< m_columns
->count(); i
++ )
72 Applet
* applet
= dynamic_cast< Applet
* >( m_columns
->itemAt( i
) );
73 debug() << "trying to save an applet";
76 KConfigGroup
cg( &conf
, QString::number( applet
->id() ) );
77 debug() << "saving applet" << applet
->name();
78 cg
.writeEntry( "plugin", applet
->pluginName() );
84 void ColumnApplet::loadConfig( KConfig
& conf
)
87 foreach( const QString
& group
, conf
.groupList() )
89 KConfigGroup
cg( &conf
, group
);
90 QString plugin
= cg
.readEntry( "plugin", QString() );
91 debug() << "loading applet:" << plugin
92 << QStringList() << group
.toUInt();
93 if( plugin
!= QString() )
94 Plasma::Containment::addApplet( plugin
);
99 QSizeF
ColumnApplet::sizeHint() const
101 debug() << "column applet returning size hint:" << m_geometry
.size();
102 return m_geometry
.size();
105 QRectF
ColumnApplet::boundingRect() const
109 // call this when the view changes size: e.g. layout needs to be recalculated
110 void ColumnApplet::updateSize() // SLOT
113 m_geometry
= scene()->sceneRect();
114 debug() << "setting geometry:" << scene()->sceneRect();
115 if( scene() ) m_columns
->setGeometry( scene()->sceneRect() );
116 setGeometry( scene()->sceneRect() );
119 void ColumnApplet::paintInterface(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, const QRect
& rect
)
123 m_background
->paint( painter
, rect
);
125 QSize size
= m_logo
->size();
127 QSize pos
= rect
.size() - size
;
128 qreal newHeight
= m_aspectRatio
* m_width
;
129 m_logo
->resize( QSize( (int)m_width
, (int)newHeight
) );
131 m_logo
->paint( painter
, QRectF( pos
.width() - 10.0, pos
.height() - 5.0, size
.width(), size
.height() ) );
136 void ColumnApplet::appletRemoved( QObject
* object
) // SLOT
142 // parts of this code come from Qt 4.3, src/gui/graphicsview/qgraphicsitem.cpp
143 void ColumnApplet::mousePressEvent( QGraphicsSceneMouseEvent* event )
146 if (event->button() == Qt::LeftButton && (flags() & ItemIsSelectable))
148 bool multiSelect = (event->modifiers() & Qt::ControlModifier) != 0;
152 scene()->clearSelection();
156 } else if (!(flags() & ItemIsMovable)) {
161 // parts of this code come from Qt 4.3, src/gui/graphicsview/qgraphicsitem.cpp
162 void ColumnApplet::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
164 // debug() << "layout manager got a mouse event";
165 if ( ( event->buttons() & Qt::LeftButton ) )
168 // Find the active view.
169 QGraphicsView *view = 0;
171 view = qobject_cast<QGraphicsView *>(event->widget()->parentWidget());
173 if ((flags() & ItemIsMovable) && (!parentItem() || !parentItem()->isSelected())) {
175 if (flags() & ItemIgnoresTransformations) {
176 // Root items that ignore transformations need to
177 // calculate their diff by mapping viewport coordinates to
178 // parent coordinates. Items whose ancestors ignore
179 // transformations can ignore this problem; their events
180 // are already mapped correctly.
181 QTransform viewToParentTransform = (sceneTransform() * view->viewportTransform()).inverted();
183 QTransform myTransform = transform().translate(pos().x(), pos().y());
184 viewToParentTransform = myTransform * viewToParentTransform;
186 diff = viewToParentTransform.map(QPointF(view->mapFromGlobal(event->screenPos())))
187 - viewToParentTransform.map(QPointF(view->mapFromGlobal(event->lastScreenPos())));
190 diff = mapToParent(event->pos()) - mapToParent(event->lastPos());
193 moveBy(diff.x(), diff.y());
195 if (flags() & ItemIsSelectable)
204 Applet
* ColumnApplet::addApplet( Applet
* applet
)
207 debug() << "m_columns:" << m_columns
;
208 m_columns
->addItem( applet
);
214 void ColumnApplet::recalculate()
216 debug() << "got child item that wants a recalculation";
217 m_columns
->invalidate();
220 QList
<QAction
*> ColumnApplet::contextActions()
226 void ColumnApplet::launchAppletBrowser() // SLOT
229 m_appletBrowser
->show();
232 bool ColumnApplet::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
234 Applet *applet = qgraphicsitem_cast<Applet*>(watched);
235 //QEvent::GraphicsSceneHoverEnter
237 // Otherwise we're watching something we shouldn't be...
243 void ColumnApplet::destroyApplet()
245 QAction
*action
= qobject_cast
<QAction
*>(sender());
251 Applet
*applet
= qobject_cast
<Applet
*>(action
->data().value
<QObject
*>());
252 Plasma::Phase::self()->animateItem(applet
, Plasma::Phase::Disappear
);
255 void ColumnApplet::appletDisappearComplete(QGraphicsItem
*item
, Plasma::Phase::Animation anim
)
257 if (anim
== Plasma::Phase::Disappear
) {
258 if (item
->parentItem() == this) {
259 Applet
*applet
= qgraphicsitem_cast
<Applet
*>(item
);
269 } // Context namespace
271 #include "ColumnApplet.moc"