*
[groove] / groove.cpp
1 #include "groove.h"
2 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)
3 #include "qmaemo5rotator.h"
4 #include <QtDBus>
5 #endif
6
7
8 groove::groove(QWidget *parent) :
9     QWidget(parent)
10 {
11     //mpg = new mpgplayer();
12     //mpg->start();
13     mBar = new QMenuBar();
14     //mBar->addAction("test");
15     sMethod = new QPushButton("Song:");
16     lineEdit = new QLineEdit("");
17     player = new sPlayer();
18     QHBoxLayout *layout = new QHBoxLayout();
19     QVBoxLayout *vlayout = new QVBoxLayout();
20     //QHBoxLayout *bottomLayout = new QHBoxLayout();
21     button = new QPushButton("Search");
22     QPushButton *dButton = new QPushButton("Queue");
23     QPushButton *stopButton = new QPushButton("Pause");
24     QPushButton *moreButton = new QPushButton("...");
25     QPushButton *nextB = new QPushButton("->");
26     topBar *ok = new topBar(this);
27     resultView = new QTableView();
28     QMenu *pushMenu = new QMenu();
29     //showFullScreen();
30     lineEdit->insert("");
31     lineEdit->setDisabled(true);
32     pushMenu->addAction("Song:");
33     //pushMenu->addAction("Artist:");
34     //pushMenu->addAction("Album:");
35     QMenu *moreAction = new QMenu();
36     //moreAction->addAction("Playlist");
37     connect(moreAction->addAction("Play Now"),SIGNAL(triggered()),this,SLOT(play()));
38     //connect(moreAction->addAction("Show download Progress"),SIGNAL(triggered()),pd,SLOT(show()));
39     moreButton->setMenu(moreAction);
40
41     //sMethod->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Maximum);
42     sMethod->setMaximumWidth(sMethod->sizeHint().rwidth());
43     sMethod->setMenu(pushMenu);
44     model = new  QStandardItemModel();
45     model->setHorizontalHeaderLabels(
46         QStringList() << "Please wait for initialization"
47                       /*<< "Artist"*/);
48     resultView->setModel(model);
49     resultView->verticalHeader()->hide();
50     resultView->horizontalHeader()->setStretchLastSection(true);
51     resultView->setSelectionBehavior(QAbstractItemView::SelectRows);
52     resultView->setSelectionMode(QAbstractItemView::SingleSelection);
53     resultView->setEditTriggers(QAbstractItemView::NoEditTriggers);
54     resultView->setColumnHidden(2,true);
55     resultView->setColumnHidden(3,true);
56     resultView->setAutoScroll(false);
57     resultView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
58     /*QPalette pal = resultView->palette();
59     pal.setBrush(QPalette::Highlight,QBrush(Qt::transparent,Qt::NoBrush));
60     resultView->setPalette(pal);*/
61     portrait = false;
62     layout->addWidget(sMethod);
63 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)
64     rot = new QMaemo5Rotator(QMaemo5Rotator::AutomaticBehavior,this);
65 #endif
66     //this->setAttribute(Qt::WA_Maemo5AutoOrientation);
67     layout->addWidget(lineEdit);
68     layout->addWidget(button);
69     vlayout->setContentsMargins(QMargins(0,0,0,0));
70     vlayout->addWidget(ok);
71     //vlayout->addLayout(layout);
72     stack = new QStackedWidget();
73     stack->addWidget(resultView);
74
75     vlayout->addWidget(stack);
76     //vlayout->addLayout(bottomLayout);
77     bBar = new bottomBar();
78     vlayout->addWidget(bBar);
79
80     /*bottomLayout->addWidget(dButton);
81     bottomLayout->addWidget(stopButton);
82     bottomLayout->addWidget(nextB);
83     bottomLayout->addWidget(moreButton);*/
84     vlayout->setMenuBar(mBar);
85     vlayout->setContentsMargins(QMargins(0,0,0,0));
86     setLayout(vlayout);
87     setWindowTitle("Groove");
88     //Create New Grooveshark connection
89     gs = new gscom();
90     //Connections
91     connect(button, SIGNAL(clicked()), this, SLOT(search()));
92     connect(gs, SIGNAL(finishedSearch()), this, SLOT(finishedS()));
93     connect(lineEdit,SIGNAL(returnPressed()),this, SLOT(search()));
94     connect(pushMenu,SIGNAL(triggered(QAction*)),this,SLOT(changeS(QAction*)));
95     connect(dButton,SIGNAL(clicked()),this, SLOT(addSongPlaylist()));
96     connect(stopButton,SIGNAL(clicked()),this,SLOT(stop()));
97     connect(moreButton,SIGNAL(clicked()),this,SLOT(moreB()));
98     //connect(rotator,SIGNAL(orientationChanged(Orientation)),this,SLOT(orientationChanged()));
99     pl = new playlist(this);
100     pl->setGscom(gs);
101     player->setPlaylist(pl);
102     connect(pl,SIGNAL(downloadProgress(int,qint64,qint64)),this,SLOT(progressUpdate(int,qint64,qint64)));
103     //connect(pl,SIGNAL(bufferReady(int)),pd,SLOT(close()));
104     connect(pl,SIGNAL(freeze(bool)),resultView,SLOT(setDisabled(bool)));
105     connect(pl,SIGNAL(freeze(bool)),pushMenu,SLOT(setDisabled(bool)));
106     connect(pl,SIGNAL(freeze(bool)),dButton,SLOT(setDisabled(bool)));
107     connect(nextB,SIGNAL(clicked()),player,SLOT(playNext()));
108     connect(ok,SIGNAL(changeTask()),this,SLOT(showOthers()));
109     connect(ok,SIGNAL(searchRequest(QString)),this,SLOT(performSearch(QString)));
110     connect(ok,SIGNAL(closeApp()),this,SLOT(close()));
111     connect(bBar,SIGNAL(addB()),this,SLOT(addSongPlaylist()));
112     connect(bBar,SIGNAL(nextB()),player,SLOT(playNext()));
113     connect(bBar,SIGNAL(pause()),this,SLOT(stop()));
114
115     connect(bBar,SIGNAL(back()),player,SLOT(back()));
116     bBar->setPlaybackProgress(100,100);
117     pwindow = new pWin();
118     stack->addWidget(pwindow);
119     stack->setCurrentWidget(resultView);
120     connect(bBar,SIGNAL(list()),this,SLOT(togglePlaylist()));
121
122 }
123 void groove::togglePlaylist()
124 {
125     if(stack->currentWidget()==pwindow)
126         stack->setCurrentWidget(resultView);
127     else
128         stack->setCurrentWidget(pwindow);
129 }
130
131 void groove::performSearch(QString s)
132 {
133     qDebug() << s;
134     resultView->setModel(gs->getSongModel(s));
135 }
136
137 void groove::search()
138 {
139     if(!lineEdit->text().isEmpty())
140     {
141     if(lineEdit->isEnabled())
142     {
143
144         if(sMethod->text().compare("Song:")==0)
145             resultView->setModel(gs->getSongModel(lineEdit->text()));
146         else if(sMethod->text().compare("Artist:")==0)
147             resultView->setModel(gs->getArtistModel(lineEdit->text()));
148         else if(sMethod->text().compare("Album")==0)
149             resultView->setModel(gs->getAlbumModel(lineEdit->text()));
150         else
151             return;
152
153
154         button->setText("Stop");
155         lineEdit->setDisabled(true);
156     }
157     else
158     {
159         resultView->setModel(model);
160         button->setText("Search");
161         resultView->setColumnWidth(0,resultView->viewport()->width()/2);
162         resultView->setColumnWidth(1,resultView->viewport()->width()/2);
163         lineEdit->setDisabled(false);
164     }
165     }
166 }
167 void groove::finishedS()
168 {
169     model = gs->getModel();
170     resultView->setModel(model);
171     button->setText("Search");
172     resultView->setColumnWidth(0,resultView->viewport()->width()/2);
173     resultView->setColumnWidth(1,resultView->viewport()->width()/2);
174     lineEdit->setDisabled(false);
175     resultView->setColumnHidden(2,true);
176 }
177 void groove::changeS( QAction * action)
178 {
179     sMethod->setText(action->text());
180     sMethod->setMaximumWidth(sMethod->sizeHint().rwidth());
181 }
182
183 void groove::showOthers()
184 {
185 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)
186     QDBusConnection c = QDBusConnection::sessionBus();
187     QDBusMessage m = QDBusMessage::createSignal("/", "com.nokia.hildon_desktop", "exit_app_view");
188
189     c.send(m);
190 #endif
191 }
192
193 void groove::play()
194 {
195     QModelIndexList selected = resultView->selectionModel()->selectedRows(0);
196     if(!selected.isEmpty())
197     {
198         QStandardItem *item = model->item(selected.first().row(),2);
199         if(item == 0)
200             return;
201         //gs->getSong();
202         player->play(pl->addSong(item));
203     }
204     //selected.
205     //if
206 }
207 void groove::addSongPlaylist()
208 {
209     QModelIndexList selected = resultView->selectionModel()->selectedRows(0);
210     if(!selected.isEmpty())
211     {
212         QStandardItem *item = model->item(selected.first().row(),2);
213         if(item == 0)
214             return;
215         //gs->getSong();
216         if(pl->currentplaying() == -1)
217         {
218             player->play(pl->addSong(item));
219         }
220         else
221             pl->addSong(item);
222         model->item(selected.first().row(),1)->setText("Added to Playlist");;
223         pwindow->addSong(model->item(selected.first().row(),0)->text());
224     }
225
226 }
227
228 void groove::stop()
229 {
230     player->pause();
231     //mpg->pause();
232 }
233 void groove::moreB()
234 {
235     qDebug() << "He pressed the button";
236 }
237 void groove::progressUpdate(int p, qint64 d, qint64 t)
238 {
239     //if(!pd->isHidden())
240     //{
241     bBar->setPlaybackProgress(d,t);
242     //}
243 }
244
245 void groove::orientationChanged()
246 {
247 #ifdef Q_WS_MAEMO_5
248     QRect screenGeometry = QApplication::desktop()->screenGeometry();
249     if (screenGeometry.width() > screenGeometry.height())
250         portrait = false;
251     else
252         portrait = true;
253 #endif
254 }