~ubuntu-branches/ubuntu/wily/kbibtex/wily

« back to all changes in this revision

Viewing changes to src/fieldlistview.cpp

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2011-07-18 09:29:48 UTC
  • mfrom: (1.1.6) (2.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20110718092948-ksxjmg7kdfamolmg
Tags: 0.3-1
* First upstream release for KDE4 (Closes: #634255). A number of search
  engines are still missing, in comparison to the 0.2 series.
* Bumped Standards-Version to 3.9.2, no changes necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
*   Copyright (C) 2004-2009 by Thomas Fischer                             *
3
 
*   fischer@unix-ag.uni-kl.de                                             *
4
 
*                                                                         *
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.                                   *
9
 
*                                                                         *
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.                          *
14
 
*                                                                         *
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
 
***************************************************************************/
20
 
#include <qstring.h>
21
 
#include <qregexp.h>
22
 
#include <qlayout.h>
23
 
#include <qheader.h>
24
 
#include <qtimer.h>
25
 
#include <qtooltip.h>
26
 
#include <qwhatsthis.h>
27
 
#include <qpushbutton.h>
28
 
#include <qcheckbox.h>
29
 
 
30
 
#include <klistview.h>
31
 
#include <klineedit.h>
32
 
#include <kdialog.h>
33
 
#include <klocale.h>
34
 
#include <kdebug.h>
35
 
#include <kiconloader.h>
36
 
 
37
 
#include <value.h>
38
 
#include <valuewidget.h>
39
 
#include <settings.h>
40
 
#include "fieldlistview.h"
41
 
 
42
 
namespace KBibTeX
43
 
{
44
 
 
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 )
47
 
    {
48
 
        setupGUI();
49
 
        m_listViewElements->installEventFilter( this );
50
 
        m_listViewElements->renameLineEdit() ->installEventFilter( this );
51
 
 
52
 
        m_value = new BibTeX::Value();
53
 
    }
54
 
 
55
 
 
56
 
    FieldListView::~FieldListView()
57
 
    {
58
 
        delete m_value;
59
 
    }
60
 
 
61
 
    void FieldListView::setValue( const BibTeX::Value *value )
62
 
    {
63
 
        if ( value != m_value )
64
 
        {
65
 
            if ( m_value != NULL )
66
 
                delete m_value;
67
 
 
68
 
            if ( value != NULL )
69
 
                m_value = new BibTeX::Value( value );
70
 
            else
71
 
                m_value = new BibTeX::Value( );
72
 
 
73
 
            reset();
74
 
            updateGUI();
75
 
 
76
 
            m_isModified = FALSE;
77
 
        }
78
 
    }
79
 
 
80
 
    BibTeX::Value *FieldListView::value()
81
 
    {
82
 
        if ( m_value->items.isEmpty() )
83
 
            return NULL;
84
 
        else
85
 
            return new BibTeX::Value( m_value );
86
 
    }
87
 
 
88
 
    void FieldListView::setEnabled( bool enabled )
89
 
    {
90
 
        m_enabled = enabled;
91
 
        updateGUI();
92
 
    }
93
 
 
94
 
    void FieldListView::setFieldType( BibTeX::EntryField::FieldType fieldType )
95
 
    {
96
 
        m_fieldType = fieldType;
97
 
 
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 ) ) );
102
 
 
103
 
        m_value->items.clear();
104
 
    }
105
 
 
106
 
    QString FieldListView::caption()
107
 
    {
108
 
        return m_caption;
109
 
    }
110
 
 
111
 
    bool FieldListView::isEmpty()
112
 
    {
113
 
        return m_value != NULL ? m_value->items.isEmpty() : TRUE;
114
 
    }
115
 
 
116
 
    bool FieldListView::isModified()
117
 
    {
118
 
        return m_isModified;
119
 
    }
120
 
 
121
 
    bool FieldListView::eventFilter( QObject *o, QEvent * e )
