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 <QHBoxLayout>
31 #include <QSpacerItem>
32 #include <QPushButton>
33 #include <QDialogButtonBox>
35 BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf ):QVLCFrame( _p_intf )
37 setWindowFlags( Qt::Tool );
38 setWindowOpacity( var_InheritFloat( p_intf, "qt-opacity" ) );
39 setWindowTitle( qtr( "Edit Bookmarks" ) );
40 setWindowRole( "vlc-bookmarks" );
42 QHBoxLayout *layout = new QHBoxLayout( this );
44 QDialogButtonBox *buttonsBox = new QDialogButtonBox( Qt::Vertical );
45 QPushButton *addButton = new QPushButton( qtr( "Create" ) );
46 addButton->setToolTip( qtr( "Create a new bookmark" ) );
47 buttonsBox->addButton( addButton, QDialogButtonBox::ActionRole );
48 QPushButton *delButton = new QPushButton( qtr( "Delete" ) );
49 delButton->setToolTip( qtr( "Delete the selected item" ) );
50 buttonsBox->addButton( delButton, QDialogButtonBox::ActionRole );
51 QPushButton *clearButton = new QPushButton( qtr( "Clear" ) );
52 clearButton->setToolTip( qtr( "Delete all the bookmarks" ) );
53 buttonsBox->addButton( clearButton, QDialogButtonBox::ResetRole );
55 QPushButton *extractButton = new QPushButton( qtr( "Extract" ) );
56 extractButton->setToolTip( qtr() );
57 buttonsBox->addButton( extractButton, QDialogButtonBox::ActionRole );
59 /* ?? Feels strange as Qt guidelines will put reject on top */
60 buttonsBox->addButton( new QPushButton( qtr( "&Close" ) ),
61 QDialogButtonBox::RejectRole);
63 bookmarksList = new QTreeWidget( this );
64 bookmarksList->setRootIsDecorated( false );
65 bookmarksList->setAlternatingRowColors( true );
66 bookmarksList->setSelectionMode( QAbstractItemView::ExtendedSelection );
67 bookmarksList->setSelectionBehavior( QAbstractItemView::SelectRows );
68 bookmarksList->setEditTriggers( QAbstractItemView::SelectedClicked );
69 bookmarksList->setColumnCount( 3 );
70 bookmarksList->resize( sizeHint() );
72 QStringList headerLabels;
73 headerLabels << qtr( "Description" );
74 headerLabels << qtr( "Bytes" );
75 headerLabels << qtr( "Time" );
76 bookmarksList->setHeaderLabels( headerLabels );
78 layout->addWidget( buttonsBox );
79 layout->addWidget( bookmarksList );
81 CONNECT( THEMIM->getIM(), bookmarksChanged(),
84 CONNECT( bookmarksList, activated( QModelIndex ), this,
85 activateItem( QModelIndex ) );
86 CONNECT( bookmarksList, itemChanged( QTreeWidgetItem*, int ),
87 this, edit( QTreeWidgetItem*, int ) );
89 BUTTONACT( addButton, add() );
90 BUTTONACT( delButton, del() );
91 BUTTONACT( clearButton, clear() );
93 BUTTONACT( extractButton, extract() );
95 CONNECT( buttonsBox, rejected(), this, close() );
97 readSettings( "Bookmarks", QSize( 435, 280 ) );
101 BookmarksDialog::~BookmarksDialog()
103 writeSettings( "Bookmarks" );
106 void BookmarksDialog::update()
108 input_thread_t *p_input = THEMIM->getInput();
109 if( !p_input ) return;
111 seekpoint_t **pp_bookmarks;
114 if( bookmarksList->topLevelItemCount() > 0 )
116 bookmarksList->model()->removeRows( 0, bookmarksList->topLevelItemCount() );
119 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
120 &i_bookmarks ) != VLC_SUCCESS )
123 for( int i = 0; i < i_bookmarks; i++ )
125 // List with the differents elements of the row
127 row << QString( qfu( pp_bookmarks[i]->psz_name ) );
128 row << QString::number( pp_bookmarks[i]->i_byte_offset );
129 int total = pp_bookmarks[i]->i_time_offset/ 1000000;
130 int hour = total / (60*60);
131 int min = (total - hour*60*60) / 60;
132 int sec = total - hour*60*60 - min*60;
134 row << str.sprintf("%02d:%02d:%02d", hour, min, sec );
135 QTreeWidgetItem *item = new QTreeWidgetItem( bookmarksList, row );
136 item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable |
137 Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
138 bookmarksList->insertTopLevelItem( i, item );
139 vlc_seekpoint_Delete( pp_bookmarks[i] );
141 free( pp_bookmarks );
144 void BookmarksDialog::add()
146 input_thread_t *p_input = THEMIM->getInput();
147 if( !p_input ) return;
149 seekpoint_t bookmark;
151 if( !input_Control( p_input, INPUT_GET_BOOKMARK, &bookmark ) )
153 QString name = THEMIM->getIM()->getName()
154 + QString::number( bookmarksList->topLevelItemCount() );
155 bookmark.psz_name = const_cast<char *>qtu( name );
157 input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
161 void BookmarksDialog::del()
163 input_thread_t *p_input = THEMIM->getInput();
164 if( !p_input ) return;
166 int i_focused = bookmarksList->currentIndex().row();
170 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
174 void BookmarksDialog::clear()
176 input_thread_t *p_input = THEMIM->getInput();
177 if( !p_input ) return;
179 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
182 void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
185 // We can only edit a item if it is the last item selected
186 if( bookmarksList->selectedItems().isEmpty() ||
187 bookmarksList->selectedItems().last() != item )
190 input_thread_t *p_input = THEMIM->getInput();
194 // We get the row number of the item
195 int i_edit = bookmarksList->indexOfTopLevelItem( item );
197 // We get the bookmarks list
198 seekpoint_t** pp_bookmarks;
199 seekpoint_t* p_seekpoint = NULL;
202 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
203 &i_bookmarks ) != VLC_SUCCESS )
206 if( i_edit >= i_bookmarks )
209 // We modify the seekpoint
210 p_seekpoint = pp_bookmarks[i_edit];
213 free( p_seekpoint->psz_name );
214 p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
216 else if( column == 1 )
217 p_seekpoint->i_byte_offset = atoi( qtu( item->text( column ) ) );
218 else if( column == 2 )
220 fields = item->text( column ).split( ":", QString::SkipEmptyParts );
221 if( fields.size() == 1 )
222 p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() );
223 else if( fields.size() == 2 )
224 p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 60 + fields[1].toInt() );
225 else if( fields.size() == 3 )
226 p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 3600 + fields[1].toInt() * 60 + fields[2].toInt() );
229 msg_Err( p_intf, "Invalid string format for time" );
234 // Send the modification
235 input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit );
238 // Clear the bookmark list
239 for( int i = 0; i < i_bookmarks; i++)
240 vlc_seekpoint_Delete( pp_bookmarks[i] );
241 free( pp_bookmarks );
244 void BookmarksDialog::extract()
249 void BookmarksDialog::activateItem( QModelIndex index )
251 input_thread_t *p_input = THEMIM->getInput();
252 if( !p_input ) return;
254 input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );