Disable lots of MediaDevicePlugin stuff to get Amarok to build and run. Also disable...
[amarok.git] / src / context / containments / ColumnApplet.cpp
blob0af0e515e5e5e856408bb624953e572008c50c4d
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/widgets/layoutanimator.h"
22 #include <QAction>
23 #include <QGraphicsScene>
24 #include <QTimeLine>
26 namespace Context
29 ColumnApplet::ColumnApplet( QObject *parent, const QVariantList &args )
30 : Context::Containment( parent, args )
31 , m_defaultColumnSize( 450 )
33 DEBUG_BLOCK
34 m_columns = new Plasma::FlowLayout( this );
35 // m_columns->setColumnWidth( m_defaultColumnSize );
37 m_background = new Svg( "widgets/amarok-wallpaper", this );
38 m_logo = new Svg( "widgets/amarok-logo", this );
39 m_logo->resize();
40 m_width = 300; // TODO hardcoding for now, do we want this configurable?
41 m_aspectRatio = (qreal)m_logo->size().height() / (qreal)m_logo->size().width();
42 m_logo->resize( (int)m_width, (int)( m_width * m_aspectRatio ) );
44 m_appletBrowserAction = new QAction(i18n("Add applet"), this);
45 connect(m_appletBrowserAction, SIGNAL(triggered(bool)), this, SLOT(launchAppletBrowser()));
46 // m_appletBrowser = new Plasma::AppletBrowser( this, "amarok" );
47 // m_appletBrowser->setParentApp( "amarok" );
48 // m_appletBrowser->hide();
51 // fetches size from scene and creates columns according.
52 // should only be called *ONCE* by the ContextView once it
53 // has been added to the scene
54 // void ColumnApplet::init() // SLOT
55 // {
56 // TODO wait until this is completely implemented in plasma
57 // and for it to not crash....
58 // foreach( Plasma::VBoxLayout* column, m_layout )
59 // {
60 //
61 // Plasma::LayoutAnimator* animator = new Plasma::LayoutAnimator;
62 // QTimeLine* timeLine = new QTimeLine;
63 // animator->setTimeLine(timeLine);
64 // animator->setEffect( Plasma::LayoutAnimator::InsertedState , Plasma::LayoutAnimator::FadeInMoveEffect );
65 // animator->setEffect( Plasma::LayoutAnimator::StandardState , Plasma::LayoutAnimator::MoveEffect );
66 // animator->setEffect( Plasma::LayoutAnimator::RemovedState , Plasma::LayoutAnimator::FadeOutMoveEffect );
67 // column->setAnimator(animator);
68 // }
69 // }
71 void ColumnApplet::saveToConfig( KConfig& conf )
73 DEBUG_BLOCK
74 for( int i = 0; i < m_columns->count(); i++ )
76 Applet* applet = dynamic_cast< Applet* >( m_columns->itemAt( i ) );
77 if( applet != 0 )
79 KConfigGroup cg( &conf, QString::number( applet->id() ) );
80 debug() << "saving applet" << applet->name();
81 cg.writeEntry( "plugin", applet->pluginName() );
84 conf.sync();
86 // TODO port
87 // for( int i = 0; i < m_layout.size(); i++ )
88 // {
89 // for( int k = 0; k < m_layout[ i ]->count() ; k++ )
90 // {
91 // Applet* applet = dynamic_cast< Applet* >( m_layout[ i ]->itemAt( k ) );
92 // if( applet != 0 )
93 // {
94 // KConfigGroup cg( &conf, QString::number( applet->id() ) );
95 // debug() << "saving applet" << applet->name();
96 // cg.writeEntry( "plugin", applet->pluginName() );
97 // cg.writeEntry( "column", QString::number( i ) );
98 // cg.writeEntry( "position", QString::number( k ) );
99 // }
100 // }
101 // }
102 // conf.sync();
105 void ColumnApplet::loadConfig( KConfig& conf )
107 DEBUG_BLOCK
108 foreach( const QString& group, conf.groupList() )
110 KConfigGroup cg( &conf, group );
111 debug() << "loading applet:" << cg.readEntry( "plugin", QString() )
112 << QStringList() << group.toUInt();
113 addApplet( cg.readEntry( "plugin", QString() ) );
115 // TODO port
117 m_layout.clear();
118 init();
119 foreach( const QString& group, conf.groupList() )
121 KConfigGroup cg( &conf, group );
122 debug() << "loading applet:" << cg.readEntry( "plugin", QString() )
123 << QStringList() << group.toUInt() << cg.readEntry( "column", QString() ) << cg.readEntry( "position", QString() );
125 ContextScene* scene = qobject_cast< ContextScene* >( this->scene() );
126 if( scene != 0 )
128 Applet* applet = scene->addApplet( cg.readEntry( "plugin", QString() ),
129 QVariantList(),
130 group.toUInt(),
131 QRectF() );
132 int column = cg.readEntry( "column", 1000 );
133 int pos = cg.readEntry( "position", 1000 );
134 debug() << "restoring applet to column:" << column << "and position:" << pos;
135 if( column >= m_layout.size() ) // was saved in a column that is not available now
136 addApplet( applet );
137 else
139 if( pos < m_layout[ column ]->count() ) // there is a position for it
140 m_layout[ column ]->insertItem( pos, applet );
141 else // just append it, there is no position for it
142 m_layout[ column ]->addItem( applet );
146 resizeColumns();*/
149 QSizeF ColumnApplet::sizeHint() const
151 debug() << "returning size hint:" << m_geometry.size();
152 return m_geometry.size();
155 QRectF ColumnApplet::boundingRect() const
157 // qreal width = 2*m_padding;
158 // qreal height = 0;
159 // foreach( Plasma::VBoxLayout* column, m_layout )
160 // {
161 // width += column->sizeHint().width();
163 return m_geometry;
165 // call this when the view changes size: e.g. layout needs to be recalculated
166 void ColumnApplet::updateSize() // SLOT
168 DEBUG_BLOCK
169 m_geometry = scene()->sceneRect();
170 debug() << "setting geometry:" << scene()->sceneRect();
171 if( scene() ) m_columns->setGeometry( scene()->sceneRect() );
172 setGeometry( scene()->sceneRect() );
175 void ColumnApplet::paintInterface(QPainter *painter,
176 const QStyleOptionGraphicsItem *option,
177 const QRect& rect)
179 DEBUG_BLOCK
180 debug() << "painting in:" << rect;
181 painter->save();
182 m_background->paint( painter, rect );
183 painter->restore();
184 QSize size = m_logo->size();
186 QSize pos = rect.size() - size;
187 qreal newHeight = m_aspectRatio * m_width;
188 m_logo->resize( QSize( (int)m_width, (int)newHeight ) );
189 painter->save();
190 debug() << "painting logo:" << QRectF( pos.width() - 10.0, pos.height() - 5.0, size.width(), size.height() );
191 m_logo->paint( painter, QRectF( pos.width() - 10.0, pos.height() - 5.0, size.width(), size.height() ) );
192 painter->restore();
196 void ColumnApplet::appletRemoved( QObject* object ) // SLOT
198 Q_UNUSED( object )
199 // basically we want to reshuffle the columns since we know something is gone
200 // resizeColumns();
203 // parts of this code come from Qt 4.3, src/gui/graphicsview/qgraphicsitem.cpp
204 void ColumnApplet::mousePressEvent( QGraphicsSceneMouseEvent* event )
206 DEBUG_BLOCK
207 if (event->button() == Qt::LeftButton && (flags() & ItemIsSelectable))
209 bool multiSelect = (event->modifiers() & Qt::ControlModifier) != 0;
210 if (!multiSelect) {
211 if (!isSelected()) {
212 if (scene())
213 scene()->clearSelection();
214 setSelected(true);
217 } else if (!(flags() & ItemIsMovable)) {
218 event->ignore();
222 // parts of this code come from Qt 4.3, src/gui/graphicsview/qgraphicsitem.cpp
223 void ColumnApplet::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
225 // debug() << "layout manager got a mouse event";
226 if ( ( event->buttons() & Qt::LeftButton ) )
229 // Find the active view.
230 QGraphicsView *view = 0;
231 if (event->widget())
232 view = qobject_cast<QGraphicsView *>(event->widget()->parentWidget());
234 if ((flags() & ItemIsMovable) && (!parentItem() || !parentItem()->isSelected())) {
235 QPointF diff;
236 if (flags() & ItemIgnoresTransformations) {
237 // Root items that ignore transformations need to
238 // calculate their diff by mapping viewport coordinates to
239 // parent coordinates. Items whose ancestors ignore
240 // transformations can ignore this problem; their events
241 // are already mapped correctly.
242 QTransform viewToParentTransform = (sceneTransform() * view->viewportTransform()).inverted();
244 QTransform myTransform = transform().translate(pos().x(), pos().y());
245 viewToParentTransform = myTransform * viewToParentTransform;
247 diff = viewToParentTransform.map(QPointF(view->mapFromGlobal(event->screenPos())))
248 - viewToParentTransform.map(QPointF(view->mapFromGlobal(event->lastScreenPos())));
249 } else
251 diff = mapToParent(event->pos()) - mapToParent(event->lastPos());
254 moveBy(diff.x(), diff.y());
256 if (flags() & ItemIsSelectable)
257 setSelected(true);
264 Applet* ColumnApplet::addApplet( const QString& name, const QVariantList& args, uint id, const QRectF &geometry, bool delayedInit )
266 Applet* applet = Plasma::Containment::addApplet( name, args, id, geometry, delayedInit );
267 addApplet( applet );
270 Applet* ColumnApplet::addApplet( Applet* applet )
272 DEBUG_BLOCK
273 debug() << "m_columns:" << m_columns;
274 m_columns->addItem( applet );
276 connect( applet, SIGNAL( changed() ), this, SLOT( recalculate() ) );
277 return applet;
280 void ColumnApplet::recalculate()
282 DEBUG_BLOCK
283 debug() << "got child item that wants a recalculation";
284 m_columns->setGeometry( m_columns->geometry() );
287 QList<QAction*> ColumnApplet::contextActions()
289 DEBUG_BLOCK
291 QList<QAction*> actions;
292 actions.append(m_appletBrowserAction);
293 debug() << "returning actions:" << actions;
294 return actions;
297 void ColumnApplet::launchAppletBrowser() // SLOT
299 DEBUG_BLOCK
300 m_appletBrowser->show();
304 } // Context namespace
306 #include "ColumnApplet.moc"