ced6f4ef67a1942aa697eff5f3c43fddfe7cf797
[groove] / groove.cpp
1 #include "groove.h"
2 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_HILDON)
3 #include "qmaemo5rotator.h"
4 #endif
5 #include <QtDBus>
6 #include "bottombar.h"
7
8 groove::groove(QWidget *parent) :
9     QWidget(parent)
10 {
11     mBar = new QMenuBar();
12     //mBar->addAction("test");
13     sMethod = new QPushButton("Song:");
14     lineEdit = new QLineEdit("");
15     player = new sPlayer();
16     QHBoxLayout *layout = new QHBoxLayout();
17     QVBoxLayout *vlayout = new QVBoxLayout();
18     QHBoxLayout *bottomLayout = new QHBoxLayout();
19     button = new QPushButton("Search");
20     QPushButton *dButton = new QPushButton("Queue");
21     QPushButton *stopButton = new QPushButton("Pause");
22     QPushButton *moreButton = new QPushButton("...");
23     QPushButton *nextB = new QPushButton("->");
24     topBar *ok = new topBar(this);
25     resultView = new QTableView();
26     QMenu *pushMenu = new QMenu();
27     //showFullScreen();
28     lineEdit->insert("");
29     lineEdit->setDisabled(true);
30     pushMenu->addAction("Song:");
31     //pushMenu->addAction("Artist:");
32     //pushMenu->addAction("Album:");
33     pd = new grooveProgressBar(this);
34     pd->hide();
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     bottomBar *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();
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 }
109 void groove::performSearch(QString s)
110 {
111     qDebug() << s;
112     resultView->setModel(gs->getSongModel(s));
113 }
114
115 void groove::search()
116 {
117     if(!lineEdit->text().isEmpty())
118     {
119     if(lineEdit->isEnabled())
120     {
121
122         if(sMethod->text().compare("Song:")==0)
123             resultView->setModel(gs->getSongModel(lineEdit->text()));
124         else if(sMethod->text().compare("Artist:")==0)
125             resultView->setModel(gs->getArtistModel(lineEdit->text()));
126         else if(sMethod->text().compare("Album")==0)
127             resultView->setModel(gs->getAlbumModel(lineEdit->text()));
128         else
129             return;
130
131
132         button->setText("Stop");
133         lineEdit->setDisabled(true);
134     }
135     else
136     {
137         resultView->setModel(model);
138         button->setText("Search");
139         resultView->setColumnWidth(0,resultView->viewport()->width()/2);
140         resultView->setColumnWidth(1,resultView->viewport()->width()/2);
141         lineEdit->setDisabled(false);
142     }
143     }
144 }
145 void groove::finishedS()
146 {
147     model = gs->getModel();
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     resultView->setColumnHidden(2,true);
154 }
155 void groove::changeS( QAction * action)
156 {
157     sMethod->setText(action->text());
158     sMethod->setMaximumWidth(sMethod->sizeHint().rwidth());
159 }
160 void groove::showOthers()
161 {
162     QDBusConnection c = QDBusConnection::sessionBus();
163     QDBusMessage m = QDBusMessage::createSignal("/", "com.nokia.hildon_desktop", "exit_app_view");
164     c.send(m);
165 }
166
167 void groove::play()
168 {
169     QModelIndexList selected = resultView->selectionModel()->selectedRows(0);
170     if(!selected.isEmpty())
171     {
172         QStandardItem *item = model->item(selected.first().row(),2);
173         if(item == 0)
174             return;
175         //gs->getSong();
176         player->play(pl->addSong(item));
177         pd->setMaximum(100);
178         pd->setValue(0);
179         pd->show();
180     }
181     //selected.
182     //if
183 }
184 void groove::addSongPlaylist()
185 {
186     QModelIndexList selected = resultView->selectionModel()->selectedRows(0);
187     if(!selected.isEmpty())
188     {
189         QStandardItem *item = model->item(selected.first().row(),2);
190         if(item == 0)
191             return;
192         //gs->getSong();
193         if(pl->currentplaying() == -1)
194         {
195             player->play(pl->addSong(item));
196         }
197         else
198             pl->addSong(item);
199         pd->setMaximum(100);
200         pd->setValue(0);
201         pd->show();
202         model->item(selected.first().row(),1)->setText("Added to Playlist");;
203     }
204 }
205
206 void groove::stop()
207 {
208     player->pause();
209 }
210 void groove::moreB()
211 {
212     qDebug() << "He pressed the button";
213 }
214 void groove::progressUpdate(int p, qint64 d, qint64 t)
215 {
216     //if(!pd->isHidden())
217     //{
218
219
220         pd->setMaximum(t);
221         pd->setValue(d);
222     //}
223 }
224
225 void groove::orientationChanged()
226 {
227 #ifdef Q_WS_MAEMO_5
228     QRect screenGeometry = QApplication::desktop()->screenGeometry();
229     if (screenGeometry.width() > screenGeometry.height())
230         portrait = false;
231     else
232         portrait = true;
233 #endif
234 }