The API changed for rotations, requiring another argument for positions.
[contacts_plasmoid.git] / contacts.cpp
blob91812d45701e6da79d9270bc001d2e55e707d059
1 /////////////////////////////////////////////////////////////////////////
2 // contacts.cpp //
3 // //
4 // Copyright(C) 2009 Igor Trindade Oliveira <igor.oliveira@indt.org.br>//
5 // Copyright(C) 2009 Adenilson Cavalcanti <adenilson.silva@idnt.org.br>//
6 // //
7 // This library is free software; you can redistribute it and/or //
8 // modify it under the terms of the GNU Lesser General Public //
9 // License as published by the Free Software Foundation; either //
10 // version 2.1 of the License, or (at your option) any later version. //
11 // //
12 // This library is distributed in the hope that it will be useful, //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
15 // Lesser General Public License for more details. //
16 // //
17 // You should have received a copy of the GNU Lesser General Public //
18 // License along with this library; if not, write to the Free Software //
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA //
20 // 02110-1301 USA //
21 /////////////////////////////////////////////////////////////////////////
22 #include "contacts.h"
23 #include "contactbutton.h"
25 #include "experimental/qkineticlistcontroller.h"
26 #include "contactsview.h"
27 #include "contactsmodel.h"
28 #include "contactitem.h"
29 #include "contactwidget.h"
31 #include <kstandarddirs.h>
34 // TODO:
35 // - improve name, email and phone alignment in contactitem
36 // - test it with plasma netbook
37 // - fix the kinetic list (or write a new one)
38 // - research how to create text shadows
39 // - fix animation memory leak
40 // - provide an interface for contact data retrieval (i.e. stub)
42 K_EXPORT_PLASMA_APPLET(contactsPlasmoid, ContactsPlasmoid)
44 const int PLASMOID_WIDTH_BORD = 22;
46 ContactsPlasmoid::ContactsPlasmoid(QObject *parent, const QVariantList &args)
47 : Plasma::Applet( parent, args ), previousDim( 0 ), mOmnianimation( 0 ),
48 mLayout( 0 ), mOmniButton( 0 )
50 setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
53 ContactsPlasmoid::~ContactsPlasmoid()
55 delete previousDim;
56 delete mOmnianimation;
57 delete pulser;
60 void ContactsPlasmoid::init()
62 /* TODO: catch allocation exceptions */
63 mLayout = new QGraphicsLinearLayout( Qt::Vertical, this );
64 resize(400, 600);
65 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
66 mLayout->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
67 mGraphicsWidget = new QGraphicsWidget(this);
68 mGraphicsWidget->resize( 400, 600 );
69 mGraphicsLayout = new QGraphicsLinearLayout(Qt::Vertical, mGraphicsWidget);
71 controller = new QtKineticListController(this);
72 controller->setView(new ContactsView);
73 controller->setModel(new ContactsModel(controller));
74 controller->setOvershootEnabled(true);
75 controller->view()->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
77 mGraphicsLayout->addItem( controller->view() );
78 mLayout->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
79 mLayout->addItem( mGraphicsWidget );
81 mOmniWidget = new OmniWidget( this );
82 mOmniWidget->setImagePath( KStandardDirs::installPath("data") + "plasma-contacts/" );
83 mOmniWidget->createItems();
84 mOmniWidget->hide();
86 mOmniButton = new ContactButton( KStandardDirs::installPath("data") + "plasma-contacts/omnibutton.png", this );
87 connect( mOmniButton, SIGNAL( clicked() ), this, SLOT( makeVisible() ) );
88 connect( mOmniWidget, SIGNAL( addItemClicked() ), this, SLOT( hideWidget() ) );
89 mOmniButton->setPos(geometry().width() -
90 mOmniButton->geometry().width() - PLASMOID_WIDTH_BORD,
91 geometry().height() -
92 mOmniButton->geometry().height() - PLASMOID_WIDTH_BORD);
94 mHomeButton = new ContactButton( KStandardDirs::installPath( "data" ) + "plasma-contacts/homebutton.png", this );
95 mHomeButton->setPos( geometry().width() +
96 mHomeButton->geometry().width() + PLASMOID_WIDTH_BORD,
97 geometry().height() +
98 mHomeButton->geometry().height() + PLASMOID_WIDTH_BORD );
100 mContactWidget = new ContactWidget( this );
101 mContactWidget->resize( 400, 600 );
103 pulser = new Pulser<ContactButton> ( mOmniButton );
104 connect( mOmniButton, SIGNAL( clicked() ), pulser, SLOT( start() ) );
106 previousDim = new QRectF( geometry() );
107 mOmnianimation = new OmniAnimation(this, mOmniWidget, mOmniButton);
109 animation();
110 rotateAnimation();
113 void ContactsPlasmoid::animation()
115 qreal widthLayoutMargin, xWidgetMargin, yWidgetMargin;
116 getContentsMargins(0, 0, &xWidgetMargin, &yWidgetMargin);
117 mLayout->getContentsMargins(0, 0, &widthLayoutMargin, 0);
118 mOmnianimation->create( &size(), widthLayoutMargin, xWidgetMargin, yWidgetMargin );
121 void ContactsPlasmoid::rotateAnimation( )
123 QGraphicsRotation *r = new QGraphicsRotation( mGraphicsWidget );
124 r->setAxis( QVector3D( 0, 1, 0 ) );
125 qreal x( mGraphicsWidget->size().width( )/2 );
126 qreal y( mGraphicsWidget->size().height()/2 );
127 r->setOrigin( QVector3D( x, y, 0.0 ) );
128 QList<QGraphicsTransform *> r1;
129 r1.append( r );
130 mGraphicsWidget->setTransformations( r1 );
132 yRotationAnim = new QPropertyAnimation( r, "angle", mGraphicsWidget );
133 yRotationAnim->setStartValue( 0.0 );
134 yRotationAnim->setEndValue( 90.0 );
135 yRotationAnim->setDuration( 500 );
137 omniButtonAnim = new QPropertyAnimation( mOmniButton, "geometry", this );
138 QRectF tmpGeometry( mOmniButton->geometry() );
139 omniButtonAnim->setStartValue( tmpGeometry );
140 omniButtonAnim->setEndValue( QRectF( tmpGeometry.x() + tmpGeometry.width() + PLASMOID_WIDTH_BORD,
141 tmpGeometry.y() + tmpGeometry.height() + PLASMOID_WIDTH_BORD,
142 tmpGeometry.width(), tmpGeometry.height() ) );
144 seqAnimation = new QSequentialAnimationGroup( this );
145 seqAnimation->addAnimation( omniButtonAnim );
146 seqAnimation->addAnimation( yRotationAnim );
148 QGraphicsRotation *r2 = new QGraphicsRotation( mContactWidget );
149 r2->setAxis( QVector3D( 0, 1, 0 ) );
150 x = mContactWidget->size().width( )/2;
151 y = mContactWidget->size().height( )/2;
152 r2->setOrigin( QVector3D( x, y, 0 ) );
153 /* XXX: should be -90.0 */
154 r2->setAngle( -101.0 );
155 QList<QGraphicsTransform *> r3;
156 r3.append( r2 );
157 mContactWidget->setTransformations( r3 );
159 yWidgetRotationAnim = new QPropertyAnimation( r2, "angle", mContactWidget );
160 /* XXX: should be -90.0 */
161 yWidgetRotationAnim->setStartValue( -101.0 );
162 yWidgetRotationAnim->setEndValue( 0 );
163 yWidgetRotationAnim->setDuration( 500 );
165 homeButtonAnim = new QPropertyAnimation( mHomeButton, "geometry", this );
166 tmpGeometry = mHomeButton->geometry();
167 homeButtonAnim->setStartValue( tmpGeometry );
168 homeButtonAnim->setEndValue( QRectF( geometry().width() - tmpGeometry.width() - PLASMOID_WIDTH_BORD,
169 geometry().height() - tmpGeometry.height() - PLASMOID_WIDTH_BORD,
170 tmpGeometry.width(), tmpGeometry.height() ) );
172 seqAnimation->addAnimation( yWidgetRotationAnim );
173 seqAnimation->addAnimation( homeButtonAnim );
175 connect( controller->view(), SIGNAL( clicked( ) ), this, SLOT( forwardAnimation( ) ) );
176 connect( mHomeButton, SIGNAL( clicked() ), this, SLOT( backwardAnimation() ) );
179 void ContactsPlasmoid::resetRotateAnimation()
181 QRectF tmpGeometry( mOmniButton->geometry() );
182 omniButtonAnim->setStartValue( tmpGeometry );
183 omniButtonAnim->setEndValue( QRectF( tmpGeometry.x() + tmpGeometry.width() + PLASMOID_WIDTH_BORD,
184 tmpGeometry.y() + tmpGeometry.height() + PLASMOID_WIDTH_BORD,
185 tmpGeometry.width(), tmpGeometry.height() ) );
187 tmpGeometry = mHomeButton->geometry();
188 homeButtonAnim->setStartValue( tmpGeometry );
189 homeButtonAnim->setEndValue( QRectF( geometry().width() - tmpGeometry.width() - PLASMOID_WIDTH_BORD,
190 geometry().height() - tmpGeometry.height() - PLASMOID_WIDTH_BORD,
191 tmpGeometry.width(), tmpGeometry.height() ) );
194 void ContactsPlasmoid::makeVisible()
196 mOmniWidget->setVisible(true);
197 QtListModelInterface *model = controller->model();
198 QtGraphicsListView *view = controller->view();
199 for ( int i = 0; i < model->count(); ++i ) {
200 ContactItem *tmp = dynamic_cast<ContactItem*>( view->itemForIndex( i ) );
201 if ( tmp )
202 tmp->setOpacityChild( false );
207 void ContactsPlasmoid::hideWidget()
209 QtListModelInterface *model = controller->model();
210 QtGraphicsListView *view = controller->view();
211 for ( int i = 0; i < model->count(); ++i ) {
212 ContactItem *tmp = dynamic_cast<ContactItem*>( view->itemForIndex( i ) );
213 if ( tmp )
214 tmp->setOpacityChild( true );
219 QSizeF ContactsPlasmoid::sizeHint( Qt::SizeHint which, const QSizeF & constraint ) const
221 switch (which) {
222 case Qt::MinimumSize:
223 return QSizeF(240,320);
224 case Qt::PreferredSize:
225 qDebug() << size();
226 return QSizeF(geometry().width(), geometry().height());
227 case Qt::MaximumSize:
228 return QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
229 default:
230 break;
233 return QGraphicsWidget::sizeHint(which, constraint);
236 void ContactsPlasmoid::resizeEvent( QGraphicsSceneResizeEvent * event )
238 if( mOmniButton && mLayout && mOmnianimation ) {
239 qreal widthLayoutMargin, xWidgetMargin, yWidgetMargin;
240 getContentsMargins(0, 0, &xWidgetMargin, &yWidgetMargin);
242 if( mOmniButton->isClipped() ) {
243 mOmniButton->setPos(size().width() -
244 mOmniButton->size().width() - PLASMOID_WIDTH_BORD,
245 size().height() -
246 mOmniButton->size().height() - PLASMOID_WIDTH_BORD);
248 mHomeButton->setPos( size().width() +
249 mHomeButton->size().width() + PLASMOID_WIDTH_BORD,
250 size().height() +
251 mHomeButton->size().height() + PLASMOID_WIDTH_BORD );
252 } else {
253 mOmniButton->setPos( size( ).width( ) +
254 mOmniButton->size( ).width( ) + PLASMOID_WIDTH_BORD,
255 size( ).height( ) +
256 mOmniButton->size( ).height( ) + PLASMOID_WIDTH_BORD);
258 mHomeButton->setPos( size().width() -
259 mHomeButton->size().width() - PLASMOID_WIDTH_BORD,
260 size().height() -
261 mHomeButton->size().height() - PLASMOID_WIDTH_BORD );
264 mLayout->getContentsMargins(0, 0, &widthLayoutMargin, 0);
265 mOmnianimation->reset( &size(), widthLayoutMargin, xWidgetMargin, yWidgetMargin );
266 resetRotateAnimation();
268 Plasma::Applet::resizeEvent(event);
271 void ContactsPlasmoid::backwardAnimation()
273 seqAnimation->setDirection( QAbstractAnimation::Backward );
274 seqAnimation->start();
277 void ContactsPlasmoid::forwardAnimation()
279 seqAnimation->setDirection( QAbstractAnimation::Forward );
280 seqAnimation->start();