Logo Search packages:      
Sourcecode: kbibtex version File versions  Download package

documentwidget.cpp

/***************************************************************************
*   Copyright (C) 2004-2006 by Thomas Fischer                             *
*   fischer@unix-ag.uni-kl.de                                             *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include <qlayout.h>
#include <qfile.h>
#include <qdragobject.h>
#include <qregexp.h>
#include <qsplitter.h>
#include <qtextedit.h>
#include <qprogressdialog.h>
#include <qbuffer.h>
#include <qtooltip.h>
#include <qapplication.h>
#include <qpushbutton.h>

#include <klocale.h>
#include <ktempfile.h>
#include <kglobalsettings.h>
#include <kurl.h>
#include <kpopupmenu.h>
#include <kiconloader.h>
#include <kio/netaccess.h>
#include <kstandarddirs.h>
#include <kmessagebox.h>
#include <kapplication.h>
#include <kdebug.h>
#include <ktextedit.h>

#include <searchbar.h>
#include <sidebar.h>
#include <documentlistview.h>
#include <documentlistviewitem.h>
#include <documentsourceview.h>
#include <macrowidget.h>
#include <commentwidget.h>
#include <entrywidget.h>
#include <fileimporterbibtex.h>
#include <fileexporterbibtex.h>
#include <fileexporterxml.h>
#include <fileexporterrtf.h>
#include <fileexporterpdf.h>
#include <fileexporterps.h>
#include <fileexporterxslt.h>
#include <fileimporterexternal.h>
#include <fileexporterexternal.h>
#include <xsltransform.h>
#include <entry.h>
#include <comment.h>
#include <macro.h>
#include <settings.h>
#include <webquery.h>

#include "documentwidget.h"

namespace KBibTeX
{

    DocumentWidget::DocumentWidget( bool isReadOnly, QWidget *parent, const char *name )
            : QTabWidget( parent, name ), m_isReadOnly( isReadOnly ), m_progressDialog( NULL ), m_newElementCounter( 1 ), m_editMode( emSource ), m_viewDocumentActionMenu( NULL ), m_searchWebsitesActionMenu( NULL ), m_find( NULL ), m_findNext( NULL ), m_copyReference( NULL )
    {
        m_bibtexfile = new BibTeX::File();

        setupGUI();
    }

    DocumentWidget::~DocumentWidget()
    {
        delete m_bibtexfile;
    }

    void DocumentWidget::setupGUI()
    {
        setAcceptDrops( TRUE );
        setFocusPolicy( QWidget::ClickFocus );

        // List view tab widget ===============================

        m_container = new QWidget( this );
        QVBoxLayout *layout = new QVBoxLayout( m_container, 0, 0 );
        addTab( m_container, i18n( "L&ist view" ) );

        m_searchBar = new SearchBar( m_container );
        layout->addWidget( m_searchBar );

        m_horSplitter = new QSplitter( Qt::Horizontal, m_container );
        layout->addWidget( m_horSplitter );
        m_horSplitter->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );

        m_sideBar = new SideBar( m_horSplitter );
        m_vertSplitter = new QSplitter( Qt::Vertical, m_horSplitter );
        m_vertSplitter->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );

        m_listViewElements = new DocumentListView( this, m_isReadOnly, m_vertSplitter );

        m_preview = new KTextEdit( m_vertSplitter );
        m_preview->setReadOnly( TRUE );
        m_preview->setAlignment( Qt::AlignTop && Qt::AlignLeft );

        // Source view tab widget =============================

        m_sourceView = new DocumentSourceView( this, m_isReadOnly, this , "source_view" );
        addTab( m_sourceView, i18n( "So&urce view" ) );
        m_sourceView->setFont( KGlobalSettings::fixedFont() );

        // Setup actions
        connect( m_searchBar, SIGNAL( doSearch( const QString&, BibTeX::EntryField::FieldType ) ), m_listViewElements, SLOT( filterText( const QString&, BibTeX::EntryField::FieldType ) ) );
        connect ( m_listViewElements, SIGNAL( selectionChanged() ), this, SLOT( slotSelectionChanged() ) );
        connect( m_listViewElements, SIGNAL( currentChanged( QListViewItem* ) ), this, SLOT( slotPreviewElement( QListViewItem* ) ) );
        connect( m_listViewElements, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( slotPreviewElement( QListViewItem* ) ) );
        connect( this, SIGNAL( currentChanged ( QWidget * ) ), this, SLOT( slotTabChanged( QWidget* ) ) );
        connect( m_sourceView, SIGNAL( modified() ), this, SLOT( slotModified() ) );
        connect( m_listViewElements, SIGNAL( modified() ), this, SLOT( slotModified() ) );
        connect( m_sideBar, SIGNAL( selected( const QString& , BibTeX::EntryField::FieldType ) ), m_searchBar, SLOT( setSearch( const QString& , BibTeX::EntryField::FieldType ) ) );
    }


    bool DocumentWidget::open( const QString &fileName, bool insertOnly )
    {
        bool result = FALSE;

        BibTeX::File::FileFormat format = BibTeX::File::formatUndefined;
        if ( fileName.endsWith( ".bib" ) )
            format = BibTeX::File::formatBibTeX;
        else
            format = BibTeX::File::formatUndefined;

        if ( format != BibTeX::File::formatUndefined )
        {
            BibTeX::FileImporter * importer = NULL;
            switch ( format )
            {
            case BibTeX::File::formatBibTeX:
                importer = new BibTeX::FileImporterBibTeX();
                break;
            default:
                {
                    // ignore, as other formats cannot be imported
                }
            }

            if ( importer != NULL )
            {
                QFile file( fileName );
                if ( file.open( IO_ReadOnly ) )
                {
                    result = open( &file, insertOnly, QString( i18n( "<qt>Loading file <b>%1</b></qt>" ) ).arg( fileName ), importer );
                    file.close();
                }
                delete importer;
            }
        }

        return result;
    }

    bool DocumentWidget::open( QIODevice *iodevice, bool insertOnly, const QString &label, BibTeX::FileImporter *importer )
    {
        bool result = FALSE;
        setEnabled( FALSE );

        bool usingDefaultImporter = importer == NULL;
        if ( usingDefaultImporter )
            importer = new BibTeX::FileImporterBibTeX();

        startProgress( label, importer );
        BibTeX::File *newFile = importer->load( iodevice );
        endProgress( importer );

        if ( usingDefaultImporter )
            delete importer;

        if ( newFile != NULL )
        {
            if ( insertOnly )
            {
                m_bibtexfile->append( newFile, NULL );
                delete newFile;
            }
            else
            {
                delete m_bibtexfile;
                m_bibtexfile = newFile;
            }

            m_listViewElements->setBibTeXFile( m_bibtexfile );
            m_sourceView->setBibTeXFile( m_bibtexfile );

            Settings * settings = Settings::self();
            settings->addToCompletion( m_bibtexfile );
            m_sideBar->refreshLists( m_bibtexfile );

            result = TRUE;
        }
        else
        {
            kdDebug() << "Could not load bibliography file from io device" << endl;
        }

        setEnabled( TRUE );
        return result;
    }

    bool DocumentWidget::save( const QString &fileName, QStringList *errorLog )
    {
        bool result = FALSE;

        BibTeX::File::FileFormat format = BibTeX::File::formatUndefined;
        if ( fileName.endsWith( ".rtf", FALSE ) )
            format = BibTeX::File::formatRTF;
        else if ( fileName.endsWith( ".pdf", FALSE ) )
            format = BibTeX::File::formatPDF;
        else if ( fileName.endsWith( ".bib", FALSE ) )
            format = BibTeX::File::formatBibTeX;
        else if ( fileName.endsWith( ".ps", FALSE ) )
            format = BibTeX::File::formatPS;
        else if ( fileName.endsWith( ".xml", FALSE ) )
            format = BibTeX::File::formatXML;
        else if ( fileName.endsWith( ".html", FALSE ) || fileName.endsWith( ".xhtml", FALSE ) || fileName.endsWith( ".htm", FALSE ) )
            format = BibTeX::File::formatHTML;
        if ( format == BibTeX::File::formatUndefined )
            return FALSE;

        QFile file( fileName );
        if ( file.open( IO_WriteOnly ) )
        {
            result = save( &file, format, QString( i18n( "<qt>Writing file <b>%1</b></qt>" ) ).arg( fileName ), errorLog );
            file.close();
        }

        return result;
    }

    bool DocumentWidget::save( QIODevice *iodevice, BibTeX::File::FileFormat format, const QString &label, QStringList *errorLog )
    {
        Settings * settings = Settings::self();

        bool result = FALSE;
        setEnabled( FALSE );

        updateFromGUI();

        BibTeX::XSLTransform * transform = NULL;

        BibTeX::FileExporter * exporter = NULL;
        switch ( format )
        {
        case BibTeX::File::formatBibTeX:
            {
                BibTeX::FileExporterBibTeX * bibtexExporter = new BibTeX::FileExporterBibTeX();
                bibtexExporter->setStringDelimiter( settings->fileIO_BibtexStringOpenDelimiter, settings->fileIO_BibtexStringCloseDelimiter );
                bibtexExporter->setEncoding( settings->fileIO_Encoding );
                exporter = bibtexExporter;
            }
            break;
        case BibTeX::File::formatXML:
            exporter = new BibTeX::FileExporterXML();
            break;
        case BibTeX::File::formatHTML:
            switch ( settings->fileIO_ExporterHTML )
            {
            case BibTeX::FileExporterExternal::exporterNone:
            case BibTeX::FileExporterExternal::exporterXSLT:
                {

                    if ( transform != NULL )
                        exporter = new BibTeX::FileExporterXSLT( transform );
                    else
                    {
                        kdDebug() << "XSLT for HTML export is not available" << endl;
                    }
                }
                break;
            default:
                {
                    exporter = new BibTeX::FileExporterExternal( settings->fileIO_ExporterHTML, BibTeX::File::formatHTML );
                }
            }
            break;
        case BibTeX::File::formatRTF:
            {
                if ( !settings->external_latex2rtfAvailable )
                {
                    QString msg = i18n( "To export a BibTeX document to the Rich Text Format (RTF) you need to have installed the program latex2rtf." ) ;
                    KMessageBox::information( this, msg );
                    errorLog->append( msg );
                    return FALSE;
                }

                BibTeX::FileExporterRTF *rtfExporter = new BibTeX::FileExporterRTF();
                rtfExporter->setLaTeXLanguage( settings->fileIO_ExportLanguage );
                rtfExporter->setLaTeXBibliographyStyle( settings->fileIO_ExportBibliographyStyle );
                exporter = rtfExporter;
            }
            break;
        case BibTeX::File::formatPDF:
            {
                BibTeX::FileExporterPDF *pdfExporter = new BibTeX::FileExporterPDF();
                pdfExporter->setLaTeXLanguage( settings->fileIO_ExportLanguage );
                pdfExporter->setLaTeXBibliographyStyle( settings->fileIO_ExportBibliographyStyle );
                exporter = pdfExporter;
            }
            break;
        case BibTeX::File::formatPS:
            {
                BibTeX::FileExporterPS *psExporter = new BibTeX::FileExporterPS();
                psExporter->setLaTeXLanguage( settings->fileIO_ExportLanguage );
                psExporter->setLaTeXBibliographyStyle( settings->fileIO_ExportBibliographyStyle );
                exporter = psExporter;
            }
            break;
        default:
            kdDebug() << "No supported export format selected" << endl;
        }

        if ( exporter != NULL )
        {
            startProgress( label, exporter );
            result = exporter->save( iodevice, m_bibtexfile, errorLog );
            endProgress( exporter );

            if ( transform != NULL )
                delete transform;
            delete exporter;
        }

        setEnabled( TRUE );
        return result;
    }

    bool DocumentWidget::newElement( const QString& elementType )
    {
        Settings * settings = Settings::self();

        if ( m_editMode == emList )
        {
            if ( elementType.lower() == "macro" )
            {
                QString name = QString( i18n( "NewMacro_%1" ) ).arg( m_newElementCounter++ );
                BibTeX::Macro *macro = new BibTeX::Macro( name );
                if ( MacroWidget::execute( macro, m_isReadOnly ) == QDialog::Accepted )
                {
                    new DocumentListViewItem( macro, m_listViewElements );
                    m_bibtexfile->appendElement( macro );
                    settings->addToCompletion( macro );
                    m_sideBar->refreshLists( m_bibtexfile );
                    return TRUE;
                }
                else
                {
                    delete macro;
                    return FALSE;
                }
            }
            else if ( elementType.lower() == "comment" )
            {
                BibTeX::Comment * comment = new BibTeX::Comment( i18n( "Put your comment here..." ) );
                if ( CommentWidget::execute( comment, m_isReadOnly ) == QDialog::Accepted )
                {
                    new DocumentListViewItem( comment, m_listViewElements );
                    m_bibtexfile->appendElement( comment );
                    return TRUE;
                }
                else
                {
                    delete comment;
                    return FALSE;
                }
            }
            else
            {
                QString name = QString( i18n( "NewEntry_%1" ) ).arg( m_newElementCounter++ );
                BibTeX::Entry *entry = new BibTeX::Entry( elementType, name );
                if ( EntryWidget::execute( entry, m_bibtexfile, m_isReadOnly ) == QDialog::Accepted )
                {
                    new DocumentListViewItem( entry, m_listViewElements );
                    m_bibtexfile->appendElement( entry );
                    settings->addToCompletion( entry );
                    m_sideBar->refreshLists( m_bibtexfile );
                    return TRUE;
                }
                else
                {
                    delete entry;
                    return FALSE;
                }
            }
        }
        else if ( m_editMode == emSource )
        {
            if ( elementType.lower() == "macro" )
            {
                QString name = QString( i18n( "NewString_%1" ) ).arg( m_newElementCounter++ );
                m_sourceView->insertLines( QString( "@string{ %1 = \"%2\" }" ).arg( name ).arg( i18n( "No text yet" ) ) );
            }
            else if ( elementType.lower() == "comment" )
            {
                m_sourceView->insertLines( i18n( "@comment{ Put your comment here... }" ) );
            }
            else
            {
                QString name = QString( i18n( "NewEntry_%1" ) ).arg( m_newElementCounter++ );
                BibTeX::Entry *entry = new BibTeX::Entry( elementType, name );

                for ( int t = 0; t < 2; t++ )
                    for ( int i = ( int ) BibTeX::EntryField::ftAbstract; i <= ( int ) BibTeX::EntryField::ftYear; i++ )
                    {
                        BibTeX::EntryField::FieldType fieldType = ( BibTeX::EntryField::FieldType ) i;
                        BibTeX::Entry::FieldRequireStatus fieldRequireStatus = BibTeX::Entry::getRequireStatus( entry->entryType(), fieldType );

                        if ( ( t == 0 && fieldRequireStatus == BibTeX::Entry::frsRequired ) || ( t == 1 && fieldRequireStatus == BibTeX::Entry::frsOptional ) )
                        {
                            BibTeX::Value * value = new BibTeX::Value();
                            value->add( new BibTeX::ValueItem( fieldRequireStatus == BibTeX::Entry::frsRequired ? i18n( "REQUIRED" ) : i18n( "optional" ), FALSE ) );
                            BibTeX::EntryField *field = new BibTeX::EntryField( fieldType );
                            field->setValue( value );
                            entry->addField( field );
                        }
                    }

                BibTeX::FileExporter * exporter = new BibTeX::FileExporterBibTeX( );
                QBuffer buffer;
                buffer.open( IO_WriteOnly );
                bool result = exporter->save( &buffer, entry );
                buffer.close();

                if ( result )
                {
                    buffer.open( IO_ReadOnly );
                    QTextStream textStream( &buffer );
                    textStream.setEncoding( QTextStream::UnicodeUTF8 );
                    QString text = textStream.read();
                    buffer.close();
                    QStringList lines = QStringList::split( '\n', text );
                    for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
                        m_sourceView->insertLines( *it );
                }

                delete exporter;
            }

            return TRUE;
        }

        return FALSE;
    }

    void DocumentWidget::updateViews()
    {
        m_listViewElements->updateVisiblity();
    }

    void DocumentWidget::setFactory( KXMLGUIFactory *factory, KXMLGUIClient *client )
    {
        m_searchBar->setFactory( factory, client );
        m_listViewElements->setFactory( factory, client );
        m_sourceView->setFactory( factory, client );

        if ( m_viewDocumentActionMenu != NULL )
            connect( m_viewDocumentActionMenu->popupMenu(), SIGNAL( activated( int ) ), this, SLOT( slotViewDocument( int ) ) );

        m_copyReference = client->action( "edit_copyref" );
        m_find = client->action( "edit_find" );
        m_findNext = client->action( "edit_find_next" );
        m_viewDocumentActionMenu = dynamic_cast<KActionMenu*>( client->action( "view_document" ) );
        m_listViewElements->setViewShowColumnsMenu( dynamic_cast<KActionMenu*>( client->action( "view_showcolumns" ) ) );
        m_searchWebsitesActionMenu = dynamic_cast<KActionMenu*>( client->action( "search_document_online" ) );
    }

    void DocumentWidget::updateViewDocumentMenu( )
    {
        KPopupMenu * popup = m_viewDocumentActionMenu->popupMenu();
        popup->clear();

        BibTeX::Element * currentElement = NULL;

        QListViewItem * item = m_listViewElements->selectedItem();
        if ( item == NULL )
            item = m_listViewElements->currentItem();
        DocumentListViewItem * dlvi = dynamic_cast<DocumentListViewItem*>( item );
        if ( dlvi )
            currentElement = dlvi->element();

        BibTeX::Entry *entry = NULL;
        if ( currentElement != NULL )
            entry = dynamic_cast<BibTeX::Entry*>( currentElement );

        m_viewDocumentActionMenu->setEnabled( FALSE );
        if ( entry != NULL )
        {
            KURL::List documentURLs = getEntryURLs( entry );
            if ( !documentURLs.isEmpty() )
            {
                for ( KURL::List::Iterator i = documentURLs.begin(); i != documentURLs.end(); ++i )
                {
                    QString prettyURL = ( *i ).prettyURL();
                    if ( prettyURL.endsWith( ".pdf", FALSE ) )
                        popup->insertItem( SmallIcon( "pdf" ), prettyURL );
                    else if ( prettyURL.endsWith( ".ps", FALSE ) )
                        popup->insertItem( SmallIcon( "ps" ), prettyURL );
                    else if ( prettyURL.endsWith( ".html", FALSE ) || prettyURL.startsWith( "http://", FALSE ) )
                        popup->insertItem( SmallIcon( "html" ), prettyURL );
                    else
                        popup->insertItem( prettyURL );
                }
                m_viewDocumentActionMenu->setEnabled( TRUE );
            }
        }
    }

    void DocumentWidget::deferredInitialization()
    {
        restoreState();
        m_listViewElements->deferredInitialization();
    }

    void DocumentWidget::saveState()
    {
        Settings * settings = Settings::self();
        settings->editing_HorSplitterSizes = m_horSplitter->sizes();
        settings->editing_VertSplitterSizes = m_vertSplitter->sizes();
    }

    void DocumentWidget::restoreState()
    {
        m_listViewElements->restoreState();
        m_searchBar->restoreState();
        m_sideBar->restoreState();

        Settings * settings = Settings::self();
        m_horSplitter->setSizes( settings->editing_HorSplitterSizes );
        m_vertSplitter->setSizes( settings->editing_VertSplitterSizes );

        if ( m_searchWebsitesActionMenu != NULL )
        {
            KPopupMenu * popup = m_searchWebsitesActionMenu->popupMenu();
            popup->clear();
            int i = 0;
            for ( QValueList<Settings::SearchURL*>::ConstIterator it = settings->searchURLs.begin(); it != settings->searchURLs.end(); ++it )
                popup->insertItem( ( *it ) ->description, ++i );
        }

        if ( settings->editing_UseSpecialFont )
            m_preview->setFont( settings->editing_SpecialFont );
        else
            m_preview->setFont( KGlobalSettings::generalFont() );

    }

    void DocumentWidget::editElement()
    {
        BibTeX::Element * element = m_listViewElements->editElement( );

        Settings * settings = Settings::self();
        settings->addToCompletion( element );
        m_sideBar->refreshLists( m_bibtexfile );
    }

    void DocumentWidget::deleteElements()
    {
        if ( !m_isReadOnly )
        {
            if ( m_editMode == emList )
            {
                m_listViewElements->deleteSelected();
                slotModified();
            }
        }
    }
    void DocumentWidget::cutElements()
    {
        if ( !m_isReadOnly )
        {
            if ( m_editMode == emList )
            {
                m_listViewElements->cutSelected();
            }
            else if ( m_editMode == emSource )
            {
                m_sourceView->cut();
            }
            slotModified();
        }
    }

    void DocumentWidget::copyElements()
    {
        if ( m_editMode == emList )
        {
            m_listViewElements->copySelected();
        }
        else if ( m_editMode == emSource )
        {
            m_sourceView->copy();
        }
    }

    void DocumentWidget::copyRefElements()
    {
        if ( m_editMode == emList )
        {
            m_listViewElements->copyRefSelected();
        }
    }

    void DocumentWidget::pasteElements()
    {
        if ( !m_isReadOnly )
        {
            if ( m_editMode == emList )
            {
                if ( m_listViewElements->paste() )
                    slotModified();
            }
            else if ( m_editMode == emSource )
            {
                if ( m_sourceView->paste() )
                    slotModified();
            }
        }
    }

    void DocumentWidget::setReadOnly( bool isReadOnly )
    {
        m_isReadOnly = isReadOnly;
        m_listViewElements->setReadOnly( m_isReadOnly );
        m_sourceView->setReadOnly( m_isReadOnly );
    }

    void DocumentWidget::configureEditor()
    {
        m_sourceView->configureEditor();
    }

    void DocumentWidget::undoEditor()
    {
        m_sourceView->undo();
    }

    void DocumentWidget::find()
    {
        if ( m_editMode == emSource )
            m_sourceView->find();
    }
    void DocumentWidget::findNext()
    {
        if ( m_editMode == emSource )
            m_sourceView->findNext();
    }

    void DocumentWidget::slotShowProgress( int current, int total )
    {
        if ( m_progressDialog != NULL )
        {
            m_progressDialog->setProgress( current, total );
            qApp->processEvents();
        }
    }

    void DocumentWidget::startProgress( const QString & label, QObject * progressFrom )
    {
        m_progressDialog = new QProgressDialog( this );
        m_progressDialog->setLabelText( label );
        connect( progressFrom, SIGNAL( progress( int, int ) ), this, SLOT( slotShowProgress( int, int ) ) );
        connect( m_progressDialog, SIGNAL( canceled() ), progressFrom, SLOT( cancel( ) ) );
        QApplication::setOverrideCursor( Qt::waitCursor );
    }

    void DocumentWidget::endProgress( QObject * progressFrom )
    {
        disconnect( progressFrom, SIGNAL( progress( int, int ) ), this, SLOT( slotShowProgress( int, int ) ) );
        disconnect( m_progressDialog, SIGNAL( canceled() ), progressFrom, SLOT( cancel( ) ) );
        delete m_progressDialog;
        m_progressDialog = NULL;
        QApplication::restoreOverrideCursor();
    }

    void DocumentWidget::searchWebsites( const QString& searchURL )
    {
        DocumentListViewItem * item = dynamic_cast<DocumentListViewItem*>( m_listViewElements->selectedItem() );
        if ( item == NULL )
            item = dynamic_cast<DocumentListViewItem*>( m_listViewElements->currentItem() );

        if ( item != NULL )
            searchWebsites( item->element(), searchURL );
    }

    void DocumentWidget::searchWebsites( BibTeX::Element * element, const QString& searchURL )
    {
        QString queryString = QString::null;

        BibTeX::Entry* entry = dynamic_cast<BibTeX::Entry*>( element );
        if ( entry != NULL )
        {
            BibTeX::EntryField * field = entry->getField( BibTeX::EntryField::ftTitle );
            if ( field && !field->value() ->isEmpty() )
                queryString = field->value() ->plainString();
        }
        else
        {
            BibTeX::Comment * comment = dynamic_cast<BibTeX::Comment*>( element );
            if ( comment != NULL )
                queryString = comment->text();
            else
            {
                BibTeX::Macro * macro = dynamic_cast<BibTeX::Macro*>( element );
                if ( macro != NULL && !macro->value() ->isEmpty() )
                    queryString = macro->value() ->plainString();
                else
                    kdDebug() << "Not yet supported" << endl;
            }
        }

        if ( queryString != QString::null )
            kapp->invokeBrowser( QString( searchURL ).arg( queryString.replace( '{', "" ).replace( '}', "" ) ) );
    }

    void DocumentWidget::doWebQuery( WebQuery* wq )
    {
        if ( !m_isReadOnly && m_editMode == emList )
        {
            BibTeX::File * bibtexFile = wq->query();

            if ( bibtexFile != NULL )
            {
                if ( m_listViewElements->insertItems( bibtexFile ) )
                {
                    Settings * settings = Settings::self();
                    settings->addToCompletion( bibtexFile );
                    slotModified();
                }

                if ( bibtexFile != NULL )
                    delete bibtexFile;
            }
        }
    }

    void DocumentWidget::updateFromGUI()
    {
        BibTeX::File * file = NULL;
        if ( currentPage() == m_sourceView )
            file = m_sourceView->getBibTeXFile();
        else if ( currentPage() == m_container )
            file = m_listViewElements->getBibTeXFile();

        if ( file != NULL && file != m_bibtexfile )
        {
            delete m_bibtexfile;
            m_bibtexfile = file;
        }
    }

    KURL::List DocumentWidget::getEntryURLs( BibTeX::Entry *entry )
    {
        QStringList urls = entry->urls();
        KURL::List result;

        for ( QStringList::Iterator it = urls.begin(); it != urls.end(); ++it )
        {
            KURL url = KURL( *it );
            if ( url.isValid() && ( !url.isLocalFile() || QFile::exists( url.path() ) ) )
                result.append( url );
        }

        return result;
    }

    void DocumentWidget::slotSelectionChanged()
    {
        int numSelected = 0;
        QListViewItemIterator it( m_listViewElements, QListViewItemIterator::Selected );
        while ( it.current() && numSelected < 3 )
        {
            numSelected++;
            it++;
        }

        emit listViewSelectionChanged( numSelected );
    }

    void DocumentWidget::slotPreviewElement( QListViewItem * item )
    {
        BibTeX::Element * currentElement = NULL;

        if ( item == NULL )
            item = m_listViewElements->selectedItem();
        if ( item == NULL )
            item = m_listViewElements->currentItem();
        if ( item != NULL )
        {
            DocumentListViewItem * dlvi = dynamic_cast<DocumentListViewItem*>( item );
            if ( dlvi != NULL && m_listViewElements->isEnabled() )
            {
                currentElement = dlvi->element();
                KStandardDirs * kstd = KGlobal::dirs();

                BibTeX::XSLTransform *transform = new BibTeX::XSLTransform( kstd->findResource( "data", "kbibtexpart/xslt/html.xsl" ) );
                if ( transform != NULL )
                {
                    BibTeX::FileExporterXSLT * exporter = new BibTeX::FileExporterXSLT( transform );
                    QBuffer buffer;
                    buffer.open( IO_WriteOnly );
                    bool result = exporter->save( &buffer, currentElement );
                    buffer.close();

                    if ( result )
                    {
                        buffer.open( IO_ReadOnly );
                        QTextStream htmlTS( &buffer );
                        htmlTS.setEncoding( QTextStream::UnicodeUTF8 );
                        QString htmlText = htmlTS.read();
                        buffer.close();
                        QString text = htmlText.remove( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ).append( "</qt>" ).prepend( "<qt>" ).replace( QRegExp( "\\\\emph\\{([^}]+)\\}" ), "<em>\\1</em>" ).replace( QRegExp( "\\\\[A-Za-z0-9]+" ), "" ).replace( '{', "" ).replace( '}', "" );
                        m_preview->setText( text );
                    }

                    delete exporter;
                    delete transform;
                }
            }
        }
    }

    void DocumentWidget::slotTabChanged( QWidget *tab )
    {
        setEnabled( FALSE );

        if ( tab == m_sourceView )
        {
            // switching from list view to source view
            BibTeX::File * bibtexFile = m_listViewElements->getBibTeXFile();
            if ( bibtexFile != NULL )
            {
                // check whether the selected widget has changed the bibtex file
                if ( bibtexFile != m_bibtexfile )
                {
                    delete m_bibtexfile;
                    m_bibtexfile = bibtexFile;
                }
                m_sourceView->setBibTeXFile( m_bibtexfile );
            }
            m_editMode = emSource;
        }
        else if ( tab == m_container )
        {
            // switching from source view to list view
            BibTeX::File * bibtexFile = m_sourceView->getBibTeXFile();
            if ( bibtexFile != NULL )
            {
                // check whether the selected widget has changed the bibtex file
                if ( bibtexFile != m_bibtexfile )
                {
                    delete m_bibtexfile;
                    m_bibtexfile = bibtexFile;

                    Settings * settings = Settings::self();
                    settings->addToCompletion( m_bibtexfile );
                    m_sideBar->refreshLists( m_bibtexfile );
                }
                m_listViewElements->setBibTeXFile( m_bibtexfile );
            }
            m_editMode = emList;
        }

        setEnabled( TRUE );

        if ( tab == m_sourceView )
            m_sourceView->setFocus();
        else if ( tab == m_container )
            m_listViewElements->setFocus();

        if ( m_find && m_findNext )
        {
            m_find->setEnabled( m_editMode == emSource );
            m_findNext->setEnabled( m_editMode == emSource );
        }

        if ( m_copyReference )
            m_copyReference->setEnabled( m_editMode == emList );

        emit undoChanged( m_editMode == emSource );
    }

    void DocumentWidget::slotModified()
    {
        if ( isEnabled() )
        {
            m_sideBar->refreshLists( m_bibtexfile );
            emit modified( );
        }
    }

    void DocumentWidget::slotViewDocument( int id )
    {
        kapp->invokeBrowser( m_viewDocumentActionMenu->popupMenu() ->text( id ) );
    }

}
#include "documentwidget.moc"

Generated by  Doxygen 1.6.0   Back to index