Merge branch 'central-widget'
[krunner.git] / raptorslide.cpp
blobb843e781f7708107cc263302d6ae9d4c2a393520
1 #include "raptorslide.h"
2 #include <KDebug>
3 #include <QTimer>
5 class RaptorSlide::Private
7 public:
8 Private(){}
9 ~Private(){}
10 // ItemList m_itemList; //Hash of ponters indexed with integer key
11 int m_index;//integer index for the slide
12 int m_count;//current itemcount
13 int m_max; // Max number of this slide.
14 QString m_name;//name of the slide
15 float m_slideLength;
16 bool m_visible;
17 float x;
18 float y;
19 int width;
20 int height;
21 RaptorSlide::Direction m_SlideLayout;
22 float opacity;
23 QTimer * hideTimer;
24 int ncol;
25 int nrow;
26 typedef QHash<int,QPoint> GridHash;
27 GridHash ghash;
30 RaptorSlide::RaptorSlide(QGraphicsItem *parent)
31 : QGraphicsItemGroup(parent)
33 d = new Private;
35 /*default states*/
36 d->m_name = "";
37 d->m_count =0;
38 d->m_max = 4;
39 d->m_slideLength = 0.0;
40 d->m_SlideLayout = Verticle;
41 d->m_visible=false;
42 d->x = 0.0;
43 d->y = 0.0;
44 d->opacity = 1.0;
45 d->hideTimer = new QTimer(this);
46 connect(d->hideTimer,SIGNAL(timeout()),this,SLOT(animate()));
47 d->hideTimer->stop();
48 d->nrow= 2;
49 d->ncol=2;
52 RaptorSlide::~RaptorSlide()
54 delete d;
58 QMap<RaptorClaw*,RaptorSlide*>& RaptorSlide::groupMap()
60 static GroupMap *group = 0;
61 if ( !group )
63 group = new GroupMap;
64 return *group;
69 QString RaptorSlide::name()
71 return d->m_name;
74 void RaptorSlide::setName(const QString &name)
76 d->m_name = name;
79 bool RaptorSlide::addItem(RaptorClaw *item)
81 if (item == NULL) {
82 kDebug() << "Null Item not added" << endl;
83 return false;
85 items.prepend(item);
86 d->m_count++;
87 doLayoutShift();
88 return true;
91 void RaptorSlide::setDirection(RaptorSlide::Direction layout)
93 d->m_SlideLayout = layout;
94 doLayoutShift();
97 RaptorSlide::Direction RaptorSlide::direction()
99 return d->m_SlideLayout;
102 void RaptorSlide::doLayoutShift()
104 float len = (direction() == Horizontal)?this->d->x:this->d->y;
106 if (direction() == Grid) {
107 //Calcaulate Gird values.
108 int itemHeight=0;
109 int itemWidth=0;
110 int totalHeight = itemHeight;
111 int totalWidth = itemWidth;
113 for (int i = 0 ; i < items.size();i++) {
114 itemWidth = items.at(i)->boundingRect().width();
115 itemHeight = items.at(i)->boundingRect().height();
116 totalWidth +=itemWidth;
117 totalHeight +=itemHeight;
120 int rowcount = 0;
121 int x = 0;
122 int y = 0;
124 kDebug() << "item height , width" << totalWidth << totalHeight;
126 for (int i =-0; i < items.size(); i++ ) {
127 if (rowcount == 2) {
128 x=0;
129 y+=itemHeight/2;
131 d->ghash[i] = QPoint(x,y);
132 items.at(i)->setPos(d->ghash[i]);
133 x+=itemWidth/2;
134 rowcount++;
137 return;
140 for (int i = 0; i < items.size(); ++i) {
141 if (direction() == Horizontal) {
142 items.at(i)->setPos(len/2,items.at(0)->y());
143 len += items.at(i)->boundingRect().width();
144 } else {
145 //item at zero to avoice starecase effect
146 items.at(i)->setPos(items.at(0)->x(),len/2);
147 len += items.at(i)->boundingRect().height();
150 d->height = len;
153 bool RaptorSlide::deleteItem(RaptorClaw *item)
155 removeFromGroup(item);
156 delete item;
157 item = 0;
158 return true;
161 RaptorClaw* RaptorSlide::find(const QString &key)
165 void RaptorSlide::show()
167 for (int i = 0; i < items.size(); ++i)
169 kDebug() << "Showing:"<< items.at(i)->name();
170 items.at(i)->show();
171 items.at(i)->setOpacity(1.0);
172 items.at(i)->update();
175 d->m_visible = true;
178 void RaptorSlide::move(float x, float y , float z)
180 d->x = x;
181 d->y = y;
183 if (items.count() >= 1) {
184 items.at(0)->setPos(x,y);
187 doLayoutShift();
190 bool RaptorSlide::isFull()
192 if (d->m_max == d->m_count) {
193 return true;
195 return false;
198 void RaptorSlide::hide()
200 // d->hideTimer->start(100);
201 for (int i = 0; i < items.size(); ++i)
203 items.at(i)->hide();
205 this->d->m_visible = false;
208 bool RaptorSlide::isVisible()
210 return d->m_visible;
213 int RaptorSlide::height()
215 return d->height;
218 void RaptorSlide::animate()
220 d->opacity = 1.0;
223 #include "raptorslide.moc"