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

« back to all changes in this revision

Viewing changes to src/idsuggestionswidget.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 <qlayout.h>
21
 
#include <qspinbox.h>
22
 
#include <qlabel.h>
23
 
#include <qcheckbox.h>
24
 
#include <qobjectlist.h>
25
 
#include <qscrollview.h>
26
 
 
27
 
#include <kglobalsettings.h>
28
 
#include <klineedit.h>
29
 
#include <kpopupmenu.h>
30
 
#include <kcombobox.h>
31
 
#include <kpushbutton.h>
32
 
#include <kdialogbase.h>
33
 
#include <kiconloader.h>
34
 
#include <klocale.h>
35
 
 
36
 
#include <idsuggestions.h>
37
 
#include <fileimporter.h>
38
 
#include <fileimporterbibtex.h>
39
 
#include "idsuggestionswidget.h"
40
 
 
41
 
#define min(a,b)  ((a)>(b)?(b):(a))
42
 
#define max(a,b)  ((a)<(b)?(b):(a))
43
 
 
44
 
namespace KBibTeX
45
 
{
46
 
    QString IdSuggestionsWidget::exampleBibTeXEntry = "@Article{ dijkstra1983terminationdetect,\nauthor = {Edsger W. Dijkstra and W. H. J. Feijen and A. J. M. {van Gasteren}},\ntitle = {{Derivation of a Termination Detection Algorithm for Distributed Computations}},\njournal = {Information Processing Letters},\nvolume = 16,\nnumber = 5,\npages = {217--219},\nmonth = jun,\nyear = 1983\n}";
47
 
 
48
 
    IdSuggestionComponent::IdSuggestionComponent( const QString& title, QWidget *parent ): QFrame( parent ), m_toBeDeleted( false ), m_title( title ), m_parent( parent )
49
 
    {
50
 
        setFrameShape( QFrame::Panel );
51
 
        setFrameShadow( QFrame::Sunken );
52
 
        setLineWidth( 1 );
53
 
    }
54
 
 
55
 
    QWidget *IdSuggestionComponent::moveWidgets( QWidget *parent )
56
 
    {
57
 
        QWidget *container = new QWidget( parent );
58
 
        QVBoxLayout *layout = new QVBoxLayout( container, 0, KDialog::spacingHint() );
59
 
        m_pushButtonUp = new KPushButton( QIconSet( SmallIcon( "up" ) ), i18n( "Up" ), container );
60
 
        m_pushButtonDown = new KPushButton( QIconSet( SmallIcon( "down" ) ), i18n( "Down" ), container );
61
 
        m_pushButtonDel = new KPushButton( QIconSet( SmallIcon( "remove" ) ), i18n( "Delete" ), container );
62
 
        layout->addWidget( m_pushButtonUp );
63
 
        layout->addWidget( m_pushButtonDown );
64
 
        layout->addWidget( m_pushButtonDel );
65
 
        layout->addStretch( 10 );
66
 
        connect( m_pushButtonUp, SIGNAL( clicked() ), this, SLOT( slotUp() ) );
67
 
        connect( m_pushButtonDown, SIGNAL( clicked() ), this, SLOT( slotDown() ) );
68
 
        connect( m_pushButtonDel, SIGNAL( clicked() ), this, SLOT( slotDelete() ) );
69
 
        return container;
70
 
    }
71
 
 
72
 
    void IdSuggestionComponent::setEnableUpDown( bool enableUp, bool enableDown )
73
 
    {
74
 
        m_pushButtonUp->setEnabled( enableUp );
75
 
        m_pushButtonDown->setEnabled( enableDown );
76
 
    }
77
 
 
78
 
    void IdSuggestionComponent::slotUp()
79
 
    {
80
 
        QVBoxLayout *layout = dynamic_cast<QVBoxLayout*>( m_parent->layout() );
81
 
        if ( layout == NULL ) return;
82
 
        int oldPos = layout->findWidget( this );
83
 
        if ( oldPos > 0 )
84
 
        {
85
 
            layout->remove( this );
86
 
            layout->insertWidget( oldPos - 1, this );
87
 
            emit moved();
88
 
        }
89
 
    }
90
 
 
91
 
    void IdSuggestionComponent::slotDown()
92
 
    {
93
 
        QVBoxLayout *layout = dynamic_cast<QVBoxLayout*>( m_parent->layout() );
94
 
        if ( layout == NULL ) return;
95
 
        int oldPos = layout->findWidget( this );
96
 
        int componentCount = dynamic_cast<IdSuggestionsWidget*>( m_parent->parent()->parent()->parent() )->numComponents();
97
 
        if ( oldPos < componentCount - 1 )
98
 
        {
99
 
            layout->remove( this );
100
 
            layout->insertWidget( oldPos + 1, this );
101
 
            emit moved();
102
 
        }
103
 
    }
104
 
 
105
 
    void IdSuggestionComponent::slotDelete()
106
 
    {
107
 
        m_toBeDeleted = true;
108
 
        emit deleted();
109
 
        delete this;
110
 
    }
111
 
 
112
 
    IdSuggestionComponentAuthor::IdSuggestionComponentAuthor( const QString &text, QWidget *parent ): IdSuggestionComponent( i18n( "Author" ), parent )
113
 
    {
114
 
        QGridLayout *layout = new QGridLayout( this, 6, 4, KDialog::marginHint(), KDialog::spacingHint() );
115
 
 
116
 
        QLabel *label = new QLabel( m_title, this );
117
 
        QFont labelFont( label->font() );
118
 
        labelFont.setBold( TRUE );
119
 
        label->setFont( labelFont );
120
 
        label->setBackgroundColor( KGlobalSettings::highlightColor() );
121
 
        label->setPaletteForegroundColor( KGlobalSettings::highlightedTextColor() );
122
 
        label->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
123
 
        layout->addMultiCellWidget( label, 0, 0, 0, 2 );
124
 
 
125
 
        m_comboBoxWhichAuthors = new KComboBox( false, this );
126
 
        layout->addMultiCellWidget( m_comboBoxWhichAuthors, 1, 1, 0, 1 );
127
 
        m_comboBoxWhichAuthors->insertItem( i18n( "All authors" ) );
128
 
        m_comboBoxWhichAuthors->insertItem( i18n( "First author only" ) );
129
 
        m_comboBoxWhichAuthors->insertItem( i18n( "All but first author" ) );
130
 
        switch ( text[0] )
131
 
        {
132
 
        case 'a': m_comboBoxWhichAuthors->setCurrentItem( 1 ); break;
133
 
        case 'z': m_comboBoxWhichAuthors->setCurrentItem( 2 ); break;
134
 
        default: m_comboBoxWhichAuthors->setCurrentItem( 0 );
135
 
        }
136
 
        connect( m_comboBoxWhichAuthors, SIGNAL( activated( const QString& ) ), SIGNAL( modified() ) );
137
 
 
138
 
        struct IdSuggestionTokenInfo info = IdSuggestions::evalToken( text.mid( 1 ) );
139
 
 
140
 
        label = new QLabel( i18n( "Casing:" ), this );
141
 
        layout->addWidget( label, 2, 0 );
142
 
        m_comboBoxCasing = new KComboBox( FALSE, this );
143
 
        label->setBuddy( m_comboBoxCasing );
144
 
        layout->addWidget( m_comboBoxCasing, 2, 1 );
145
 
        m_comboBoxCasing->insertItem( i18n( "No change" ) );
146
 
        m_comboBoxCasing->insertItem( i18n( "Lower case" ) );
147
 
        m_comboBoxCasing->insertItem( i18n( "Upper case" ) );
148
 
        if ( info.toLower )
149
 
            m_comboBoxCasing->setCurrentItem( 1 );
150
 
        else if ( info.toUpper )
151
 
            m_comboBoxCasing->setCurrentItem( 2 );
152
 
        else
153
 
            m_comboBoxCasing->setCurrentItem( 0 );
154
 
        connect( m_comboBoxCasing, SIGNAL( activated( const QString& ) ), SIGNAL( modified() ) );
155
 
 
156
 
        label = new QLabel( i18n( "Only first letters:" ), this );
157
 
        layout->addWidget( label, 3, 0 );
158
 
        m_spinBoxLen = new QSpinBox( this );
159
 
        label->setBuddy( m_spinBoxLen );
160
 
        layout->addWidget( m_spinBoxLen, 3, 1 );
161
 
        m_spinBoxLen->setMinValue( 0 );
162
 
        m_spinBoxLen->setMaxValue( 9 );
163
 
        m_spinBoxLen->setSpecialValueText( i18n( "Complete name" ) );
164
 
        m_spinBoxLen->setValue( info.len > 9 ? 0 : info.len );
165
 
        m_spinBoxLen->setMinimumWidth( m_spinBoxLen->fontMetrics().width( i18n( "Complete name" ) ) + 32 );
166
 
        connect( m_spinBoxLen, SIGNAL( valueChanged( int ) ), SIGNAL( modified() ) );
167
 
 
168
 
        label = new QLabel( i18n( "Text between authors:" ), this );
169
 
        layout->addWidget( label, 4, 0 );
170
 
        m_lineEditInBetween = new KLineEdit( this );
171
 
        label->setBuddy( m_lineEditInBetween );
172
 
        layout->addWidget( m_lineEditInBetween, 4, 1 );
173
 
        m_lineEditInBetween->setText( info.inBetween );
174
 
        connect( m_lineEditInBetween, SIGNAL( textChanged( const QString& ) ), SIGNAL( modified() ) );
175
 
 
176
 
        layout->setRowStretch( 5, 1 );
177
 
        layout->setColStretch( 1, 1 );
178
 
        layout->setColSpacing( 2, KDialog::spacingHint() * 3 );
179
 
        layout->addMultiCellWidget( moveWidgets( this ), 0, 5, 3, 3 );
180
 
    }
181
 
 
182
 
    QString IdSuggestionComponentAuthor::text() const
183
 
    {
184
 
        if ( m_toBeDeleted ) return QString::null;
185
 
 
186
 
        QString result;
187
 
        switch ( m_comboBoxWhichAuthors->currentItem() )
188
 
        {
189
 
        case 1: result = "a"; break;
190
 
        case 2: result = "z"; break;
191
 
        default: result = "A";
192
 
        }
193
 
        if ( m_spinBoxLen->value() > 0 && m_spinBoxLen->value() <= 9 ) result.append( QString::number( m_spinBoxLen->value() ) );
194
 
        if ( m_comboBoxCasing->currentItem() == 1 ) result.append( "l" );
195
 
        else if ( m_comboBoxCasing->currentItem() == 2 ) result.append( "u" );
196
 
        if ( !m_lineEditInBetween->text().isEmpty() ) result.append( '"' ).append( m_lineEditInBetween->text() );
197
 
 
198
 
        return result;
199
 
    }
200
 
 
201
 
    IdSuggestionComponentTitle::IdSuggestionComponentTitle( const QString &text, QWidget *parent ): IdSuggestionComponent( i18n( "Title" ), parent )
202
 
    {
203
 
        QGridLayout *layout = new QGridLayout( this, 6, 4, KDialog::marginHint(), KDialog::spacingHint() );
204
 
 
205
 
        QLabel *label = new QLabel( m_title, this );
206
 
        QFont labelFont( label->font() );
207
 
        labelFont.setBold( TRUE );
208
 
        label->setFont( labelFont );
209
 
        label->setBackgroundColor( KGlobalSettings::highlightColor() );
210
 
        label->setPaletteForegroundColor( KGlobalSettings::highlightedTextColor() );
211
 
        label->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
212
 
        layout->addMultiCellWidget( label, 0, 0, 0, 2 );
213
 
 
214
 
        m_checkBoxRemoveSmallWords = new QCheckBox( i18n( "Remove small words" ), this );
215
 
        layout->addMultiCellWidget( m_checkBoxRemoveSmallWords, 1, 1, 0, 1 );
216
 
        m_checkBoxRemoveSmallWords->setChecked( text[0] == 'T' );
217
 
        connect( m_checkBoxRemoveSmallWords, SIGNAL( toggled( bool ) ), SIGNAL( modified() ) );
218
 
 
219
 
        struct IdSuggestionTokenInfo info = IdSuggestions::evalToken( text.mid( 1 ) );
220
 
 
221
 
        label = new QLabel( i18n( "Casing:" ), this );
222
 
        layout->addWidget( label, 2, 0 );
223
 
        m_comboBoxCasing = new KComboBox( FALSE, this );
224
 
        label->setBuddy( m_comboBoxCasing );
225
 
        layout->addWidget( m_comboBoxCasing, 2, 1 );
226
 
        m_comboBoxCasing->insertItem( i18n( "No change" ) );
227
 
        m_comboBoxCasing->insertItem( i18n( "Lower case" ) );
228
 
        m_comboBoxCasing->insertItem( i18n( "Upper case" ) );
229
 
        if ( info.toLower )
230
 
            m_comboBoxCasing->setCurrentItem( 1 );
231
 
        else if ( info.toUpper )
232
 
            m_comboBoxCasing->setCurrentItem( 2 );
233
 
        else
234
 
            m_comboBoxCasing->setCurrentItem( 0 );
235
 
        connect( m_comboBoxCasing, SIGNAL( textChanged( const QString& ) ), SIGNAL( modified() ) );
236
 
 
237
 
        label = new QLabel( i18n( "Only first letters:" ), this );
238
 
        layout->addWidget( label, 3, 0 );
239
 
        m_spinBoxLen = new QSpinBox( this );
240
 
        label->setBuddy( m_spinBoxLen );
241
 
        layout->addWidget( m_spinBoxLen, 3, 1 );
242
 
        m_spinBoxLen->setMinValue( 0 );
243
 
        m_spinBoxLen->setMaxValue( 9 );
244
 
        m_spinBoxLen->setSpecialValueText( i18n( "Complete title" ) );
245
 
        m_spinBoxLen->setValue( info.len > 9 ? 0 : info.len );
246
 
        m_spinBoxLen->setMinimumWidth( m_spinBoxLen->fontMetrics().width( i18n( "Complete title" ) ) + 32 );
247
 
        connect( m_spinBoxLen, SIGNAL( valueChanged( int ) ), SIGNAL( modified() ) );
248
 
 
249
 
        label = new QLabel( i18n( "Text between words:" ), this );
250
 
        layout->addWidget( label, 4, 0 );
251
 
        m_lineEditInBetween = new KLineEdit( this );
252
 
        label->setBuddy( m_lineEditInBetween );
253
 
        layout->addWidget( m_lineEditInBetween, 4, 1 );
254
 
        m_lineEditInBetween->setText( info.inBetween );
255
 
        connect( m_lineEditInBetween, SIGNAL( textChanged( const QString& ) ), SIGNAL( modified() ) );
256
 
 
257
 
        layout->setRowStretch( 5, 1 );
258
 
        layout->setColStretch( 1, 1 );
259
 
        layout->setColSpacing( 2, KDialog::spacingHint() *2 );
260
 
        layout->addMultiCellWidget( moveWidgets( this ), 0, 5, 3, 3 );
261
 
    }
262
 
 
263
 
    QString IdSuggestionComponentTitle::text() const
264
 
    {
265
 
        if ( m_toBeDeleted ) return QString::null;
266
 
 
267
 
        QString result = m_checkBoxRemoveSmallWords->isChecked() ? "T" : "t";
268
 
        if ( m_spinBoxLen->value() > 0 && m_spinBoxLen->value() <= 9 ) result.append( QString::number( m_spinBoxLen->value() ) );
269
 
        if ( m_comboBoxCasing->currentItem() == 1 ) result.append( "l" );
270
 
        else if ( m_comboBoxCasing->currentItem() == 2 ) result.append( "u" );
271
 
        if ( !m_lineEditInBetween->text().isEmpty() ) result.append( '"' ).append( m_lineEditInBetween->text() );
272
 
 
273
 
        return result;
274
 
    }
275
 
 
276
 
    IdSuggestionComponentYear::IdSuggestionComponentYear( const QString &text, QWidget *parent ): IdSuggestionComponent( i18n( "Year" ), parent )
277
 
    {
278
 
        QGridLayout *layout = new QGridLayout( this, 3, 4, KDialog::marginHint(), KDialog::spacingHint() );
279
 
 
280
 
        QLabel *label = new QLabel( m_title, this );
281
 
        QFont labelFont( label->font() );
282
 
        labelFont.setBold( TRUE );
283
 
        label->setFont( labelFont );
284
 
        label->setBackgroundColor( KGlobalSettings::highlightColor() );
285
 
        label->setPaletteForegroundColor( KGlobalSettings::highlightedTextColor() );
286
 
        label->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
287
 
        layout->addMultiCellWidget( label, 0, 0, 0, 2 );
288
 
 
289
 
        label = new QLabel( i18n( "Year:" ), this );
290
 
        layout->addWidget( label, 1, 0 );
291
 
        m_comboBoxDigits = new KComboBox( this );
292
 
        label->setBuddy( m_comboBoxDigits );
293
 
        layout->addWidget( m_comboBoxDigits, 1, 1 );
294
 
        m_comboBoxDigits->insertItem( i18n( "2 digits" ) );
295
 
        m_comboBoxDigits->insertItem( i18n( "4 digits" ) );
296
 
        m_comboBoxDigits->setCurrentItem( text[0] == 'y' ? 0 : 1 );
297
 
        connect( m_comboBoxDigits, SIGNAL( activated( int ) ), SIGNAL( modified() ) );
298
 
 
299
 
        layout->setColStretch( 1, 1 );
300
 
        layout->setRowStretch( 2, 1 );
301
 
        layout->setColSpacing( 2, KDialog::spacingHint() *2 );
302
 
        layout->addMultiCellWidget( moveWidgets( this ), 0, 2, 3, 3 );
303
 
    }
304
 
 
305
 
    QString IdSuggestionComponentYear::text() const
306
 
    {
307
 
        if ( m_toBeDeleted ) return QString::null;
308
 
        return m_comboBoxDigits->currentItem() == 0 ? "y" : "Y";
309
 
    }
310
 
 
311
 
    IdSuggestionComponentText::IdSuggestionComponentText( const QString &text, QWidget *parent ): IdSuggestionComponent( i18n( "Text" ), parent )
312
 
    {
313
 
        QGridLayout *layout = new QGridLayout( this, 3, 4, KDialog::marginHint(), KDialog::spacingHint() );
314
 
 
315
 
        QLabel *label = new QLabel( m_title, this );
316
 
        QFont labelFont( label->font() );
317
 
        labelFont.setBold( TRUE );
318
 
        label->setFont( labelFont );
319
 
        label->setBackgroundColor( KGlobalSettings::highlightColor() );
320
 
        label->setPaletteForegroundColor( KGlobalSettings::highlightedTextColor() );
321
 
        label->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
322
 
        layout->addMultiCellWidget( label, 0, 0, 0, 2 );
323
 
 
324
 
        label = new QLabel( i18n( "Text in between:" ), this );
325
 
        layout->addWidget( label, 1, 0 );
326
 
        m_lineEditInBetween = new KLineEdit( this );
327
 
        label->setBuddy( m_lineEditInBetween );
328
 
        layout->addWidget( m_lineEditInBetween, 1, 1 );
329
 
        m_lineEditInBetween->setText( text.mid( 1 ) );
330
 
        connect( m_lineEditInBetween, SIGNAL( textChanged( const QString& ) ), SIGNAL( modified() ) );
331
 
 
332
 
        layout->setColStretch( 1, 1 );
333
 
        layout->setRowStretch( 2, 1 );
334
 
        layout->setColSpacing( 2, KDialog::spacingHint() * 2 );
335
 
        layout->addMultiCellWidget( moveWidgets( this ), 0, 2, 3, 3 );
336
 
    }
337
 
 
338
 
    QString IdSuggestionComponentText::text() const
339
 
    {
340
 
        if ( m_toBeDeleted ) return QString::null;
341
 
        return m_lineEditInBetween->text().isEmpty() ? QString::null : QString( "\"" ).append( m_lineEditInBetween->text() );
342
 
    }
343
 
 
344
 
    IdSuggestionsScrollView::IdSuggestionsScrollView( QWidget *parent, const char*name ): QScrollView( parent, name ), m_widget( NULL )
345
 
    {
346
 
        setMinimumHeight( 256 );
347
 
        setHScrollBarMode( QScrollView::AlwaysOff );
348
 
        setVScrollBarMode( QScrollView::AlwaysOn );
349
 
        setLineWidth( 0 );
350
 
    }
351
 
 
352
 
    IdSuggestionsScrollView::~IdSuggestionsScrollView()
353
 
    {
354
 
        // nothing
355
 
    }
356
 
 
357
 
    void IdSuggestionsScrollView::viewportResizeEvent( QResizeEvent * )
358
 
    {
359
 
        if ( m_widget != NULL )
360
 
            m_widget->setFixedWidth( viewport()->width() );
361
 
    }
362
 
 
363
 
    IdSuggestionsWidget::IdSuggestionsWidget( const QString &formatStr, KDialogBase *parent, const char *name )
364
 
            : QWidget( parent, name ), m_originalFormatStr( formatStr ), m_parent( parent )
365
 
    {
366
 
        BibTeX::FileImporter *importer = new BibTeX::FileImporterBibTeX( false );
367
 
        BibTeX::File *file = importer->load( exampleBibTeXEntry );
368
 
        m_example = new BibTeX::Entry( dynamic_cast<BibTeX::Entry*>( *( file->begin() ) ) );
369
 
        delete file;
370
 
        delete importer;
371
 
 
372
 
        setupGUI();
373
 
        reset( formatStr );
374
 
    }
375
 
 
376
 
    IdSuggestionsWidget::~IdSuggestionsWidget()
377
 
    {
378
 
        // nothing
379
 
    }
380
 
 
381
 
    int IdSuggestionsWidget::numComponents()
382
 
    {
383
 
        return m_componentCount;
384
 
    }
385
 
 
386
 
    QDialog::DialogCode IdSuggestionsWidget::execute( QString &formatStr, QWidget *parent, const char *name )
387
 
    {
388
 
        KDialogBase * dlg = new KDialogBase( parent, name, true, i18n( "Edit Id Suggestions" ), KDialogBase::Ok | KDialogBase::Cancel );
389
 
        IdSuggestionsWidget* ui = new IdSuggestionsWidget( formatStr, dlg, "IdSuggestionsWidget" );
390
 
        dlg->setMainWidget( ui );
391
 
 
392
 
        QDialog::DialogCode result = ( QDialog::DialogCode ) dlg->exec();
393
 
        if ( result == QDialog::Accepted )
394
 
            ui->apply( formatStr );
395
 
 
396
 
        delete( ui );
397
 
        delete( dlg );
398
 
 
399
 
        return result;
400
 
    }
401
 
 
402
 
    void IdSuggestionsWidget::reset( const QString& formatStr )
403
 
    {
404
 
        QLayoutIterator it = m_listOfComponents->layout()->iterator();
405
 
        QLayoutItem *child;
406
 
        while (( child = it.current() ) != 0 )
407
 
        {
408
 
            IdSuggestionComponent *component = dynamic_cast<IdSuggestionComponent*>( child->widget() );
409
 
            ++it;
410
 
            if ( component != NULL )
411
 
                delete component;
412
 
        }
413
 
 
414
 
        m_componentCount = 0;
415
 
        QStringList lines = QStringList::split( '|', formatStr );
416
 
        for ( QStringList::Iterator it = lines.begin(); it != lines.end();++it )
417
 
        {
418
 
            IdSuggestionComponent *component = NULL;
419
 
            if (( *it )[0] == 'a' || ( *it )[0] == 'A' )
420
 
                component = new IdSuggestionComponentAuthor( *it, m_listOfComponents );
421
 
            else if (( *it )[0] == 't' || ( *it )[0] == 'T' )
422
 
                component = new IdSuggestionComponentTitle( *it, m_listOfComponents );
423
 
            else if (( *it )[0] == 'y' || ( *it )[0] == 'Y' )
424
 
                component = new IdSuggestionComponentYear( *it, m_listOfComponents );
425
 
            else if (( *it )[0] == '"' )
426
 
                component = new IdSuggestionComponentText( *it, m_listOfComponents );
427
 
 
428
 
            if ( component != NULL )
429
 
            {
430
 
                ++m_componentCount;
431
 
                connect( component, SIGNAL( moved() ), this, SLOT( updateGUI() ) );
432
 
                connect( component, SIGNAL( deleted() ), this, SLOT( componentDeleted() ) );
433
 
                connect( component, SIGNAL( modified() ), this, SLOT( updateExample() ) );
434
 
            }
435
 
        }
436
 
 
437
 
        m_listOfComponents->adjustSize();
438
 
        m_scrollViewComponents->verticalScrollBar()->adjustSize();
439
 
        updateGUI();
440
 
        m_scrollViewComponents->resize( m_scrollViewComponents->width(), min( 384, m_listOfComponents->height() + 2 ) );
441
 
    }
442
 
 
443
 
    void IdSuggestionsWidget::apply( QString& formatStr )
444
 
    {
445
 
        bool first = TRUE;
446
 
        formatStr = "";
447
 
        QLayoutIterator it = m_listOfComponents->layout()->iterator();
448
 
        QLayoutItem *child;
449
 
        while (( child = it.current() ) != 0 )
450
 
        {
451
 
            IdSuggestionComponent *component = dynamic_cast<IdSuggestionComponent*>( child->widget() );
452
 
            QString text = QString::null;
453
 
            if ( component != NULL && ( text = component->text() ) != QString::null )
454
 
            {
455
 
                if ( first ) first = FALSE; else formatStr.append( "|" );
456
 
                formatStr.append( text );
457
 
            }
458
 
            ++it;
459
 
        }
460
 
    }
461
 
 
462
 
    void IdSuggestionsWidget::setupGUI()
463
 
    {
464
 
        QGridLayout *gridLayout = new QGridLayout( this, 3, 2, 0, KDialog::spacingHint() );
465
 
        gridLayout->setRowStretch( 2, 1 );
466
 
        gridLayout->setColStretch( 0, 1 );
467
 
 
468
 
        m_labelExample = new QLabel( this );
469
 
        gridLayout->addMultiCellWidget( m_labelExample, 0, 1, 0, 0 );
470
 
 
471
 
        m_pushButtonAdd = new KPushButton( i18n( "Add" ), this );
472
 
        gridLayout->addWidget( m_pushButtonAdd, 1, 1 );
473
 
 
474
 
        m_scrollViewComponents = new IdSuggestionsScrollView( this );
475
 
        m_listOfComponents = new QWidget( m_scrollViewComponents->viewport() );
476
 
        m_scrollViewComponents->setMainWidget( m_listOfComponents );
477
 
        m_scrollViewComponents->addChild( m_listOfComponents );
478
 
        gridLayout->addMultiCellWidget( m_scrollViewComponents, 2, 2, 0, 1 );
479
 
        QVBoxLayout *listLayout = new QVBoxLayout( m_listOfComponents, 0, KDialog::spacingHint() );
480
 
        listLayout->setAutoAdd( TRUE );
481
 
 
482
 
        KPopupMenu *addMenu = new KPopupMenu( m_pushButtonAdd );
483
 
        addMenu->insertItem( i18n( "Author" ), 1 );
484
 
        addMenu->insertItem( i18n( "Year" ), 2 );
485
 
        addMenu->insertItem( i18n( "Title" ), 3 );
486
 
        addMenu->insertItem( i18n( "Text" ), 4 );
487
 
        connect( addMenu, SIGNAL( activated( int ) ), this, SLOT( addMenuActivated( int ) ) );
488
 
        m_pushButtonAdd->setPopup( addMenu );
489
 
    }
490
 
 
491
 
    void IdSuggestionsWidget::addMenuActivated( int id )
492
 
    {
493
 
        IdSuggestionComponent *comp = NULL;
494
 
        if ( id == 1 )
495
 
            comp = new IdSuggestionComponentAuthor( "a", m_listOfComponents );
496
 
        else if ( id == 2 )
497
 
            comp = new IdSuggestionComponentYear( "y", m_listOfComponents );
498
 
        else if ( id == 3 )
499
 
            comp = new IdSuggestionComponentTitle( "t", m_listOfComponents );
500
 
        else if ( id == 4 )
501
 
            comp = new IdSuggestionComponentText( "", m_listOfComponents );
502
 
 
503
 
        if ( comp != NULL )
504
 
        {
505
 
            ++m_componentCount;
506
 
 
507
 
            comp->show();
508
 
 
509
 
            connect( comp, SIGNAL( moved() ), this, SLOT( updateGUI() ) );
510
 
            connect( comp, SIGNAL( deleted() ), this, SLOT( componentDeleted() ) );
511
 
            connect( comp, SIGNAL( modified() ), this, SLOT( updateExample() ) );
512
 
            m_listOfComponents->adjustSize();
513
 
            m_scrollViewComponents->ensureVisible( 10, m_listOfComponents->height() - 2 );
514
 
            updateGUI();
515
 
        }
516
 
    }
517
 
 
518
 
    void IdSuggestionsWidget::updateGUI()
519
 
    {
520
 
        QLayoutIterator it = m_listOfComponents->layout()->iterator();
521
 
        QLayoutItem *child = NULL;
522
 
        IdSuggestionComponent *lastComponent = NULL;
523
 
        bool first = TRUE;
524
 
        int i = 0;
525
 
        while (( child = it.current() ) != 0 )
526
 
        {
527
 
            IdSuggestionComponent *component = dynamic_cast<IdSuggestionComponent*>( child->widget() );
528
 
            ++it;
529
 
            if ( component != NULL )
530
 
            {
531
 
                if ( first )
532
 
                {
533
 
                    first = FALSE;
534
 
                    component->setEnableUpDown( FALSE, m_componentCount > 1 );
535
 
                }
536
 
                else
537
 
                    component->setEnableUpDown( TRUE, i < m_componentCount - 1 );
538
 
 
539
 
                ++i;
540
 
                lastComponent = component;
541
 
            }
542
 
        }
543
 
 
544
 
        if ( lastComponent != NULL )
545
 
            lastComponent->setEnableUpDown( m_componentCount > 1, FALSE );
546
 
 
547
 
        m_scrollViewComponents->updateGeometry();
548
 
        m_parent->enableButtonOK( m_componentCount > 0 );
549
 
        updateExample();
550
 
    }
551
 
 
552
 
    void IdSuggestionsWidget::componentDeleted()
553
 
    {
554
 
        --m_componentCount;
555
 
        updateGUI();
556
 
    }
557
 
 
558
 
    void IdSuggestionsWidget::updateExample()
559
 
    {
560
 
        QString formatStr;
561
 
        apply( formatStr );
562
 
        QString formatted = IdSuggestions::formatId( m_example, formatStr );
563
 
        m_labelExample->setText( QString( i18n( "<qt>Example:<br/><b>%1</b></qt>" ) ).arg( formatted ) );
564
 
    }
565
 
 
566
 
}
567
 
#include "idsuggestionswidget.moc"