]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/bookmarks.cpp
Don't include config.h from the headers - refs #297.
[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 #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 bookmark" ) );
41
42     QGridLayout *layout = new QGridLayout( this );
43
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" ) );
48
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 );
61
62
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 );
70
71     CONNECT( bookmarksList, activated( QModelIndex ), this,
72              activateItem( QModelIndex ) );
73     CONNECT( bookmarksList, itemChanged( QTreeWidgetItem*, int ), this, edit( QTreeWidgetItem*, int ) );
74
75     BUTTONACT( addButton, add() );
76     BUTTONACT( delButton, del() );
77     BUTTONACT( clearButton, clear() );
78     BUTTONACT( extractButton, extract() );
79
80 }
81
82 BookmarksDialog::~BookmarksDialog()
83 {
84 }
85
86 void BookmarksDialog::update()
87 {
88     input_thread_t *p_input = THEMIM->getInput();
89     if( !p_input ) return;
90
91     seekpoint_t **pp_bookmarks;
92     int i_bookmarks;
93
94     if( bookmarksList->topLevelItemCount() > 0 )
95     {
96         bookmarksList->model()->removeRows( 0, bookmarksList->topLevelItemCount() );
97     }
98
99     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
100                        &i_bookmarks ) != VLC_SUCCESS )
101         return;
102
103     for( int i = 0; i < i_bookmarks; i++ )
104     {
105         // List with the differents elements of the row
106         QStringList row;
107         row << QString( pp_bookmarks[i]->psz_name );
108         row << QString( "%1" ).arg( pp_bookmarks[i]->i_byte_offset );
109         row << QString( "%1" ).arg( pp_bookmarks[i]->i_time_offset / 1000000 );
110         QTreeWidgetItem *item = new QTreeWidgetItem( bookmarksList, row );
111         item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable |
112                         Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
113         bookmarksList->insertTopLevelItem( i, item );
114     }
115
116 }
117
118 void BookmarksDialog::add()
119 {
120     input_thread_t *p_input = THEMIM->getInput();
121     if( !p_input ) return;
122
123     seekpoint_t bookmark;
124     vlc_value_t pos;
125     bookmark.psz_name = NULL;
126     bookmark.i_byte_offset = 0;
127     bookmark.i_time_offset = 0;
128
129     input_Control( p_input, INPUT_GET_BYTE_POSITION, &bookmark.i_byte_offset );
130     var_Get( p_input, "time", &pos );
131     bookmark.i_time_offset = pos.i_time;
132     input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
133
134     update();
135
136 }
137
138 void BookmarksDialog::del()
139 {
140     input_thread_t *p_input = THEMIM->getInput();
141     if( !p_input ) return;
142
143     int i_focused = bookmarksList->currentIndex().row();
144
145     if( i_focused >= 0 )
146     {
147         input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
148     }
149
150     update();
151 }
152
153 void BookmarksDialog::clear()
154 {
155     input_thread_t *p_input = THEMIM->getInput();
156     if( !p_input ) return;
157
158     input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
159
160     update();
161 }
162
163 void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
164 {
165     // We can only edit a item if it is the last item selected
166     if( bookmarksList->selectedItems().isEmpty() ||
167         bookmarksList->selectedItems().last() != item )
168         return;
169
170     input_thread_t *p_input = THEMIM->getInput();
171     if( !p_input ) return;
172
173     // We get the row number of the item
174     int i_edit = bookmarksList->indexOfTopLevelItem( item );
175
176     // We get the bookmarks list
177     seekpoint_t **pp_bookmarks;
178     int i_bookmarks;
179
180     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
181                        &i_bookmarks ) != VLC_SUCCESS )
182         return;
183
184     if( i_edit >= i_bookmarks )
185         return;
186
187     // We modify the seekpoint
188     seekpoint_t *p_seekpoint = pp_bookmarks[i_edit];
189     if( column == 0 )
190         p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
191     else if( column == 1 )
192         p_seekpoint->i_byte_offset = atoi( qtu( item->text( column ) ) );
193     else if( column == 2 )
194         p_seekpoint->i_time_offset = 1000000 * atoll( qtu( item->text( column ) ) );
195
196     if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit ) !=
197         VLC_SUCCESS )
198         return;
199
200     update();
201
202 }
203
204 void BookmarksDialog::extract()
205 {
206     // TODO
207 }
208
209 void BookmarksDialog::activateItem( QModelIndex index )
210 {
211     input_thread_t *p_input = THEMIM->getInput();
212     if( !p_input ) return;
213
214     input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );
215 }