122
 
    {
123
 
        if ( o == m_listViewElements->renameLineEdit() )
124
 
        {
125
 
            if ( e->type() == QEvent::Hide )
126
 
                itemRenameDone();
127
 
        }
128
 
        else if ( e->type() == QEvent::AccelOverride )
129
 
        {
130
 
            QKeyEvent * ke = static_cast<QKeyEvent*>( e );
131
 
            //override delete action
132
 
            if ( ke->key() == Key_Delete && ke->state() == NoButton )
133
 
            {
134
 
                slotDelete();
135
 
                ke->accept();
136
 
                return true;
137
 
            }
138
 
            else if ( ke->key() == Key_F2 && ke->state() == NoButton )
139
 
            {
140
 
                slotEdit();
141
 
                ke->accept();
142
 
                return true;
143
 
            }
144
 
            else if ( ke->key() == Key_A && ke->state() == ControlButton )
145
 
            {
146
 
                slotAdd();
147
 
                ke->accept();
148
 
                return true;
149
 
            }
150
 
            else if ( ke->key() == Key_Up && ke->state() == ControlButton )
151
 
            {
152
 
                slotUp();
153
 
                ke->accept();
154
 
                return true;
155
 
            }
156
 
            else if ( ke->key() == Key_Down && ke->state() == ControlButton )
157
 
            {
158
 
                slotDown();
159
 
                ke->accept();
160
 
                return true;
161
 
            }
162
 
            else if ( ke->key() == Key_C && ke->state() == ( ControlButton | AltButton ) )
163
 
            {
164
 
                slotComplex();
165
 
                ke->accept();
166
 
                return true;
167
 
            }
168
 
        }
169
 
        return false;
170
 
    }
171
 
 
172
 
 
173
 
    void FieldListView::updateGUI()
174
 
    {
175
 
        disconnect( m_checkBoxEtAl, SIGNAL( toggled( bool ) ), this, SLOT( apply() ) );
176
 
        if ( m_value != NULL && !m_isComplex )
177
 
        {
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 );
186
 
        }
187
 
        else
188
 
        {
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 );
196
 
        }
197
 
        connect( m_checkBoxEtAl, SIGNAL( toggled( bool ) ), this, SLOT( apply() ) );
198
 
    }
199
 
 
200
 
    void FieldListView::slotAdd()
201
 
    {
202
 
        if ( isSimple() )
203
 
        {
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 );
206
 
            updateGUI();
207
 
            QTimer::singleShot( 100, this, SLOT( slotEdit() ) );
208
 
        }
209
 
    }
210
 
 
211
 
    void FieldListView::slotEdit()
212
 
    {
213
 
        if ( isSimple() )
214
 
        {
215
 
            KListViewItem * item = static_cast<KListViewItem*>( m_listViewElements->selectedItem() );
216
 
            if ( item != NULL )
217
 
                m_listViewElements->rename( item, 0 );
218
 
        }
219
 
    }
220
 
 
221
 
    void FieldListView::slotDelete()
222
 
    {
223
 
        QListViewItem * item = m_listViewElements->selectedItem();
224
 
        if ( isSimple() && item != NULL )
225
 
        {
226
 
            delete item;
227
 
            apply();
228
 
            updateGUI();
229
 
 
230
 
            m_isModified = TRUE;
231
 
        }
232
 
    }
233
 
 
234
 
    void FieldListView::slotUp()
235
 
    {
236
 
        QListViewItem * item = m_listViewElements->selectedItem();
237
 
        if ( isSimple() && !m_listViewElements->isRenaming() && item != NULL && item -> itemAbove() != NULL )
238
 
        {
239
 
            item->itemAbove() ->moveItem( item );
240
 
            apply();
241
 
            updateGUI();
242
 
 
243
 
            m_isModified = TRUE;
244
 
        }
245
 
    }
246
 
 
247
 
    void FieldListView::slotDown()
248
 
    {
249
 
        QListViewItem * item = m_listViewElements->selectedItem();
250
 
        if ( isSimple() && !m_listViewElements->isRenaming() && item != NULL && item -> itemBelow() != NULL )
251
 
        {
252
 
            item->moveItem( item->itemBelow() );
253
 
            apply();
254
 
            updateGUI();
255
 
 
256
 
            m_isModified = TRUE;
257
 
        }
258
 
    }
259
 
 
260
 
    void FieldListView::slotComplex()
261
 
    {
262
 
        if ( !m_listViewElements->isRenaming() && ValueWidget::execute( m_caption, m_fieldType, m_value, m_isReadOnly, this ) == QDialog::Accepted )
263
 
        {
264
 
            reset();
265
 
            updateGUI();
266
 
 
267
 
            m_isModified = TRUE;
268
 
        }
269
 
    }
270
 
 
271
 
    void FieldListView::slotListViewDoubleClicked( QListViewItem * lvi )
272
 
    {
273
 
        if ( lvi == NULL )
274
 
            slotAdd();
275
 
    }
276
 
 
277
 
    void FieldListView::slotItemRenamed( QListViewItem * item, int /*col*/, const QString & text )
278
 
    {
279
 
        if ( text.isEmpty() && isSimple() && item != NULL )
280
 
        {
281
 
            delete item;
282
 
            updateGUI();
283
 
        }
284
 
 
285
 
        apply();
286
 
        m_isModified = TRUE;
287
 
    }
288
 
 
289
 
    void FieldListView::setupGUI()
