~ubuntu-branches/ubuntu/precise/kbibtex/precise

« back to all changes in this revision

Viewing changes to src/settingsidsuggestions.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 <qcheckbox.h>
22
 
#include <qtimer.h>
23
 
#include <qvalidator.h>
24
 
#include <qheader.h>
25
 
#include <qlabel.h>
26
 
 
27
 
#include <kinputdialog.h>
28
 
#include <klistview.h>
29
 
#include <kiconloader.h>
30
 
#include <kpushbutton.h>
31
 
#include <klocale.h>
32
 
#include <kdialog.h>
33
 
#include <klineedit.h>
34
 
 
35
 
#include <idsuggestions.h>
36
 
#include <idsuggestionswidget.h>
37
 
#include <settings.h>
38
 
#include <fileimporter.h>
39
 
#include <fileimporterbibtex.h>
40
 
#include "settingsidsuggestions.h"
41
 
 
42
 
namespace KBibTeX
43
 
{
44
 
    QString SettingsIdSuggestions::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}";
45
 
 
46
 
    IdSuggestionsListViewItem::IdSuggestionsListViewItem( KListView *list, const QString& plainText, BibTeX::Entry *example ): KListViewItem( list ), m_original( plainText ), m_example( example )
47
 
    {
48
 
        KListViewItem::setText( 0, parse( plainText ) );
49
 
        KListViewItem::setMultiLinesEnabled( TRUE );
50
 
    }
51
 
 
52
 
    IdSuggestionsListViewItem::IdSuggestionsListViewItem( KListView *list, KListViewItem *prev, const QString& plainText, BibTeX::Entry *example ): KListViewItem( list, prev ), m_original( plainText ), m_example( example )
53
 
    {
54
 
        KListViewItem::setText( 0, parse( plainText ) );
55
 
        KListViewItem::setMultiLinesEnabled( TRUE );
56
 
    }
57
 
 
58
 
    void IdSuggestionsListViewItem::setText( int col, const QString& text )
59
 
    {
60
 
        if ( col == 0 )
61
 
            KListViewItem::setText( 0, parse( text ) );
62
 
        else
63
 
            KListViewItem::setText( col, text );
64
 
    }
65
 
 
66
 
    int IdSuggestionsListViewItem::width( const QFontMetrics & fm, const QListView *, int c ) const
67
 
    {
68
 
        int max = 100;
69
 
        QStringList lines = QStringList::split( '\n', text( c ) );
70
 
        for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
71
 
        {
72
 
            int w = fm.width( *it );
73
 
            if ( w > max ) max = w;
74
 
        }
75
 
 
76
 
        return max + 36;
77
 
    }
78
 
 
79
 
    QString IdSuggestionsListViewItem::originalText() const
80
 
    {
81
 
        return m_original;
82
 
    }
83
 
 
84
 
    QString IdSuggestionsListViewItem::parse( const QString&plainText )
85
 
    {
86
 
        m_original = plainText;
87
 
        QString result = IdSuggestions::formatStrToHuman( plainText );
88
 
        if ( m_example != NULL )
89
 
        {
90
 
            result.append( QString( i18n( "\nExample: %1" ) ).arg( IdSuggestions::formatId( m_example, plainText ) ) );
91
 
        }
92
 
        return result;
93
 
    }
94
 
 
95
 
    SettingsIdSuggestions::SettingsIdSuggestions( QWidget *parent, const char *name )
96
 
            : QWidget( parent, name )
97
 
    {
98
 
        m_validator = new QRegExpValidator( QRegExp( "[^\\s]+" ), this );
99
 
        setupGUI();
100
 
 
101
 
        BibTeX::FileImporter *importer = new BibTeX::FileImporterBibTeX( false );
102
 
        BibTeX::File *file = importer->load( exampleBibTeXEntry );
103
 
        m_example = new BibTeX::Entry( dynamic_cast<BibTeX::Entry*>( *( file->begin() ) ) );
104
 
        delete file;
105
 
        delete importer;
106
 
    }
107
 
 
108
 
    SettingsIdSuggestions::~SettingsIdSuggestions()
109
 
    {
110
 
        delete m_example;
111
 
        delete m_validator;
112
 
    }
113
 
 
114
 
    void SettingsIdSuggestions::applyData()
