1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/*****************************************************************************
* bookmarks.cpp : Bookmarks
****************************************************************************
* Copyright (C) 2007-2008 the VideoLAN team
*
* Authors: Antoine Lejeune <phytos@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "dialogs/bookmarks.hpp"
#include "input_manager.hpp"
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QModelIndexList>
BookmarksDialog::BookmarksDialog( intf_thread_t *_p_intf ):QVLCFrame( _p_intf )
{
b_ignore_updates = false;
setWindowFlags( Qt::Tool );
setWindowOpacity( var_InheritFloat( p_intf, "qt-opacity" ) );
setWindowTitle( qtr( "Edit Bookmarks" ) );
setWindowRole( "vlc-bookmarks" );
QHBoxLayout *layout = new QHBoxLayout( this );
QDialogButtonBox *buttonsBox = new QDialogButtonBox( Qt::Vertical );
QPushButton *addButton = new QPushButton( qtr( "Create" ) );
addButton->setToolTip( qtr( "Create a new bookmark" ) );
buttonsBox->addButton( addButton, QDialogButtonBox::ActionRole );
delButton = new QPushButton( qtr( "Delete" ) );
delButton->setToolTip( qtr( "Delete the selected item" ) );
buttonsBox->addButton( delButton, QDialogButtonBox::ActionRole );
clearButton = new QPushButton( qtr( "Clear" ) );
clearButton->setToolTip( qtr( "Delete all the bookmarks" ) );
buttonsBox->addButton( clearButton, QDialogButtonBox::ResetRole );
#if 0
QPushButton *extractButton = new QPushButton( qtr( "Extract" ) );
extractButton->setToolTip( qtr() );
buttonsBox->addButton( extractButton, QDialogButtonBox::ActionRole );
#endif
/* ?? Feels strange as Qt guidelines will put reject on top */
buttonsBox->addButton( new QPushButton( qtr( "&Close" ) ),
QDialogButtonBox::RejectRole);
bookmarksList = new QTreeWidget( this );
bookmarksList->setRootIsDecorated( false );
bookmarksList->setAlternatingRowColors( true );
bookmarksList->setSelectionMode( QAbstractItemView::ExtendedSelection );
bookmarksList->setSelectionBehavior( QAbstractItemView::SelectRows );
bookmarksList->setEditTriggers( QAbstractItemView::SelectedClicked );
bookmarksList->setColumnCount( 3 );
bookmarksList->resize( sizeHint() );
QStringList headerLabels;
headerLabels << qtr( "Description" );
headerLabels << qtr( "Bytes" );
headerLabels << qtr( "Time" );
bookmarksList->setHeaderLabels( headerLabels );
layout->addWidget( buttonsBox );
layout->addWidget( bookmarksList );
CONNECT( THEMIM->getIM(), bookmarksChanged(),
this, update() );
CONNECT( bookmarksList, activated( QModelIndex ), this,
activateItem( QModelIndex ) );
CONNECT( bookmarksList, itemChanged( QTreeWidgetItem*, int ),
this, edit( QTreeWidgetItem*, int ) );
CONNECT( bookmarksList->model(), rowsInserted( const QModelIndex &, int, int ),
this, updateButtons() );
CONNECT( bookmarksList->model(), rowsRemoved( const QModelIndex &, int, int ),
this, updateButtons() );
CONNECT( bookmarksList->selectionModel(), selectionChanged( const QItemSelection &, const QItemSelection & ),
this, updateButtons() );
BUTTONACT( addButton, add() );
BUTTONACT( delButton, del() );
BUTTONACT( clearButton, clear() );
#if 0
BUTTONACT( extractButton, extract() );
#endif
CONNECT( buttonsBox, rejected(), this, close() );
updateButtons();
restoreWidgetPosition( "Bookmarks", QSize( 435, 280 ) );
updateGeometry();
}
BookmarksDialog::~BookmarksDialog()
{
saveWidgetPosition( "Bookmarks" );
}
void BookmarksDialog::updateButtons()
{
clearButton->setEnabled( bookmarksList->model()->rowCount() > 0 );
delButton->setEnabled( bookmarksList->selectionModel()->hasSelection() );
}
void BookmarksDialog::update()
{
if ( b_ignore_updates ) return;
input_thread_t *p_input = THEMIM->getInput();
if( !p_input ) return;
seekpoint_t **pp_bookmarks;
int i_bookmarks = 0;
if( bookmarksList->topLevelItemCount() > 0 )
{
bookmarksList->model()->removeRows( 0, bookmarksList->topLevelItemCount() );
}
if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
&i_bookmarks ) != VLC_SUCCESS )
return;
for( int i = 0; i < i_bookmarks; i++ )
{
// List with the differents elements of the row
QStringList row;
row << QString( qfu( pp_bookmarks[i]->psz_name ) );
row << qfu("-");
int total = pp_bookmarks[i]->i_time_offset/ 1000000;
int hour = total / (60*60);
int min = (total - hour*60*60) / 60;
int sec = total - hour*60*60 - min*60;
QString str;
row << str.sprintf("%02d:%02d:%02d", hour, min, sec );
QTreeWidgetItem *item = new QTreeWidgetItem( bookmarksList, row );
item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable |
Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
bookmarksList->insertTopLevelItem( i, item );
vlc_seekpoint_Delete( pp_bookmarks[i] );
}
free( pp_bookmarks );
}
void BookmarksDialog::add()
{
input_thread_t *p_input = THEMIM->getInput();
if( !p_input ) return;
seekpoint_t bookmark;
if( !input_Control( p_input, INPUT_GET_BOOKMARK, &bookmark ) )
{
QString name = THEMIM->getIM()->getName() + " #"
+ QString::number( bookmarksList->topLevelItemCount() );
bookmark.psz_name = const_cast<char *>qtu( name );
input_Control( p_input, INPUT_ADD_BOOKMARK, &bookmark );
}
}
void BookmarksDialog::del()
{
input_thread_t *p_input = THEMIM->getInput();
if( !p_input ) return;
QModelIndexList selected = bookmarksList->selectionModel()->selectedIndexes();
if ( !selected.empty() )
{
b_ignore_updates = true;
QModelIndexList::Iterator it = selected.end();
for( --it; it != selected.begin(); it-- )
{
/* FIXME: Find out why selectedIndexes() doesn't follow the
SelectRows selectionBehavior() and returns all columns */
if ( (*it).column() == 0 )
input_Control( p_input, INPUT_DEL_BOOKMARK, (*it).row() );
}
if ( (*it).column() == 0 )
input_Control( p_input, INPUT_DEL_BOOKMARK, (*it).row() );
b_ignore_updates = false;
update();
}
}
void BookmarksDialog::clear()
{
input_thread_t *p_input = THEMIM->getInput();
if( !p_input ) return;
input_Control( p_input, INPUT_CLEAR_BOOKMARKS );
}
void BookmarksDialog::edit( QTreeWidgetItem *item, int column )
{
QStringList fields;
// We can only edit a item if it is the last item selected
if( bookmarksList->selectedItems().isEmpty() ||
bookmarksList->selectedItems().last() != item )
return;
input_thread_t *p_input = THEMIM->getInput();
if( !p_input )
return;
// We get the row number of the item
int i_edit = bookmarksList->indexOfTopLevelItem( item );
// We get the bookmarks list
seekpoint_t** pp_bookmarks;
seekpoint_t* p_seekpoint = NULL;
int i_bookmarks;
if( input_Control( p_input, INPUT_GET_BOOKMARKS, &pp_bookmarks,
&i_bookmarks ) != VLC_SUCCESS )
return;
if( i_edit >= i_bookmarks )
goto clear;
// We modify the seekpoint
p_seekpoint = pp_bookmarks[i_edit];
if( column == 0 )
{
free( p_seekpoint->psz_name );
p_seekpoint->psz_name = strdup( qtu( item->text( column ) ) );
}
else if( column == 2 )
{
fields = item->text( column ).split( ":", QString::SkipEmptyParts );
if( fields.count() == 1 )
p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() );
else if( fields.count() == 2 )
p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 60 + fields[1].toInt() );
else if( fields.count() == 3 )
p_seekpoint->i_time_offset = 1000000 * ( fields[0].toInt() * 3600 + fields[1].toInt() * 60 + fields[2].toInt() );
else
{
msg_Err( p_intf, "Invalid string format for time" );
goto clear;
}
}
// Send the modification
input_Control( p_input, INPUT_CHANGE_BOOKMARK, p_seekpoint, i_edit );
clear:
// Clear the bookmark list
for( int i = 0; i < i_bookmarks; i++)
vlc_seekpoint_Delete( pp_bookmarks[i] );
free( pp_bookmarks );
}
void BookmarksDialog::extract()
{
// TODO
}
void BookmarksDialog::activateItem( QModelIndex index )
{
input_thread_t *p_input = THEMIM->getInput();
if( !p_input ) return;
input_Control( p_input, INPUT_SET_BOOKMARK, index.row() );
}
void BookmarksDialog::toggleVisible()
{
/* Update, to show existing bookmarks in case a new playlist
was opened */
if( !isVisible() )
{
update();
}
QVLCFrame::toggleVisible();
}
|