]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/bookmarks.cpp
qt4_bookmarks: fix encoding.
[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
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "dialogs/bookmarks.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( THEMIM->getIM(), bookmarksChanged(),
84              this, update() );
85
86     CONNECT( bookmarksList, activated( QModelIndex ), this,
87              activateItem( QModelIndex ) );
88     CONNECT( bookmarksList, itemChanged( QTreeWidgetItem*, int ),
89              this, edit( QTreeWidgetItem*, int ) );
90
91     BUTTONACT( addButton, add() );
92     BUTTONACT( delButton, del() );
93     BUTTONACT( clearButton, clear() );
94 #if 0
95     BUTTONACT( extractButton, extract() );
96 #endif
97     BUTTONACT( closeButton, close() );
98
99     readSettings( "Bookmarks", QSize( 435, 280 ) );
100     updateGeometry();
101 }
102
103 BookmarksDialog::~BookmarksDialog()
104 {
105     writeSettings( "Bookmarks" );
106 }
107
108 void BookmarksDialog::update()
109 {
110     input_thread_t *p_input = THEMIM->getInput();
111     if( !p_input ) return;
112
113     seekpoint_t **pp_bookmarks;
114     int i_bookmarks;
115
116     if( bookmarksList->topLevelItemCount() > 0 )
117     {
118         bookmarksList->model()->removeRows( 0, bookmarksList->topLevelItemCount() );
119     }
120
121     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
122                        &i_bookmarks ) != VLC_SUCCESS )
123         return;
124
125     for( int i = 0; i < i_bookmarks; i++ )
126     {
127         // List with the differents elements of the row
128         QStringList row;
129         row << QString( qfu( pp_bookmarks[i]->psz_name ) );
130         row << QString( "%1" ).arg( pp_bookmarks[i]->i_byte_offset );
131         int total = pp_bookmarks[i]->i_time_offset/ 1000000;
132         int hour = total / (60*60);
133         int min = (total - hour*60*60) / 60;
134         int sec = total - hour*60*60 - min*60;
135         QString str;
136         row << str.sprintf("%02d:%02d:%02d", hour, min, sec );
137         QTreeWidgetItem *item = new QTreeWidgetItem( bookmarksList, row );
138         item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable |
139                         Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
140         bookmarksList->insertTopLevelItem( i, item );
141         vlc_seekpoint_Delete( pp_bookmarks[i] );
142     }
143     free( pp_bookmarks );
144 }
145
146 void BookmarksDialog::add()
147 {
148     input_thread_t *p_input = THEMIM->getInput();
149     if( !p_input ) return;
150
151     seekpoint_t bookmark;
152
153     if( !input_Control( p_input, INPUT_GET_BOOKMARK, &bookmark ) )
154     {
155         bookmark.psz_name = const_cast<char *>qtu( THEMIM->getIM()->getName() +
156                    QString("_%1" ).arg( bookmarksList->topLevelItemCount() ) );
157
158         input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
159     }
160 }
161
162 void BookmarksDialog::del()
163 {
164     input_thread_t *p_input = THEMIM->getInput();
165     if( !p_input ) return;
166
167     int i_focused = bookmarksList->currentIndex().row();
168
169     if( i_focused >= 0 )
170     {
171         input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
172     }
173 }
174
175 void BookmarksDialog::clear()
176 {
177     input_thread_t *p_input = THEMIM->getInput();
178     if( !p_input ) return;
179
180     input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
181 }
182
183 void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
184 {
185     QStringList fields;
186     // We can only edit a item if it is the last item selected
187     if( bookmarksList->selectedItems().isEmpty() ||
188         bookmarksList->selectedItems().last() != item )
189         return;
190
191     input_thread_t *p_input = THEMIM->getInput();
192     if( !p_input )
193         return;
194
195     // We get the row number of the item
196     int i_edit = bookmarksList->indexOfTopLevelItem( item );
197
198     // We get the bookmarks list
199     seekpoint_t** pp_bookmarks;
200     seekpoint_t*  p_seekpoint = NULL;
201     int i_bookmarks;
202
203     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
204                        &i_bookmarks ) != VLC_SUCCESS )
205         return;
206
207     if( i_edit >= i_bookmarks )
208         goto clear;
209
210     // We modify the seekpoint
211     p_seekpoint = pp_bookmarks[i_edit];
212     if( column == 0 )
213     {
214         free( p_seekpoint->psz_name );
215         p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
216     }
217     else if( column == 1 )
218         p_seekpoint->i_byte_offset = atoi( qtu( item->text( column ) ) );
219     else if( column == 2 )
220     {
221         fields = item->text( column ).split( ":", QString::SkipEmptyParts );
222         if( fields.size() == 1 )
223             p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() );
224         else if( fields.size() == 2 )
225             p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 60 + fields[1].toInt() );
226         else if( fields.size() == 3 )
227             p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 3600 + fields[1].toInt() * 60 + fields[2].toInt() );
228         else
229         {
230             msg_Err( p_intf, "Invalid string format for time" );
231             goto clear;
232         }
233     }
234
235     // Send the modification
236     if( input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit ) !=
237         VLC_SUCCESS )
238         goto clear;
239
240 // Clear the bookmark list
241 clear:
242     for( int i = 0; i < i_bookmarks; i++)
243     {
244         if( p_seekpoint != pp_bookmarks[i] )
245             vlc_seekpoint_Delete( pp_bookmarks[i] );
246     }
247     free( pp_bookmarks );
248 }
249
250 void BookmarksDialog::extract()
251 {
252     // TODO
253 }
254
255 void BookmarksDialog::activateItem( QModelIndex index )
256 {
257     input_thread_t *p_input = THEMIM->getInput();
258     if( !p_input ) return;
259
260     input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );
261 }