115
 
    {
116
 
        Settings * settings = Settings::self();
117
 
 
118
 
        settings->idSuggestions_formatStrList.clear();
119
 
        settings->idSuggestions_default = -1;
120
 
        settings->idSuggestions_forceDefault = m_checkBoxForceDefault->isChecked();
121
 
        int i = 0;
122
 
        for ( QListViewItemIterator it( m_listIdSuggestions ); it.current(); ++i, ++it )
123
 
        {
124
 
            IdSuggestionsListViewItem *item = dynamic_cast<IdSuggestionsListViewItem*>( *it );
125
 
            settings->idSuggestions_formatStrList.append( item->originalText() );
126
 
            if ( item == m_defaultSuggestionItem )
127
 
                settings->idSuggestions_default = i;
128
 
        }
129
 
        settings->idSuggestions_smallWords.clear();
130
 
        for ( QListViewItemIterator it( m_listSmallWords ); it.current(); ++it )
131
 
            settings->idSuggestions_smallWords << it.current()->text( 0 );
132
 
    }
133
 
 
134
 
    void SettingsIdSuggestions::readData()
135
 
    {
136
 
        Settings * settings = Settings::self();
137
 
 
138
 
        IdSuggestionsListViewItem *prev = NULL;
139
 
        m_listIdSuggestions->clear();
140
 
        m_defaultSuggestionItem = NULL;
141
 
        m_checkBoxForceDefault->setChecked( settings->idSuggestions_forceDefault );
142
 
        m_checkBoxForceDefault->setEnabled( settings->idSuggestions_default >= 0 );
143
 
        int i = 0;
144
 
        for ( QStringList::ConstIterator it = settings->idSuggestions_formatStrList.begin(); it != settings->idSuggestions_formatStrList.end(); ++i, ++it )
145
 
        {
146
 
            prev = new IdSuggestionsListViewItem( m_listIdSuggestions, prev, *it, m_example );
147
 
            prev->setPixmap( 0, SmallIcon( "filter" ) );
148
 
            if ( i == settings->idSuggestions_default )
149
 
                m_defaultSuggestionItem = prev;
150
 
        }
151
 
 
152
 
        if ( m_defaultSuggestionItem != NULL )
153
 
            m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
154
 
 
155
 
        m_listSmallWords->clear();
156
 
        for ( QStringList::ConstIterator it = settings->idSuggestions_smallWords.begin(); it != settings->idSuggestions_smallWords.end(); ++it )
157
 
            new KListViewItem( m_listSmallWords, *it );
158
 
        m_lineEditSmallWords->setText( "" );
159
 
        slotListSmallWordsChanged();
160
 
 
161
 
        updateGUI();
162
 
    }
163
 
 
164
 
    void SettingsIdSuggestions::slotConfigChanged()
165
 
    {
166
 
        emit configChanged();
167
 
    }
168
 
 
169
 
    void SettingsIdSuggestions::slotNewIdSuggestion()
170
 
    {
171
 
        IdSuggestionsListViewItem * item = new IdSuggestionsListViewItem( m_listIdSuggestions, "a|Y|T", m_example );
172
 
        item->setPixmap( 0, SmallIcon( "filter" ) );
173
 
        m_listIdSuggestions->setSelected( item, TRUE );
174
 
        QTimer::singleShot( 100, this, SLOT( slotEditIdSuggestion() ) );
175
 
    }
176
 
 
177
 
    void SettingsIdSuggestions::slotEditIdSuggestion()
178
 
    {
179
 
        IdSuggestionsListViewItem * item = static_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
180
 
        if ( item != NULL )
181
 
        {
182
 
            QString formatStr = item->originalText();
183
 
            if ( IdSuggestionsWidget::execute( formatStr, this ) == QDialog::Accepted )
184
 
            {
185
 
                item->setText( 0, formatStr );
186
 
                emit configChanged();
187
 
            }
188
 
        }
189
 
        updateGUI();
190
 
    }
191
 
 
192
 
    void SettingsIdSuggestions::slotDeleteIdSuggestion()
193
 
    {
194
 
        IdSuggestionsListViewItem * item = static_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
195
 
        if ( item != NULL )
196
 
        {
197
 
            if ( m_defaultSuggestionItem == item )
198
 
                m_defaultSuggestionItem = NULL;
199
 
            m_checkBoxForceDefault->setEnabled( m_defaultSuggestionItem != NULL );
200
 
 
201
 
            delete item;
202
 
            emit configChanged();
203
 
        }
204
 
        updateGUI();
205
 
    }
