1
/***************************************************************************
2
* Copyright (C) 2004-2009 by Thomas Fischer *
3
* fischer@unix-ag.uni-kl.de *
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. *
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. *
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>
24
#include <qwhatsthis.h>
27
#include <kapplication.h>
28
#include <klistview.h>
30
#include <kiconloader.h>
31
#include <kio/netaccess.h>
32
#include <kpushbutton.h>
33
#include <klineedit.h>
35
#include <fieldlineedit.h>
37
#include "entrywidgetother.h"
42
EntryWidgetOther::EntryWidgetOther( BibTeX::File *bibtexfile, bool isReadOnly, QWidget *parent, const char *name )
43
: EntryWidgetTab( bibtexfile, isReadOnly, parent, name ), m_isModified( FALSE )
48
EntryWidgetOther::~EntryWidgetOther()
50
m_listViewFields->clear();
53
bool EntryWidgetOther::isModified()
58
void EntryWidgetOther::updateGUI( BibTeX::Entry::EntryType /*entryType*/, bool /*enableAll*/ )
63
void EntryWidgetOther::apply( BibTeX::Entry *entry )
65
Settings * settings = Settings::self();
66
QStringList toBeDeleted;
67
for ( QValueList<BibTeX::EntryField*>::ConstIterator it = entry->begin(); it != entry->end(); it++ )
69
BibTeX::EntryField *field = *it;
70
bool doDel = field->fieldType() == BibTeX::EntryField::ftUnknown;
71
if ( !doDel ) continue;
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;
78
toBeDeleted.append( ftn );
80
for ( QStringList::ConstIterator it = toBeDeleted.begin(); it != toBeDeleted.end(); ++it )
81
entry->deleteField( *it );
83
for ( QListViewItemIterator it( m_listViewFields ); it.current(); it++ )
85
ValueListViewItem *vlvi = dynamic_cast<KBibTeX::ValueListViewItem*>( it.current() );
88
BibTeX::EntryField * field = new BibTeX::EntryField( vlvi->title() );
89
field->setValue( vlvi->value() );
90
entry->addField( field );
97
void EntryWidgetOther::reset( BibTeX::Entry *entry )
99
m_listViewFields->clear();
101
Settings * settings = Settings::self();
102
for ( QValueList<BibTeX::EntryField*>::ConstIterator it = entry->begin(); it != entry->end(); it++ )
104
BibTeX::EntryField *field = *it;
105
bool doAdd = field->fieldType() == BibTeX::EntryField::ftUnknown;
106
if ( !doAdd ) continue;
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;
113
new ValueListViewItem( field->fieldTypeName(), field->value(), m_listViewFields );
116
m_isModified = FALSE;
119
void EntryWidgetOther::updateWarnings( BibTeX::Entry::EntryType /*entryType*/, QListView * /*listViewWarnings*/ )
121
// no warnings neccessary for user fields
124
void EntryWidgetOther::setupGUI()
126
QGridLayout * gridLayout = new QGridLayout( this, 5, 3, KDialog::marginHint(), KDialog::spacingHint(), "gridLayout" );
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 );
137
m_pushButtonAdd = new KPushButton( i18n( "&Add" ), this, "m_pushButtonAdd" );
138
gridLayout->addWidget( m_pushButtonAdd, 0, 2 );
139
m_pushButtonAdd->setIconSet( QIconSet( SmallIcon( "add" ) ) );
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 );
149
QSpacerItem* spacer = new QSpacerItem( 20, 110, QSizePolicy::Minimum, QSizePolicy::Expanding );
150
gridLayout->addItem( spacer, 2, 0 );
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 );
159
label = new QLabel( i18n( "&List:" ), this );
160
label->setBuddy( m_listViewFields );
161
label->setAlignment( Qt::AlignTop );
162
gridLayout->addWidget( label, 3, 0 );
164
m_pushButtonDelete = new KPushButton( i18n( "&Delete" ), this, "m_pushButtonDelete" );
165
gridLayout->addWidget( m_pushButtonDelete, 3, 2 );
166
m_pushButtonDelete->setIconSet( QIconSet( SmallIcon( "delete" ) ) );
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" ) ) );
172
spacer = new QSpacerItem( 20, 110, QSizePolicy::Minimum, QSizePolicy::Expanding );
173
gridLayout->addItem( spacer, 5, 2 );
175
m_pushButtonOpen->setEnabled( FALSE );
176
m_pushButtonAdd->setEnabled( FALSE );
177
m_pushButtonDelete->setEnabled( FALSE );
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() ) );
187
void EntryWidgetOther::updateGUI()
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;
195
m_listViewFields->setSelected( item, TRUE );
197
m_listViewFields->clearSelection();
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" ) ) );
204
bool validURL = FALSE;
207
KURL url = Settings::locateFile( item->text( 1 ), m_bibtexfile->fileName, this );
208
validURL = url.isValid();
211
m_pushButtonOpen->setEnabled( validURL );
214
void EntryWidgetOther::fieldExecute( QListViewItem * item )
216
ValueListViewItem * vlvi = dynamic_cast<KBibTeX::ValueListViewItem*>( item );
219
m_lineEditKey->setText( vlvi->title() );
220
m_fieldLineEditValue->setValue( vlvi->value() );
224
void EntryWidgetOther::addClicked()
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;
231
vlvi->setValue( m_fieldLineEditValue->value() );
233
new ValueListViewItem( key, m_fieldLineEditValue->value(), m_listViewFields );
240
void EntryWidgetOther::deleteClicked()
242
QListViewItem * item = m_listViewFields->findItem( m_lineEditKey->text(), 0 );
247
m_lineEditKey->setText( "" );
248
m_fieldLineEditValue->setValue( new BibTeX::Value() );
255
void EntryWidgetOther::openClicked()
257
if ( m_internalURL.isValid() )
259
BibTeX::Value * value = m_fieldLineEditValue->value();
260
Settings::openUrl( m_internalURL, this );
264
m_fieldLineEditValue->setEnabled( FALSE );
267
ValueListViewItem::ValueListViewItem( const QString& title, BibTeX::Value *value, QListView *parent )
268
: QListViewItem( parent ), m_title( title )
270
m_value = new BibTeX::Value( value );
274
ValueListViewItem::~ValueListViewItem()
279
const BibTeX::Value *ValueListViewItem::value()
284
QString ValueListViewItem::title()
289
void ValueListViewItem::setValue( BibTeX::Value *value )
291
if ( value != m_value )
293
if ( m_value != NULL )
297
m_value = new BibTeX::Value( value );
299
m_value = new BibTeX::Value();
305
void ValueListViewItem::setTitle( const QString &title )
311
void ValueListViewItem::setTexts( const QString& title )
314
if ( m_value != NULL )
315
setText( 1, m_value->text() );
319
#include "entrywidgetother.moc"