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
***************************************************************************/
24
#include <qprogressbar.h>
28
#include <kpushbutton.h>
29
#include <kapplication.h>
30
#include <kmessagebox.h>
31
#include <ktextedit.h>
32
#include <klistview.h>
39
#include <macrowidget.h>
40
#include <entrywidget.h>
41
#include <preamblewidget.h>
43
#include <fileexporterbibtex.h>
44
#include "mergeelements.h"
48
MergeElementsCliqueItem::MergeElementsCliqueItem( BibTeX::Entry* _entry, BibTeX::Macro* _macro, BibTeX::Preamble* _preamble, QListView *parent )
49
: QCheckListItem( parent, _entry == NULL ?( _macro == NULL ? _preamble->value()->text() : _macro->key() ) : _entry->id(), QCheckListItem::CheckBox ), entry( _entry ), macro( _macro ), preamble( _preamble )
54
void MergeElementsCliqueItem::stateChange( bool )
56
emit stateChanged( this );
59
MergeEntriesAlternativesController::MergeEntriesAlternativesController( const QString &label, QListView *parent )
60
: QCheckListItem( parent, label, QCheckListItem::RadioButtonController ), fieldType( BibTeX::EntryField::ftUnknown ), fieldName( label )
65
MergeEntriesAlternativesController::MergeEntriesAlternativesController( BibTeX::EntryField::FieldType _fieldType, QListView *parent )
66
: QCheckListItem( parent, BibTeX::EntryField::fieldTypeToString( _fieldType ), QCheckListItem::RadioButtonController ), fieldType( _fieldType ), fieldName( BibTeX::EntryField::fieldTypeToString( _fieldType ) )
71
MergeMacrosAlternativesController::MergeMacrosAlternativesController( bool isKey, QListView *parent )
72
: QCheckListItem( parent, isKey ? i18n( "Key" ) : i18n( "Value" ), QCheckListItem::RadioButtonController )
77
MergeEntriesAlternativesItem::MergeEntriesAlternativesItem( BibTeX::EntryField *_field, MergeEntriesAlternativesController *parent )
78
: QCheckListItem( parent, _field->value()->text(), QCheckListItem::RadioButton ), field( _field )
83
MergeMacrosAlternativesItem::MergeMacrosAlternativesItem( BibTeX::Value *_value, MergeMacrosAlternativesController *parent )
84
: QCheckListItem( parent, _value->text(), QCheckListItem::RadioButton ), value( _value )
89
MergeElementsAlternativesId::MergeElementsAlternativesId( const QString & _id, MergeEntriesAlternativesController *parent ) : QCheckListItem( parent, _id, QCheckListItem::RadioButton ), id( _id )
94
MergeMacroAlternativesKey::MergeMacroAlternativesKey( const QString & _key, MergeMacrosAlternativesController *parent ) : QCheckListItem( parent, _key, QCheckListItem::RadioButton ), key( _key )
99
MergeEntriesAlternativesEntryType::MergeEntriesAlternativesEntryType( const QString & _typeString, MergeEntriesAlternativesController *parent )
100
: QCheckListItem( parent, _typeString, QCheckListItem::RadioButton ), typeString( _typeString ), type( BibTeX::Entry::entryTypeFromString( _typeString ) )
105
MergeEntriesAlternativesEntryType::MergeEntriesAlternativesEntryType( BibTeX::Entry::EntryType _type, MergeEntriesAlternativesController *parent )
106
: QCheckListItem( parent, BibTeX::Entry::entryTypeToString( _type ), QCheckListItem::RadioButton ), typeString( BibTeX::Entry::entryTypeToString( _type ) ), type( _type )
111
MergePreambleAlternativesController::MergePreambleAlternativesController( QListView *parent )
112
: QCheckListItem( parent, i18n( "Preamble text" ), QCheckListItem::RadioButtonController )
117
MergePreambleAlternatives::MergePreambleAlternatives( const QString &_text, MergePreambleAlternativesController *parent )
118
:QCheckListItem( parent, _text, QCheckListItem::RadioButton ), text( _text )
123
MergeElements::MergeElements( QWidget *parent )
124
: KDialogBase( parent, "MergeElements", true, "undefined", Ok | Cancel | User1 | User2, User1, true, KGuiItem( i18n( "Next" ), "next" ), KGuiItem( i18n( "Previous" ), "previous" ) ), m_currentCliqueIndex( 0 )
129
MergeElements::~MergeElements()
131
KConfig * config = kapp->config();
132
config->setGroup( "MergeElements" );
133
saveWindowSize( config );
136
void MergeElements::setupGUI()
138
QWidget *vboxContainer = new QWidget( this );
139
setMainWidget( vboxContainer );
140
QBoxLayout *vboxLayout = new QVBoxLayout( vboxContainer, 0, KDialog::spacingHint() );
141
vboxLayout->setResizeMode( QLayout::Minimum );
143
QLabel *label = new QLabel( i18n( "Select elements to merge. At least two elements must be checked to perform a merge operation. Checked entries will be replaced by the merged element, unchecked elements will be kept." ), vboxContainer );
144
label->setAlignment( Qt::WordBreak );
145
vboxLayout->addWidget( label );
146
m_listViewClique = new KListView( vboxContainer );
147
m_listViewClique->addColumn( i18n( "Entry/Macro Id" ) );
148
m_listViewClique->setFullWidth( true );
149
m_listViewClique->setAllColumnsShowFocus( true );
150
vboxLayout->addWidget( m_listViewClique );
151
vboxLayout->setStretchFactor( m_listViewClique, 1 );
152
label->setBuddy( m_listViewClique );
154
m_progressBar = new QProgressBar( vboxContainer );
155
vboxLayout->addWidget( m_progressBar );
157
vboxLayout->addSpacing( KDialog::spacingHint() * 2 );
159
label = new QLabel( i18n( "Choose from this list which alternatives you want to keep in the merged element." ), vboxContainer );
160
label->setAlignment( Qt::WordBreak );
161
vboxLayout->addWidget( label );
162
m_listViewAlternatives = new KListView( vboxContainer );
163
m_listViewAlternatives->addColumn( i18n( "Field/Key" ) );
164
m_listViewAlternatives->setFullWidth( true );
165
m_listViewAlternatives->setAllColumnsShowFocus( true );
166
vboxLayout->addWidget( m_listViewAlternatives );
167
vboxLayout->setStretchFactor( m_listViewAlternatives, 3 );
168
label->setBuddy( m_listViewAlternatives );
170
connect( m_listViewClique, SIGNAL( doubleClicked( QListViewItem * ) ), this, SLOT( slotPreviewElement( QListViewItem * ) ) );
171
connect( this, SIGNAL( user1Clicked() ), this, SLOT( slotNextClique() ) );
172
connect( this, SIGNAL( user2Clicked() ), this, SLOT( slotPreviousClique() ) );
173
connect( this, SIGNAL( okClicked() ), this, SLOT( saveCurrentMergeSet() ) );
176
void MergeElements::setClique( int cliqueIndex )
178
if ( m_currentCliqueIndex != cliqueIndex )
179
saveCurrentMergeSet();
180
m_currentCliqueIndex = cliqueIndex;
182
m_listViewClique->clear();
183
FindDuplicates::DuplicateClique clique = m_duplicateCliqueList[cliqueIndex];
185
for ( FindDuplicates::DuplicateClique::Iterator it = clique.begin(); it != clique.end(); ++it )
187
BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( *it );
190
MergeElementsCliqueItem *item = new MergeElementsCliqueItem( entry, NULL, NULL, m_listViewClique );
191
connect( item, SIGNAL( stateChanged( MergeElementsCliqueItem* ) ), this, SLOT( slotRefreshAlternatives() ) );
195
BibTeX::Macro *macro = dynamic_cast<BibTeX::Macro*>( *it );
198
MergeElementsCliqueItem *item = new MergeElementsCliqueItem( NULL, macro, NULL, m_listViewClique );
199
connect( item, SIGNAL( stateChanged( MergeElementsCliqueItem* ) ), this, SLOT( slotRefreshAlternatives() ) );
203
BibTeX::Preamble *preamble = dynamic_cast<BibTeX::Preamble*>( *it );
204
if ( preamble!=NULL )
206
MergeElementsCliqueItem *item = new MergeElementsCliqueItem( NULL, NULL, preamble, m_listViewClique );
207
connect( item, SIGNAL( stateChanged( MergeElementsCliqueItem* ) ), this, SLOT( slotRefreshAlternatives() ) );
212
restoreCurrentMergeSet();
214
enableButton( User1, ( cliqueIndex < ( int )( m_duplicateCliqueList.size() ) - 1 ) && m_duplicateCliqueList.size() > 1 );
215
enableButton( User2, cliqueIndex > 0 && m_duplicateCliqueList.size() > 1 );
216
m_progressBar->setProgress( cliqueIndex, m_duplicateCliqueList.size() - 1 );
219
void MergeElements::saveCurrentMergeSet()
221
if ( m_mergeSetList[m_currentCliqueIndex] == NULL )
222
m_mergeSetList[m_currentCliqueIndex] = new MergeSet;
225
m_mergeSetList[m_currentCliqueIndex]->entries.clear();
226
m_mergeSetList[m_currentCliqueIndex]->fields.clear();
228
m_mergeSetList[m_currentCliqueIndex]->type = BibTeX::Entry::etUnknown;
229
m_mergeSetList[m_currentCliqueIndex]->typeString = QString::null;
230
m_mergeSetList[m_currentCliqueIndex]->id = QString::null;
231
m_mergeSetList[m_currentCliqueIndex]->macroKey = QString::null;
232
m_mergeSetList[m_currentCliqueIndex]->macroValue = NULL;
233
m_mergeSetList[m_currentCliqueIndex]->preambleText=QString::null;
235
for ( QListViewItemIterator it( m_listViewClique, QListViewItemIterator::Checked ); it.current(); ++it )
237
MergeElementsCliqueItem *eci = dynamic_cast<MergeElementsCliqueItem*>( *it );
238
BibTeX::Entry *entry = eci->entry;
240
m_mergeSetList[m_currentCliqueIndex]->entries.append( entry );
241
BibTeX::Macro *macro = eci->macro;
243
m_mergeSetList[m_currentCliqueIndex]->macros.append( macro );
244
BibTeX::Preamble *preamble = eci->preamble;
245
if ( preamble !=NULL )
246
m_mergeSetList[m_currentCliqueIndex]->preambles.append( preamble );
249
for ( QListViewItemIterator it( m_listViewAlternatives, QListViewItemIterator::Checked ); it.current(); ++it )
251
MergeEntriesAlternativesItem *item = dynamic_cast<MergeEntriesAlternativesItem*>( *it );
254
BibTeX::EntryField *field = item->field;
255
m_mergeSetList[m_currentCliqueIndex]->fields.append( field );
259
MergeElementsAlternativesId *item = dynamic_cast<MergeElementsAlternativesId*>( *it );
261
m_mergeSetList[m_currentCliqueIndex]->id = item->id;
264
MergeEntriesAlternativesEntryType *itemT = dynamic_cast<MergeEntriesAlternativesEntryType*>( *it );
267
m_mergeSetList[m_currentCliqueIndex]->typeString = itemT->typeString;
268
m_mergeSetList[m_currentCliqueIndex]->type = itemT->type;
272
MergeMacroAlternativesKey *itemK = dynamic_cast<MergeMacroAlternativesKey*>( *it );
274
m_mergeSetList[m_currentCliqueIndex]->macroKey = itemK->key;
277
MergeMacrosAlternativesItem *itemMA = dynamic_cast<MergeMacrosAlternativesItem*>( *it );
278
if ( itemMA != NULL )
279
m_mergeSetList[m_currentCliqueIndex]->macroValue = itemMA->value;
282
MergePreambleAlternatives *itemP=dynamic_cast<MergePreambleAlternatives*>( *it );
284
m_mergeSetList[m_currentCliqueIndex]->preambleText=itemP->text;
293
void MergeElements::restoreCurrentMergeSet()
295
if ( m_mergeSetList[m_currentCliqueIndex] == NULL )
297
m_listViewAlternatives->clear();
301
for ( QListViewItemIterator it( m_listViewClique ); it.current(); ++it )
303
MergeElementsCliqueItem *item = dynamic_cast<MergeElementsCliqueItem*>( *it );
304
BibTeX::Entry *entry = item->entry;
305
BibTeX::Macro *macro = item->macro;
306
BibTeX::Preamble *preamble = item->preamble;
308
for ( QValueList<BibTeX::Entry*>::Iterator it2 = m_mergeSetList[m_currentCliqueIndex]->entries.begin(); it2 != m_mergeSetList[m_currentCliqueIndex]->entries.end(); ++it2 )
310
if ( entry->id() == ( *it2 )->id() )
316
else if ( macro != NULL )
318
for ( QValueList<BibTeX::Macro*>::Iterator it2 = m_mergeSetList[m_currentCliqueIndex]->macros.begin(); it2 != m_mergeSetList[m_currentCliqueIndex]->macros.end(); ++it2 )
319
if ( macro->key() == ( *it2 )->key() )
325
else if ( preamble!=NULL )
326
for ( QValueList<BibTeX::Preamble*>::Iterator it2 = m_mergeSetList[m_currentCliqueIndex]->preambles.begin(); it2 != m_mergeSetList[m_currentCliqueIndex]->preambles.end(); ++it2 )
327
if ( preamble->value()->text() == ( *it2 )->value()->text() )
334
slotRefreshAlternatives();
336
for ( QListViewItemIterator it( m_listViewAlternatives ); it.current(); ++it )
338
MergeEntriesAlternativesItem *item = dynamic_cast<MergeEntriesAlternativesItem*>( *it );
341
for ( QValueList<BibTeX::EntryField*>::Iterator it2 = m_mergeSetList[m_currentCliqueIndex]->fields.begin(); it2 != m_mergeSetList[m_currentCliqueIndex]->fields.end(); ++it2 )
342
if ( item->field->fieldTypeName().lower() == ( *it2 )->fieldTypeName().lower() && item->field->value()->text() == ( *it2 )->value()->text() )
350
MergeElementsAlternativesId *item = dynamic_cast<MergeElementsAlternativesId*>( *it );
353
if ( item->id == m_mergeSetList[m_currentCliqueIndex]->id )
358
MergeEntriesAlternativesEntryType *item = dynamic_cast<MergeEntriesAlternativesEntryType*>( *it );
361
if (( item->type != BibTeX::Entry::etUnknown && item->type == m_mergeSetList[m_currentCliqueIndex]->type ) || ( item->typeString.lower() == m_mergeSetList[m_currentCliqueIndex]->typeString.lower() ) )
366
MergeMacrosAlternativesItem *mai = dynamic_cast<MergeMacrosAlternativesItem*>( *it );
369
if ( mai->value == m_mergeSetList[m_currentCliqueIndex]->macroValue )
374
MergeMacroAlternativesKey *mak = dynamic_cast<MergeMacroAlternativesKey*>( *it );
377
if ( mak->key == m_mergeSetList[m_currentCliqueIndex]->macroKey )
382
MergePreambleAlternatives *mpa =dynamic_cast<MergePreambleAlternatives*>( *it );
385
if ( mpa->text==m_mergeSetList[m_currentCliqueIndex]->preambleText )
389
qDebug( "Item is of unknown type" );
398
void MergeElements::applyMergeSet( BibTeX::File *bibTeXFile, BibTeX::File *otherBibTeXFile )
400
int n = m_duplicateCliqueList.size();
401
for ( int i = 0; i < n; ++i )
403
if ( m_mergeSetList[i] == NULL ) continue;
405
if ( !m_mergeSetList[i]->entries.isEmpty() )
407
QString id = m_mergeSetList[i]->id == QString::null ? ( *m_mergeSetList[i]->entries.begin() )->id() : m_mergeSetList[i]->id;
409
BibTeX::Entry *newEntry = NULL;
410
if ( m_mergeSetList[i]->type == BibTeX::Entry::etUnknown )
412
if ( m_mergeSetList[i]->typeString == QString::null )
414
BibTeX::Entry *firstEntry = *m_mergeSetList[i]->entries.begin();
415
if ( firstEntry->entryType() == BibTeX::Entry::etUnknown )
416
newEntry = new BibTeX::Entry( firstEntry->entryTypeString(), id );
418
newEntry = new BibTeX::Entry( firstEntry->entryType(), id );
421
newEntry = new BibTeX::Entry( m_mergeSetList[i]->typeString, id );
424
newEntry = new BibTeX::Entry( m_mergeSetList[i]->type, id );
426
for ( QValueList<BibTeX::EntryField*>::Iterator it = m_mergeSetList[i]->fields.begin(); it != m_mergeSetList[i]->fields.end(); ++it )
428
newEntry->addField( new BibTeX::EntryField( *it ) );
431
for ( QValueList<BibTeX::Entry*>::Iterator it = m_mergeSetList[i]->entries.begin(); it != m_mergeSetList[i]->entries.end(); ++it )
432
for ( QValueList<BibTeX::EntryField*>::ConstIterator fIt = ( *it )->begin(); fIt != ( *it )->end(); ++fIt )
433
if ( newEntry->getField(( *fIt )->fieldTypeName() ) == NULL )
435
newEntry->addField( new BibTeX::EntryField( *fIt ) );
438
for ( QValueList<BibTeX::Entry*>::Iterator it = m_mergeSetList[i]->entries.begin(); it != m_mergeSetList[i]->entries.end(); ++it )
440
BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( bibTeXFile->containsKey(( *it )->id() ) );
442
bibTeXFile->deleteElement( entry );
445
BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( otherBibTeXFile->containsKey(( *it )->id() ) );
447
otherBibTeXFile->deleteElement( entry );
450
bibTeXFile->appendElement( newEntry );
452
else if ( !m_mergeSetList[i]->macros.isEmpty() )
454
BibTeX::Macro *newMacro = new BibTeX::Macro( m_mergeSetList[i]->macroKey );
455
newMacro->setValue( m_mergeSetList[i]->macroValue );
457
for ( QValueList<BibTeX::Macro*>::Iterator it = m_mergeSetList[i]->macros.begin(); it != m_mergeSetList[i]->macros.end(); ++it )
459
bibTeXFile->deleteElement( *it );
460
if ( otherBibTeXFile != NULL )
461
otherBibTeXFile->deleteElement( *it );
464
bibTeXFile->appendElement( newMacro );
466
else if ( !m_mergeSetList[i]->preambles.isEmpty() )
468
BibTeX::Preamble *newPreamble = new BibTeX::Preamble( m_mergeSetList[i]->preambleText );
470
for ( QValueList<BibTeX::Preamble*>::Iterator it = m_mergeSetList[i]->preambles.begin(); it != m_mergeSetList[i]->preambles.end(); ++it )
472
bibTeXFile->deleteElement( *it );
473
if ( otherBibTeXFile != NULL )
474
otherBibTeXFile->deleteElement( *it );
477
bibTeXFile->appendElement( newPreamble );
482
int MergeElements::mergeDuplicates( BibTeX::File *bibTeXFile )
484
setCaption( i18n( "Find Duplicates" ) );
485
Settings * settings = Settings::self( NULL );
486
int sensitivity = ( int )( FindDuplicates::maxDistance / exp( log( 10 ) * settings->editing_findDuplicatesSensitivity / 10.0 ) );
487
qDebug( "sensitivity= %i / %i", sensitivity, FindDuplicates::maxDistance );
488
FindDuplicates findDuplicates( m_duplicateCliqueList, sensitivity, bibTeXFile, parentWidget( true ) );
490
if ( m_duplicateCliqueList.isEmpty() )
492
KMessageBox::information( parentWidget( true ), i18n( "No duplicates found." ), i18n( "Find Duplicates" ) );
493
return QDialog::Rejected;
496
m_mergeSetList = new MergeSet*[m_duplicateCliqueList.size()];
497
memset( m_mergeSetList, 0, sizeof( MergeSet* )*m_duplicateCliqueList.size() );
499
qDebug( "%i cliques", m_duplicateCliqueList.size() );
503
if ( result == QDialog::Accepted )
504
applyMergeSet( bibTeXFile );
506
delete[] m_mergeSetList;
511
void MergeElements::slotRefreshAlternatives()
513
QMap<BibTeX::EntryField::FieldType, MergeEntriesAlternativesController*> mapFieldToController;
514
QMap<BibTeX::EntryField::FieldType, First> firstEntryData;
516
MergePreambleAlternativesController* preambleController = NULL;
517
MergeMacrosAlternativesController* macroKeyController = NULL;
518
MergeMacrosAlternativesController* macroValueController = NULL;
519
MergeEntriesAlternativesController *idController = NULL;
520
MergeEntriesAlternativesController *typeController = NULL;
521
QString firstId = QString::null;
522
QString firstMacroKey = QString::null;
523
BibTeX::Value *firstMacroValue = NULL;
524
QString firstPreambleText = QString::null;
525
BibTeX::Entry::EntryType firstType = BibTeX::Entry::etUnknown;
526
QString firstTypeString = QString::null;
528
m_listViewAlternatives->clear();
530
for ( QListViewItemIterator it( m_listViewClique, QListViewItemIterator::Checked ); it.current(); ++it )
532
MergeElementsCliqueItem *meci = dynamic_cast<MergeElementsCliqueItem*>( *it );
533
BibTeX::Entry *entry = NULL;
534
BibTeX::Macro *macro = NULL;
535
BibTeX::Preamble *preamble = NULL;
536
if ( meci != NULL && ( entry = meci->entry ) != NULL )
540
firstId = entry->id();
541
firstType = entry->entryType();
542
firstTypeString = entry->entryTypeString();
543
for ( BibTeX::Entry::EntryFields::const_iterator efi = entry->begin(); efi != entry->end(); ++efi )
548
firstEntryData.insert(( *efi )->fieldType(), first );
553
if ( idController == NULL )
555
if ( entry->id() != firstId )
557
idController = new MergeEntriesAlternativesController( i18n( "Id" ), m_listViewAlternatives );
558
idController->setOpen( true );
559
MergeElementsAlternativesId *item = new MergeElementsAlternativesId( firstId, idController );
561
new MergeElementsAlternativesId( entry->id(), idController );
566
QString thisText = entry->id();
568
for ( QListViewItem *cur = idController->firstChild(); isNew && cur != NULL; cur = cur->nextSibling() )
570
MergeElementsAlternativesId *meai = dynamic_cast<MergeElementsAlternativesId*>( cur );
571
isNew = meai->id != thisText;
574
new MergeElementsAlternativesId( thisText, idController );
577
if ( typeController == NULL )
579
if (( firstType != BibTeX::Entry::etUnknown && entry->entryType() != firstType ) || ( entry->entryTypeString().lower() != entry->entryTypeString().lower() ) )
581
typeController = new MergeEntriesAlternativesController( i18n( "Type" ), m_listViewAlternatives );
582
typeController->setOpen( true );
583
MergeEntriesAlternativesEntryType *item = firstType != BibTeX::Entry::etUnknown ? new MergeEntriesAlternativesEntryType( firstType, typeController ) : new MergeEntriesAlternativesEntryType( firstTypeString, typeController );
585
if ( entry->entryType() != BibTeX::Entry::etUnknown )
586
new MergeEntriesAlternativesEntryType( entry->entryType(), typeController );
588
new MergeEntriesAlternativesEntryType( entry->entryTypeString(), typeController );
593
QString typeString = entry->entryTypeString();
594
BibTeX::Entry::EntryType type = entry->entryType();
596
for ( QListViewItem *cur = typeController->firstChild(); isNew && cur != NULL; cur = cur->nextSibling() )
598
MergeEntriesAlternativesEntryType *meat = dynamic_cast<MergeEntriesAlternativesEntryType*>( cur );
599
isNew = type == BibTeX::Entry::etUnknown && meat->typeString != typeString || meat->type != type;
603
if ( type != BibTeX::Entry::etUnknown )
604
new MergeEntriesAlternativesEntryType( type, typeController );
606
new MergeEntriesAlternativesEntryType( typeString, typeController );
610
for ( BibTeX::Entry::EntryFields::const_iterator efi = entry->begin(); efi != entry->end(); ++efi )
611
if ( mapFieldToController.contains(( *efi )->fieldType() ) )
613
MergeEntriesAlternativesController *controller = mapFieldToController[( *efi )->fieldType()];
614
QString thisText = ( *efi )->value()->text();
616
for ( QListViewItem *cur = controller->firstChild(); isNew && cur != NULL; cur = cur->nextSibling() )
618
MergeEntriesAlternativesItem *meai = dynamic_cast<MergeEntriesAlternativesItem*>( cur );
619
isNew = meai->field->value()->text() != thisText;
622
new MergeEntriesAlternativesItem( *efi, controller );
624
else if ( firstEntryData.contains(( *efi )->fieldType() ) )
626
QString firstText = firstEntryData[( *efi )->fieldType()].field->value()->text();
627
QString thisText = ( *efi )->value()->text();
628
if ( firstText != thisText )
630
MergeEntriesAlternativesController *controller = new MergeEntriesAlternativesController(( *efi )->fieldType(), m_listViewAlternatives );
631
controller->setOpen( true );
632
MergeEntriesAlternativesItem *item = new MergeEntriesAlternativesItem( firstEntryData[( *efi )->fieldType()].field, controller );
634
item = new MergeEntriesAlternativesItem( *efi, controller );
635
mapFieldToController.insert(( *efi )->fieldType(), controller );
643
firstEntryData.insert(( *efi )->fieldType(), first );
647
else if ( meci != NULL && ( macro = meci->macro ) != NULL )
651
firstMacroKey = macro->key();
652
firstMacroValue = macro->value();
656
if ( macroKeyController == NULL )
658
if ( macro->key() != firstMacroKey )
660
macroKeyController = new MergeMacrosAlternativesController( true, m_listViewAlternatives );
661
macroKeyController->setOpen( true );
662
MergeMacroAlternativesKey *item = new MergeMacroAlternativesKey( firstMacroKey, macroKeyController );
664
new MergeMacroAlternativesKey( macro->key(), macroKeyController );
669
QString thisText = macro->key();
671
for ( QListViewItem *cur = macroKeyController->firstChild(); isNew && cur != NULL; cur = cur->nextSibling() )
673
MergeMacroAlternativesKey *mak = dynamic_cast<MergeMacroAlternativesKey*>( cur );
674
isNew = mak->key != thisText;
677
new MergeMacroAlternativesKey( thisText, macroKeyController );
681
if ( macroValueController == NULL )
683
if ( firstMacroValue->text() != macro->value()->text() )
685
macroValueController = new MergeMacrosAlternativesController( false, m_listViewAlternatives );
686
macroValueController->setOpen( true );
687
MergeMacrosAlternativesItem *item = new MergeMacrosAlternativesItem( firstMacroValue, macroValueController );
689
new MergeMacrosAlternativesItem( macro->value(), macroValueController );
694
QString macroString = macro->value()->text();
696
for ( QListViewItem *cur = macroValueController->firstChild(); isNew && cur != NULL; cur = cur->nextSibling() )
698
MergeMacrosAlternativesItem *mai = dynamic_cast<MergeMacrosAlternativesItem*>( cur );
699
isNew = macroString != mai->value->text();
702
new MergeMacrosAlternativesItem( macro->value(), macroValueController );
705
else if ( meci != NULL && ( preamble = meci->preamble ) != NULL )
708
firstPreambleText = preamble->value()->text();
711
if ( preambleController == NULL )
713
if ( preamble->value()->text() != firstPreambleText )
715
preambleController = new MergePreambleAlternativesController( m_listViewAlternatives );
716
preambleController->setOpen( true );
717
MergePreambleAlternatives *item = new MergePreambleAlternatives( firstPreambleText, preambleController );
719
new MergePreambleAlternatives( preamble->value()->text(), preambleController );
724
QString thisText = preamble->value()->text();
726
for ( QListViewItem *cur = preambleController->firstChild(); isNew && cur != NULL; cur = cur->nextSibling() )
728
MergePreambleAlternatives *mpa = dynamic_cast<MergePreambleAlternatives*>( cur );
729
isNew = mpa->text != thisText;
732
new MergePreambleAlternatives( thisText, preambleController );
740
void MergeElements::slotNextClique()
742
if ( m_currentCliqueIndex < ( int )( m_duplicateCliqueList.size() ) - 1 )
744
setClique( m_currentCliqueIndex + 1 );
747
enableButton( User1, false );
750
void MergeElements::slotPreviousClique()
752
if ( m_currentCliqueIndex > 0 )
754
setClique( m_currentCliqueIndex - 1 );
757
enableButton( User2, false );
760
void MergeElements::slotPreviewElement( QListViewItem *item )
762
MergeElementsCliqueItem *meci = dynamic_cast<MergeElementsCliqueItem*>( item );
763
if ( meci != NULL && meci->entry != NULL )
764
KBibTeX::EntryWidget::execute( meci->entry, NULL, TRUE, FALSE, this );
765
else if ( meci != NULL && meci->macro != NULL )
766
KBibTeX::MacroWidget::execute( meci->macro, TRUE, this );
767
else if ( meci!=NULL&& meci->preamble!=NULL )
768
KBibTeX::PreambleWidget::execute( meci->preamble, TRUE, this );
771
/* This function was taken form KMainWindow of KDE 3.5 and modified to fit KBibTeX */
772
void MergeElements::saveWindowSize( KConfig *config ) const
774
int scnum = QApplication::desktop()->screenNumber( parentWidget() );
775
QRect desk = QApplication::desktop()->screenGeometry( scnum );
778
// save maximalization as desktop size + 1 in that direction
779
KWin::WindowInfo info = KWin::windowInfo( winId(), NET::WMState );
780
w = info.state() & NET::MaxHoriz ? desk.width() + 1 : width();
781
h = info.state() & NET::MaxVert ? desk.height() + 1 : height();
785
w = desk.width() + 1;
786
h = desk.height() + 1;
788
//TODO: add "Maximized" property instead "+1" hack
790
QRect size( desk.width(), w, desk.height(), h );
791
bool defaultSize = false;//( size == d->defaultWindowSize );
792
QString widthString = QString::fromLatin1( "Width %1" ).arg( desk.width() );
793
QString heightString = QString::fromLatin1( "Height %1" ).arg( desk.height() );
794
if ( !config->hasDefault( widthString ) && defaultSize )
795
config->revertToDefault( widthString );
797
config->writeEntry( widthString, w );
799
if ( !config->hasDefault( heightString ) && defaultSize )
800
config->revertToDefault( heightString );
802
config->writeEntry( heightString, h );
805
void MergeElements::showEvent( QShowEvent * )
807
qDebug( "showEvent" );
808
QTimer::singleShot( 10, this, SLOT( slotRestore() ) );
811
void MergeElements::slotRestore()
813
qDebug( "slotRestore" );
814
KConfig * config = kapp->config();
815
config->setGroup( "MergeElements" );
816
restoreWindowSize( config );
819
/* This function was taken form KMainWindow of KDE 3.5 and modified to fit KBibTeX */
820
void MergeElements::restoreWindowSize( KConfig *config )
823
int scnum = QApplication::desktop()->screenNumber( parentWidget() );
824
QRect desk = QApplication::desktop()->screenGeometry( scnum );
825
QSize size( config->readNumEntry( QString::fromLatin1( "Width %1" ).arg( desk.width() ), 0 ),
826
config->readNumEntry( QString::fromLatin1( "Height %1" ).arg( desk.height() ), 0 ) );
827
if ( size.isEmpty() )
829
// try the KDE 2.0 way
830
size = QSize( config->readNumEntry( QString::fromLatin1( "Width" ), 0 ),
831
config->readNumEntry( QString::fromLatin1( "Height" ), 0 ) );
832
if ( !size.isEmpty() )
834
// make sure the other resolutions don't get old settings
835
config->writeEntry( QString::fromLatin1( "Width" ), 0 );
836
config->writeEntry( QString::fromLatin1( "Height" ), 0 );
839
if ( !size.isEmpty() )
842
int state = ( size.width() > desk.width() ? NET::MaxHoriz : 0 )
843
| ( size.height() > desk.height() ? NET::MaxVert : 0 );
844
if (( state & NET::Max ) == NET::Max )
846
else if (( state & NET::MaxHoriz ) == NET::MaxHoriz )
847
resize( width(), size.height() );
848
else if (( state & NET::MaxVert ) == NET::MaxVert )
849
resize( size.width(), height() );
852
// QWidget::showMaximized() is both insufficient and broken
853
KWin::setState( winId(), state );
855
if ( size.width() > desk.width() || size.height() > desk.height() )
856
setWindowState( WindowMaximized );
864
#include "mergeelements.moc"