206
 
 
207
 
    void SettingsIdSuggestions::slotMoveUpIdSuggestion()
208
 
    {
209
 
        IdSuggestionsListViewItem * item = dynamic_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
210
 
        if ( item != NULL && item -> itemAbove() != NULL )
211
 
        {
212
 
            IdSuggestionsListViewItem *itemAbove = dynamic_cast<IdSuggestionsListViewItem*>( item->itemAbove() );
213
 
            QString text = item->originalText();
214
 
            item->setText( 0, itemAbove->originalText( ) );
215
 
            itemAbove->setText( 0, text );
216
 
            m_listIdSuggestions->setCurrentItem( itemAbove );
217
 
            m_listIdSuggestions->ensureItemVisible( itemAbove );
218
 
 
219
 
            if ( m_defaultSuggestionItem == itemAbove )
220
 
            {
221
 
                itemAbove->setPixmap( 0, SmallIcon( "filter" ) );
222
 
                m_defaultSuggestionItem = item;
223
 
            }
224
 
            else if ( m_defaultSuggestionItem == item )
225
 
            {
226
 
                item->setPixmap( 0, SmallIcon( "filter" ) );
227
 
                m_defaultSuggestionItem = itemAbove;
228
 
            }
229
 
            if ( m_defaultSuggestionItem != NULL )
230
 
                m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
231
 
        }
232
 
    }
233
 
 
234
 
    void SettingsIdSuggestions::slotMoveDownIdSuggestion()
235
 
    {
236
 
        IdSuggestionsListViewItem * item = dynamic_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
237
 
        if ( item != NULL && item -> itemBelow() != NULL )
238
 
        {
239
 
            IdSuggestionsListViewItem *itemBelow = dynamic_cast<IdSuggestionsListViewItem*>( item->itemBelow() );
240
 
            QString text = item->originalText();
241
 
            item->setText( 0, itemBelow->originalText( ) );
242
 
            itemBelow->setText( 0, text );
243
 
            m_listIdSuggestions->setCurrentItem( itemBelow );
244
 
            m_listIdSuggestions->ensureItemVisible( itemBelow );
245
 
 
246
 
            if ( m_defaultSuggestionItem == itemBelow )
247
 
            {
248
 
                itemBelow->setPixmap( 0, SmallIcon( "filter" ) );
249
 
                m_defaultSuggestionItem = item;
250
 
            }
251
 
            else if ( m_defaultSuggestionItem == item )
252
 
            {
253
 
                item->setPixmap( 0, SmallIcon( "filter" ) );
254
 
                m_defaultSuggestionItem = itemBelow;
255
 
            }
256
 
            if ( m_defaultSuggestionItem != NULL )
257
 
                m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
258
 
        }
259
 
    }
260
 
 
261
 
    void SettingsIdSuggestions::slotToggleDefault()
262
 
    {
263
 
        if ( m_defaultSuggestionItem != NULL )
264
 
            m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "filter" ) );
265
 
 
266
 
        QListViewItem *item = m_listIdSuggestions->selectedItem();
267
 
        if ( item == m_defaultSuggestionItem )
268
 
            m_defaultSuggestionItem = NULL;
269
 
        else
270
 
        {
271
 
            m_defaultSuggestionItem = item;
272
 
            m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
273
 
        }
274
 
        m_checkBoxForceDefault->setEnabled( m_defaultSuggestionItem != NULL );
275
 
    }
276
 
 
277
 
    void SettingsIdSuggestions::slotEditSmallWordsChanged( const QString &newWord )
278
 
    {
279
 
        bool result = !newWord.isEmpty();
280
 
 
281
 
        for ( QListViewItemIterator it( m_listSmallWords ); result && it.current(); ++it )
282
 
            result &= it.current()->text( 0 ) != newWord;
283
 
 
284
 
        m_buttonAddSmallWord->setEnabled( result );
285
 
    }
286
 
 
287
 
    void SettingsIdSuggestions::slotAddSmallWord()
288
 
    {
289
 
        new KListViewItem( m_listSmallWords, m_lineEditSmallWords->text() );
290
 
        m_lineEditSmallWords->setText( "" );
291
 
        emit configChanged();
292
 
    }
293
 
 
294
 
    void SettingsIdSuggestions::slotDeleteSmallWord()
