Ignore:
Timestamp:
Mar 16, 2012, 4:02:47 PM (13 years ago)
Author:
Silvan Scherrer
Message:

SMPlayer: 0.7.1 trunk update

Location:
smplayer/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • smplayer/trunk

  • smplayer/trunk/src/favorites.cpp

    r119 r124  
    11/*  smplayer, GUI front-end for mplayer.
    2     Copyright (C) 2006-2011 Ricardo Villalba <rvm@escomposlinux.org>
     2    Copyright (C) 2006-2012 Ricardo Villalba <rvm@users.sourceforge.net>
    33
    44    This program is free software; you can redistribute it and/or modify
     
    2020#include "favoriteeditor.h"
    2121
    22 #include <QMenu>
    2322#include <QAction>
    2423#include <QSettings>
     
    2625#include <QTextStream>
    2726#include <QInputDialog>
    28 
    29 //#define FIRST_MENU_ENTRY 3
    30 #define FIRST_MENU_ENTRY 2
    31 
    32 Favorites::Favorites(QString filename, QWidget * parent) : QObject(parent)
     27#include <QFileInfo>
     28
     29//#define FIRST_MENU_ENTRY 4
     30#define FIRST_MENU_ENTRY 3
     31
     32Favorites::Favorites(QString filename, QWidget * parent) : QMenu(parent)
    3333{
    3434        _filename = filename;
    35         _menu = 0;
    3635
    3736        parent_widget = parent;
     
    5251        connect(previous_act, SIGNAL(triggered()), this, SLOT(previous()));
    5352
     53        add_current_act = new QAction( "Add current media", this);
     54        add_current_act->setEnabled(false);
     55        connect(add_current_act, SIGNAL(triggered()), SLOT(addCurrentPlaying()));
     56
     57        retranslateStrings();
     58
    5459        load();
     60
     61        connect( this, SIGNAL(triggered(QAction *)),
     62             this, SLOT(triggered_slot(QAction *)) );
     63
     64        addAction(edit_act);
     65        addAction(add_current_act);
     66        //addAction(jump_act);
     67        addSeparator();
     68
     69        populateMenu();
     70
    5571}
    5672
    5773Favorites::~Favorites() {
     74        /* qDebug("Favorites::~Favorites"); */
     75
    5876        save();
    59         if (_menu != 0) delete _menu;
    60 }
    61 
    62 QMenu * Favorites::menu() {
    63         if (_menu == 0) createMenu();
    64         return _menu;
    65 }
    66 
    67 void Favorites::createMenu() {
    68         _menu = new QMenu(parent_widget);
    69         connect( _menu, SIGNAL(triggered(QAction *)),
    70              this, SLOT(triggered_slot(QAction *)) );
    71 
    72         _menu->addAction(edit_act);
    73         //_menu->addAction(jump_act);
    74         _menu->addSeparator();
    75 
    76         populateMenu();
     77        delete_children();
     78}
     79
     80void Favorites::delete_children() {
     81        for (int n=0; n < child.count(); n++) {
     82                if (child[n]) delete child[n];
     83                child[n] = 0;
     84        }
     85        child.clear();
     86}
     87
     88void Favorites::retranslateStrings() {
     89        edit_act->setText( tr("&Edit...") );
     90        jump_act->setText( tr("&Jump...") );
     91        next_act->setText( tr("&Next") );
     92        previous_act->setText( tr("&Previous") );
     93        add_current_act->setText( tr("&Add current media") );
     94}
     95
     96Favorites * Favorites::createNewObject(QString filename, QWidget * parent) {
     97        return new Favorites(filename, parent);
    7798}
    7899
     
    81102                QString i = QString::number(n+1);
    82103                QString name = QString("%1 - " + f_list[n].name() ).arg( i.insert( i.size()-1, '&' ), 3, ' ' );
    83                 QAction * a = _menu->addAction( name );
    84                 a->setData( f_list[n].file() );
    85                 a->setIcon( QIcon( f_list[n].icon() ) );
    86                 a->setStatusTip( f_list[n].file() );
     104                if (f_list[n].isSubentry()) {
     105                        Favorites * new_fav = createNewObject(f_list[n].file(), parent_widget);
     106                        new_fav->getCurrentMedia(received_file_playing, received_title);
     107                        connect(this, SIGNAL(sendCurrentMedia(const QString &, const QString &)),
     108                    new_fav, SLOT(getCurrentMedia(const QString &, const QString &)));
     109                        /*
     110                        new_fav->editAct()->setText( editAct()->text() );
     111                        new_fav->jumpAct()->setText( jumpAct()->text() );
     112                        new_fav->nextAct()->setText( nextAct()->text() );
     113                        new_fav->previousAct()->setText( previousAct()->text() );
     114                        new_fav->addCurrentAct()->setText( addCurrentAct()->text() );
     115                        */
     116
     117                        child.push_back(new_fav);
     118                       
     119                        QAction * a = addMenu( new_fav );
     120                        a->setText( name );
     121                        a->setIcon( QIcon( f_list[n].icon() ) );
     122                } else {
     123                        QAction * a = addAction( name );
     124                        a->setData( f_list[n].file() );
     125                        a->setIcon( QIcon( f_list[n].icon() ) );
     126                        a->setStatusTip( f_list[n].file() );
     127                }
    87128        }
    88129}
     
    90131void Favorites::updateMenu() {
    91132        // Remove all except the first 2 items
    92         while (_menu->actions().count() > FIRST_MENU_ENTRY) {
    93                 QAction * a = _menu->actions()[FIRST_MENU_ENTRY];
    94                 _menu->removeAction( a );
     133        while (actions().count() > FIRST_MENU_ENTRY) {
     134                QAction * a = actions()[FIRST_MENU_ENTRY];
     135                removeAction( a );
    95136                a->deleteLater();
    96137        }
     138
     139        delete_children();
    97140
    98141        populateMenu();
     
    110153
    111154void Favorites::markCurrent() {
    112         for (int n = FIRST_MENU_ENTRY; n < _menu->actions().count(); n++) {
    113                 QAction * a = _menu->actions()[n];
     155        for (int n = FIRST_MENU_ENTRY; n < actions().count(); n++) {
     156                QAction * a = actions()[n];
    114157                QString file = a->data().toString();
    115158                QFont f = a->font();
    116159
    117                 if (file == current_file) {
     160                if ((!file.isEmpty()) && (file == current_file)) {
    118161                        f.setBold(true);
    119162                        a->setFont( f );
     
    136179
    137180        int current = findFile(current_file);
    138         //if (current < 0) current = 0;
    139 
    140         current++;
    141         if (current >= f_list.count()) current = 0;
    142 
    143         QAction * a = _menu->actions()[current+FIRST_MENU_ENTRY]; // Skip "edit" and separator
     181
     182        int i = current;
     183        if (current < 0) current = 0;
     184
     185        do {
     186                i++;
     187                if (i == current) break;
     188                if (i >= f_list.count()) i = 0;
     189        } while (f_list[i].isSubentry());
     190
     191        QAction * a = actions()[i+FIRST_MENU_ENTRY]; // Skip "edit" and separator
    144192        if (a != 0) {
    145193                a->trigger();
     
    151199
    152200        int current = findFile(current_file);
    153         //if (current < 0) current = 0;
    154 
    155         current--;
    156         if (current < 0) current = f_list.count()-1;
    157 
    158         QAction * a = _menu->actions()[current+FIRST_MENU_ENTRY]; // Skip "edit" and separator
     201
     202        int i = current;
     203        if (current < 0) current = 0;
     204
     205        do {
     206                i--;
     207                if (i == current) break;
     208                if (i < 0) i = f_list.count()-1;
     209        } while (f_list[i].isSubentry());
     210
     211        QAction * a = actions()[i+FIRST_MENU_ENTRY]; // Skip "edit" and separator
    159212        if (a != 0) {
    160213                a->trigger();
     214        }
     215}
     216
     217void Favorites::getCurrentMedia(const QString & filename, const QString & title) {
     218        qDebug("Favorites::getCurrentMedia: '%s', '%s'", filename.toUtf8().constData(), title.toUtf8().constData());
     219
     220        if (!filename.isEmpty()) {
     221                received_file_playing = filename;
     222                received_title = title;
     223
     224                emit sendCurrentMedia(filename, title);
     225
     226                add_current_act->setEnabled(true);
     227        }
     228}
     229
     230void Favorites::addCurrentPlaying() {
     231        if (received_file_playing.isEmpty()) {
     232                qDebug("Favorites::addCurrentPlaying: received file is empty, doing nothing");
     233        } else {
     234                Favorite fav;
     235                fav.setName(received_title);
     236                fav.setFile(received_file_playing);
     237                f_list.append(fav);
     238                save();
     239                updateMenu();
    161240        }
    162241}
     
    174253                        stream << "#EXTINF:0,";
    175254                        stream << f_list[n].name() << ",";
    176                         stream << f_list[n].icon() << "\n";
     255                        stream << f_list[n].icon() << ",";
     256                        stream << f_list[n].isSubentry() << "\n";
    177257                        stream << f_list[n].file() << "\n";
    178258                }
     
    185265
    186266        QRegExp m3u_id("^#EXTM3U|^#M3U");
    187         QRegExp info("^#EXTINF:(.*),(.*),(.*)");
     267        QRegExp info1("^#EXTINF:(.*),(.*),(.*)");
     268        QRegExp info2("^#EXTINF:(.*),(.*),(.*),(.*)");
    188269
    189270    QFile f( _filename );
     
    200281        while ( !stream.atEnd() ) {
    201282            line = stream.readLine(); // line of text excluding '\n'
     283                        //qDebug("info2.indexIn: %d", info2.indexIn(line));
     284                        //qDebug("info1.indexIn: %d", info1.indexIn(line));
    202285            //qDebug( " * line: '%s'", line.toUtf8().data() );
    203286                        if (m3u_id.indexIn(line)!=-1) {
     
    206289                        }
    207290                        else
    208                         if (info.indexIn(line) != -1) {
    209                                 fav.setName( info.cap(2) );
    210                                 fav.setIcon( info.cap(3) );
     291                        if (info2.indexIn(line) != -1) {
     292                                fav.setName( info2.cap(2) );
     293                                fav.setIcon( info2.cap(3) );
     294                                fav.setSubentry( info2.cap(4).toInt() == 1 );
     295                        }
     296                        else
     297                        // Compatibility with old files
     298                        if (info1.indexIn(line) != -1) {
     299                                fav.setName( info1.cap(2) );
     300                                fav.setIcon( info1.cap(3) );
     301                                fav.setSubentry( false );
    211302                        }
    212303                        else
     
    224315                                fav.setFile("");
    225316                                fav.setIcon("");
     317                                fav.setSubentry(false);
    226318                        }
    227319        }
     
    236328
    237329        e.setData(f_list);
     330        e.setStorePath( QFileInfo(_filename).absolutePath() );
    238331
    239332        if (e.exec() == QDialog::Accepted) {
    240333                f_list = e.data();
     334                save();
    241335                updateMenu();
    242336                /*
     
    258352                last_item = item;
    259353                item--;
    260                 _menu->actions()[item+FIRST_MENU_ENTRY]->trigger();
     354                actions()[item+FIRST_MENU_ENTRY]->trigger();
     355        }
     356}
     357
     358// Language change stuff
     359void Favorites::changeEvent(QEvent *e) {
     360        if (e->type() == QEvent::LanguageChange) {
     361                retranslateStrings();
     362        } else {
     363                QWidget::changeEvent(e);
    261364        }
    262365}
Note: See TracChangeset for help on using the changeset viewer.