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/widgets/layoutanimator.h"
23 #include <QGraphicsScene>
29 ColumnApplet::ColumnApplet( QObject
*parent
, const QVariantList
&args
)
30 : Context::Containment( parent
, args
)
31 , m_defaultColumnSize( 450 )
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 );
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
56 // TODO wait until this is completely implemented in plasma
57 // and for it to not crash....
58 // foreach( Plasma::VBoxLayout* column, m_layout )
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);
71 void ColumnApplet::saveToConfig( KConfig
& conf
)
74 for( int i
= 0; i
< m_columns
->count(); i
++ )
76 Applet
* applet
= dynamic_cast< Applet
* >( m_columns
->itemAt( i
) );
79 KConfigGroup
cg( &conf
, QString::number( applet
->id() ) );
80 debug() << "saving applet" << applet
->name();
81 cg
.writeEntry( "plugin", applet
->pluginName() );
87 // for( int i = 0; i < m_layout.size(); i++ )
89 // for( int k = 0; k < m_layout[ i ]->count() ; k++ )
91 // Applet* applet = dynamic_cast< Applet* >( m_layout[ i ]->itemAt( k ) );
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 ) );
105 void ColumnApplet::loadConfig( KConfig
& conf
)
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() ) );
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() );
128 Applet* applet = scene->addApplet( cg.readEntry( "plugin", QString() ),
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
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 );
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;
159 // foreach( Plasma::VBoxLayout* column, m_layout )
161 // width += column->sizeHint().width();
165 // call this when the view changes size: e.g. layout needs to be recalculated
166 void ColumnApplet::updateSize() // SLOT
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
,
180 debug() << "painting in:" << rect
;
182 m_background
->paint( painter
, rect
);
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
) );
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() ) );
196 void ColumnApplet::appletRemoved( QObject
* object
) // SLOT
199 // basically we want to reshuffle the columns since we know something is gone
203 // parts of this code come from Qt 4.3, src/gui/graphicsview/qgraphicsitem.cpp
204 void ColumnApplet::mousePressEvent( QGraphicsSceneMouseEvent* event )
207 if (event->button() == Qt::LeftButton && (flags() & ItemIsSelectable))
209 bool multiSelect = (event->modifiers() & Qt::ControlModifier) != 0;
213 scene()->clearSelection();
217 } else if (!(flags() & ItemIsMovable)) {
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;
232 view = qobject_cast<QGraphicsView *>(event->widget()->parentWidget());
234 if ((flags() & ItemIsMovable) && (!parentItem() || !parentItem()->isSelected())) {
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())));
251 diff = mapToParent(event->pos()) - mapToParent(event->lastPos());
254 moveBy(diff.x(), diff.y());
256 if (flags() & ItemIsSelectable)
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
);
270 Applet
* ColumnApplet::addApplet( Applet
* applet
)
273 debug() << "m_columns:" << m_columns
;
274 m_columns
->addItem( applet
);
276 connect( applet
, SIGNAL( changed() ), this, SLOT( recalculate() ) );
280 void ColumnApplet::recalculate()
283 debug() << "got child item that wants a recalculation";
284 m_columns
->setGeometry( m_columns
->geometry() );
287 QList
<QAction
*> ColumnApplet::contextActions()
291 QList
<QAction
*> actions
;
292 actions
.append(m_appletBrowserAction
);
293 debug() << "returning actions:" << actions
;
297 void ColumnApplet::launchAppletBrowser() // SLOT
300 m_appletBrowser
->show();
304 } // Context namespace
306 #include "ColumnApplet.moc"