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
***************************************************************************/
21
#include <qcheckbox.h>
23
#include <qvalidator.h>
27
#include <kinputdialog.h>
28
#include <klistview.h>
29
#include <kiconloader.h>
30
#include <kpushbutton.h>
33
#include <klineedit.h>
35
#include <idsuggestions.h>
36
#include <idsuggestionswidget.h>
38
#include <fileimporter.h>
39
#include <fileimporterbibtex.h>
40
#include "settingsidsuggestions.h"
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}";
46
IdSuggestionsListViewItem::IdSuggestionsListViewItem( KListView *list, const QString& plainText, BibTeX::Entry *example ): KListViewItem( list ), m_original( plainText ), m_example( example )
48
KListViewItem::setText( 0, parse( plainText ) );
49
KListViewItem::setMultiLinesEnabled( TRUE );
52
IdSuggestionsListViewItem::IdSuggestionsListViewItem( KListView *list, KListViewItem *prev, const QString& plainText, BibTeX::Entry *example ): KListViewItem( list, prev ), m_original( plainText ), m_example( example )
54
KListViewItem::setText( 0, parse( plainText ) );
55
KListViewItem::setMultiLinesEnabled( TRUE );
58
void IdSuggestionsListViewItem::setText( int col, const QString& text )
61
KListViewItem::setText( 0, parse( text ) );
63
KListViewItem::setText( col, text );
66
int IdSuggestionsListViewItem::width( const QFontMetrics & fm, const QListView *, int c ) const
69
QStringList lines = QStringList::split( '\n', text( c ) );
70
for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
72
int w = fm.width( *it );
73
if ( w > max ) max = w;
79
QString IdSuggestionsListViewItem::originalText() const
84
QString IdSuggestionsListViewItem::parse( const QString&plainText )
86
m_original = plainText;
87
QString result = IdSuggestions::formatStrToHuman( plainText );
88
if ( m_example != NULL )
90
result.append( QString( i18n( "\nExample: %1" ) ).arg( IdSuggestions::formatId( m_example, plainText ) ) );
95
SettingsIdSuggestions::SettingsIdSuggestions( QWidget *parent, const char *name )
96
: QWidget( parent, name )
98
m_validator = new QRegExpValidator( QRegExp( "[^\\s]+" ), this );
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() ) ) );
108
SettingsIdSuggestions::~SettingsIdSuggestions()
114
void SettingsIdSuggestions::applyData()
116
Settings * settings = Settings::self();
118
settings->idSuggestions_formatStrList.clear();
119
settings->idSuggestions_default = -1;
120
settings->idSuggestions_forceDefault = m_checkBoxForceDefault->isChecked();
122
for ( QListViewItemIterator it( m_listIdSuggestions ); it.current(); ++i, ++it )
124
IdSuggestionsListViewItem *item = dynamic_cast<IdSuggestionsListViewItem*>( *it );
125
settings->idSuggestions_formatStrList.append( item->originalText() );
126
if ( item == m_defaultSuggestionItem )
127
settings->idSuggestions_default = i;
129
settings->idSuggestions_smallWords.clear();
130
for ( QListViewItemIterator it( m_listSmallWords ); it.current(); ++it )
131
settings->idSuggestions_smallWords << it.current()->text( 0 );
134
void SettingsIdSuggestions::readData()
136
Settings * settings = Settings::self();
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 );
144
for ( QStringList::ConstIterator it = settings->idSuggestions_formatStrList.begin(); it != settings->idSuggestions_formatStrList.end(); ++i, ++it )
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;
152
if ( m_defaultSuggestionItem != NULL )
153
m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
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();
164
void SettingsIdSuggestions::slotConfigChanged()
166
emit configChanged();
169
void SettingsIdSuggestions::slotNewIdSuggestion()
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() ) );
177
void SettingsIdSuggestions::slotEditIdSuggestion()
179
IdSuggestionsListViewItem * item = static_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
182
QString formatStr = item->originalText();
183
if ( IdSuggestionsWidget::execute( formatStr, this ) == QDialog::Accepted )
185
item->setText( 0, formatStr );
186
emit configChanged();
192
void SettingsIdSuggestions::slotDeleteIdSuggestion()
194
IdSuggestionsListViewItem * item = static_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
197
if ( m_defaultSuggestionItem == item )
198
m_defaultSuggestionItem = NULL;
199
m_checkBoxForceDefault->setEnabled( m_defaultSuggestionItem != NULL );
202
emit configChanged();
207
void SettingsIdSuggestions::slotMoveUpIdSuggestion()
209
IdSuggestionsListViewItem * item = dynamic_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
210
if ( item != NULL && item -> itemAbove() != NULL )
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 );
219
if ( m_defaultSuggestionItem == itemAbove )
221
itemAbove->setPixmap( 0, SmallIcon( "filter" ) );
222
m_defaultSuggestionItem = item;
224
else if ( m_defaultSuggestionItem == item )
226
item->setPixmap( 0, SmallIcon( "filter" ) );
227
m_defaultSuggestionItem = itemAbove;
229
if ( m_defaultSuggestionItem != NULL )
230
m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
234
void SettingsIdSuggestions::slotMoveDownIdSuggestion()
236
IdSuggestionsListViewItem * item = dynamic_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
237
if ( item != NULL && item -> itemBelow() != NULL )
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 );
246
if ( m_defaultSuggestionItem == itemBelow )
248
itemBelow->setPixmap( 0, SmallIcon( "filter" ) );
249
m_defaultSuggestionItem = item;
251
else if ( m_defaultSuggestionItem == item )
253
item->setPixmap( 0, SmallIcon( "filter" ) );
254
m_defaultSuggestionItem = itemBelow;
256
if ( m_defaultSuggestionItem != NULL )
257
m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
261
void SettingsIdSuggestions::slotToggleDefault()
263
if ( m_defaultSuggestionItem != NULL )
264
m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "filter" ) );
266
QListViewItem *item = m_listIdSuggestions->selectedItem();
267
if ( item == m_defaultSuggestionItem )
268
m_defaultSuggestionItem = NULL;
271
m_defaultSuggestionItem = item;
272
m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
274
m_checkBoxForceDefault->setEnabled( m_defaultSuggestionItem != NULL );
277
void SettingsIdSuggestions::slotEditSmallWordsChanged( const QString &newWord )
279
bool result = !newWord.isEmpty();
281
for ( QListViewItemIterator it( m_listSmallWords ); result && it.current(); ++it )
282
result &= it.current()->text( 0 ) != newWord;
284
m_buttonAddSmallWord->setEnabled( result );
287
void SettingsIdSuggestions::slotAddSmallWord()
289
new KListViewItem( m_listSmallWords, m_lineEditSmallWords->text() );
290
m_lineEditSmallWords->setText( "" );
291
emit configChanged();
294
void SettingsIdSuggestions::slotDeleteSmallWord()
296
bool changed = false;
297
QListViewItemIterator it( m_listSmallWords, QListViewItemIterator::Selected );
298
while ( it.current() )
300
QListViewItem *cur = it.current();
307
emit configChanged();
310
void SettingsIdSuggestions::slotListSmallWordsChanged()
312
QListViewItemIterator it( m_listSmallWords, QListViewItemIterator::Selected );
313
m_buttonDeleteSmallWord->setEnabled( it.current() != NULL );
316
void SettingsIdSuggestions::updateGUI()
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 );
327
void SettingsIdSuggestions::setupGUI()
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 );
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() ) );
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() ) );
370
m_checkBoxForceDefault = new QCheckBox( i18n( "Use default id suggestion when editing new entries" ), this );
371
gridLayout->addMultiCellWidget( m_checkBoxForceDefault, 7, 7, 0, 1 );
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() ) );
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() ) );
400
#include "settingsidsuggestions.moc"