]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/bookmarks.cpp
Qt4: use var_Inherit
[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( intf_thread_t *_p_intf ):QVLCFrame( _p_intf )
35 {
36     setWindowFlags( Qt::Tool );
37     setWindowOpacity( var_InheritFloat( p_intf, "qt-opacity" ) );
38     setWindowTitle( qtr( "Edit Bookmarks" ) );
39     setWindowRole( "vlc-bookmarks" );
40
41     QGridLayout *layout = new QGridLayout( this );
42
43     QPushButton *addButton = new QPushButton( qtr( "Create" ) );
44     addButton->setToolTip( qtr( "Create a new bookmark" ) );
45     QPushButton *delButton = new QPushButton( qtr( "Delete" ) );
46     delButton->setToolTip( qtr( "Delete the selected item" ) );
47     QPushButton *clearButton = new QPushButton( qtr( "Clear" ) );
48     clearButton->setToolTip( qtr( "Delete all the bookmarks" ) );
49 #if 0
50     QPushButton *extractButton = new QPushButton( qtr( "Extract" ) );
51     extractButton->setToolTip( qtr() );
52 #endif
53     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
54
55     bookmarksList = new QTreeWidget( this );
56     bookmarksList->setRootIsDecorated( false );
57     bookmarksList->setAlternatingRowColors( true );
58     bookmarksList->setSelectionMode( QAbstractItemView::ExtendedSelection );
59     bookmarksList->setSelectionBehavior( QAbstractItemView::SelectRows );
60     bookmarksList->setEditTriggers( QAbstractItemView::SelectedClicked );
61     bookmarksList->setColumnCount( 3 );
62     bookmarksList->resize( sizeHint() );
63
64     QStringList headerLabels;
65     headerLabels << qtr( "Description" );
66     headerLabels << qtr( "Bytes" );
67     headerLabels << qtr( "Time" );
68     bookmarksList->setHeaderLabels( headerLabels );
69
70
71     layout->addWidget( addButton, 0, 0 );
72     layout->addWidget( delButton, 1, 0 );
73     layout->addWidget( clearButton, 2, 0 );
74     layout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding ), 4, 0 );
75 #if 0
76     layout->addWidget( extractButton, 5, 0 );
77 #endif
78     layout->addWidget( bookmarksList, 0, 1, 6, 2);
79     layout->setColumnStretch( 1, 1 );
80     layout->addWidget( closeButton, 7, 2 );
81
82     CONNECT( THEMIM->getIM(), bookmarksChanged(),
83              this, update() );
84
85     CONNECT( bookmarksList, activated( QModelIndex ), this,
86              activateItem( QModelIndex ) );
87     CONNECT( bookmarksList, itemChanged( QTreeWidgetItem*, int ),
88              this, edit( QTreeWidgetItem*, int ) );
89
90     BUTTONACT( addButton, add() );
91     BUTTONACT( delButton, del() );
92     BUTTONACT( clearButton, clear() );
93 #if 0
94     BUTTONACT( extractButton, extract() );
95 #endif
96     BUTTONACT( closeButton, close() );
97
98     readSettings( "Bookmarks", QSize( 435, 280 ) );
99     updateGeometry();
100 }
101
102 BookmarksDialog::~BookmarksDialog()
103 {
104     writeSettings( "Bookmarks" );
105 }
106
107 void BookmarksDialog::update()
108 {
109     input_thread_t *p_input = THEMIM->getInput();
110     if( !p_input ) return;
111
112     seekpoint_t **pp_bookmarks;
113     int i_bookmarks;
114
115     if( bookmarksList->topLevelItemCount() > 0 )
116     {
117         bookmarksList->model()->removeRows( 0, bookmarksList->topLevelItemCount() );
118     }
119
120     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
121                        &i_bookmarks ) != VLC_SUCCESS )
122         return;
123
124     for( int i = 0; i < i_bookmarks; i++ )
125     {
126         // List with the differents elements of the row
127         QStringList row;
128         row << QString( qfu( pp_bookmarks[i]->psz_name ) );
129         row << QString::number( pp_bookmarks[i]->i_byte_offset );
130         int total = pp_bookmarks[i]->i_time_offset/ 1000000;
131         int hour = total / (60*60);
132         int min = (total - hour*60*60) / 60;
133         int sec = total - hour*60*60 - min*60;
134         QString str;
135         row << str.sprintf("%02d:%02d:%02d", hour, min, sec );
136         QTreeWidgetItem *item = new QTreeWidgetItem( bookmarksList, row );
137         item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable |
138                         Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
139         bookmarksList->insertTopLevelItem( i, item );
140         vlc_seekpoint_Delete( pp_bookmarks[i] );
141     }
142     free( pp_bookmarks );
143 }
144
145 void BookmarksDialog::add()
146 {
147     input_thread_t *p_input = THEMIM->getInput();
148     if( !p_input ) return;
149
150     seekpoint_t bookmark;
151
152     if( !input_Control( p_input, INPUT_GET_BOOKMARK, &bookmark ) )
153     {
154         bookmark.psz_name = const_cast<char *>qtu( THEMIM->getIM()->getName() +
155                    QString::number( bookmarksList->topLevelItemCount() ) );
156
157         input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
158     }
159 }
160
161 void BookmarksDialog::del()
162 {
163     input_thread_t *p_input = THEMIM->getInput();
164     if( !p_input ) return;
165
166     int i_focused = bookmarksList->currentIndex().row();
167
168     if( i_focused >= 0 )
169     {
170         input_Control( p_input, INPUT_DEL_BOOKMARK, i_focused );
171     }
172 }
173
174 void BookmarksDialog::clear()
175 {
176     input_thread_t *p_input = THEMIM->getInput();
177     if( !p_input ) return;
178
179     input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
180 }
181
182 void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
183 {
184     QStringList fields;
185     // We can only edit a item if it is the last item selected
186     if( bookmarksList->selectedItems().isEmpty() ||
187         bookmarksList->selectedItems().last() != item )
188         return;
189
190     input_thread_t *p_input = THEMIM->getInput();
191     if( !p_input )
192         return;
193
194     // We get the row number of the item
195     int i_edit = bookmarksList->indexOfTopLevelItem( item );
196
197     // We get the bookmarks list
198     seekpoint_t** pp_bookmarks;
199     seekpoint_t*  p_seekpoint = NULL;
200     int i_bookmarks;
201
202     if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
203                        &i_bookmarks ) != VLC_SUCCESS )
204         return;
205
206     if( i_edit >= i_bookmarks )
207         goto clear;
208
209     // We modify the seekpoint
210     p_seekpoint = pp_bookmarks[i_edit];
211     if( column == 0 )
212     {
213         free( p_seekpoint->psz_name );
214         p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
215     }
216     else if( column == 1 )
217         p_seekpoint->i_byte_offset = atoi( qtu( item->text( column ) ) );
218     else if( column == 2 )
219     {
220         fields = item->text( column ).split( ":", QString::SkipEmptyParts );
221         if( fields.size() == 1 )
222             p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() );
223         else if( fields.size() == 2 )
224             p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 60 + fields[1].toInt() );
225         else if( fields.size() == 3 )
226             p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 3600 + fields[1].toInt() * 60 + fields[2].toInt() );
227         else
228         {
229             msg_Err( p_intf, "Invalid string format for time" );
230             goto clear;
231         }
232     }
233
234     // Send the modification
235     input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit );
236
237 clear:
238     // Clear the bookmark list
239     for( int i = 0; i < i_bookmarks; i++)
240         vlc_seekpoint_Delete( pp_bookmarks[i] );
241     free( pp_bookmarks );
242 }
243
244 void BookmarksDialog::extract()
245 {
246     // TODO
247 }
248
249 void BookmarksDialog::activateItem( QModelIndex index )
250 {
251     input_thread_t *p_input = THEMIM->getInput();
252     if( !p_input ) return;
253
254     input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );
255 }