64
68
DocumentListView::DocumentListView( KBibTeX::DocumentWidget *docWidget, bool isReadOnly, QWidget *parent, const char *name )
65
: KListView( parent, name ), m_docWidget( docWidget ), m_contextMenu( NULL ), m_headerMenu( NULL ), m_isReadOnly( isReadOnly ), m_newElementCounter( 1 )
69
: KListView( parent, name ), m_docWidget( docWidget ), m_bibtexFile( NULL ), m_contextMenu( NULL ), m_headerMenu( NULL ), m_isReadOnly( isReadOnly ), m_newElementCounter( 1 )
67
71
setAllColumnsShowFocus( true );
68
72
setShowSortIndicator( true );
69
73
setSelectionMode( QListView::Extended );
70
header() ->setClickEnabled ( TRUE );
74
header() ->setClickEnabled( TRUE );
71
75
header() ->setMovingEnabled( TRUE );
74
78
// setDragEnabled ( true );
75
79
// setDragAutoScroll( true );
76
80
setAcceptDrops( TRUE );
77
setDropVisualizer ( TRUE );
81
setDropVisualizer( TRUE );
79
83
connect( header(), SIGNAL( clicked( int ) ), this, SLOT( setSortingColumn( int ) ) );
80
84
connect( this, SIGNAL( contextMenu( KListView *, QListViewItem *, const QPoint & ) ), this, SLOT( showBibtexListContextMenu( KListView *, QListViewItem *, const QPoint & ) ) );
108
112
void DocumentListView::setItems()
110
114
QApplication::setOverrideCursor( Qt::waitCursor );
111
setUpdatesEnabled( FALSE );
116
KProgressDialog *prgDlg = new KProgressDialog( this, "prgDlg", i18n( "Updating" ), i18n( "Updating main view" ), TRUE );
118
KProgress *progress = prgDlg->progressBar();
119
progress->setTotalSteps( m_bibtexFile->count() );
121
bool update = viewport()->isUpdatesEnabled();
122
viewport()->setUpdatesEnabled( FALSE );
114
125
for ( unsigned int i = 0; i < m_bibtexFile->count(); i++ )
116
127
BibTeX::Element *element = m_bibtexFile->at( i );
117
QListViewItem *item = new DocumentListViewItem( element, this );
128
QListViewItem *item = new DocumentListViewItem( m_bibtexFile, element, this );
118
129
item->setVisible( m_filter.isEmpty() || element->containsPattern( m_filter, m_filterFieldType ) );
130
progress->setProgress( i );
133
kapp->processEvents();
122
setUpdatesEnabled( TRUE );
136
//setUpdatesEnabled( TRUE );
137
viewport()->setUpdatesEnabled( update );
124
141
QApplication::restoreOverrideCursor();
128
144
bool DocumentListView::insertItems( BibTeX::File *items, KBibTeX::DocumentListViewItem *after )
146
if ( m_bibtexFile == NULL )
147
m_bibtexFile = new BibTeX::File();
130
149
for ( BibTeX::File::ElementList::iterator it = items->begin(); it != items->end(); it++ )
132
151
BibTeX::Element *element = BibTeX::File::cloneElement( *it );
134
153
m_bibtexFile->appendElement( element, after == NULL ? NULL : after->element() );
135
after = new DocumentListViewItem( element, this, after );
154
after = new DocumentListViewItem( m_bibtexFile, element, this, after );
136
155
after->setUnreadStatus( TRUE );
137
156
m_unreadItems.append( after );
198
217
m_headerMenu->setCheckable( TRUE );
199
218
connect( m_headerMenu, SIGNAL( activated( int ) ), this, SLOT( activateShowColumnMenu( int ) ) );
201
Settings * settings = Settings::self();
220
Settings * settings = Settings::self( m_bibtexFile );
203
222
int item = m_headerMenu->insertItem( i18n( "Element Type" ), 0 );
204
223
m_headerMenu->setItemChecked( item, settings->editing_MainListColumnsWidth[ 0 ] > 0 );
207
226
for ( int i = 0; i <= ( int ) BibTeX::EntryField::ftYear - ( int ) BibTeX::EntryField::ftAbstract; i++ )
209
BibTeX::EntryField::FieldType fieldType = ( BibTeX::EntryField::FieldType ) ( i + ( int ) BibTeX::EntryField::ftAbstract );
228
BibTeX::EntryField::FieldType fieldType = ( BibTeX::EntryField::FieldType )( i + ( int ) BibTeX::EntryField::ftAbstract );
210
229
QString label = Settings::fieldTypeToI18NString( fieldType );
211
230
item = m_headerMenu->insertItem( label, ( int ) fieldType + 2 );
212
231
m_headerMenu->setItemChecked( item, settings->editing_MainListColumnsWidth[ i + 2 ] > 0 );
269
291
QListViewItem * lvi = selectedItem();
270
292
if ( lvi == NULL )
271
293
lvi = currentItem();
273
294
DocumentListViewItem * dlvi = NULL;
274
295
if ( lvi != NULL )
275
296
dlvi = dynamic_cast<KBibTeX::DocumentListViewItem *>( lvi );
277
BibTeX::FileImporter *importer = NULL;
278
if ( BibTeX::FileImporterBibTeX::guessCanDecode( kapp->clipboard() ->text() ) )
279
importer = new BibTeX::FileImporterBibTeX();
283
// Decoding the paste text as bibtex failed. Maybe the user wants to paste the text as
284
// link address, abstract, etc...
298
QString clipboardText = kapp->clipboard() ->text();
299
/** check if clipboard contains BibTeX content */
300
if ( BibTeX::FileImporterBibTeX::guessCanDecode( clipboardText ) )
302
Settings * settings = Settings::self( m_bibtexFile );
303
BibTeX::FileImporter *importer = new BibTeX::FileImporterBibTeX( settings->editing_FirstNameFirst );
304
BibTeX::File *clipboardData = importer->load( clipboardText );
307
if ( clipboardData != NULL && clipboardData->count() > 0 )
309
bool result = insertItems( clipboardData, dlvi );
310
delete clipboardData;
318
/** Decoding the paste text as bibtex failed. Maybe the user wants
319
to paste the text as link address, abstract, etc... */
320
if ( !dlvi ) // no list view item selected to add data to
323
// fetch BibTeX element from current list view item
287
324
BibTeX::Entry * element = dynamic_cast<BibTeX::Entry*>( dlvi->element() );
290
329
KPopupMenu * popup = new KPopupMenu( this, "pastePopup" );
291
330
popup->insertTitle( i18n( "Paste text as..." ) );
292
for ( int i = 0; i <= ( int ) BibTeX::EntryField::ftYear; i++ )
331
for ( int i = ( int ) BibTeX::EntryField::ftAuthor; i <= ( int ) BibTeX::EntryField::ftYear; i++ )
294
BibTeX::EntryField::FieldType ft;
295
ft = ( BibTeX::EntryField::FieldType ) i;
333
BibTeX::EntryField::FieldType ft = ( BibTeX::EntryField::FieldType ) i;
296
334
popup->insertItem( Settings::fieldTypeToI18NString( ft ), i );
298
336
popup->insertSeparator();
299
337
QIconSet cancelPixmap = KGlobal::iconLoader() ->loadIconSet( "cancel", KIcon::Small );
300
338
int cancelId = popup->insertItem( cancelPixmap, i18n( "Cancel" ) );
301
341
int selectedId = popup->exec( QCursor::pos() );
302
342
if ( selectedId == cancelId || selectedId == -1 )
343
return FALSE; // cancel menu
345
// determine field to add clipboard value to
304
346
BibTeX::EntryField::FieldType fieldType = ( BibTeX::EntryField::FieldType ) selectedId;
305
347
BibTeX::EntryField * field = element->getField( fieldType );
308
350
field = new BibTeX::EntryField( fieldType );
309
351
element->addField( field );
311
353
else if ( field->value() != NULL )
312
354
delete field->value();
314
BibTeX::Value * value = NULL;
356
clipboardText = BibTeX::EncoderLaTeX::currentEncoderLaTeX() ->encode( clipboardText );
358
// create new value from clipboard's content
359
BibTeX::Value * value = new BibTeX::Value();
315
360
if ( fieldType == BibTeX::EntryField::ftAuthor || fieldType == BibTeX::EntryField::ftEditor )
316
value = new BibTeX::ValuePersons();
362
Settings * settings = Settings::self( m_bibtexFile );
363
value->items.append( new BibTeX::PersonContainer( clipboardText, settings->editing_FirstNameFirst ) );
365
else if ( fieldType == BibTeX::EntryField::ftKeywords )
366
value->items.append( new BibTeX::KeywordContainer( clipboardText ) );
318
value = new BibTeX::Value();
319
QString stringValue = kapp->clipboard() ->text();
320
// FIXME: I assume that the encoding is correct here; i am not sure if
321
// this is ok; maybe I need a construct like in
322
// BibTeXFileImporterBibTeX::load() here;
323
// maybe Thomas Fischer knows if this needs fixing...
324
// TF: I assume that all clipboard content is UTF-8 ...
325
stringValue = BibTeX::EncoderLaTeX::currentEncoderLaTeX() ->encode( stringValue );
368
value->items.append( new BibTeX::PlainText( clipboardText ) );
327
value->add( new BibTeX::ValueItem( stringValue, FALSE ) );
328
370
field->setValue( value );
332
BibTeX::File *clipboardData = importer->load( kapp->clipboard() ->text() );
335
if ( clipboardData != NULL )
336
return insertItems( clipboardData, dlvi );
376
void DocumentListView::sendSelectedToLyx()
378
QStringList refsToSend;
379
QListViewItemIterator it( this, QListViewItemIterator::Selected );
380
while ( it.current() )
382
DocumentListViewItem * kblvi = dynamic_cast<DocumentListViewItem*>( it.current() );
383
BibTeX::Entry *entry = dynamic_cast<BibTeX::Entry*>( kblvi->element() );
384
if ( entry != NULL && kblvi->isVisible() )
385
refsToSend.append( entry->id() );
389
Settings * settings = Settings::self( m_bibtexFile );
390
QString lyxPipeFilename = settings->detectLyXInPipe();
391
kdDebug() << "sendSelectedToLyx: lyxPipeFilename= " << lyxPipeFilename << endl;
392
QFile pipe( lyxPipeFilename );
393
if ( pipe.exists() && pipe.open( IO_WriteOnly ) )
395
QTextStream * writer = new QTextStream( &pipe );
396
QString msg = "LYXCMD:kbibtex:citation-insert:" + refsToSend.join( "," );
397
*writer << msg << endl;
402
KMessageBox::error( this, ( lyxPipeFilename.isEmpty() ? i18n( "Cannot establish a link to LyX" ) : QString( i18n( "Cannot establish a link to LyX using the pipe \"%1\"" ) ).arg( lyxPipeFilename ) ) + i18n( "\nMaybe LyX is not running?" ), i18n( "Error communicating with LyX" ) );
341
405
void DocumentListView::slotDoubleClick( QListViewItem *item )
343
Settings * settings = Settings::self();
407
Settings * settings = Settings::self( m_bibtexFile );
344
408
bool openingDocumentOK = FALSE;
346
410
if ( settings->editing_MainListDoubleClickAction == 1 )
413
477
BibTeX::Macro* macro = dynamic_cast<BibTeX::Macro*>( lvi->element() );
415
480
dialogResult = KBibTeX::MacroWidget::execute( macro, m_isReadOnly );
416
if ( dialogResult == QDialog::Accepted )
481
if ( dialogResult == QDialog::Accepted )
486
BibTeX::Preamble* preamble = dynamic_cast<BibTeX::Preamble*>( lvi->element() );
489
dialogResult = KBibTeX::PreambleWidget::execute( preamble, m_isReadOnly );
490
if ( dialogResult == QDialog::Accepted )
468
544
void DocumentListView::setSortingColumn( int column )
470
Settings * settings = Settings::self();
546
Settings * settings = Settings::self( m_bibtexFile );
471
547
settings->editing_MainListSortingColumn = column;
472
548
settings->editing_MainListSortingOrder = ( sortOrder() == Qt::Ascending ) ? 1 : -1;
475
551
bool DocumentListView::acceptDrag( QDropEvent * event ) const
477
return QTextDrag::canDecode( event ) || QUriDrag::canDecode( event );;
553
return QTextDrag::canDecode( event ) || QUriDrag::canDecode( event );
480
void DocumentListView::saveColumnIndex( int col )
556
void DocumentListView::saveColumnIndex()
482
Settings * settings = Settings::self();
558
Settings * settings = Settings::self( m_bibtexFile );
483
559
QHeader *hdr = header();
485
int from = col == -1 ? 0 : col, to = col == -1 ? columns() : ( col + 1 );
487
for ( int i = from; i < to; i++ )
561
for ( int i = 0; i < columns(); i++ )
489
562
settings->editing_MainListColumnsIndex[ i ] = hdr->mapToIndex( i );
493
565
void DocumentListView::restoreColumnIndex()
495
Settings * settings = Settings::self();
567
Settings * settings = Settings::self( m_bibtexFile );
496
568
QHeader *hdr = header();
498
570
for ( int i = 0; i < columns(); i++ )
549
621
QStrList urlList;
551
if ( QUriDrag::decode( event, urlList ) )
623
if ( QTextDrag::decode( event, text ) && KURL( text ).isValid() )
624
urlList.append( text );
626
if ( !urlList.isEmpty() || QUriDrag::decode( event, urlList ) )
553
QString url = urlList.at ( 0 );
628
QString url = urlList.at( 0 );
555
630
if ( ! KIO::NetAccess::download( url, tmpFile, 0 ) )
568
643
text = QString( ba );
570
645
KIO::NetAccess::removeTempFile( tmpFile );
573
else if ( ! QTextDrag::decode( event, text ) )
647
else if ( !QTextDrag::decode( event, text ) )
576
650
event->accept( TRUE );
577
651
DocumentListViewItem * dlvi = dynamic_cast<KBibTeX::DocumentListViewItem *>( item );
578
652
BibTeX::FileImporter *importer = NULL;
579
653
if ( BibTeX::FileImporterBibTeX::guessCanDecode( text ) )
580
importer = new BibTeX::FileImporterBibTeX();
655
Settings * settings = Settings::self( m_bibtexFile );
656
importer = new BibTeX::FileImporterBibTeX( settings->editing_FirstNameFirst );
581
658
// else if (BibTeX::FileImporterExternal::guessCanDecode( text ))
582
659
// importer = new BibTeX::FileImporterExternal();