1 /***************************************************************************
2 * copyright : (C) 2007 Leo Franchi <lfranchi@gmail.com> *
4 * Significant parts of this code is inspired *
5 * and/or copied from KDE Plasma sources, available *
6 * at kdebase/workspace/plasma *
8 **************************************************************************/
10 /***************************************************************************
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
17 ***************************************************************************/
19 #include "ContextView.h"
22 #include "amarokconfig.h"
23 #include "ColumnApplet.h"
25 #include "ContextScene.h"
26 #include "DataEngineManager.h"
28 #include "enginecontroller.h"
33 #include <QPixmapCache>
34 #include <QWheelEvent>
36 #include <KPluginInfo>
37 #include <KServiceTypeTrader>
40 #define DEBUG_PREFIX "ContextView"
45 ContextView
* ContextView::s_self
= 0;
48 ContextView::ContextView( QWidget
* parent
)
49 : QGraphicsView( parent
)
50 , EngineObserver( EngineController::instance() )
59 //make sure cache is large enough for background
60 QPixmapCache::setCacheLimit ( 10 * 1024 );
63 // setFrameShape( QFrame::NoFrame );
64 setAutoFillBackground( true );
66 setScene( new ContextScene( rect(), this ) );
67 scene()->setItemIndexMethod( QGraphicsScene::BspTreeIndex
);
68 //TODO: Figure out a way to use rubberband and ScrollHandDrag
69 //setDragMode( QGraphicsView::RubberBandDrag );
70 setTransformationAnchor( QGraphicsView::AnchorUnderMouse
);
71 setCacheMode( QGraphicsView::CacheBackground
);
72 setInteractive( true );
73 setAcceptDrops( true );
74 setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff
);
75 setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff
);
76 setMouseTracking( true );
78 // here we initialize all the Plasma paths to Amarok paths
79 Theme::self()->setApplication( "amarok" );
80 contextScene()->setAppletMimeType( "text/x-amarokappletservicename" );
82 m_background
= new Svg( "widgets/amarok-wallpaper", this );
83 m_logo
= new Svg( "widgets/amarok-logo", this );
85 m_width
= 300; // TODO hardcoding for now, do we want this configurable?
86 m_aspectRatio
= (qreal
)m_logo
->size().height() / (qreal
)m_logo
->size().width();
87 m_logo
->resize( (int)m_width
, (int)( m_width
* m_aspectRatio
) );
89 m_columns
= new ColumnApplet();
90 scene()->addItem( m_columns
);
93 connect(scene(), SIGNAL( appletRemoved( QObject
* ) ), m_columns
, SLOT( appletRemoved( QObject
* ) ) );
99 ContextView::~ContextView()
105 void ContextView::clear()
110 void ContextView::clear( const ContextState
& state
)
112 QString name
= "amarok_";
116 else if( state
== Current
)
119 return; // startup, or some other wierd case
122 // now we save the state, remembering the column info etc
123 KConfig
appletConfig( name
);
124 // erase previous config
125 foreach( const QString
& group
, appletConfig
.groupList() )
126 appletConfig
.deleteGroup( group
);
128 m_columns
->saveToConfig( appletConfig
);
130 contextScene()->clearApplets();
134 void ContextView::engineStateChanged( Engine::State state
, Engine::State oldState
)
137 Q_UNUSED( oldState
); Q_UNUSED( state
);
141 case Engine::Playing
:
154 void ContextView::showHome()
160 messageNotify( m_curState
);
163 void ContextView::showCurrentTrack()
167 m_curState
= Current
;
169 messageNotify( Current
);
172 // loads applets onto the ContextScene from saved data, using m_curState
173 void ContextView::loadConfig()
176 QString cur
= "amarok_";
177 if( m_curState
== Home
)
178 cur
+= QString( "home" );
179 else if( m_curState
== Current
)
180 cur
+= QString( "current" );
183 contextScene()->clearApplets();
184 KConfig
appletConfig( cur
, KConfig::OnlyLocal
);
185 m_columns
->loadConfig( appletConfig
);
188 void ContextView::engineNewMetaData( const MetaBundle
&, bool )
194 Applet
* ContextView::addApplet(const QString
& name
, const QStringList
& args
)
196 QVariantList argList
;
197 QStringListIterator
i(args
);
199 argList
<< QVariant( i
.next() );
201 AppletPointer applet
= contextScene()->addApplet( name
, argList
);
203 return m_columns
->addApplet( applet
);
206 void ContextView::zoomIn()
208 //TODO: Change level of detail when zooming
213 void ContextView::zoomOut()
219 ContextScene
* ContextView::contextScene()
221 return static_cast<ContextScene
*>( scene() );
224 void ContextView::drawBackground( QPainter
* painter
, const QRectF
& rect
)
227 m_background
->paint( painter
, rect
);
229 QSize size
= m_logo
->size();
231 QSize pos
= m_background
->size() - size
;
232 qreal newHeight
= m_aspectRatio
* m_width
;
233 m_logo
->resize( QSize( (int)m_width
, (int)newHeight
) );
235 m_logo
->paint( painter
, QRectF( pos
.width() - 10.0, pos
.height() - 5.0, size
.width(), size
.height() ) );
240 void ContextView::resizeEvent( QResizeEvent
* event
)
244 if ( testAttribute( Qt::WA_PendingResizeEvent
) ) {
245 return; // lets not do this more than necessary, shall we?
248 scene()->setSceneRect( rect() );
250 m_background
->resize( width(), height() );
255 void ContextView::wheelEvent( QWheelEvent
* event
)
257 if ( scene() && scene()->itemAt( event
->pos() ) ) {
258 QGraphicsView::wheelEvent( event
);
262 if ( event
->modifiers() & Qt::ControlModifier
) {
263 if ( event
->delta() < 0 ) {
271 void ContextView::contextMenuEvent(QContextMenuEvent
*event
)
274 QGraphicsView::contextMenuEvent( event
);
278 QPointF point
= event
->pos();
279 QPointF globalPoint
= event
->globalPos();
281 QGraphicsItem
* item
= scene()->itemAt(point
);
282 Plasma::Applet
* applet
= 0;
285 applet
= qgraphicsitem_cast
<Plasma::Applet
*>(item
);
290 item
= item
->parentItem();
294 //kDebug() << "context menu event " << immutable;
296 if (contextScene() && contextScene()->isImmutable()) {
297 QGraphicsView::contextMenuEvent(event
);
301 //FIXME: change this to show this only in debug mode (or not at all?)
302 // before final release
304 } else if (applet
->isImmutable()) {
305 QGraphicsView::contextMenuEvent(event
);
308 //desktopMenu.addSeparator();
309 bool hasEntries
= false;
310 if (applet
->hasConfigurationInterface()) {
311 QAction
* configureApplet
= new QAction(i18n("%1 Settings...", applet
->name()), this);
312 connect(configureApplet
, SIGNAL(triggered(bool)),
313 applet
, SLOT(showConfigurationInterface()));
314 desktopMenu
.addAction(configureApplet
);
318 if (!contextScene() || !contextScene()->isImmutable()) {
319 QAction
* closeApplet
= new QAction(i18n("Close this %1", applet
->name()), this);
320 connect(closeApplet
, SIGNAL(triggered(bool)),
321 applet
, SLOT(deleteLater()));
322 desktopMenu
.addAction(closeApplet
);
327 QGraphicsView::contextMenuEvent(event
);
333 desktopMenu
.exec(globalPoint
.toPoint());
337 } // Context namespace
339 #include "ContextView.moc"