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