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 *****************************************************************************/
27 #include "dialogs/bookmarks.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", QSize( 435, 206 ) );
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 int total = pp_bookmarks[i]->i_time_offset/ 1000000;
129 int hour = total / (60*60);
130 int min = (total - hour*60*60) / 60;
131 int sec = total - hour*60*60 - min*60;
133 row << str.sprintf("%02d:%02d:%02d", hour, min, sec );
134 QTreeWidgetItem *item = new QTreeWidgetItem( bookmarksList, row );
135 item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable |
136 Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
137 bookmarksList->insertTopLevelItem( i, item );
138 vlc_seekpoint_Delete( pp_bookmarks[i] );
140 free( pp_bookmarks );
143 void BookmarksDialog::add()
145 input_thread_t *p_input = THEMIM->getInput();
146 if( !p_input ) return;
148 seekpoint_t bookmark;
150 if( !input_Control( p_input, INPUT_GET_BOOKMARK, &bookmark ) )
152 bookmark.psz_name = qtu( THEMIM->getIM()->getName() +
153 QString("_%1" ).arg( bookmarksList->topLevelItemCount() ) );
155 input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
160 void BookmarksDialog::del()
162 input_thread_t *p_input = THEMIM->getInput();
163 if( !p_input ) return;
165 int i_focused = bookmarksList->currentIndex().row();
169 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
175 void BookmarksDialog::clear()
177 input_thread_t *p_input = THEMIM->getInput();
178 if( !p_input ) return;
180 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
185 void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
188 // We can only edit a item if it is the last item selected
189 if( bookmarksList->selectedItems().isEmpty() ||
190 bookmarksList->selectedItems().last() != item )
193 input_thread_t *p_input = THEMIM->getInput();
197 // We get the row number of the item
198 int i_edit = bookmarksList->indexOfTopLevelItem( item );
200 // We get the bookmarks list
201 seekpoint_t** pp_bookmarks;
202 seekpoint_t* p_seekpoint;
205 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
206 &i_bookmarks ) != VLC_SUCCESS )
209 if( i_edit >= i_bookmarks )
212 // We modify the seekpoint
213 p_seekpoint = pp_bookmarks[i_edit];
216 free( p_seekpoint->psz_name );
217 p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
219 else if( column == 1 )
220 p_seekpoint->i_byte_offset = atoi( qtu( item->text( column ) ) );
221 else if( column == 2 )
223 fields = item->text( column ).split( ":", QString::SkipEmptyParts );
224 if( fields.size() == 1 )
225 p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() );
226 else if( fields.size() == 2 )
227 p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 60 + fields[1].toInt() );
228 else if( fields.size() == 3 )
229 p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 3600 + fields[1].toInt() * 60 + fields[2].toInt() );
232 msg_Err( p_intf, "Invalid string format for time" );
237 // Send the modification
238 if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit ) !=
242 // Everything goes fine : update
245 // Clear the bookmark list
247 for( int i = 0; i < i_bookmarks; i++)
249 if( p_seekpoint != pp_bookmarks[i] )
250 vlc_seekpoint_Delete( pp_bookmarks[i] );
252 free( pp_bookmarks );
255 void BookmarksDialog::extract()
260 void BookmarksDialog::activateItem( QModelIndex index )
262 input_thread_t *p_input = THEMIM->getInput();
263 if( !p_input ) return;
265 input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );