Fix annoying bug that prevented Bookmark dialog to be shown at first time.
[vlc/davidf-public.git] / modules / gui / qt4 / dialogs / bookmarks.cpp
blob097150e37f52f4624f285a4e1a813ae1bd08ee99
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 *****************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
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" ) );
50 #if 0
51 QPushButton *extractButton = new QPushButton( qtr( "Extract" ) );
52 extractButton->setToolTip( qtr() );
53 #endif
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 );
76 #if 0
77 layout->addWidget( extractButton, 5, 0 );
78 #endif
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() );
91 #if 0
92 BUTTONACT( extractButton, extract() );
93 #endif
94 BUTTONACT( closeButton, close() );
96 readSettings( "Bookmarks", QSize( 435, 206 ) );
97 updateGeometry();
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;
111 int i_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 )
120 return;
122 for( int i = 0; i < i_bookmarks; i++ )
124 // List with the differents elements of the row
125 QStringList 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;
143 vlc_value_t pos;
144 bookmark.psz_name = qtu( THEMIM->getIM()->getName() +
145 QString("_%1" ).arg( bookmarksList->topLevelItemCount() ) );
146 bookmark.i_byte_offset = 0;
147 bookmark.i_time_offset = 0;
149 input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
150 var_Get( p_input, "time", &pos );
151 bookmark.i_time_offset = pos.i_time;
152 input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
154 update();
158 void BookmarksDialog::del()
160 input_thread_t *p_input = THEMIM->getInput();
161 if( !p_input ) return;
163 int i_focused = bookmarksList->currentIndex().row();
165 if( i_focused >= 0 )
167 input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
170 update();
173 void BookmarksDialog::clear()
175 input_thread_t *p_input = THEMIM->getInput();
176 if( !p_input ) return;
178 input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
180 update();
183 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 )
188 return;
190 input_thread_t *p_input = THEMIM->getInput();
191 if( !p_input ) return;
193 // We get the row number of the item
194 int i_edit = bookmarksList->indexOfTopLevelItem( item );
196 // We get the bookmarks list
197 seekpoint_t **pp_bookmarks;
198 int i_bookmarks;
200 if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
201 &i_bookmarks ) != VLC_SUCCESS )
202 return;
204 if( i_edit >= i_bookmarks )
205 return;
207 // We modify the seekpoint
208 seekpoint_t *p_seekpoint = pp_bookmarks[i_edit];
209 if( column == 0 )
210 p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
211 else if( column == 1 )
212 p_seekpoint->i_byte_offset = atoi( qtu( item->text( column ) ) );
213 else if( column == 2 )
214 p_seekpoint->i_time_offset = 1000000 * atoll( qtu( item->text( column ) ) );
216 if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit ) !=
217 VLC_SUCCESS )
218 return;
220 update();
224 void BookmarksDialog::extract()
226 // TODO
229 void BookmarksDialog::activateItem( QModelIndex index )
231 input_thread_t *p_input = THEMIM->getInput();
232 if( !p_input ) return;
234 input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );