]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/bookmarks.cpp
Fix Bookmarks: missing button, missing tooltips, wrong resize on startup.
[vlc] / modules / gui / qt4 / dialogs / bookmarks.cpp
1 /*****************************************************************************
2  * bookmarks.cpp : Bookmarks
3  ****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  *
6  * Authors: Antoine Lejeune <phytos@via.ecp.fr>
7  *
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.
12  *
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.
17  *
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
25
26 #include "dialogs/bookmarks.hpp"
27 #include "dialogs_provider.hpp"
28 #include "input_manager.hpp"
29
30 #include <QGridLayout>
31 #include <QSpacerItem>
32 #include <QPushButton>
33
34 BookmarksDialog *BookmarksDialog::instance = NULL;
35
36 BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf ):QVLCFrame( _p_intf )
37 {
38     setWindowFlags( Qt::Tool );
39     setWindowOpacity( config_GetFloat( p_intf, "qt-opacity" ) );
40     setWindowTitle( qtr( "Edit Bookmarks" ) );
41
42     QGridLayout *layout = new QGridLayout( this );
43
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" ) );
55
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() );
64
65     QStringList headerLabels;
66     headerLabels << qtr( "Description" );
67     headerLabels << qtr( "Bytes" );
68     headerLabels << qtr( "Time" );
69     bookmarksList->setHeaderLabels( headerLabels );
70
71
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 );
82
83     CONNECT( bookmarksList, activated( QModelIndex ), this,
84              activateItem( QModelIndex ) );
85     CONNECT( bookmarksList, itemChanged( QTreeWidgetItem*, int ),
86              this, edit( QTreeWidgetItem*, int ) );
87
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() );
95
96     readSettings( "Bookmarks" );
97     updateGeometry();
98 }
99
100 BookmarksDialog::~BookmarksDialog()
101 {
102     writeSettings( "Bookmarks" );
103 }
104
105 void BookmarksDialog::update()
106 {
107     input_thread_t *p_input = THEMIM->getInput();
108     if( !p_input ) return;
109
110     seekpoint_t **pp_bookmarks;
111     int i_bookmarks;
112
113     if( bookmarksList->topLevelItemCount() > 0 )
114     {
115         bookmarksList->model()->removeRows( 0, bookmarksList->topLevelItemCount() );
116     }
117
118     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
119                        &i_bookmarks ) != VLC_SUCCESS )
120         return;
121
122     for( int i = 0; i < i_bookmarks; i++ )
123     {
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 );
133     }
134
135 }
136
137 void BookmarksDialog::add()
138 {
139     input_thread_t *p_input = THEMIM->getInput();
140     if( !p_input ) return;
141
142     seekpoint_t bookmark;
143     vlc_value_t pos;
144     bookmark.psz_name = NULL;
145     bookmark.i_byte_offset = 0;
146     bookmark.i_time_offset = 0;
147
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 );
152
153     update();
154
155 }
156
157 void BookmarksDialog::del()
158 {
159     input_thread_t *p_input = THEMIM->getInput();
160     if( !p_input ) return;
161
162     int i_focused = bookmarksList->currentIndex().row();
163
164     if( i_focused >= 0 )
165     {
166         input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
167     }
168
169     update();
170 }
171
172 void BookmarksDialog::clear()
173 {
174     input_thread_t *p_input = THEMIM->getInput();
175     if( !p_input ) return;
176
177     input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
178
179     update();
180 }
181
182 void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
183 {
184     // We can only edit a item if it is the last item selected
185     if( bookmarksList->selectedItems().isEmpty() ||
186         bookmarksList->selectedItems().last() != item )
187         return;
188
189     input_thread_t *p_input = THEMIM->getInput();
190     if( !p_input ) return;
191
192     // We get the row number of the item
193     int i_edit = bookmarksList->indexOfTopLevelItem( item );
194
195     // We get the bookmarks list
196     seekpoint_t **pp_bookmarks;
197     int i_bookmarks;
198
199     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
200                        &i_bookmarks ) != VLC_SUCCESS )
201         return;
202
203     if( i_edit >= i_bookmarks )
204         return;
205
206     // We modify the seekpoint
207     seekpoint_t *p_seekpoint = pp_bookmarks[i_edit];
208     if( column == 0 )
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 ) ) );
214
215     if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit ) !=
216         VLC_SUCCESS )
217         return;
218
219     update();
220
221 }
222
223 void BookmarksDialog::extract()
224 {
225     // TODO
226 }
227
228 void BookmarksDialog::activateItem( QModelIndex index )
229 {
230     input_thread_t *p_input = THEMIM->getInput();
231     if( !p_input ) return;
232
233     input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );
234 }