summaryrefslogtreecommitdiffstats
path: root/src/entrywidgetkeyword.cpp
blob: 8a40c716bdf77e4b6dd403711817de52022753cf (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/***************************************************************************
*   Copyright (C) 2004-2009 by Thomas Fischer                             *
*   [email protected]                                             *
*                                                                         *
*   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 <tqlayout.h>
#include <tqregexp.h>
#include <tqpushbutton.h>
#include <tqtooltip.h>
#include <tqtimer.h>
#include <tqlabel.h>

#include <kdialog.h>
#include <tdelocale.h>
#include <kiconloader.h>
#include <kdialogbase.h>
#include <tdemessagebox.h>
#include <tdelistview.h>
#include <kguiitem.h>
#include <kdebug.h>

#include "settings.h"
#include "entrywidgetkeyword.h"

namespace KBibTeX
{
    KeywordListViewItem::KeywordListViewItem( TDEListView * parent, const TQString & text, bool global ) : TQCheckListItem( parent, text, TQCheckListItem::CheckBox )
    {
        setGlobal( global );
    }

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

    void KeywordListViewItem::setGlobal( bool global )
    {
        m_isGlobal = global;
        if ( m_isGlobal )
        {
            setText( 1, i18n( "Global" ) );
            setPixmap( 1, SmallIcon( "package" ) );
        }
        else
        {
            setText( 1, i18n( "In this file only" ) );
            setPixmap( 1, SmallIcon( "edit-copy" ) );
        }
    }

    EntryWidgetKeyword::EntryWidgetKeyword( BibTeX::File *bibtexfile, bool isReadOnly, TQWidget *parent, const char *name ) : EntryWidgetTab( bibtexfile, isReadOnly, parent, name ), m_bibtexfile( bibtexfile ), m_isModified( FALSE ), m_numKeywords( 0 )
    {
        setupGUI();
    }

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

    bool EntryWidgetKeyword::isModified()
    {
        return m_isModified;
    }

    void EntryWidgetKeyword::updateGUI( BibTeX::Entry::EntryType /*entryType*/, bool /*enableAll*/ )
    {
        // nothing
    }

    void EntryWidgetKeyword::apply( BibTeX::Entry *entry )
    {
        readListView();

        /**
         * Update entry
        */
        if ( m_usedKeywords.empty() )
            entry->deleteField( BibTeX::EntryField::ftKeywords );
        else
        {
            BibTeX::EntryField * field = entry->getField( BibTeX::EntryField::ftKeywords );
            BibTeX::Value *value = NULL;
            if ( field == NULL )
            {
                field = new BibTeX::EntryField( BibTeX::EntryField::ftKeywords );
                entry->addField( field );
            }
            value = field->value();
            value->items.clear();
            BibTeX::KeywordContainer *keywordContainer = new BibTeX::KeywordContainer( m_usedKeywords );
            value->items.append( keywordContainer );
        }

        /**
         * Update global keyword list
        */
        KBibTeX::Settings * settings = KBibTeX::Settings::self();
        settings->keyword_GlobalList = m_globalKeywords;
    }

    void EntryWidgetKeyword::reset( BibTeX::Entry *entry )
    {
        BibTeX::EntryField * field = entry->getField( BibTeX::EntryField::ftKeywords );
        BibTeX::KeywordContainer *keywordContainer = NULL;
        if ( field != NULL && ( keywordContainer = dynamic_cast<BibTeX::KeywordContainer*>( field->value()->items.first() ) ) != NULL )
            for ( TQValueList<BibTeX::Keyword*>::Iterator it = keywordContainer->keywords.begin(); it != keywordContainer->keywords.end(); ++it )
                m_usedKeywords.append(( *it )->text() );

        if ( m_bibtexfile != NULL )
            m_fileKeywords = m_bibtexfile->getAllValuesAsStringList( BibTeX::EntryField::ftKeywords );

        KBibTeX::Settings * settings = KBibTeX::Settings::self();
        m_globalKeywords = settings->keyword_GlobalList;

        m_availableKeywords = TQStringList( m_globalKeywords );
        for ( TQStringList::Iterator it = m_fileKeywords.begin(); it !=  m_fileKeywords.end(); ++it )
            if ( !m_availableKeywords.contains( *it ) )
                m_availableKeywords.append( *it );
        for ( TQStringList::Iterator it = m_usedKeywords.begin(); it !=  m_usedKeywords.end(); ++it )
            if ( !m_availableKeywords.contains( *it ) )
                m_availableKeywords.append( *it );

        setListView();
    }

    void EntryWidgetKeyword::updateWarnings( BibTeX::Entry::EntryType /*entryType*/, TQListView */*listViewWarnings*/ )
    {
        // nothing
    }

    void EntryWidgetKeyword::slotSelectionChanged()
    {
        bool hasCurrent = m_listviewKeywords->selectedItem() != NULL;

        m_buttonEdit->setEnabled( hasCurrent );
        m_buttonToggleGlobal->setEnabled( hasCurrent );
    }

    void EntryWidgetKeyword::slotKeywordRenamed( TQListViewItem * item, const TQString & text, int /*col*/ )
    {
        KeywordListViewItem *kwlvi = dynamic_cast<KeywordListViewItem*>( item );
        if ( text.isEmpty() )
        {
            item->setText( 0, m_beforeRenaming );
            kwlvi->setOn( FALSE );
        }
        else if ( text != m_beforeRenaming )
        {
            if ( m_availableKeywords.contains( text ) )
            {
                item->setText( 0, m_beforeRenaming );
                KMessageBox::error( this, TQString( i18n( "The keyword '%1' does already exist in the list of keywords.\nThe old name has been restored." ) ).arg( text ), i18n( "Renaming keyword failed" ) );
            }
            else
            {
                m_availableKeywords.remove( m_beforeRenaming );
                m_availableKeywords.append( text );
                if ( kwlvi->isGlobal() )
                {
                    m_globalKeywords.remove( m_beforeRenaming );
                    m_globalKeywords.append( text );
                }
                else
                {
                    m_fileKeywords.remove( m_beforeRenaming );
                    m_fileKeywords.append( text );
                }
                TQCheckListItem *checkedItem = dynamic_cast<TQCheckListItem*>( item );
                if ( checkedItem != NULL )
                    checkedItem->setOn( TRUE );
            }
        }
    }

    void EntryWidgetKeyword::slotNewKeyword()
    {
        KeywordListViewItem * item = new KeywordListViewItem( m_listviewKeywords, TQString( i18n( "May only contain ASCII characters, in case of doubt keep English form", "NewKeyword%1" ) ).arg( ++m_numKeywords ), FALSE );
        m_listviewKeywords->setSelected( item, TRUE );
        TQTimer::singleShot( 100, this, SLOT( slotEditKeyword() ) );
    }

    void EntryWidgetKeyword::slotEditKeyword()
    {
        TQListViewItem * item = m_listviewKeywords->selectedItem();
        if ( item != NULL )
        {
            m_beforeRenaming = item->text( 0 );
            m_listviewKeywords->rename( item, 0 );
        }
    }

    void EntryWidgetKeyword::slotToggleGlobal()
    {
        KeywordListViewItem *item = dynamic_cast<KeywordListViewItem*>( m_listviewKeywords->selectedItem() );
        if ( item != NULL )
        {
            bool isGlobal = item->isGlobal();
            if ( isGlobal )
                m_globalKeywords.remove( item->text( 0 ) );
            else
                m_globalKeywords.append( item->text( 0 ) );

            item->setGlobal( !isGlobal );
        }
    }

    void EntryWidgetKeyword::setupGUI()
    {
        TQGridLayout * gridLayout = new TQGridLayout( this, 6, 2, KDialog::marginHint(), KDialog::spacingHint(), "gridLayout" );
        gridLayout->setRowStretch( 4, 1 );

        m_listviewKeywords = new TDEListView( this );
        m_listviewKeywords->setEnabled( !m_isReadOnly );
        m_listviewKeywords->addColumn( i18n( "Keyword" ) );
        m_listviewKeywords->addColumn( i18n( "Origin" ) );
        gridLayout->addMultiCellWidget( m_listviewKeywords, 0, 4, 0, 0 );
        m_listviewKeywords->setAllColumnsShowFocus( TRUE );
        connect( m_listviewKeywords, SIGNAL( currentChanged( TQListViewItem* ) ), this, SLOT( slotSelectionChanged() ) );
        connect( m_listviewKeywords, SIGNAL( clicked( TQListViewItem * ) ), this, SLOT( slotSelectionChanged() ) );
        connect( m_listviewKeywords, SIGNAL( itemRenamed( TQListViewItem*, const TQString&, int ) ), this, SLOT( slotKeywordRenamed( TQListViewItem*, const TQString&, int ) ) );

        m_buttonNew = new TQPushButton( i18n( "keyword", "New" ), this );
        m_buttonNew->setEnabled( !m_isReadOnly );
        m_buttonNew->setIconSet( TQIconSet( SmallIcon( "add" ) ) );
        TQToolTip::add( m_buttonNew, i18n( "Add a new keyword to the list" ) );
        gridLayout->addWidget( m_buttonNew, 0, 1 );
        connect( m_buttonNew, SIGNAL( clicked() ), this, SLOT( slotNewKeyword() ) );

        m_buttonEdit = new TQPushButton( i18n( "keyword", "Edit" ), this );
        m_buttonEdit->setIconSet( TQIconSet( SmallIcon( "edit" ) ) );
        TQToolTip::add( m_buttonEdit, i18n( "Edit the selected keyword" ) );
        gridLayout->addWidget( m_buttonEdit, 1, 1 );
        m_buttonEdit->setEnabled( FALSE );
        connect( m_buttonEdit, SIGNAL( clicked() ), this, SLOT( slotEditKeyword() ) );

        m_buttonToggleGlobal = new TQPushButton( i18n( "keyword", "Toggle &global" ), this );
        m_buttonToggleGlobal->setIconSet( TQIconSet( SmallIcon( "package" ) ) );
        TQToolTip::add( m_buttonToggleGlobal, i18n( "Add or remove the selected keyword to or from the global list" ) );
        gridLayout->addWidget( m_buttonToggleGlobal, 2, 1 );
        m_buttonToggleGlobal->setEnabled( FALSE );
        connect( m_buttonToggleGlobal, SIGNAL( clicked() ), this, SLOT( slotToggleGlobal() ) );

        TQLabel *label = new TQLabel( i18n( "There is no need to delete keywords. Simply uncheck unwanted keywords and make them non-global.\nGlobal keywords can also be edited in the settings dialog." ), this );
        label->setAlignment( TQLabel::WordBreak | TQLabel::AlignTop );
        gridLayout->addMultiCellWidget( label, 5, 5, 0, 1 );
    }

    void EntryWidgetKeyword::setListView()
    {
        m_availableKeywords.sort();
        m_listviewKeywords->clear();
        for ( TQStringList::Iterator it = m_availableKeywords.begin(); it != m_availableKeywords.end(); ++it )
        {
            KeywordListViewItem *item = new KeywordListViewItem( m_listviewKeywords, *it,  m_globalKeywords.contains( *it ) );
            if ( m_usedKeywords.contains( *it ) )
                item->setOn( TRUE );
        }
    }

    void EntryWidgetKeyword::readListView()
    {
        m_usedKeywords.clear();
        for ( TQListViewItemIterator it = TQListViewItemIterator( m_listviewKeywords, TQListViewItemIterator::Checked ); it.current() != NULL; ++it )
            m_usedKeywords.append(( *it ) ->text( 0 ) );
    }
}
#include "entrywidgetkeyword.moc"