]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/bookmarks.cpp
Qt4 - Bookmark implementation by Antoine Lejeune - phytos # via
[vlc] / modules / gui / qt4 / dialogs / bookmarks.cpp
1 /*****************************************************************************
2  * bookmarks.cpp : Bookmarks
3  ****************************************************************************
4  * Copyright (C) 2006 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
23 #include "dialogs/bookmarks.hpp"
24 #include "dialogs_provider.hpp"
25 #include "input_manager.hpp"
26
27 #include <QGridLayout>
28 #include <QSpacerItem>
29 #include <QPushButton>
30
31 BookmarksDialog *BookmarksDialog::instance = NULL;
32
33 BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf ):QVLCFrame( _p_intf )
34 {
35     setWindowFlags( Qt::Tool );
36     setWindowOpacity( config_GetFloat( p_intf, "qt-opacity" ) );
37     setWindowTitle( qtr( "Edit bookmark" ) );
38     
39     QGridLayout *layout = new QGridLayout( this );
40     
41     QPushButton *addButton = new QPushButton( qtr( "Add" ) );
42     QPushButton *delButton = new QPushButton( qtr( "Delete" ) );
43     QPushButton *clearButton = new QPushButton( qtr( "Clear" ) );
44     QPushButton *extractButton = new QPushButton( qtr( "Extract" ) );
45     
46     bookmarksList = new QTreeWidget( this );
47     bookmarksList->setRootIsDecorated( false );
48     bookmarksList->setAlternatingRowColors( true );
49     bookmarksList->setSelectionMode( QAbstractItemView::ExtendedSelection );
50     bookmarksList->setSelectionBehavior( QAbstractItemView::SelectRows );
51     bookmarksList->setEditTriggers( QAbstractItemView::SelectedClicked );
52     bookmarksList->setColumnCount( 3 );
53     QStringList headerLabels;
54     headerLabels << qtr( "Description" );
55     headerLabels << qtr( "Bytes" );
56     headerLabels << qtr( "Time" );
57     bookmarksList->setHeaderLabels( headerLabels );
58     
59     
60     layout->addWidget( addButton, 0, 0 );
61     layout->addWidget( delButton, 1, 0 );
62     layout->addWidget( clearButton, 2, 0 );
63     layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding ), 4, 0 );
64     layout->addWidget( extractButton, 5, 0 );
65     layout->addWidget( bookmarksList, 0, 1, 6, 1);
66     layout->setColumnStretch( 1, 1 );
67     
68     CONNECT( bookmarksList, activated( QModelIndex ), this,
69              activateItem( QModelIndex ) );
70     CONNECT( bookmarksList, itemChanged( QTreeWidgetItem*, int ), this, edit( QTreeWidgetItem*, int ) );
71     
72     BUTTONACT( addButton, add() );
73     BUTTONACT( delButton, del() );
74     BUTTONACT( clearButton, clear() );
75     BUTTONACT( extractButton, extract() );
76     
77 }
78
79 BookmarksDialog::~BookmarksDialog()
80 {
81 }
82
83 void BookmarksDialog::update()
84 {
85     input_thread_t *p_input = THEMIM->getInput();
86     if( !p_input ) return;
87     
88     seekpoint_t **pp_bookmarks;
89     int i_bookmarks;
90     
91     if( bookmarksList->topLevelItemCount() > 0 )
92     {
93         bookmarksList->model()->removeRows( 0, bookmarksList->topLevelItemCount() );
94     }
95     
96     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, 
97                        &i_bookmarks ) != VLC_SUCCESS )
98         return;
99     
100     for( int i = 0; i < i_bookmarks; i++ )
101     {
102         // List with the differents elements of the row
103         QStringList row;
104         row << QString( pp_bookmarks[i]->psz_name );
105         row << QString( "%1" ).arg( pp_bookmarks[i]->i_byte_offset );
106         row << QString( "%1" ).arg( pp_bookmarks[i]->i_time_offset / 1000000 );
107         QTreeWidgetItem *item = new QTreeWidgetItem( bookmarksList, row );
108         item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable |
109                         Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
110         bookmarksList->insertTopLevelItem( i, item );
111     }
112     
113 }
114
115 void BookmarksDialog::add()
116 {
117     input_thread_t *p_input = THEMIM->getInput();
118     if( !p_input ) return;
119     
120     seekpoint_t bookmark;
121     vlc_value_t pos;
122     bookmark.psz_name = NULL;
123     bookmark.i_byte_offset = 0;
124     bookmark.i_time_offset = 0;
125     
126     input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
127     var_Get( p_input, "time", &pos );
128     bookmark.i_time_offset = pos.i_time;
129     input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
130     
131     update();
132     
133 }
134
135 void BookmarksDialog::del()
136 {
137     input_thread_t *p_input = THEMIM->getInput();
138     if( !p_input ) return;
139     
140     int i_focused = bookmarksList->currentIndex().row();
141     
142     if( i_focused >= 0 )
143     {
144         input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
145     }
146     
147     update(); 
148 }
149
150 void BookmarksDialog::clear()
151 {
152     input_thread_t *p_input = THEMIM->getInput();
153     if( !p_input ) return;
154     
155     input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
156     
157     update();
158 }
159
160 void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
161 {
162     // We can only edit a item if it is the last item selected
163     if( bookmarksList->selectedItems().isEmpty() ||
164         bookmarksList->selectedItems().last() != item )
165         return;
166     
167     input_thread_t *p_input = THEMIM->getInput();
168     if( !p_input ) return;
169     
170     // We get the row number of the item
171     int i_edit = bookmarksList->indexOfTopLevelItem( item );
172     
173     // We get the bookmarks list
174     seekpoint_t **pp_bookmarks;
175     int i_bookmarks;
176     
177     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks, 
178                        &i_bookmarks ) != VLC_SUCCESS )
179         return;
180     
181     if( i_edit >= i_bookmarks )
182         return;
183     
184     // We modify the seekpoint
185     seekpoint_t *p_seekpoint = pp_bookmarks[i_edit];
186     if( column == 0 )
187         p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
188     else if( column == 1 )
189         p_seekpoint->i_byte_offset = atoi( qtu( item->text( column ) ) );
190     else if( column == 2 )
191         p_seekpoint->i_time_offset = 1000000 * atoll( qtu( item->text( column ) ) );
192     
193     if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit ) !=
194         VLC_SUCCESS )
195         return;
196     
197     update();
198     
199 }
200
201 void BookmarksDialog::extract()
202 {
203     // TODO
204 }
205
206 void BookmarksDialog::activateItem( QModelIndex index )
207 {
208     input_thread_t *p_input = THEMIM->getInput();
209     if( !p_input ) return;
210     
211     input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );
212 }