295
 
    {
296
 
        bool changed = false;
297
 
        QListViewItemIterator it( m_listSmallWords, QListViewItemIterator::Selected );
298
 
        while ( it.current() )
299
 
        {
300
 
            QListViewItem *cur = it.current();
301
 
            ++it;
302
 
            delete cur;
303
 
            changed = true;
304
 
        }
305
 
 
306
 
        if ( changed )
307
 
            emit configChanged();
308
 
    }
309
 
 
310
 
    void SettingsIdSuggestions::slotListSmallWordsChanged()
311
 
    {
312
 
        QListViewItemIterator it( m_listSmallWords, QListViewItemIterator::Selected );
313
 
        m_buttonDeleteSmallWord->setEnabled( it.current() != NULL );
314
 
    }
315
 
 
316
 
    void SettingsIdSuggestions::updateGUI()
317
 
    {
318
 
        QListViewItem *item = m_listIdSuggestions->selectedItem();
319
 
        bool selected = item != NULL;
320
 
        m_buttonEditIdSuggestion->setEnabled( selected );
321
 
        m_buttonDeleteIdSuggestion->setEnabled( selected );
322
 
        m_buttonMoveDownIdSuggestion->setEnabled( selected && item->itemBelow() != NULL );
323
 
        m_buttonMoveUpIdSuggestion->setEnabled( selected && item->itemAbove() != NULL );
324
 
        m_buttonToggleDefault->setEnabled( selected );
325
 
    }
326
 
 
327
 
    void SettingsIdSuggestions::setupGUI()
