summaryrefslogtreecommitdiffstats
path: root/src/documentlistviewitem.cpp
blob: ce4db300704f092d1c6a000e221960dc86187826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/***************************************************************************
*   Copyright (C) 2004-2009 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 <ntqlistview.h>
#include <ntqpainter.h>
#include <ntqpen.h>

#include <klocale.h>

#include "documentlistviewitem.h"
#include "entry.h"
#include "comment.h"
#include <preamble.h>
#include "macro.h"

namespace KBibTeX
{
    DocumentListViewItem::DocumentListViewItem( BibTeX::File *file, BibTeX::Element *element, DocumentListView *parent ) : KListViewItem( parent, parent->lastItem() ), m_element( element ), m_bibtexFile( file ), m_parent( parent ), m_unreadStatus( FALSE )
    {
        setTexts();
    }

    DocumentListViewItem::DocumentListViewItem( BibTeX::File *file, BibTeX::Element *element, DocumentListView *parent, TQListViewItem *after ) : KListViewItem( parent, after ), m_element( element ), m_bibtexFile( file ), m_parent( parent )
    {
        setTexts();
    }

    DocumentListViewItem::~DocumentListViewItem()
    {
        // nothing
    }

    BibTeX::Element* DocumentListViewItem::element()
    {
        return m_element;
    }

    void DocumentListViewItem::updateItem()
    {
        setTexts();
    }

    void DocumentListViewItem::setUnreadStatus( bool unread )
    {
        m_unreadStatus = unread;
    }

    void DocumentListViewItem::paintCell( TQPainter *p, const TQColorGroup &cg, int column, int width, int align )
    {
        if ( !p )
            return ;

        // make unread items bold
        if ( m_unreadStatus )
        {
            TQFont f = p->font();
            f.setBold( TRUE );
            p->setFont( f );
        }

        KListViewItem::paintCell( p, cg, column, width, align );
    }


    void DocumentListViewItem::setTexts()
    {
        BibTeX::Entry * entry = dynamic_cast<BibTeX::Entry*>( m_element );

        if ( entry )
        {
            entry = new BibTeX::Entry( entry );
            m_bibtexFile->completeReferencedFields( entry );

            if ( entry->entryType() != BibTeX::Entry::etUnknown )
                setText( 0, BibTeX::Entry::entryTypeToString( entry->entryType() ) );
            else
                setText( 0, entry->entryTypeString() );
            setText( 1, entry->id() );

            for ( int i = 2; i < m_parent->columns(); i++ )
            {
                BibTeX::EntryField::FieldType fieldType = ( BibTeX::EntryField::FieldType )( i - 2 + ( int ) BibTeX::EntryField::ftAbstract );
                BibTeX::EntryField *field = entry->getField( fieldType );
                BibTeX::Value *value = NULL;
                if ( field != NULL && (( value = field->value() ) != NULL ) )
                    setText( i, value->text().replace( '{', "" ).replace( '}', "" ).replace( '~', "" ) );
                else
                    setText( i, "" );
            }

            delete entry;
        }
        else
        {
            BibTeX::Comment *comment = dynamic_cast<BibTeX::Comment*>( m_element );
            if ( comment )
            {
                setText( 0, i18n( "Comment" ) );
                TQString text = comment->text();
                text.replace( '\n', ' ' );
                setText(( int ) BibTeX::EntryField::ftTitle - ( int ) BibTeX::EntryField::ftAbstract + 2, text );
            }
            else
            {
                BibTeX::Macro * macro = dynamic_cast<BibTeX::Macro*>( m_element );
                if ( macro )
                {
                    setText( 0, i18n( "Macro" ) );
                    setText( 1, macro->key() );
                    BibTeX::Value *value = macro->value();
                    int i = ( int ) BibTeX::EntryField::ftTitle - ( int ) BibTeX::EntryField::ftAbstract + 2;
                    if ( value )
                        setText( i, macro->value() ->text() );
                    else
                        setText( i, "" );
                }
                else
                {
                    BibTeX::Preamble *preamble = dynamic_cast<BibTeX::Preamble*>( m_element );
                    if ( preamble )
                    {
                        setText( 0, i18n( "Preamble" ) );
                        BibTeX::Value *value = preamble->value();
                        int i = ( int ) BibTeX::EntryField::ftTitle - ( int ) BibTeX::EntryField::ftAbstract + 2;
                        if ( value )
                            setText( i, preamble->value() ->text() );
                        else
                            setText( i, "" );
                    }

                }
            }
        }
    }
}