1
/***************************************************************************
2
* Copyright (C) 2004-2009 by Thomas Fischer *
3
* fischer@unix-ag.uni-kl.de *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
26
#include <qwhatsthis.h>
27
#include <qpushbutton.h>
28
#include <qcheckbox.h>
30
#include <klistview.h>
31
#include <klineedit.h>
35
#include <kiconloader.h>
38
#include <valuewidget.h>
40
#include "fieldlistview.h"
45
FieldListView::FieldListView( const QString& caption, const QString& prefixNew, bool isReadOnly, QWidget *parent, const char *name )
46
: QWidget( parent, name ), m_value( new BibTeX::Value() ), m_caption( caption ), m_prefixNew( prefixNew ), m_isReadOnly( isReadOnly ), m_enabled( TRUE ), m_isComplex( FALSE ), m_isModified( FALSE ), m_newValueCounter( 0 ), m_fieldType( BibTeX::EntryField::ftUnknown )
49
m_listViewElements->installEventFilter( this );
50
m_listViewElements->renameLineEdit() ->installEventFilter( this );
52
m_value = new BibTeX::Value();
56
FieldListView::~FieldListView()
61
void FieldListView::setValue( const BibTeX::Value *value )
63
if ( value != m_value )
65
if ( m_value != NULL )
69
m_value = new BibTeX::Value( value );
71
m_value = new BibTeX::Value( );
80
BibTeX::Value *FieldListView::value()
82
if ( m_value->items.isEmpty() )
85
return new BibTeX::Value( m_value );
88
void FieldListView::setEnabled( bool enabled )
94
void FieldListView::setFieldType( BibTeX::EntryField::FieldType fieldType )
96
m_fieldType = fieldType;
98
Settings * settings = Settings::self();
99
m_listViewElements->renameLineEdit() ->setCompletionObject( settings->completion( m_fieldType ) );
100
QToolTip::add( m_listViewElements, QString( i18n( "BibTeX field '%1'" ) ).arg( BibTeX::EntryField::fieldTypeToString( fieldType ) ) );
101
QWhatsThis::add( m_listViewElements, QString( i18n( "BibTeX field '%1'" ) ).arg( BibTeX::EntryField::fieldTypeToString( fieldType ) ) );
103
m_value->items.clear();
106
QString FieldListView::caption()
111
bool FieldListView::isEmpty()
113
return m_value != NULL ? m_value->items.isEmpty() : TRUE;
116
bool FieldListView::isModified()
121
bool FieldListView::eventFilter( QObject *o, QEvent * e )
123
if ( o == m_listViewElements->renameLineEdit() )
125
if ( e->type() == QEvent::Hide )
128
else if ( e->type() == QEvent::AccelOverride )
130
QKeyEvent * ke = static_cast<QKeyEvent*>( e );
131
//override delete action
132
if ( ke->key() == Key_Delete && ke->state() == NoButton )
138
else if ( ke->key() == Key_F2 && ke->state() == NoButton )
144
else if ( ke->key() == Key_A && ke->state() == ControlButton )
150
else if ( ke->key() == Key_Up && ke->state() == ControlButton )
156
else if ( ke->key() == Key_Down && ke->state() == ControlButton )
162
else if ( ke->key() == Key_C && ke->state() == ( ControlButton | AltButton ) )
173
void FieldListView::updateGUI()
175
disconnect( m_checkBoxEtAl, SIGNAL( toggled( bool ) ), this, SLOT( apply() ) );
176
if ( m_value != NULL && !m_isComplex )
178
bool isElementSelected = m_listViewElements->selectedItem() != NULL;
179
m_pushButtonAdd->setEnabled( !m_isReadOnly );
180
m_pushButtonEdit->setEnabled( !m_isReadOnly && isElementSelected );
181
m_pushButtonDelete->setEnabled( !m_isReadOnly && isElementSelected );
182
m_pushButtonUp->setEnabled( !m_isReadOnly && isElementSelected && m_listViewElements->selectedItem() != m_listViewElements->firstChild() );
183
m_pushButtonDown->setEnabled( !m_isReadOnly && isElementSelected && m_listViewElements->selectedItem() != m_listViewElements->lastItem() );
184
m_listViewElements->setEnabled( !m_isReadOnly );
185
m_checkBoxEtAl->setEnabled( !m_isReadOnly );
189
m_pushButtonAdd->setEnabled( FALSE );
190
m_pushButtonEdit->setEnabled( FALSE );
191
m_pushButtonDelete->setEnabled( FALSE );
192
m_pushButtonUp->setEnabled( FALSE );
193
m_pushButtonDown->setEnabled( FALSE );
194
m_listViewElements->setEnabled( FALSE );
195
m_checkBoxEtAl->setEnabled( FALSE );
197
connect( m_checkBoxEtAl, SIGNAL( toggled( bool ) ), this, SLOT( apply() ) );
200
void FieldListView::slotAdd()
204
KListViewItem * item = new KListViewItem( m_listViewElements, m_listViewElements->lastItem(), QString( "%1%2" ).arg( m_prefixNew ).arg( ++m_newValueCounter ) );
205
m_listViewElements->setSelected( item, TRUE );
207
QTimer::singleShot( 100, this, SLOT( slotEdit() ) );
211
void FieldListView::slotEdit()
215
KListViewItem * item = static_cast<KListViewItem*>( m_listViewElements->selectedItem() );
217
m_listViewElements->rename( item, 0 );
221
void FieldListView::slotDelete()
223
QListViewItem * item = m_listViewElements->selectedItem();
224
if ( isSimple() && item != NULL )
234
void FieldListView::slotUp()
236
QListViewItem * item = m_listViewElements->selectedItem();
237
if ( isSimple() && !m_listViewElements->isRenaming() && item != NULL && item -> itemAbove() != NULL )
239
item->itemAbove() ->moveItem( item );
247
void FieldListView::slotDown()
249
QListViewItem * item = m_listViewElements->selectedItem();
250
if ( isSimple() && !m_listViewElements->isRenaming() && item != NULL && item -> itemBelow() != NULL )
252
item->moveItem( item->itemBelow() );
260
void FieldListView::slotComplex()
262
if ( !m_listViewElements->isRenaming() && ValueWidget::execute( m_caption, m_fieldType, m_value, m_isReadOnly, this ) == QDialog::Accepted )
271
void FieldListView::slotListViewDoubleClicked( QListViewItem * lvi )
277
void FieldListView::slotItemRenamed( QListViewItem * item, int /*col*/, const QString & text )
279
if ( text.isEmpty() && isSimple() && item != NULL )
289
void FieldListView::setupGUI()
291
Settings * settings = Settings::self();
293
QGridLayout * layout = new QGridLayout( this, 8, 2, 0, KDialog::spacingHint() );
294
layout->setRowStretch( 5, 1 );
296
m_listViewElements = new KListView( this );
297
layout->addMultiCellWidget( m_listViewElements, 0, 6, 0, 0 );
298
m_listViewElements->renameLineEdit() ->setCompletionObject( settings->completion( m_fieldType ) );
299
m_listViewElements->renameLineEdit() ->setCompletionMode( KGlobalSettings::CompletionPopup );
300
m_listViewElements->renameLineEdit() ->completionObject() ->setIgnoreCase( true );
301
m_listViewElements->setDefaultRenameAction( QListView::Accept );
302
m_listViewElements->addColumn( m_caption );
303
m_listViewElements->setSorting( -1, TRUE );
304
m_listViewElements->setItemsRenameable( TRUE );
305
if ( settings->editing_UseSpecialFont )
306
m_listViewElements->setFont( settings->editing_SpecialFont );
307
m_listViewElements->header() ->setFont( KGlobalSettings::generalFont() );
309
m_listViewElements->header() ->setClickEnabled( FALSE );
310
m_listViewElements->header() ->setStretchEnabled( TRUE, 0 );
311
connect( m_listViewElements, SIGNAL( selectionChanged() ), this, SLOT( updateGUI() ) );
312
connect( m_listViewElements, SIGNAL( clicked( QListViewItem * ) ), this, SLOT( updateGUI() ) );
313
connect( m_listViewElements, SIGNAL( doubleClicked( QListViewItem * ) ), this, SLOT( slotListViewDoubleClicked( QListViewItem * ) ) );
314
connect( m_listViewElements, SIGNAL( currentChanged( QListViewItem * ) ), this, SLOT( updateGUI() ) );
315
connect( m_listViewElements, SIGNAL( itemRenamed( QListViewItem*, int, const QString& ) ), this, SLOT( slotItemRenamed( QListViewItem*, int, const QString& ) ) );
317
m_pushButtonAdd = new QPushButton( i18n( "Add" ), this );
318
layout->addWidget( m_pushButtonAdd, 0, 1 );
319
m_pushButtonAdd->setIconSet( QIconSet( SmallIcon( "add" ) ) );
320
connect( m_pushButtonAdd, SIGNAL( clicked() ), this, SLOT( slotAdd() ) );
321
QToolTip::add( m_pushButtonAdd, QString( i18n( "Add new '%1' item (Ctrl+A)" ) ).arg( m_caption ) );
323
m_pushButtonEdit = new QPushButton( i18n( "Edit" ), this );
324
layout->addWidget( m_pushButtonEdit, 1, 1 );
325
m_pushButtonEdit->setIconSet( QIconSet( SmallIcon( "edit" ) ) );
326
connect( m_pushButtonEdit, SIGNAL( clicked() ), this, SLOT( slotEdit() ) );
327
QToolTip::add( m_pushButtonEdit, QString( i18n( "Edit current '%1' item (F2)" ) ).arg( m_caption ) );
329
m_pushButtonDelete = new QPushButton( i18n( "Delete" ), this );
330
layout->addWidget( m_pushButtonDelete, 2, 1 );
331
m_pushButtonDelete->setIconSet( QIconSet( SmallIcon( "editdelete" ) ) );
332
connect( m_pushButtonDelete, SIGNAL( clicked() ), this, SLOT( slotDelete() ) );
333
QToolTip::add( m_pushButtonDelete, QString( i18n( "Delete current '%1' item (Del)" ) ).arg( m_caption ) );
335
m_pushButtonUp = new QPushButton( i18n( "Up" ), this );
336
layout->addWidget( m_pushButtonUp, 3, 1 );
337
m_pushButtonUp->setIconSet( QIconSet( SmallIcon( "up" ) ) );
338
connect( m_pushButtonUp, SIGNAL( clicked() ), this, SLOT( slotUp() ) );
339
QToolTip::add( m_pushButtonUp, QString( i18n( "Move current '%1' item up (Ctrl+Up)" ) ).arg( m_caption ) );
341
m_pushButtonDown = new QPushButton( i18n( "Down" ), this );
342
layout->addWidget( m_pushButtonDown, 4, 1 );
343
m_pushButtonDown->setIconSet( QIconSet( SmallIcon( "down" ) ) );
344
connect( m_pushButtonDown, SIGNAL( clicked() ), this, SLOT( slotDown() ) );
345
QToolTip::add( m_pushButtonDown, QString( i18n( "Move current '%1' item down (Ctrl+Down)" ) ).arg( m_caption ) );
347
m_pushButtonComplexEdit = new QPushButton( i18n( "Complex..." ), this );
348
layout->addWidget( m_pushButtonComplexEdit, 6, 1 );
349
m_pushButtonComplexEdit->setIconSet( QIconSet( SmallIcon( "leftjust" ) ) );
350
connect( m_pushButtonComplexEdit, SIGNAL( clicked() ), this, SLOT( slotComplex() ) );
351
QToolTip::add( m_pushButtonComplexEdit, QString( i18n( "Edit current '%1' item as a concatenated string (Ctrl+Alt+C)" ) ).arg( m_caption ) );
353
m_checkBoxEtAl = new QCheckBox( i18n( "... and others (et al.)" ), this );
354
layout->addMultiCellWidget( m_checkBoxEtAl, 7, 7, 0, 1 );
355
connect( m_checkBoxEtAl, SIGNAL( toggled( bool ) ), this, SLOT( apply() ) );
358
void FieldListView::apply()
360
QStringList elements;
361
Settings * settings = Settings::self();
363
m_value->items.clear();
365
for ( QListViewItemIterator it( m_listViewElements ); it.current(); it++ )
367
QString text = it.current() ->text( 0 );
368
elements.append( text );
371
if ( elements.count() > 0 )
373
m_value->items.clear();
374
BibTeX::PersonContainer *container = new BibTeX::PersonContainer( settings->editing_FirstNameFirst );
376
switch ( m_fieldType )
378
case BibTeX::EntryField::ftAuthor:
379
case BibTeX::EntryField::ftEditor:
381
for ( QStringList::ConstIterator it = elements.constBegin(); it != elements.constEnd(); ++it )
383
BibTeX::Person *person = new BibTeX::Person( *it, settings->editing_FirstNameFirst );
384
container->persons.append( person );
389
kdDebug() << "Don't know how to handle entries of type " << BibTeX::EntryField::fieldTypeToString( m_fieldType ) << endl;
392
if ( m_checkBoxEtAl->isChecked() )
393
container->persons.append( new BibTeX::Person( "others", settings->editing_FirstNameFirst ) );
395
if ( !container->persons.isEmpty() )
396
m_value->items.append( container );
400
settings->addToCompletion( m_value, m_fieldType );
404
void FieldListView::reset()
406
disconnect( m_checkBoxEtAl, SIGNAL( toggled( bool ) ), this, SLOT( apply() ) );
407
m_listViewElements->clear();
408
m_checkBoxEtAl->setChecked( FALSE );
411
for ( QValueList<BibTeX::ValueItem*>::ConstIterator it = m_value->items.constBegin(); !m_isComplex && it != m_value->items.constEnd(); ++it )
413
BibTeX::MacroKey *macroKey = dynamic_cast<BibTeX::MacroKey *>( *it );
414
m_isComplex = macroKey != NULL;
418
switch ( m_fieldType )
420
case BibTeX::EntryField::ftAuthor:
421
case BibTeX::EntryField::ftEditor:
423
for ( QValueList<BibTeX::ValueItem*>::ConstIterator it = m_value->items.constBegin(); it != m_value->items.constEnd(); ++it )
425
BibTeX::PersonContainer *container = dynamic_cast<BibTeX::PersonContainer *>( *it );
426
if ( container != NULL )
427
for ( QValueList<BibTeX::Person*>::ConstIterator pit = container->persons.constBegin(); pit != container->persons.constEnd(); ++pit )
429
QString text = ( *pit )->text();
430
if ( text == "others" )
431
m_checkBoxEtAl->setChecked( TRUE );
433
new QListViewItem( m_listViewElements, m_listViewElements->lastItem(), text );
439
kdDebug() << "Don't know how to handle entries of type " << BibTeX::EntryField::fieldTypeToString( m_fieldType ) << endl;
442
connect( m_checkBoxEtAl, SIGNAL( toggled( bool ) ), this, SLOT( apply() ) );
445
bool FieldListView::isSimple()
447
return m_value->items.count() == 0 || ( m_value->items.count() == 1 && dynamic_cast<BibTeX::MacroKey*>( m_value->items.first() ) == NULL );
450
void FieldListView::itemRenameDone()
455
#include "fieldlistview.moc"