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