~ubuntu-branches/ubuntu/precise/kbibtex/precise

« back to all changes in this revision

Viewing changes to src/entrywidgetother.cpp

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2011-07-18 09:29:48 UTC
  • mfrom: (1.1.6) (2.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20110718092948-ksxjmg7kdfamolmg
Tags: 0.3-1
* First upstream release for KDE4 (Closes: #634255). A number of search
  engines are still missing, in comparison to the 0.2 series.
* Bumped Standards-Version to 3.9.2, no changes necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
*   Copyright (C) 2004-2009 by Thomas Fischer                             *
3
 
*   fischer@unix-ag.uni-kl.de                                             *
4
 
*                                                                         *
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.                                   *
9
 
*                                                                         *
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.                          *
14
 
*                                                                         *
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
 
***************************************************************************/
20
 
#include <qstringlist.h>
21
 
#include <qlayout.h>
22
 
#include <qtooltip.h>
23
 
#include <qlabel.h>
24
 
#include <qwhatsthis.h>
25
 
 
26
 
#include <kdialog.h>
27
 
#include <kapplication.h>
28
 
#include <klistview.h>
29
 
#include <klocale.h>
30
 
#include <kiconloader.h>
31
 
#include <kio/netaccess.h>
32
 
#include <kpushbutton.h>
33
 
#include <klineedit.h>
34
 
 
35
 
#include <fieldlineedit.h>
36
 
#include <settings.h>
37
 
#include "entrywidgetother.h"
38
 
 
39
 
namespace KBibTeX
40
 
{
41
 
 
42
 
    EntryWidgetOther::EntryWidgetOther( BibTeX::File *bibtexfile, bool isReadOnly, QWidget *parent, const char *name )
43
 
            : EntryWidgetTab( bibtexfile, isReadOnly, parent, name ), m_isModified( FALSE )
44
 
    {
45
 
        setupGUI();
46
 
    }
47
 
 
48
 
    EntryWidgetOther::~EntryWidgetOther()
49
 
    {
50
 
        m_listViewFields->clear();
51
 
    }
52
 
 
53
 
    bool EntryWidgetOther::isModified()
54
 
    {
55
 
        return m_isModified;
56
 
    }
57
 
 
58
 
    void EntryWidgetOther::updateGUI( BibTeX::Entry::EntryType /*entryType*/, bool /*enableAll*/ )
59
 
    {
60
 
        // nothing
61
 
    }
62
 
 
63
 
    void EntryWidgetOther::apply( BibTeX::Entry *entry )
64
 
    {
65
 
        Settings * settings = Settings::self();
66
 
        QStringList toBeDeleted;
67
 
        for ( QValueList<BibTeX::EntryField*>::ConstIterator it = entry->begin(); it != entry->end(); it++ )
68
 
        {
69
 
            BibTeX::EntryField *field = *it;
70
 
            bool doDel = field->fieldType() == BibTeX::EntryField::ftUnknown;
71
 
            if ( !doDel ) continue;
72
 
 
73
 
            QString ftn = field->fieldTypeName().lower();
74
 
            for ( unsigned int i = 0; doDel && i < settings->userDefinedInputFields.count(); ++i )
75
 
                doDel &= settings->userDefinedInputFields[i]->name.lower() != ftn;
76
 
 
77
 
            if ( doDel )
78
 
                toBeDeleted.append( ftn );
79
 
        }
80
 
        for ( QStringList::ConstIterator it = toBeDeleted.begin(); it != toBeDeleted.end(); ++it )
81
 
            entry->deleteField( *it );
82
 
 
83
 
        for ( QListViewItemIterator it( m_listViewFields ); it.current(); it++ )
84
 
        {
85
 
            ValueListViewItem *vlvi = dynamic_cast<KBibTeX::ValueListViewItem*>( it.current() );
86
 
            if ( vlvi != NULL )
87
 
            {
88
 
                BibTeX::EntryField * field = new BibTeX::EntryField( vlvi->title() );
89
 
                field->setValue( vlvi->value() );
90
 
                entry->addField( field );
91
 
            }
92
 
        }
93
 
 
94
 
        m_isModified = FALSE;
95
 
    }
96
 
 
97
 
    void EntryWidgetOther::reset( BibTeX::Entry *entry )
98
 
    {
99
 
        m_listViewFields->clear();
100
 
 
101
 
        Settings * settings = Settings::self();
102
 
        for ( QValueList<BibTeX::EntryField*>::ConstIterator it = entry->begin(); it != entry->end(); it++ )
103
 
        {
104
 
            BibTeX::EntryField *field = *it;
105
 
            bool doAdd = field->fieldType() == BibTeX::EntryField::ftUnknown;
106
 
            if ( !doAdd ) continue;
107
 
 
108
 
            QString ftn = field->fieldTypeName().lower();
109
 
            for ( unsigned int i = 0; doAdd && i < settings->userDefinedInputFields.count(); ++i )
110
 
                doAdd &= settings->userDefinedInputFields[i]->name.lower() != ftn;
111
 
 
112
 
            if ( doAdd )
113
 
                new ValueListViewItem( field->fieldTypeName(), field->value(), m_listViewFields );
114
 
        }
115
 
 
116
 
        m_isModified = FALSE;
117
 
    }
118
 
 
119
 
    void EntryWidgetOther::updateWarnings( BibTeX::Entry::EntryType /*entryType*/, QListView * /*listViewWarnings*/ )
120
 
    {
121
 
        // no warnings neccessary for user fields
122
 
    }
123
 
 
124
 
    void EntryWidgetOther::setupGUI()
125
 
    {
126
 
        QGridLayout * gridLayout = new QGridLayout( this, 5, 3, KDialog::marginHint(), KDialog::spacingHint(), "gridLayout" );
127
 
 
128
 
        m_lineEditKey = new KLineEdit( this, "m_lineEditKey" );
129
 
        m_lineEditKey->setReadOnly( m_isReadOnly );
130
 
        gridLayout->addWidget( m_lineEditKey, 0, 1 );
131
 
        QToolTip::add( m_lineEditKey, i18n( "Name of the user-defined field" ) );
132
 
        QWhatsThis::add( m_lineEditKey, i18n( "The name of the user-defined field. Should only contain letters and numbers." ) );
133
 
        QLabel *label = new QLabel( i18n( "&Name:" ), this );
134
 
        label->setBuddy( m_lineEditKey );
135
 
        gridLayout->addWidget( label, 0, 0 );
136
 
 
137
 
        m_pushButtonAdd = new KPushButton( i18n( "&Add" ), this, "m_pushButtonAdd" );
138
 
        gridLayout->addWidget( m_pushButtonAdd, 0, 2 );
139
 
        m_pushButtonAdd->setIconSet( QIconSet( SmallIcon( "add" ) ) );
140
 
 
141
 
        m_fieldLineEditValue = new KBibTeX::FieldLineEdit( i18n( "Value" ), KBibTeX::FieldLineEdit::itMultiLine, m_isReadOnly, this, "m_fieldLineEditValue" );
142
 
        gridLayout->addMultiCellWidget( m_fieldLineEditValue, 1, 2, 1, 2 );
143
 
        QToolTip::add( m_fieldLineEditValue, i18n( "Content of the user-defined field" ) );
144
 
        QWhatsThis::add( m_fieldLineEditValue, i18n( "The content of the user-defined field. May contain any text." ) );
145
 
        label = new QLabel( i18n( "&Content:" ), this );
146
 
        label->setBuddy( m_fieldLineEditValue );
147
 
        gridLayout->addWidget( label, 1, 0 );
148
 
 
149
 
        QSpacerItem* spacer = new QSpacerItem( 20, 110, QSizePolicy::Minimum, QSizePolicy::Expanding );
150
 
        gridLayout->addItem( spacer, 2, 0 );
151
 
 
152
 
        m_listViewFields = new KListView( this, "m_listViewFields" );
153
 
        m_listViewFields->addColumn( i18n( "Key" ) );
154
 
        m_listViewFields->addColumn( i18n( "Value" ) );
155
 
        m_listViewFields->setAllColumnsShowFocus( true );
156
 
        m_listViewFields->setFullWidth( TRUE );
157
 
        gridLayout->addMultiCellWidget( m_listViewFields, 3, 5, 1, 1 );
158
 
 
159
 
        label = new QLabel( i18n( "&List:" ), this );
160
 
        label->setBuddy( m_listViewFields );
161
 
        label->setAlignment( Qt::AlignTop );
162
 
        gridLayout->addWidget( label, 3, 0 );
163
 
 
164
 
        m_pushButtonDelete = new KPushButton( i18n( "&Delete" ), this, "m_pushButtonDelete" );
165
 
        gridLayout->addWidget( m_pushButtonDelete, 3, 2 );
166
 
        m_pushButtonDelete->setIconSet( QIconSet( SmallIcon( "delete" ) ) );
167
 
 
168
 
        m_pushButtonOpen = new KPushButton( i18n( "Op&en" ), this, "m_pushButtonOpen" );
169
 
        gridLayout->addWidget( m_pushButtonOpen, 4, 2 );
170
 
        m_pushButtonOpen->setIconSet( QIconSet( SmallIcon( "fileopen" ) ) );
171
 
 
172
 
        spacer = new QSpacerItem( 20, 110, QSizePolicy::Minimum, QSizePolicy::Expanding );
173
 
        gridLayout->addItem( spacer, 5, 2 );
174
 
 
175
 
        m_pushButtonOpen->setEnabled( FALSE );
176
 
        m_pushButtonAdd->setEnabled( FALSE );
177
 
        m_pushButtonDelete->setEnabled( FALSE );
178
 
 
179
 
        connect( m_listViewFields, SIGNAL( clicked( QListViewItem*, const QPoint&, int ) ), this, SLOT( fieldExecute( QListViewItem* ) ) );
180
 
        connect( m_lineEditKey, SIGNAL( textChanged( const QString& ) ), this, SLOT( updateGUI() ) );
181
 
        connect( m_fieldLineEditValue, SIGNAL( textChanged( ) ), this, SLOT( updateGUI() ) );
182
 
        connect( m_pushButtonAdd, SIGNAL( clicked( ) ), this, SLOT( addClicked( ) ) );
183
 
        connect( m_pushButtonDelete, SIGNAL( clicked( ) ), this, SLOT( deleteClicked( ) ) );
184
 
        connect( m_pushButtonOpen, SIGNAL( clicked() ), this, SLOT( openClicked() ) );
185
 
    }
186
 
 
187
 
    void EntryWidgetOther::updateGUI()
188
 
    {
189
 
        QString text = m_lineEditKey->text();
190
 
        QListViewItem * item = m_listViewFields->findItem( text, 0 ) ;
191
 
        bool contains = item != NULL;
192
 
        bool isUnknown = BibTeX::EntryField::fieldTypeFromString( text ) == BibTeX::EntryField::ftUnknown;
193
 
 
194
 
        if ( contains )
195
 
            m_listViewFields->setSelected( item, TRUE );
196
 
        else
197
 
            m_listViewFields->clearSelection();
198
 
 
199
 
        m_pushButtonDelete->setEnabled( !m_isReadOnly && contains );
200
 
        m_pushButtonAdd->setEnabled( !m_isReadOnly && !text.isEmpty() && !m_fieldLineEditValue->isEmpty() && isUnknown );
201
 
        m_pushButtonAdd->setText( contains ? i18n( "&Apply" ) : i18n( "&Add" ) );
202
 
        m_pushButtonAdd->setIconSet( QIconSet( SmallIcon( contains ? "apply" : "fileopen" ) ) );
203
 
 
204
 
        bool validURL = FALSE;
205
 
        if ( contains )
206
 
        {
207
 
            KURL url = Settings::locateFile( item->text( 1 ), m_bibtexfile->fileName, this );
208
 
            validURL = url.isValid();
209
 
            m_internalURL = url;
210
 
        }
211
 
        m_pushButtonOpen->setEnabled( validURL );
212
 
    }
213
 
 
214
 
    void EntryWidgetOther::fieldExecute( QListViewItem * item )
215
 
    {
216
 
        ValueListViewItem * vlvi = dynamic_cast<KBibTeX::ValueListViewItem*>( item );
217
 
        if ( vlvi != NULL )
218
 
        {
219
 
            m_lineEditKey->setText( vlvi->title() );
220
 
            m_fieldLineEditValue->setValue( vlvi->value() );
221
 
        }
222
 
    }
223
 
 
224
 
    void EntryWidgetOther::addClicked()
225
 
    {
226
 
        QString key = m_lineEditKey->text();
227
 
        QListViewItem * item = m_listViewFields->findItem( key, 0 ) ;
228
 
        ValueListViewItem * vlvi = item != NULL ? dynamic_cast<KBibTeX::ValueListViewItem*>( item ) : NULL;
229
 
 
230
 
        if ( vlvi != NULL )
231
 
            vlvi->setValue( m_fieldLineEditValue->value() );
232
 
        else
233
 
            new ValueListViewItem( key, m_fieldLineEditValue->value(), m_listViewFields );
234
 
 
235
 
        updateGUI();
236
 
 
237
 
        m_isModified = TRUE;
238
 
    }
239
 
 
240
 
    void EntryWidgetOther::deleteClicked()
241
 
    {
242
 
        QListViewItem * item = m_listViewFields->findItem( m_lineEditKey->text(), 0 );
243
 
 
244
 
        if ( item != NULL )
245
 
        {
246
 
            delete item;
247
 
            m_lineEditKey->setText( "" );
248
 
            m_fieldLineEditValue->setValue( new BibTeX::Value() );
249
 
            updateGUI();
250
 
        }
251
 
 
252
 
        m_isModified = TRUE;
253
 
    }
254
 
 
255
 
    void EntryWidgetOther::openClicked()
256
 
    {
257
 
        if ( m_internalURL.isValid() )
258
 
        {
259
 
            BibTeX::Value * value = m_fieldLineEditValue->value();
260
 
            Settings::openUrl( m_internalURL, this );
261
 
            delete value;
262
 
        }
263
 
        else
264
 
            m_fieldLineEditValue->setEnabled( FALSE );
265
 
    }
266
 
 
267
 
    ValueListViewItem::ValueListViewItem( const QString& title, BibTeX::Value *value, QListView *parent )
268
 
            : QListViewItem( parent ), m_title( title )
269
 
    {
270
 
        m_value = new BibTeX::Value( value );
271
 
        setTexts( m_title );
272
 
    }
273
 
 
274
 
    ValueListViewItem::~ValueListViewItem()
275
 
    {
276
 
        delete m_value;
277
 
    }
278
 
 
279
 
    const BibTeX::Value *ValueListViewItem::value()
280
 
    {
281
 
        return m_value;
282
 
    }
283
 
 
284
 
    QString ValueListViewItem::title()
285
 
    {
286
 
        return m_title;
287
 
    }
288
 
 
289
 
    void ValueListViewItem::setValue( BibTeX::Value *value )
290
 
    {
291
 
        if ( value != m_value )
292
 
        {
293
 
            if ( m_value != NULL )
294
 
                delete m_value;
295
 
 
296
 
            if ( value != NULL )
297
 
                m_value = new BibTeX::Value( value );
298
 
            else
299
 
                m_value = new BibTeX::Value();
300
 
 
301
 
        }
302
 
        setTexts( m_title );
303
 
    }
304
 
 
305
 
    void ValueListViewItem::setTitle( const QString &title )
306
 
    {
307
 
        m_title = title;
308
 
        setTexts( title );
309
 
    }
310
 
 
311
 
    void ValueListViewItem::setTexts( const QString& title )
312
 
    {
313
 
        setText( 0, title );
314
 
        if ( m_value != NULL )
315
 
            setText( 1, m_value->text() );
316
 
    }
317
 
 
318
 
}
319
 
#include "entrywidgetother.moc"