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