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