Revert previous commit, was incorrect
[amarok.git] / src / context / containments / ColumnApplet.cpp
blobf3e54744e906a9466e1335bb3f900686ea57d309
1 /***************************************************************************
2 * copyright : (C) 2007 Leo Franchi <lfranchi@gmail.com> *
3 **************************************************************************/
5 /***************************************************************************
6 * *
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. *
11 * *
12 ***************************************************************************/
14 #include "ColumnApplet.h"
16 #include "ContextScene.h"
17 #include "debug.h"
18 #include "Svg.h"
20 #include "plasma/layouts/layoutanimator.h"
21 #include "plasma/phase.h"
23 #include <KAuthorized>
24 #include <KMenu>
26 #include <QAction>
27 #include <QGraphicsScene>
28 #include <QTimeLine>
30 namespace Context
33 ColumnApplet::ColumnApplet( QObject *parent, const QVariantList &args )
34 : Context::Containment( parent, args )
35 , m_actions( 0 )
36 , m_defaultColumnSize( 450 )
38 DEBUG_BLOCK
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 );
47 m_logo->resize();
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 )
68 DEBUG_BLOCK
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";
74 if( applet != 0 )
76 KConfigGroup cg( &conf, QString::number( applet->id() ) );
77 debug() << "saving applet" << applet->name();
78 cg.writeEntry( "plugin", applet->pluginName() );
81 conf.sync();
84 void ColumnApplet::loadConfig( KConfig& conf )
86 DEBUG_BLOCK
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 );
96 recalculate();
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
107 return m_geometry;
109 // call this when the view changes size: e.g. layout needs to be recalculated
110 void ColumnApplet::updateSize() // SLOT
112 DEBUG_BLOCK
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)
121 Q_UNUSED( option );
122 painter->save();
123 m_background->paint( painter, rect );
124 painter->restore();
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 ) );
130 painter->save();
131 m_logo->paint( painter, QRectF( pos.width() - 10.0, pos.height() - 5.0, size.width(), size.height() ) );
132 painter->restore();
136 void ColumnApplet::appletRemoved( QObject* object ) // SLOT
138 Q_UNUSED( object )
139 recalculate();
142 // parts of this code come from Qt 4.3, src/gui/graphicsview/qgraphicsitem.cpp
143 void ColumnApplet::mousePressEvent( QGraphicsSceneMouseEvent* event )
145 DEBUG_BLOCK
146 if (event->button() == Qt::LeftButton && (flags() & ItemIsSelectable))
148 bool multiSelect = (event->modifiers() & Qt::ControlModifier) != 0;
149 if (!multiSelect) {
150 if (!isSelected()) {
151 if (scene())
152 scene()->clearSelection();
153 setSelected(true);
156 } else if (!(flags() & ItemIsMovable)) {
157 event->ignore();
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;
170 if (event->widget())
171 view = qobject_cast<QGraphicsView *>(event->widget()->parentWidget());
173 if ((flags() & ItemIsMovable) && (!parentItem() || !parentItem()->isSelected())) {
174 QPointF diff;
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())));
188 } else
190 diff = mapToParent(event->pos()) - mapToParent(event->lastPos());
193 moveBy(diff.x(), diff.y());
195 if (flags() & ItemIsSelectable)
196 setSelected(true);
204 Applet* ColumnApplet::addApplet( Applet* applet )
206 DEBUG_BLOCK
207 debug() << "m_columns:" << m_columns;
208 m_columns->addItem( applet );
210 recalculate();
211 return applet;
214 void ColumnApplet::recalculate()
216 debug() << "got child item that wants a recalculation";
217 m_columns->invalidate();
220 QList<QAction*> ColumnApplet::contextActions()
222 DEBUG_BLOCK
223 return *m_actions;
226 void ColumnApplet::launchAppletBrowser() // SLOT
228 DEBUG_BLOCK
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...
238 Q_ASSERT(applet!=0);
240 return false;
243 void ColumnApplet::destroyApplet()
245 QAction *action = qobject_cast<QAction*>(sender());
247 if (!action) {
248 return;
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);
261 if (applet) {
262 applet->destroy();
269 } // Context namespace
271 #include "ColumnApplet.moc"