1 /*****************************************************************************
2 * bookmarks.cpp : Bookmarks
3 ****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
6 * Authors: Antoine Lejeune <phytos@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
26 #include "dialogs/bookmarks.hpp"
27 #include "dialogs_provider.hpp"
28 #include "input_manager.hpp"
30 #include <QGridLayout>
31 #include <QSpacerItem>
32 #include <QPushButton>
34 BookmarksDialog *BookmarksDialog::instance = NULL;
36 BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf ):QVLCFrame( _p_intf )
38 setWindowFlags( Qt::Tool );
39 setWindowOpacity( config_GetFloat( p_intf, "qt-opacity" ) );
40 setWindowTitle( qtr( "Edit Bookmarks" ) );
42 QGridLayout *layout = new QGridLayout( this );
44 QPushButton *addButton = new QPushButton( qtr( "Create" ) );
45 addButton->setToolTip( qtr( "Create a new bookmark" ) );
46 QPushButton *delButton = new QPushButton( qtr( "Delete" ) );
47 delButton->setToolTip( qtr( "Delete the selected item" ) );
48 QPushButton *clearButton = new QPushButton( qtr( "Clear" ) );
49 clearButton->setToolTip( qtr( "Delete all the bookmarks" ) );
51 QPushButton *extractButton = new QPushButton( qtr( "Extract" ) );
52 extractButton->setToolTip( qtr() );
54 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
56 bookmarksList = new QTreeWidget( this );
57 bookmarksList->setRootIsDecorated( false );
58 bookmarksList->setAlternatingRowColors( true );
59 bookmarksList->setSelectionMode( QAbstractItemView::ExtendedSelection );
60 bookmarksList->setSelectionBehavior( QAbstractItemView::SelectRows );
61 bookmarksList->setEditTriggers( QAbstractItemView::SelectedClicked );
62 bookmarksList->setColumnCount( 3 );
63 bookmarksList->resize( sizeHint() );
65 QStringList headerLabels;
66 headerLabels << qtr( "Description" );
67 headerLabels << qtr( "Bytes" );
68 headerLabels << qtr( "Time" );
69 bookmarksList->setHeaderLabels( headerLabels );
72 layout->addWidget( addButton, 0, 0 );
73 layout->addWidget( delButton, 1, 0 );
74 layout->addWidget( clearButton, 2, 0 );
75 layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding ), 4, 0 );
77 layout->addWidget( extractButton, 5, 0 );
79 layout->addWidget( bookmarksList, 0, 1, 6, 2);
80 layout->setColumnStretch( 1, 1 );
81 layout->addWidget( closeButton, 7, 2 );
83 CONNECT( bookmarksList, activated( QModelIndex ), this,
84 activateItem( QModelIndex ) );
85 CONNECT( bookmarksList, itemChanged( QTreeWidgetItem*, int ),
86 this, edit( QTreeWidgetItem*, int ) );
88 BUTTONACT( addButton, add() );
89 BUTTONACT( delButton, del() );
90 BUTTONACT( clearButton, clear() );
92 BUTTONACT( extractButton, extract() );
94 BUTTONACT( closeButton, close() );
96 readSettings( "Bookmarks" );
100 BookmarksDialog::~BookmarksDialog()
102 writeSettings( "Bookmarks" );
105 void BookmarksDialog::update()
107 input_thread_t *p_input = THEMIM->getInput();
108 if( !p_input ) return;
110 seekpoint_t **pp_bookmarks;
113 if( bookmarksList->topLevelItemCount() > 0 )
115 bookmarksList->model()->removeRows( 0, bookmarksList->topLevelItemCount() );
118 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
119 &i_bookmarks ) != VLC_SUCCESS )
122 for( int i = 0; i < i_bookmarks; i++ )
124 // List with the differents elements of the row
126 row << QString( pp_bookmarks[i]->psz_name );
127 row << QString( "%1" ).arg( pp_bookmarks[i]->i_byte_offset );
128 row << QString( "%1" ).arg( pp_bookmarks[i]->i_time_offset / 1000000 );
129 QTreeWidgetItem *item = new QTreeWidgetItem( bookmarksList, row );
130 item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable |
131 Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
132 bookmarksList->insertTopLevelItem( i, item );
137 void BookmarksDialog::add()
139 input_thread_t *p_input = THEMIM->getInput();
140 if( !p_input ) return;
142 seekpoint_t bookmark;
144 bookmark.psz_name = NULL;
145 bookmark.i_byte_offset = 0;
146 bookmark.i_time_offset = 0;
148 input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
149 var_Get( p_input, "time", &pos );
150 bookmark.i_time_offset = pos.i_time;
151 input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
157 void BookmarksDialog::del()
159 input_thread_t *p_input = THEMIM->getInput();
160 if( !p_input ) return;
162 int i_focused = bookmarksList->currentIndex().row();
166 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
172 void BookmarksDialog::clear()
174 input_thread_t *p_input = THEMIM->getInput();
175 if( !p_input ) return;
177 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
182 void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
184 // We can only edit a item if it is the last item selected
185 if( bookmarksList->selectedItems().isEmpty() ||
186 bookmarksList->selectedItems().last() != item )
189 input_thread_t *p_input = THEMIM->getInput();
190 if( !p_input ) return;
192 // We get the row number of the item
193 int i_edit = bookmarksList->indexOfTopLevelItem( item );
195 // We get the bookmarks list
196 seekpoint_t **pp_bookmarks;
199 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
200 &i_bookmarks ) != VLC_SUCCESS )
203 if( i_edit >= i_bookmarks )
206 // We modify the seekpoint
207 seekpoint_t *p_seekpoint = pp_bookmarks[i_edit];
209 p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
210 else if( column == 1 )
211 p_seekpoint->i_byte_offset = atoi( qtu( item->text( column ) ) );
212 else if( column == 2 )
213 p_seekpoint->i_time_offset = 1000000 * atoll( qtu( item->text( column ) ) );
215 if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit ) !=
223 void BookmarksDialog::extract()
228 void BookmarksDialog::activateItem( QModelIndex index )
230 input_thread_t *p_input = THEMIM->getInput();
231 if( !p_input ) return;
233 input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );