**
[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->addWidget(ok);
70     //vlayout->addLayout(layout);
71     vlayout->addWidget(resultView);
72     //vlayout->addLayout(bottomLayout);
73     bBar = new bottomBar();
74     vlayout->addWidget(bBar);
75     vlayout->setSpacing(0);
76     bottomLayout->addWidget(dButton);
77     bottomLayout->addWidget(stopButton);
78     bottomLayout->addWidget(nextB);
79     bottomLayout->addWidget(moreButton);
80     vlayout->setMenuBar(mBar);
81     vlayout->setContentsMargins(QMargins());
82     setLayout(vlayout);
83     setWindowTitle("Groove");
84     //Create New Grooveshark connection
85     gs = new gscom();
86     //Connections
87     connect(button, SIGNAL(clicked()), this, SLOT(search()));
88     connect(gs, SIGNAL(finishedSearch()), this, SLOT(finishedS()));
89     connect(lineEdit,SIGNAL(returnPressed()),this, SLOT(search()));
90     connect(pushMenu,SIGNAL(triggered(QAction*)),this,SLOT(changeS(QAction*)));
91     connect(dButton,SIGNAL(clicked()),this, SLOT(addSongPlaylist()));
92     connect(stopButton,SIGNAL(clicked()),this,SLOT(stop()));
93     connect(moreButton,SIGNAL(clicked()),this,SLOT(moreB()));
94     //connect(rotator,SIGNAL(orientationChanged(Orientation)),this,SLOT(orientationChanged()));
95     pl = new playlist(this);
96     pl->setGscom(gs);
97     player->setPlaylist(pl);
98     connect(pl,SIGNAL(downloadProgress(int,qint64,qint64)),this,SLOT(progressUpdate(int,qint64,qint64)));
99     //connect(pl,SIGNAL(bufferReady(int)),pd,SLOT(close()));
100     connect(pl,SIGNAL(freeze(bool)),resultView,SLOT(setDisabled(bool)));
101     connect(pl,SIGNAL(freeze(bool)),pushMenu,SLOT(setDisabled(bool)));
102     connect(pl,SIGNAL(freeze(bool)),dButton,SLOT(setDisabled(bool)));
103     connect(nextB,SIGNAL(clicked()),player,SLOT(playNext()));
104     connect(ok,SIGNAL(changeTask()),this,SLOT(showOthers()));
105     connect(ok,SIGNAL(searchRequest(QString)),this,SLOT(performSearch(QString)));
106     connect(ok,SIGNAL(closeApp()),this,SLOT(close()));
107     connect(bBar,SIGNAL(addB()),this,SLOT(addSongPlaylist()));
108     connect(bBar,SIGNAL(nextB()),player,SLOT(playNext()));
109     connect(bBar,SIGNAL(pause()),this,SLOT(stop()));
110
111     connect(bBar,SIGNAL(back()),player,SLOT(back()));
112     bBar->setPlaybackProgress(100,100);
113     pwindow = new pWin();
114
115 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)
116     this->setAttribute(Qt::WA_Maemo5StackedWindow);
117     pwindow->setParent(this);
118     pwindow->setWindowFlags(pwindow->windowFlags() | Qt::Window);
119     pwindow->setAttribute(Qt::WA_Maemo5StackedWindow);
120 #endif
121     pwindow->show();
122
123 }
124 void groove::performSearch(QString s)
125 {
126     qDebug() << s;
127     resultView->setModel(gs->getSongModel(s));
128 }
129
130 void groove::search()
131 {
132     if(!lineEdit->text().isEmpty())
133     {
134     if(lineEdit->isEnabled())
135     {
136
137         if(sMethod->text().compare("Song:")==0)
138             resultView->setModel(gs->getSongModel(lineEdit->text()));
139         else if(sMethod->text().compare("Artist:")==0)
140             resultView->setModel(gs->getArtistModel(lineEdit->text()));
141         else if(sMethod->text().compare("Album")==0)
142             resultView->setModel(gs->getAlbumModel(lineEdit->text()));
143         else
144             return;
145
146
147         button->setText("Stop");
148         lineEdit->setDisabled(true);
149     }
150     else
151     {
152         resultView->setModel(model);
153         button->setText("Search");
154         resultView->setColumnWidth(0,resultView->viewport()->width()/2);
155         resultView->setColumnWidth(1,resultView->viewport()->width()/2);
156         lineEdit->setDisabled(false);
157     }
158     }
159 }
160 void groove::finishedS()
161 {
162     model = gs->getModel();
163     resultView->setModel(model);
164     button->setText("Search");
165     resultView->setColumnWidth(0,resultView->viewport()->width()/2);
166     resultView->setColumnWidth(1,resultView->viewport()->width()/2);
167     lineEdit->setDisabled(false);
168     resultView->setColumnHidden(2,true);
169 }
170 void groove::changeS( QAction * action)
171 {
172     sMethod->setText(action->text());
173     sMethod->setMaximumWidth(sMethod->sizeHint().rwidth());
174 }
175
176 void groove::showOthers()
177 {
178 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)
179     QDBusConnection c = QDBusConnection::sessionBus();
180     QDBusMessage m = QDBusMessage::createSignal("/", "com.nokia.hildon_desktop", "exit_app_view");
181
182     c.send(m);
183 #endif
184 }
185
186 void groove::play()
187 {
188     QModelIndexList selected = resultView->selectionModel()->selectedRows(0);
189     if(!selected.isEmpty())
190     {
191         QStandardItem *item = model->item(selected.first().row(),2);
192         if(item == 0)
193             return;
194         //gs->getSong();
195         player->play(pl->addSong(item));
196     }
197     //selected.
198     //if
199 }
200 void groove::addSongPlaylist()
201 {
202     QModelIndexList selected = resultView->selectionModel()->selectedRows(0);
203     if(!selected.isEmpty())
204     {
205         QStandardItem *item = model->item(selected.first().row(),2);
206         if(item == 0)
207             return;
208         //gs->getSong();
209         if(pl->currentplaying() == -1)
210         {
211             player->play(pl->addSong(item));
212         }
213         else
214             pl->addSong(item);
215         model->item(selected.first().row(),1)->setText("Added to Playlist");;
216         pwindow->addSong(model->item(selected.first().row(),0)->text());
217     }
218
219 }
220
221 void groove::stop()
222 {
223     player->pause();
224     //mpg->pause();
225 }
226 void groove::moreB()
227 {
228     qDebug() << "He pressed the button";
229 }
230 void groove::progressUpdate(int p, qint64 d, qint64 t)
231 {
232     //if(!pd->isHidden())
233     //{
234     bBar->setPlaybackProgress(d,t);
235     //}
236 }
237
238 void groove::orientationChanged()
239 {
240 #ifdef Q_WS_MAEMO_5
241     QRect screenGeometry = QApplication::desktop()->screenGeometry();
242     if (screenGeometry.width() > screenGeometry.height())
243         portrait = false;
244     else
245         portrait = true;
246 #endif
247 }