f8c30c04849c69dc14dca95403ef5f1aa4eecc72
[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
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     resultView = new QTableView();
24     QMenu *pushMenu = new QMenu();
25     //showFullScreen();
26     lineEdit->insert("");
27     lineEdit->setDisabled(true);
28     pushMenu->addAction("Song:");
29     //pushMenu->addAction("Artist:");
30     //pushMenu->addAction("Album:");
31     pd = new grooveProgressBar(this);
32     pd->hide();
33     QMenu *moreAction = new QMenu();
34     //moreAction->addAction("Playlist");
35     connect(moreAction->addAction("Play Now"),SIGNAL(triggered()),this,SLOT(play()));
36     connect(moreAction->addAction("Show download Progress"),SIGNAL(triggered()),pd,SLOT(show()));
37     moreButton->setMenu(moreAction);
38
39     //sMethod->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Maximum);
40     sMethod->setMaximumWidth(sMethod->sizeHint().rwidth());
41     sMethod->setMenu(pushMenu);
42     model = new  QStandardItemModel();
43     model->setHorizontalHeaderLabels(
44         QStringList() << "Please wait for initialization"
45                       /*<< "Artist"*/);
46     resultView->setModel(model);
47     resultView->verticalHeader()->hide();
48     resultView->horizontalHeader()->setStretchLastSection(true);
49     resultView->setSelectionBehavior(QAbstractItemView::SelectRows);
50     resultView->setSelectionMode(QAbstractItemView::SingleSelection);
51     resultView->setEditTriggers(QAbstractItemView::NoEditTriggers);
52     resultView->setColumnHidden(2,true);
53     resultView->setColumnHidden(3,true);
54     resultView->setAutoScroll(false);
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->addLayout(layout);
67     vlayout->addWidget(resultView);
68     vlayout->addLayout(bottomLayout);
69     bottomLayout->addWidget(dButton);
70     bottomLayout->addWidget(stopButton);
71     bottomLayout->addWidget(nextB);
72     bottomLayout->addWidget(moreButton);
73     vlayout->setMenuBar(mBar);
74     setLayout(vlayout);
75     setWindowTitle("GrooveShark");
76     //Create New Grooveshark connection
77     gs = new gscom();
78     //Connections
79     connect(button, SIGNAL(clicked()), this, SLOT(search()));
80     connect(gs, SIGNAL(finishedSearch()), this, SLOT(finishedS()));
81     connect(lineEdit,SIGNAL(returnPressed()),this, SLOT(search()));
82     connect(pushMenu,SIGNAL(triggered(QAction*)),this,SLOT(changeS(QAction*)));
83     connect(dButton,SIGNAL(clicked()),this, SLOT(addSongPlaylist()));
84     connect(stopButton,SIGNAL(clicked()),this,SLOT(stop()));
85     connect(moreButton,SIGNAL(clicked()),this,SLOT(moreB()));
86     //connect(rotator,SIGNAL(orientationChanged(Orientation)),this,SLOT(orientationChanged()));
87     pl = new playlist();
88     pl->setGscom(gs);
89     player->setPlaylist(pl);
90     connect(pl,SIGNAL(downloadProgress(int,qint64,qint64)),this,SLOT(progressUpdate(int,qint64,qint64)));
91     connect(pl,SIGNAL(bufferReady(int)),pd,SLOT(close()));
92     connect(pl,SIGNAL(freeze(bool)),resultView,SLOT(setDisabled(bool)));
93     connect(pl,SIGNAL(freeze(bool)),pushMenu,SLOT(setDisabled(bool)));
94     connect(pl,SIGNAL(freeze(bool)),dButton,SLOT(setDisabled(bool)));
95     connect(nextB,SIGNAL(clicked()),player,SLOT(playNext()));
96 }
97 void groove::search()
98 {
99     if(!lineEdit->text().isEmpty())
100     {
101     if(lineEdit->isEnabled())
102     {
103
104         if(sMethod->text().compare("Song:")==0)
105             resultView->setModel(gs->getSongModel(lineEdit->text()));
106         else if(sMethod->text().compare("Artist:")==0)
107             resultView->setModel(gs->getArtistModel(lineEdit->text()));
108         else if(sMethod->text().compare("Album")==0)
109             resultView->setModel(gs->getAlbumModel(lineEdit->text()));
110         else
111             return;
112
113
114         button->setText("Stop");
115         lineEdit->setDisabled(true);
116     }
117     else
118     {
119         resultView->setModel(model);
120         button->setText("Search");
121         resultView->setColumnWidth(0,resultView->viewport()->width()/2);
122         resultView->setColumnWidth(1,resultView->viewport()->width()/2);
123         lineEdit->setDisabled(false);
124     }
125     }
126 }
127 void groove::finishedS()
128 {
129     model = gs->getModel();
130     resultView->setModel(model);
131     button->setText("Search");
132     resultView->setColumnWidth(0,resultView->viewport()->width()/2);
133     resultView->setColumnWidth(1,resultView->viewport()->width()/2);
134     lineEdit->setDisabled(false);
135     resultView->setColumnHidden(2,true);
136 }
137 void groove::changeS( QAction * action)
138 {
139     sMethod->setText(action->text());
140     sMethod->setMaximumWidth(sMethod->sizeHint().rwidth());
141 }
142 void groove::play()
143 {
144     QModelIndexList selected = resultView->selectionModel()->selectedRows(0);
145     if(!selected.isEmpty())
146     {
147         QStandardItem *item = model->item(selected.first().row(),2);
148         if(item == 0)
149             return;
150         //gs->getSong();
151         player->play(pl->addSong(item));
152         pd->setMaximum(100);
153         pd->setValue(0);
154         pd->show();
155     }
156     //selected.
157     //if
158 }
159 void groove::addSongPlaylist()
160 {
161     QModelIndexList selected = resultView->selectionModel()->selectedRows(0);
162     if(!selected.isEmpty())
163     {
164         QStandardItem *item = model->item(selected.first().row(),2);
165         if(item == 0)
166             return;
167         //gs->getSong();
168         if(pl->currentplaying() == -1)
169         {
170             player->play(pl->addSong(item));
171         }
172         else
173             pl->addSong(item);
174         pd->setMaximum(100);
175         pd->setValue(0);
176         pd->show();
177         model->item(selected.first().row(),1)->setText("Added to Playlist");;
178     }
179 }
180
181 void groove::stop()
182 {
183     player->pause();
184 }
185 void groove::moreB()
186 {
187     qDebug() << "He pressed the button";
188 }
189 void groove::progressUpdate(int p, qint64 d, qint64 t)
190 {
191     //if(!pd->isHidden())
192     //{
193
194
195         pd->setMaximum(t);
196         pd->setValue(d);
197     //}
198 }
199
200 void groove::orientationChanged()
201 {
202 #ifdef Q_WS_MAEMO_5
203     QRect screenGeometry = QApplication::desktop()->screenGeometry();
204     if (screenGeometry.width() > screenGeometry.height())
205         portrait = false;
206     else
207         portrait = true;
208 #endif
209 }