328
 
    {
329
 
        QGridLayout * gridLayout = new QGridLayout( this, 13, 2, 0, KDialog::spacingHint(), "gridLayout" );
330
 
        gridLayout->setRowStretch( 6, 2 );
331
 
        gridLayout->setRowSpacing( 8, KDialog::spacingHint() );
332
 
        gridLayout->setRowStretch( 12, 1 );
333
 
        gridLayout->setColStretch( 0, 1 );
334
 
 
335
 
        m_listIdSuggestions = new KListView( this );
336
 
        m_listIdSuggestions->setSorting( -1, FALSE );
337
 
        m_listIdSuggestions->addColumn( i18n( "Id Suggestions" ) );
338
 
        m_listIdSuggestions->header()->setClickEnabled( FALSE );
339
 
        m_listIdSuggestions->setFullWidth( true );
340
 
        gridLayout->addMultiCellWidget( m_listIdSuggestions, 0, 6, 0, 0 );
341
 
        connect( m_listIdSuggestions, SIGNAL( selectionChanged() ), this, SLOT( updateGUI() ) );
342
 
        connect( m_listIdSuggestions, SIGNAL( currentChanged( QListViewItem * ) ), this, SLOT( updateGUI() ) );
343
 
        connect( m_listIdSuggestions, SIGNAL( doubleClicked( QListViewItem*, const QPoint &, int ) ), this, SLOT( slotEditIdSuggestion() ) );
344
 
 
345
 
        m_buttonNewIdSuggestion = new KPushButton( i18n( "id suggestion",  "New" ), this );
346
 
        m_buttonNewIdSuggestion->setIconSet( QIconSet( SmallIcon( "add" ) ) );
347
 
        gridLayout->addWidget( m_buttonNewIdSuggestion, 0, 1 );
348
 
        connect( m_buttonNewIdSuggestion, SIGNAL( clicked() ), this, SLOT( slotNewIdSuggestion() ) );
349
 
        m_buttonEditIdSuggestion = new KPushButton( i18n( "id suggestion", "Edit" ), this );
350
 
        m_buttonEditIdSuggestion->setIconSet( QIconSet( SmallIcon( "edit" ) ) );
351
 
        gridLayout->addWidget( m_buttonEditIdSuggestion, 1, 1 );
352
 
        connect( m_buttonEditIdSuggestion, SIGNAL( clicked() ), this, SLOT( slotEditIdSuggestion() ) );
353
 
        m_buttonDeleteIdSuggestion = new KPushButton( i18n( "id suggestion", "Delete" ), this );
354
 
        m_buttonDeleteIdSuggestion->setIconSet( QIconSet( SmallIcon( "editdelete" ) ) );
355
 
        gridLayout->addWidget( m_buttonDeleteIdSuggestion, 2, 1 );
356
 
        connect( m_buttonDeleteIdSuggestion, SIGNAL( clicked() ), this, SLOT( slotDeleteIdSuggestion() ) );
357
 
        m_buttonMoveUpIdSuggestion = new KPushButton( i18n( "id suggestion", "Up" ), this );
358
 
        m_buttonMoveUpIdSuggestion->setIconSet( QIconSet( SmallIcon( "up" ) ) );
359
 
        gridLayout->addWidget( m_buttonMoveUpIdSuggestion, 3, 1 );
360
 
        connect( m_buttonMoveUpIdSuggestion, SIGNAL( clicked() ), this, SLOT( slotMoveUpIdSuggestion() ) );
361
 
        m_buttonMoveDownIdSuggestion = new KPushButton( i18n( "id suggestion", "Down" ), this );
362
 
        m_buttonMoveDownIdSuggestion->setIconSet( QIconSet( SmallIcon( "down" ) ) );
363
 
        gridLayout->addWidget( m_buttonMoveDownIdSuggestion, 4, 1 );
364
 
        connect( m_buttonMoveDownIdSuggestion, SIGNAL( clicked() ), this, SLOT( slotMoveDownIdSuggestion() ) );
365
 
        m_buttonToggleDefault = new KPushButton( i18n( "Toogle default" ), this );
366
 
        m_buttonToggleDefault->setIconSet( QIconSet( SmallIcon( "favorites" ) ) );
367
 
        gridLayout->addWidget( m_buttonToggleDefault, 5, 1 );
368
 
        connect( m_buttonToggleDefault, SIGNAL( clicked() ), this, SLOT( slotToggleDefault() ) );
369
 
 
370
 
        m_checkBoxForceDefault = new QCheckBox( i18n( "Use default id suggestion when editing new entries" ), this );
371
 
        gridLayout->addMultiCellWidget( m_checkBoxForceDefault, 7, 7, 0, 1 );
372
 
 
373
 
        QLabel *label = new QLabel( i18n( "Small Words:" ), this );
374
 
        gridLayout->addMultiCellWidget( label, 9, 9, 0, 1 );
375
 
        m_lineEditSmallWords = new KLineEdit( this );
376
 
        label->setBuddy( m_lineEditSmallWords );
377
 
        gridLayout->addWidget( m_lineEditSmallWords, 10, 0 );
378
 
        connect( m_lineEditSmallWords, SIGNAL( textChanged( const QString & ) ), this, SLOT( slotEditSmallWordsChanged( const QString & ) ) );
379
 
        m_buttonAddSmallWord = new KPushButton( i18n( "Add" ), this );
380
 
        m_buttonAddSmallWord->setEnabled( false );
381
 
        m_buttonAddSmallWord->setIconSet( QIconSet( SmallIcon( "add" ) ) );
382
 
        gridLayout->addWidget( m_buttonAddSmallWord, 10, 1 );
383
 
        connect( m_buttonAddSmallWord, SIGNAL( clicked() ), this, SLOT( slotAddSmallWord() ) );
384
 
 
385
 
        m_listSmallWords = new KListView( this );
386
 
        m_listSmallWords->addColumn( i18n( "Small Words" ) );
387
 
        m_listSmallWords->header()->setClickEnabled( true );
388
 
        m_listSmallWords->setFullWidth( true );
389
 
        connect( m_listSmallWords, SIGNAL( selectionChanged() ), this, SLOT( slotListSmallWordsChanged() ) );
390
 
        connect( m_listSmallWords, SIGNAL( currentChanged( QListViewItem * ) ), this, SLOT( slotListSmallWordsChanged() ) );
391
 
        gridLayout->addMultiCellWidget( m_listSmallWords, 11, 12, 0, 0 );
392
 
        m_buttonDeleteSmallWord = new KPushButton( i18n( "Delete" ), this );
393
 
        m_buttonDeleteSmallWord->setEnabled( false );
394
 
        m_buttonDeleteSmallWord->setIconSet( QIconSet( SmallIcon( "editdelete" ) ) );
395
 
        gridLayout->addWidget( m_buttonDeleteSmallWord, 11, 1 );
396
 
        connect( m_buttonDeleteSmallWord, SIGNAL( clicked() ), this, SLOT( slotDeleteSmallWord() ) );
397
 
    }
398
 
}
399
 
 
400
 
#include "settingsidsuggestions.moc"