]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/bookmarks.cpp
bfea0eb061f31f99d21958ddfe8a0e0470f70cae
[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 <QHBoxLayout>
31 #include <QSpacerItem>
32 #include <QPushButton>
33 #include <QDialogButtonBox>
34 #include <QModelIndexList>
35
36 BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf ):QVLCFrame( _p_intf )
37 {
38     b_ignore_updates = false;
39     setWindowFlags( Qt::Tool );
40     setWindowOpacity( var_InheritFloat( p_intf, "qt-opacity" ) );
41     setWindowTitle( qtr( "Edit Bookmarks" ) );
42     setWindowRole( "vlc-bookmarks" );
43
44     QHBoxLayout *layout = new QHBoxLayout( this );
45
46     QDialogButtonBox *buttonsBox = new QDialogButtonBox( Qt::Vertical );
47     QPushButton *addButton = new QPushButton( qtr( "Create" ) );
48     addButton->setToolTip( qtr( "Create a new bookmark" ) );
49     buttonsBox->addButton( addButton, QDialogButtonBox::ActionRole );
50     delButton = new QPushButton( qtr( "Delete" ) );
51     delButton->setToolTip( qtr( "Delete the selected item" ) );
52     buttonsBox->addButton( delButton, QDialogButtonBox::ActionRole );
53     clearButton = new QPushButton( qtr( "Clear" ) );
54     clearButton->setToolTip( qtr( "Delete all the bookmarks" ) );
55     buttonsBox->addButton( clearButton, QDialogButtonBox::ResetRole );
56 #if 0
57     QPushButton *extractButton = new QPushButton( qtr( "Extract" ) );
58     extractButton->setToolTip( qtr() );
59     buttonsBox->addButton( extractButton, QDialogButtonBox::ActionRole );
60 #endif
61     /* ?? Feels strange as Qt guidelines will put reject on top */
62     buttonsBox->addButton( new QPushButton( qtr( "&Close" ) ),
63                           QDialogButtonBox::RejectRole);
64
65     bookmarksList = new QTreeWidget( this );
66     bookmarksList->setRootIsDecorated( false );
67     bookmarksList->setAlternatingRowColors( true );
68     bookmarksList->setSelectionMode( QAbstractItemView::ExtendedSelection );
69     bookmarksList->setSelectionBehavior( QAbstractItemView::SelectRows );
70     bookmarksList->setEditTriggers( QAbstractItemView::SelectedClicked );
71     bookmarksList->setColumnCount( 3 );
72     bookmarksList->resize( sizeHint() );
73
74     QStringList headerLabels;
75     headerLabels << qtr( "Description" );
76     headerLabels << qtr( "Bytes" );
77     headerLabels << qtr( "Time" );
78     bookmarksList->setHeaderLabels( headerLabels );
79
80     layout->addWidget( buttonsBox );
81     layout->addWidget( bookmarksList );
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     CONNECT( bookmarksList->model(), rowsInserted( const QModelIndex &, int, int ),
91              this, updateButtons() );
92     CONNECT( bookmarksList->model(), rowsRemoved( const QModelIndex &, int, int ),
93              this, updateButtons() );
94     CONNECT( bookmarksList->selectionModel(), selectionChanged( const QItemSelection &, const QItemSelection & ),
95              this, updateButtons() );
96     BUTTONACT( addButton, add() );
97     BUTTONACT( delButton, del() );
98     BUTTONACT( clearButton, clear() );
99
100 #if 0
101     BUTTONACT( extractButton, extract() );
102 #endif
103     CONNECT( buttonsBox, rejected(), this, close() );
104     updateButtons();
105
106     restoreWidgetPosition( "Bookmarks", QSize( 435, 280 ) );
107     updateGeometry();
108 }
109
110 BookmarksDialog::~BookmarksDialog()
111 {
112     saveWidgetPosition( "Bookmarks" );
113 }
114
115 void BookmarksDialog::updateButtons()
116 {
117     clearButton->setEnabled( bookmarksList->model()->rowCount() > 0 );
118     delButton->setEnabled( bookmarksList->selectionModel()->hasSelection() );
119 }
120
121 void BookmarksDialog::update()
122 {
123     if ( b_ignore_updates ) return;
124     input_thread_t *p_input = THEMIM->getInput();
125     if( !p_input ) return;
126
127     seekpoint_t **pp_bookmarks;
128     int i_bookmarks = 0;
129
130     if( bookmarksList->topLevelItemCount() > 0 )
131     {
132         bookmarksList->model()->removeRows( 0, bookmarksList->topLevelItemCount() );
133     }
134
135     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
136                        &i_bookmarks ) != VLC_SUCCESS )
137         return;
138
139     for( int i = 0; i < i_bookmarks; i++ )
140     {
141         // List with the differents elements of the row
142         QStringList row;
143         row << QString( qfu( pp_bookmarks[i]->psz_name ) );
144         row << qfu("-");
145         int total = pp_bookmarks[i]->i_time_offset/ 1000000;
146         int hour = total / (60*60);
147         int min = (total - hour*60*60) / 60;
148         int sec = total - hour*60*60 - min*60;
149         QString str;
150         row << str.sprintf("%02d:%02d:%02d", hour, min, sec );
151         QTreeWidgetItem *item = new QTreeWidgetItem( bookmarksList, row );
152         item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable |
153                         Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
154         bookmarksList->insertTopLevelItem( i, item );
155         vlc_seekpoint_Delete( pp_bookmarks[i] );
156     }
157     free( pp_bookmarks );
158 }
159
160 void BookmarksDialog::add()
161 {
162     input_thread_t *p_input = THEMIM->getInput();
163     if( !p_input ) return;
164
165     seekpoint_t bookmark;
166
167     if( !input_Control( p_input, INPUT_GET_BOOKMARK, &bookmark ) )
168     {
169         QString name = THEMIM->getIM()->getName() + " #"
170                      + QString::number( bookmarksList->topLevelItemCount() );
171         bookmark.psz_name = const_cast<char *>qtu( name );
172
173         input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
174     }
175 }
176
177 void BookmarksDialog::del()
178 {
179     input_thread_t *p_input = THEMIM->getInput();
180     if( !p_input ) return;
181
182     QModelIndexList selected = bookmarksList->selectionModel()->selectedIndexes();
183     if ( !selected.empty() )
184     {
185         b_ignore_updates = true;
186         QModelIndexList::Iterator it = selected.end();
187         for( --it; it != selected.begin(); it-- )
188         {
189             /* FIXME: Find out why selectedIndexes() doesn't follow the
190             SelectRows selectionBehavior() and returns all columns */
191             if ( (*it).column() == 0 )
192                 input_Control( p_input, INPUT_DEL_BOOKMARK, (*it).row() );
193         }
194         if ( (*it).column() == 0 )
195             input_Control( p_input, INPUT_DEL_BOOKMARK, (*it).row() );
196         b_ignore_updates = false;
197         update();
198     }
199 }
200
201 void BookmarksDialog::clear()
202 {
203     input_thread_t *p_input = THEMIM->getInput();
204     if( !p_input ) return;
205
206     input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
207 }
208
209 void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
210 {
211     QStringList fields;
212     // We can only edit a item if it is the last item selected
213     if( bookmarksList->selectedItems().isEmpty() ||
214         bookmarksList->selectedItems().last() != item )
215         return;
216
217     input_thread_t *p_input = THEMIM->getInput();
218     if( !p_input )
219         return;
220
221     // We get the row number of the item
222     int i_edit = bookmarksList->indexOfTopLevelItem( item );
223
224     // We get the bookmarks list
225     seekpoint_t** pp_bookmarks;
226     seekpoint_t*  p_seekpoint = NULL;
227     int i_bookmarks;
228
229     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
230                        &i_bookmarks ) != VLC_SUCCESS )
231         return;
232
233     if( i_edit >= i_bookmarks )
234         goto clear;
235
236     // We modify the seekpoint
237     p_seekpoint = pp_bookmarks[i_edit];
238     if( column == 0 )
239     {
240         free( p_seekpoint->psz_name );
241         p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
242     }
243     else if( column == 2 )
244     {
245         fields = item->text( column ).split( ":", QString::SkipEmptyParts );
246         if( fields.count() == 1 )
247             p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() );
248         else if( fields.count() == 2 )
249             p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 60 + fields[1].toInt() );
250         else if( fields.count() == 3 )
251             p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 3600 + fields[1].toInt() * 60 + fields[2].toInt() );
252         else
253         {
254             msg_Err( p_intf, "Invalid string format for time" );
255             goto clear;
256         }
257     }
258
259     // Send the modification
260     input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit );
261
262 clear:
263     // Clear the bookmark list
264     for( int i = 0; i < i_bookmarks; i++)
265         vlc_seekpoint_Delete( pp_bookmarks[i] );
266     free( pp_bookmarks );
267 }
268
269 void BookmarksDialog::extract()
270 {
271     // TODO
272 }
273
274 void BookmarksDialog::activateItem( QModelIndex index )
275 {
276     input_thread_t *p_input = THEMIM->getInput();
277     if( !p_input ) return;
278
279     input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );
280 }
281
282 void BookmarksDialog::toggleVisible()
283 {
284     /* Update, to show existing bookmarks in case a new playlist
285        was opened */
286     if( !isVisible() )
287     {
288         update();
289     }
290     QVLCFrame::toggleVisible();
291 }