]> git.sur5r.net Git - minitube/blobdiff - src/ListModel.cpp
Upload 1.9-1 to unstable
[minitube] / src / ListModel.cpp
old mode 100755 (executable)
new mode 100644 (file)
index e7592ec..851c152
@@ -3,6 +3,7 @@
 
 #define MAX_ITEMS 10
 static const QString recentKeywordsKey = "recentKeywords";
+static const QString recentChannelsKey = "recentChannels";
 
 ListModel::ListModel(QWidget *parent) : QAbstractListModel(parent) {
     youtubeSearch = 0;
@@ -11,6 +12,10 @@ ListModel::ListModel(QWidget *parent) : QAbstractListModel(parent) {
     m_activeVideo = 0;
     m_activeRow = -1;
     skip = 1;
+
+    hoveredRow = -1;
+    authorHovered = false;
+    authorPressed = false;
 }
 
 ListModel::~ListModel() {
@@ -78,29 +83,13 @@ QVariant ListModel::data(const QModelIndex &index, int role) const {
     case ActiveTrackRole:
         return video == m_activeVideo;
     case Qt::DisplayRole:
-    case Qt::StatusTipRole:
         return video->title();
-        /*
-        case Qt::ToolTipRole:
-          
-            QString tooltip;
-            if (!element.firstChildElement().text().isEmpty()) {
-                tooltip.append(QString("<b>").append(element.firstChildElement().text()).append("</b><br/>"));
-            }
-            if (!fromDate.isEmpty()) {
-                tooltip.append("<i>Pubblicato il</i> ").append(fromDate);
-            }
-            if (!toDate.isEmpty()) {
-                tooltip.append("<br/><i>Scadenza</i>: ").append(toDate);
-            }
-            tooltip.append("<br/><i>Tipo</i>: ").append(typeName)
-                .append("<br/><i>Id</i>: ").appen    QFont boldFont;
-    boldFont.setBold(true);d(id);
-            return tooltip;
-            */
-        
-        // case StreamUrlRole:
-        // return video->streamUrl();
+    case HoveredItemRole:
+        return hoveredRow == index.row();
+    case AuthorHoveredRole:
+        return authorHovered;
+    case AuthorPressedRole:
+        return authorPressed;
     }
     
     return QVariant();
@@ -112,8 +101,6 @@ void ListModel::setActiveRow( int row) {
         m_activeRow = row;
         m_activeVideo = videoAt(row);
         
-        // setStateOfRow( row, Item::Played );
-        
         int oldactiverow = m_activeRow;
         
         if ( rowExists( oldactiverow ) )
@@ -136,6 +123,13 @@ int ListModel::nextRow() const {
     return -1;
 }
 
+int ListModel::previousRow() const {
+    int prevRow = m_activeRow - 1;
+    if (rowExists(prevRow))
+        return prevRow;
+    return -1;
+}
+
 Video* ListModel::videoAt( int row ) const {
     if ( rowExists( row ) )
         return videos.at( row );
@@ -203,6 +197,10 @@ void ListModel::searchFinished(int total) {
 
     // update the message item
     emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
+
+    if (!youtubeSearch->getSuggestions().isEmpty()) {
+        emit haveSuggestions(youtubeSearch->getSuggestions());
+    }
 }
 
 void ListModel::searchError(QString message) {
@@ -221,18 +219,39 @@ void ListModel::addVideo(Video* video) {
     
     // first result!
     if (videos.size() == 1) {
-        // autoplay
-        setActiveRow(0);
+
+        // manualplay
+        QSettings settings;
+        if (!settings.value("manualplay", false).toBool())
+            setActiveRow(0);
 
         // save keyword
         QString query = searchParams->keywords();
-        QSettings settings;
-        QStringList keywords = settings.value(recentKeywordsKey).toStringList();
-        keywords.removeAll(query);
-        keywords.prepend(query);
-        while (keywords.size() > 10)
-            keywords.removeLast();
-        settings.setValue(recentKeywordsKey, keywords);
+        if (!query.isEmpty() && !searchParams->isTransient()) {
+            if (query.startsWith("http://")) {
+                // Save the video title
+                query += "|" + videos.first()->title();
+            }
+            QStringList keywords = settings.value(recentKeywordsKey).toStringList();
+            keywords.removeAll(query);
+            keywords.prepend(query);
+            while (keywords.size() > 10)
+                keywords.removeLast();
+            settings.setValue(recentKeywordsKey, keywords);
+        }
+
+        // save channel
+        QString channel = searchParams->author();
+        if (!channel.isEmpty() && !searchParams->isTransient()) {
+            QSettings settings;
+            QStringList channels = settings.value(recentChannelsKey).toStringList();
+            channels.removeAll(channel);
+            channels.prepend(channel);
+            while (channels.size() > 10)
+                channels.removeLast();
+            settings.setValue(recentChannelsKey, channels);
+        }
+
     }
 
 }
@@ -268,6 +287,7 @@ void ListModel::removeIndexes(QModelIndexList &indexes) {
     QList<Video*> originalList(videos);
     QList<Video*> delitems;
     foreach (QModelIndex index, indexes) {
+        if (index.row() >= originalList.size()) continue;
         Video* video = originalList.at(index.row());
         int idx = videos.indexOf(video);
         if (idx != -1) {
@@ -377,20 +397,21 @@ QModelIndex ListModel::indexForVideo(Video* video) {
 }
 
 void ListModel::move(QModelIndexList &indexes, bool up) {
-
     QList<Video*> movedVideos;
 
     foreach (QModelIndex index, indexes) {
         int row = index.row();
+        if (row >= videos.size()) continue;
         // qDebug() << "index row" << row;
         Video *video = videoAt(row);
         movedVideos << video;
     }
 
-    int counter = 1;
+    int end=up ? -1 : rowCount()-1, mod=up ? -1 : 1;
     foreach (Video *video, movedVideos) {
 
         int row = rowForVideo(video);
+        if (row+mod==end) { end=row; continue; }
         // qDebug() << "video row" << row;
         removeRows(row, 1, QModelIndex());
 
@@ -401,9 +422,53 @@ void ListModel::move(QModelIndexList &indexes, bool up) {
         videos.insert(row, video);
         endInsertRows();
 
-        counter++;
     }
 
     emit needSelectionFor(movedVideos);
 
 }
+
+/* row hovering */
+
+void ListModel::setHoveredRow(int row) {
+    int oldRow = hoveredRow;
+    hoveredRow = row;
+    emit dataChanged( createIndex( oldRow, 0 ), createIndex( oldRow, columnCount() - 1 ) );
+    emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, columnCount() - 1 ) );
+}
+
+void ListModel::clearHover() {
+    emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, columnCount() - 1 ) );
+    hoveredRow = -1;
+}
+
+/* clickable author */
+
+void ListModel::enterAuthorHover() {
+    if (authorHovered) return;
+    authorHovered = true;
+    updateAuthor();
+}
+
+void ListModel::exitAuthorHover() {
+    if (!authorHovered) return;
+    authorHovered = false;
+    updateAuthor();
+    setHoveredRow(hoveredRow);
+}
+
+void ListModel::enterAuthorPressed() {
+    if (authorPressed) return;
+    authorPressed = true;
+    updateAuthor();
+}
+
+void ListModel::exitAuthorPressed() {
+    if (!authorPressed) return;
+    authorPressed = false;
+    updateAuthor();
+}
+
+void ListModel::updateAuthor() {
+    emit dataChanged( createIndex( hoveredRow, 0 ), createIndex( hoveredRow, columnCount() - 1 ) );
+}