290
 
    {
291
 
        Settings * settings = Settings::self();
292
 
 
293
 
        QGridLayout * layout = new QGridLayout( this, 8, 2, 0, KDialog::spacingHint() );
294
 
        layout->setRowStretch( 5, 1 );
295
 
 
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() );
308
 
 
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& ) ) );
316
 
 
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 ) );
322
 
 
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 ) );
328
 
 
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 ) );
334
 
 
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 ) );
340
 
 
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 ) );
346
 
 
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 ) );
352
 
 
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() ) );
356
 
    }
357
 
 
358
 
    void FieldListView::apply()
359
 
    {
360
 
        QStringList elements;
361
 
        Settings * settings = Settings::self();
362
 
 
363
 
        m_value->items.clear();
364
 
 
365
 
        for ( QListViewItemIterator it( m_listViewElements ); it.current(); it++ )
366
 
        {
367
 
            QString text = it.current() ->text( 0 );
368
 
            elements.append( text );
369
 
        }
370
 
 
371
 
        if ( elements.count() > 0 )
372
 
        {
373
 
            m_value->items.clear();
374
 
            BibTeX::PersonContainer *container = new BibTeX::PersonContainer( settings->editing_FirstNameFirst );
375
 
 
376
 
            switch ( m_fieldType )
377
 
            {
378
 
            case BibTeX::EntryField::ftAuthor:
379
 
            case BibTeX::EntryField::ftEditor:
380
 
                {
381
 
                    for ( QStringList::ConstIterator it = elements.constBegin(); it != elements.constEnd(); ++it )
382
 
                    {
383
 
                        BibTeX::Person *person = new BibTeX::Person( *it, settings->editing_FirstNameFirst );
384
 
                        container->persons.append( person );
385
 
                    }
386
 
                }
387
 
                break;
388
 
            default:
389
 
                kdDebug() << "Don't know how to handle entries of type " << BibTeX::EntryField::fieldTypeToString( m_fieldType ) << endl;
390
 
            }
391
 
 
392
 
            if ( m_checkBoxEtAl->isChecked() )
393
 
                container->persons.append( new BibTeX::Person( "others", settings->editing_FirstNameFirst ) );
394
 
 
395
 
            if ( !container->persons.isEmpty() )
396
 
                m_value->items.append( container );
397
 
            else
398
 
                delete container;
399
 
 
400
 
            settings->addToCompletion( m_value, m_fieldType );
401
 
        }
402
 
    }
403
 
 
404
 
    void FieldListView::reset()
405
 
    {
406
 
        disconnect( m_checkBoxEtAl, SIGNAL( toggled( bool ) ), this, SLOT( apply() ) );
407
 
        m_listViewElements->clear();
408
 
        m_checkBoxEtAl->setChecked( FALSE );
409
 
 
410
 
        m_isComplex = FALSE;
411
 
        for ( QValueList<BibTeX::ValueItem*>::ConstIterator it = m_value->items.constBegin(); !m_isComplex && it != m_value->items.constEnd(); ++it )
412
 
        {
413
 
            BibTeX::MacroKey *macroKey = dynamic_cast<BibTeX::MacroKey *>( *it );
414
 
            m_isComplex = macroKey != NULL;
415
 
        }
416
 
 
417
 
        if ( !m_isComplex )
418
 
            switch ( m_fieldType )
419
 
            {
420
 
            case BibTeX::EntryField::ftAuthor:
421
 
            case BibTeX::EntryField::ftEditor:
422
 
                {
423
 
                    for ( QValueList<BibTeX::ValueItem*>::ConstIterator it = m_value->items.constBegin(); it != m_value->items.constEnd(); ++it )
424
 
                    {
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 )
428
 
                            {
429
 
                                QString text = ( *pit )->text();
430
 
                                if ( text == "others" )
431
 
                                    m_checkBoxEtAl->setChecked( TRUE );
432
 
                                else
433
 
                                    new QListViewItem( m_listViewElements, m_listViewElements->lastItem(), text );
434
 
                            }
435
 
                    }
436
 
                }
437
 
                break;
438
 
            default:
439
 
                kdDebug() << "Don't know how to handle entries of type " << BibTeX::EntryField::fieldTypeToString( m_fieldType ) << endl;
440
 
            }
441
 
 
442
 
        connect( m_checkBoxEtAl, SIGNAL( toggled( bool ) ), this, SLOT( apply() ) );
443
 
    }
444
 
 
445
 
    bool FieldListView::isSimple()
446
 
    {
447
 
        return m_value->items.count() == 0 || ( m_value->items.count() == 1 && dynamic_cast<BibTeX::MacroKey*>( m_value->items.first() ) == NULL );
448
 
    }
449
 
 
450
 
    void FieldListView::itemRenameDone()
451
 
    {
452
 
        apply();
453
 
    }
454
 
}
455
 
#include "fieldlistview.moc"