bookmarks.cpp 6.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*****************************************************************************
 * bookmarks.cpp : Bookmarks
 ****************************************************************************
 * Copyright (C) 2006 the VideoLAN team
 *
 * Authors: Antoine Lejeune <phytos@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "dialogs/bookmarks.hpp"
#include "dialogs_provider.hpp"
#include "input_manager.hpp"

#include <QGridLayout>
#include <QSpacerItem>
#include <QPushButton>

BookmarksDialog *BookmarksDialog::instance = NULL;

BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf ):QVLCFrame( _p_intf )
{
    setWindowFlags( Qt::Tool );
    setWindowOpacity( config_GetFloat( p_intf, "qt-opacity" ) );
    setWindowTitle( qtr( "Edit bookmark" ) );
38

39
    QGridLayout *layout = new QGridLayout( this );
40

41 42 43 44
    QPushButton *addButton = new QPushButton( qtr( "Add" ) );
    QPushButton *delButton = new QPushButton( qtr( "Delete" ) );
    QPushButton *clearButton = new QPushButton( qtr( "Clear" ) );
    QPushButton *extractButton = new QPushButton( qtr( "Extract" ) );
45

46 47 48 49 50 51 52 53 54 55 56 57
    bookmarksList = new QTreeWidget( this );
    bookmarksList->setRootIsDecorated( false );
    bookmarksList->setAlternatingRowColors( true );
    bookmarksList->setSelectionMode( QAbstractItemView::ExtendedSelection );
    bookmarksList->setSelectionBehavior( QAbstractItemView::SelectRows );
    bookmarksList->setEditTriggers( QAbstractItemView::SelectedClicked );
    bookmarksList->setColumnCount( 3 );
    QStringList headerLabels;
    headerLabels << qtr( "Description" );
    headerLabels << qtr( "Bytes" );
    headerLabels << qtr( "Time" );
    bookmarksList->setHeaderLabels( headerLabels );
58 59


60 61 62 63 64 65 66
    layout->addWidget( addButton, 0, 0 );
    layout->addWidget( delButton, 1, 0 );
    layout->addWidget( clearButton, 2, 0 );
    layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding ), 4, 0 );
    layout->addWidget( extractButton, 5, 0 );
    layout->addWidget( bookmarksList, 0, 1, 6, 1);
    layout->setColumnStretch( 1, 1 );
67

68 69 70
    CONNECT( bookmarksList, activated( QModelIndex ), this,
             activateItem( QModelIndex ) );
    CONNECT( bookmarksList, itemChanged( QTreeWidgetItem*, int ), this, edit( QTreeWidgetItem*, int ) );
71

72 73 74 75
    BUTTONACT( addButton, add() );
    BUTTONACT( delButton, del() );
    BUTTONACT( clearButton, clear() );
    BUTTONACT( extractButton, extract() );
76

77 78 79 80 81 82 83 84 85 86
}

BookmarksDialog::~BookmarksDialog()
{
}

void BookmarksDialog::update()
{
    input_thread_t *p_input = THEMIM->getInput();
    if( !p_input ) return;
87

88 89
    seekpoint_t **pp_bookmarks;
    int i_bookmarks;
90

91 92 93 94
    if( bookmarksList->topLevelItemCount() > 0 )
    {
        bookmarksList->model()->removeRows( 0, bookmarksList->topLevelItemCount() );
    }
95 96

    if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
97 98
                       &i_bookmarks ) != VLC_SUCCESS )
        return;
99

100 101 102 103 104 105 106 107 108 109 110 111
    for( int i = 0; i < i_bookmarks; i++ )
    {
        // List with the differents elements of the row
        QStringList row;
        row << QString( pp_bookmarks[i]->psz_name );
        row << QString( "%1" ).arg( pp_bookmarks[i]->i_byte_offset );
        row << QString( "%1" ).arg( pp_bookmarks[i]->i_time_offset / 1000000 );
        QTreeWidgetItem *item = new QTreeWidgetItem( bookmarksList, row );
        item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable |
                        Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        bookmarksList->insertTopLevelItem( i, item );
    }
112

113 114 115 116 117 118
}

void BookmarksDialog::add()
{
    input_thread_t *p_input = THEMIM->getInput();
    if( !p_input ) return;
119

120 121 122 123 124
    seekpoint_t bookmark;
    vlc_value_t pos;
    bookmark.psz_name = NULL;
    bookmark.i_byte_offset = 0;
    bookmark.i_time_offset = 0;
125

126 127 128 129
    input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
    var_Get( p_input, "time", &pos );
    bookmark.i_time_offset = pos.i_time;
    input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
130

131
    update();
132

133 134 135 136 137 138
}

void BookmarksDialog::del()
{
    input_thread_t *p_input = THEMIM->getInput();
    if( !p_input ) return;
139

140
    int i_focused = bookmarksList->currentIndex().row();
141

142 143 144 145
    if( i_focused >= 0 )
    {
        input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
    }
146 147

    update();
148 149 150 151 152 153
}

void BookmarksDialog::clear()
{
    input_thread_t *p_input = THEMIM->getInput();
    if( !p_input ) return;
154

155
    input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
156

157 158 159 160 161 162 163 164 165
    update();
}

void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
{
    // We can only edit a item if it is the last item selected
    if( bookmarksList->selectedItems().isEmpty() ||
        bookmarksList->selectedItems().last() != item )
        return;
166

167 168
    input_thread_t *p_input = THEMIM->getInput();
    if( !p_input ) return;
169

170 171
    // We get the row number of the item
    int i_edit = bookmarksList->indexOfTopLevelItem( item );
172

173 174 175
    // We get the bookmarks list
    seekpoint_t **pp_bookmarks;
    int i_bookmarks;
176 177

    if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
178 179
                       &i_bookmarks ) != VLC_SUCCESS )
        return;
180

181 182
    if( i_edit >= i_bookmarks )
        return;
183

184 185 186 187 188 189 190 191
    // We modify the seekpoint
    seekpoint_t *p_seekpoint = pp_bookmarks[i_edit];
    if( column == 0 )
        p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
    else if( column == 1 )
        p_seekpoint->i_byte_offset = atoi( qtu( item->text( column ) ) );
    else if( column == 2 )
        p_seekpoint->i_time_offset = 1000000 * atoll( qtu( item->text( column ) ) );
192

193 194 195
    if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit ) !=
        VLC_SUCCESS )
        return;
196

197
    update();
198

199 200 201 202 203 204 205 206 207 208 209
}

void BookmarksDialog::extract()
{
    // TODO
}

void BookmarksDialog::activateItem( QModelIndex index )
{
    input_thread_t *p_input = THEMIM->getInput();
    if( !p_input ) return;
210

211 212
    input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );
}