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 bookmark" ) );
42 QGridLayout *layout = new QGridLayout( this );
44 QPushButton *addButton = new QPushButton( qtr( "Add" ) );
45 QPushButton *delButton = new QPushButton( qtr( "Delete" ) );
46 QPushButton *clearButton = new QPushButton( qtr( "Clear" ) );
47 QPushButton *extractButton = new QPushButton( qtr( "Extract" ) );
49 bookmarksList = new QTreeWidget( this );
50 bookmarksList->setRootIsDecorated( false );
51 bookmarksList->setAlternatingRowColors( true );
52 bookmarksList->setSelectionMode( QAbstractItemView::ExtendedSelection );
53 bookmarksList->setSelectionBehavior( QAbstractItemView::SelectRows );
54 bookmarksList->setEditTriggers( QAbstractItemView::SelectedClicked );
55 bookmarksList->setColumnCount( 3 );
56 QStringList headerLabels;
57 headerLabels << qtr( "Description" );
58 headerLabels << qtr( "Bytes" );
59 headerLabels << qtr( "Time" );
60 bookmarksList->setHeaderLabels( headerLabels );
63 layout->addWidget( addButton, 0, 0 );
64 layout->addWidget( delButton, 1, 0 );
65 layout->addWidget( clearButton, 2, 0 );
66 layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding ), 4, 0 );
67 layout->addWidget( extractButton, 5, 0 );
68 layout->addWidget( bookmarksList, 0, 1, 6, 1);
69 layout->setColumnStretch( 1, 1 );
71 CONNECT( bookmarksList, activated( QModelIndex ), this,
72 activateItem( QModelIndex ) );
73 CONNECT( bookmarksList, itemChanged( QTreeWidgetItem*, int ), this, edit( QTreeWidgetItem*, int ) );
75 BUTTONACT( addButton, add() );
76 BUTTONACT( delButton, del() );
77 BUTTONACT( clearButton, clear() );
78 BUTTONACT( extractButton, extract() );
80 readSettings( "Bookmarks" );
84 BookmarksDialog::~BookmarksDialog()
86 writeSettings( "Bookmarks" );
89 void BookmarksDialog::update()
91 input_thread_t *p_input = THEMIM->getInput();
92 if( !p_input ) return;
94 seekpoint_t **pp_bookmarks;
97 if( bookmarksList->topLevelItemCount() > 0 )
99 bookmarksList->model()->removeRows( 0, bookmarksList->topLevelItemCount() );
102 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
103 &i_bookmarks ) != VLC_SUCCESS )
106 for( int i = 0; i < i_bookmarks; i++ )
108 // List with the differents elements of the row
110 row << QString( pp_bookmarks[i]->psz_name );
111 row << QString( "%1" ).arg( pp_bookmarks[i]->i_byte_offset );
112 row << QString( "%1" ).arg( pp_bookmarks[i]->i_time_offset / 1000000 );
113 QTreeWidgetItem *item = new QTreeWidgetItem( bookmarksList, row );
114 item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable |
115 Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
116 bookmarksList->insertTopLevelItem( i, item );
121 void BookmarksDialog::add()
123 input_thread_t *p_input = THEMIM->getInput();
124 if( !p_input ) return;
126 seekpoint_t bookmark;
128 bookmark.psz_name = NULL;
129 bookmark.i_byte_offset = 0;
130 bookmark.i_time_offset = 0;
132 input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
133 var_Get( p_input, "time", &pos );
134 bookmark.i_time_offset = pos.i_time;
135 input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
141 void BookmarksDialog::del()
143 input_thread_t *p_input = THEMIM->getInput();
144 if( !p_input ) return;
146 int i_focused = bookmarksList->currentIndex().row();
150 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
156 void BookmarksDialog::clear()
158 input_thread_t *p_input = THEMIM->getInput();
159 if( !p_input ) return;
161 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
166 void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
168 // We can only edit a item if it is the last item selected
169 if( bookmarksList->selectedItems().isEmpty() ||
170 bookmarksList->selectedItems().last() != item )
173 input_thread_t *p_input = THEMIM->getInput();
174 if( !p_input ) return;
176 // We get the row number of the item
177 int i_edit = bookmarksList->indexOfTopLevelItem( item );
179 // We get the bookmarks list
180 seekpoint_t **pp_bookmarks;
183 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
184 &i_bookmarks ) != VLC_SUCCESS )
187 if( i_edit >= i_bookmarks )
190 // We modify the seekpoint
191 seekpoint_t *p_seekpoint = pp_bookmarks[i_edit];
193 p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
194 else if( column == 1 )
195 p_seekpoint->i_byte_offset = atoi( qtu( item->text( column ) ) );
196 else if( column == 2 )
197 p_seekpoint->i_time_offset = 1000000 * atoll( qtu( item->text( column ) ) );
199 if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit ) !=
207 void BookmarksDialog::extract()
212 void BookmarksDialog::activateItem( QModelIndex index )
214 input_thread_t *p_input = THEMIM->getInput();
215 if( !p_input ) return;
217 input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );