You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kbibtex/src/settingsidsuggestions.cpp

401 lines
18 KiB

12 years ago
/***************************************************************************
* 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 <ntqlayout.h>
#include <ntqcheckbox.h>
#include <ntqtimer.h>
#include <ntqvalidator.h>
#include <ntqheader.h>
#include <ntqlabel.h>
12 years ago
#include <kinputdialog.h>
#include <klistview.h>
#include <kiconloader.h>
#include <kpushbutton.h>
#include <klocale.h>
#include <kdialog.h>
#include <klineedit.h>
#include <idsuggestions.h>
#include <idsuggestionswidget.h>
#include <settings.h>
#include <fileimporter.h>
#include <fileimporterbibtex.h>
#include "settingsidsuggestions.h"
namespace KBibTeX
{
TQString SettingsIdSuggestions::exampleBibTeXEntry = "@Article{ dijkstra1983terminationdetect,\nauthor = {Edsger W. Dijkstra and W. H. J. Feijen and A. J. M. {van Gasteren}},\ntitle = {{Derivation of a Termination Detection Algorithm for Distributed Computations}},\njournal = {Information Processing Letters},\nvolume = 16,\nnumber = 5,\npages = {217--219},\nmonth = jun,\nyear = 1983\n}";
12 years ago
IdSuggestionsListViewItem::IdSuggestionsListViewItem( KListView *list, const TQString& plainText, BibTeX::Entry *example ): KListViewItem( list ), m_original( plainText ), m_example( example )
12 years ago
{
KListViewItem::setText( 0, parse( plainText ) );
KListViewItem::setMultiLinesEnabled( TRUE );
}
IdSuggestionsListViewItem::IdSuggestionsListViewItem( KListView *list, KListViewItem *prev, const TQString& plainText, BibTeX::Entry *example ): KListViewItem( list, prev ), m_original( plainText ), m_example( example )
12 years ago
{
KListViewItem::setText( 0, parse( plainText ) );
KListViewItem::setMultiLinesEnabled( TRUE );
}
void IdSuggestionsListViewItem::setText( int col, const TQString& text )
12 years ago
{
if ( col == 0 )
KListViewItem::setText( 0, parse( text ) );
else
KListViewItem::setText( col, text );
}
int IdSuggestionsListViewItem::width( const TQFontMetrics & fm, const TQListView *, int c ) const
12 years ago
{
int max = 100;
TQStringList lines = TQStringList::split( '\n', text( c ) );
for ( TQStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
12 years ago
{
int w = fm.width( *it );
if ( w > max ) max = w;
}
return max + 36;
}
TQString IdSuggestionsListViewItem::originalText() const
12 years ago
{
return m_original;
}
TQString IdSuggestionsListViewItem::parse( const TQString&plainText )
12 years ago
{
m_original = plainText;
TQString result = IdSuggestions::formatStrToHuman( plainText );
12 years ago
if ( m_example != NULL )
{
result.append( TQString( i18n( "\nExample: %1" ) ).arg( IdSuggestions::formatId( m_example, plainText ) ) );
12 years ago
}
return result;
}
SettingsIdSuggestions::SettingsIdSuggestions( TQWidget *parent, const char *name )
: TQWidget( parent, name )
12 years ago
{
m_validator = new TQRegExpValidator( TQRegExp( "[^\\s]+" ), this );
12 years ago
setupGUI();
BibTeX::FileImporter *importer = new BibTeX::FileImporterBibTeX( false );
BibTeX::File *file = importer->load( exampleBibTeXEntry );
m_example = new BibTeX::Entry( dynamic_cast<BibTeX::Entry*>( *( file->begin() ) ) );
delete file;
delete importer;
}
SettingsIdSuggestions::~SettingsIdSuggestions()
{
delete m_example;
delete m_validator;
}
void SettingsIdSuggestions::applyData()
{
Settings * settings = Settings::self();
settings->idSuggestions_formatStrList.clear();
settings->idSuggestions_default = -1;
settings->idSuggestions_forceDefault = m_checkBoxForceDefault->isChecked();
int i = 0;
for ( TQListViewItemIterator it( m_listIdSuggestions ); it.current(); ++i, ++it )
12 years ago
{
IdSuggestionsListViewItem *item = dynamic_cast<IdSuggestionsListViewItem*>( *it );
settings->idSuggestions_formatStrList.append( item->originalText() );
if ( item == m_defaultSuggestionItem )
settings->idSuggestions_default = i;
}
settings->idSuggestions_smallWords.clear();
for ( TQListViewItemIterator it( m_listSmallWords ); it.current(); ++it )
12 years ago
settings->idSuggestions_smallWords << it.current()->text( 0 );
}
void SettingsIdSuggestions::readData()
{
Settings * settings = Settings::self();
IdSuggestionsListViewItem *prev = NULL;
m_listIdSuggestions->clear();
m_defaultSuggestionItem = NULL;
m_checkBoxForceDefault->setChecked( settings->idSuggestions_forceDefault );
m_checkBoxForceDefault->setEnabled( settings->idSuggestions_default >= 0 );
int i = 0;
for ( TQStringList::ConstIterator it = settings->idSuggestions_formatStrList.begin(); it != settings->idSuggestions_formatStrList.end(); ++i, ++it )
12 years ago
{
prev = new IdSuggestionsListViewItem( m_listIdSuggestions, prev, *it, m_example );
prev->setPixmap( 0, SmallIcon( "filter" ) );
if ( i == settings->idSuggestions_default )
m_defaultSuggestionItem = prev;
}
if ( m_defaultSuggestionItem != NULL )
m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
m_listSmallWords->clear();
for ( TQStringList::ConstIterator it = settings->idSuggestions_smallWords.begin(); it != settings->idSuggestions_smallWords.end(); ++it )
12 years ago
new KListViewItem( m_listSmallWords, *it );
m_lineEditSmallWords->setText( "" );
slotListSmallWordsChanged();
updateGUI();
}
void SettingsIdSuggestions::slotConfigChanged()
{
emit configChanged();
}
void SettingsIdSuggestions::slotNewIdSuggestion()
{
IdSuggestionsListViewItem * item = new IdSuggestionsListViewItem( m_listIdSuggestions, "a|Y|T", m_example );
item->setPixmap( 0, SmallIcon( "filter" ) );
m_listIdSuggestions->setSelected( item, TRUE );
TQTimer::singleShot( 100, this, SLOT( slotEditIdSuggestion() ) );
12 years ago
}
void SettingsIdSuggestions::slotEditIdSuggestion()
{
IdSuggestionsListViewItem * item = static_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
if ( item != NULL )
{
TQString formatStr = item->originalText();
if ( IdSuggestionsWidget::execute( formatStr, this ) == TQDialog::Accepted )
12 years ago
{
item->setText( 0, formatStr );
emit configChanged();
}
}
updateGUI();
}
void SettingsIdSuggestions::slotDeleteIdSuggestion()
{
IdSuggestionsListViewItem * item = static_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
if ( item != NULL )
{
if ( m_defaultSuggestionItem == item )
m_defaultSuggestionItem = NULL;
m_checkBoxForceDefault->setEnabled( m_defaultSuggestionItem != NULL );
delete item;
emit configChanged();
}
updateGUI();
}
void SettingsIdSuggestions::slotMoveUpIdSuggestion()
{
IdSuggestionsListViewItem * item = dynamic_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
if ( item != NULL && item -> itemAbove() != NULL )
{
IdSuggestionsListViewItem *itemAbove = dynamic_cast<IdSuggestionsListViewItem*>( item->itemAbove() );
TQString text = item->originalText();
12 years ago
item->setText( 0, itemAbove->originalText( ) );
itemAbove->setText( 0, text );
m_listIdSuggestions->setCurrentItem( itemAbove );
m_listIdSuggestions->ensureItemVisible( itemAbove );
if ( m_defaultSuggestionItem == itemAbove )
{
itemAbove->setPixmap( 0, SmallIcon( "filter" ) );
m_defaultSuggestionItem = item;
}
else if ( m_defaultSuggestionItem == item )
{
item->setPixmap( 0, SmallIcon( "filter" ) );
m_defaultSuggestionItem = itemAbove;
}
if ( m_defaultSuggestionItem != NULL )
m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
}
}
void SettingsIdSuggestions::slotMoveDownIdSuggestion()
{
IdSuggestionsListViewItem * item = dynamic_cast<IdSuggestionsListViewItem*>( m_listIdSuggestions->selectedItem() );
if ( item != NULL && item -> itemBelow() != NULL )
{
IdSuggestionsListViewItem *itemBelow = dynamic_cast<IdSuggestionsListViewItem*>( item->itemBelow() );
TQString text = item->originalText();
12 years ago
item->setText( 0, itemBelow->originalText( ) );
itemBelow->setText( 0, text );
m_listIdSuggestions->setCurrentItem( itemBelow );
m_listIdSuggestions->ensureItemVisible( itemBelow );
if ( m_defaultSuggestionItem == itemBelow )
{
itemBelow->setPixmap( 0, SmallIcon( "filter" ) );
m_defaultSuggestionItem = item;
}
else if ( m_defaultSuggestionItem == item )
{
item->setPixmap( 0, SmallIcon( "filter" ) );
m_defaultSuggestionItem = itemBelow;
}
if ( m_defaultSuggestionItem != NULL )
m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
}
}
void SettingsIdSuggestions::slotToggleDefault()
{
if ( m_defaultSuggestionItem != NULL )
m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "filter" ) );
TQListViewItem *item = m_listIdSuggestions->selectedItem();
12 years ago
if ( item == m_defaultSuggestionItem )
m_defaultSuggestionItem = NULL;
else
{
m_defaultSuggestionItem = item;
m_defaultSuggestionItem->setPixmap( 0, SmallIcon( "favorites" ) );
}
m_checkBoxForceDefault->setEnabled( m_defaultSuggestionItem != NULL );
}
void SettingsIdSuggestions::slotEditSmallWordsChanged( const TQString &newWord )
12 years ago
{
bool result = !newWord.isEmpty();
for ( TQListViewItemIterator it( m_listSmallWords ); result && it.current(); ++it )
12 years ago
result &= it.current()->text( 0 ) != newWord;
m_buttonAddSmallWord->setEnabled( result );
}
void SettingsIdSuggestions::slotAddSmallWord()
{
new KListViewItem( m_listSmallWords, m_lineEditSmallWords->text() );
m_lineEditSmallWords->setText( "" );
emit configChanged();
}
void SettingsIdSuggestions::slotDeleteSmallWord()
{
bool changed = false;
TQListViewItemIterator it( m_listSmallWords, TQListViewItemIterator::Selected );
12 years ago
while ( it.current() )
{
TQListViewItem *cur = it.current();
12 years ago
++it;
delete cur;
changed = true;
}
if ( changed )
emit configChanged();
}
void SettingsIdSuggestions::slotListSmallWordsChanged()
{
TQListViewItemIterator it( m_listSmallWords, TQListViewItemIterator::Selected );
12 years ago
m_buttonDeleteSmallWord->setEnabled( it.current() != NULL );
}
void SettingsIdSuggestions::updateGUI()
{
TQListViewItem *item = m_listIdSuggestions->selectedItem();
12 years ago
bool selected = item != NULL;
m_buttonEditIdSuggestion->setEnabled( selected );
m_buttonDeleteIdSuggestion->setEnabled( selected );
m_buttonMoveDownIdSuggestion->setEnabled( selected && item->itemBelow() != NULL );
m_buttonMoveUpIdSuggestion->setEnabled( selected && item->itemAbove() != NULL );
m_buttonToggleDefault->setEnabled( selected );
}
void SettingsIdSuggestions::setupGUI()
{
TQGridLayout * gridLayout = new TQGridLayout( this, 13, 2, 0, KDialog::spacingHint(), "gridLayout" );
12 years ago
gridLayout->setRowStretch( 6, 2 );
gridLayout->setRowSpacing( 8, KDialog::spacingHint() );
gridLayout->setRowStretch( 12, 1 );
gridLayout->setColStretch( 0, 1 );
m_listIdSuggestions = new KListView( this );
m_listIdSuggestions->setSorting( -1, FALSE );
m_listIdSuggestions->addColumn( i18n( "Id Suggestions" ) );
m_listIdSuggestions->header()->setClickEnabled( FALSE );
m_listIdSuggestions->setFullWidth( true );
gridLayout->addMultiCellWidget( m_listIdSuggestions, 0, 6, 0, 0 );
connect( m_listIdSuggestions, SIGNAL( selectionChanged() ), this, SLOT( updateGUI() ) );
connect( m_listIdSuggestions, SIGNAL( currentChanged( TQListViewItem * ) ), this, SLOT( updateGUI() ) );
connect( m_listIdSuggestions, SIGNAL( doubleClicked( TQListViewItem*, const TQPoint &, int ) ), this, SLOT( slotEditIdSuggestion() ) );
12 years ago
m_buttonNewIdSuggestion = new KPushButton( i18n( "id suggestion", "New" ), this );
m_buttonNewIdSuggestion->setIconSet( TQIconSet( SmallIcon( "add" ) ) );
12 years ago
gridLayout->addWidget( m_buttonNewIdSuggestion, 0, 1 );
connect( m_buttonNewIdSuggestion, SIGNAL( clicked() ), this, SLOT( slotNewIdSuggestion() ) );
m_buttonEditIdSuggestion = new KPushButton( i18n( "id suggestion", "Edit" ), this );
m_buttonEditIdSuggestion->setIconSet( TQIconSet( SmallIcon( "edit" ) ) );
12 years ago
gridLayout->addWidget( m_buttonEditIdSuggestion, 1, 1 );
connect( m_buttonEditIdSuggestion, SIGNAL( clicked() ), this, SLOT( slotEditIdSuggestion() ) );
m_buttonDeleteIdSuggestion = new KPushButton( i18n( "id suggestion", "Delete" ), this );
m_buttonDeleteIdSuggestion->setIconSet( TQIconSet( SmallIcon( "editdelete" ) ) );
12 years ago
gridLayout->addWidget( m_buttonDeleteIdSuggestion, 2, 1 );
connect( m_buttonDeleteIdSuggestion, SIGNAL( clicked() ), this, SLOT( slotDeleteIdSuggestion() ) );
m_buttonMoveUpIdSuggestion = new KPushButton( i18n( "id suggestion", "Up" ), this );
m_buttonMoveUpIdSuggestion->setIconSet( TQIconSet( SmallIcon( "up" ) ) );
12 years ago
gridLayout->addWidget( m_buttonMoveUpIdSuggestion, 3, 1 );
connect( m_buttonMoveUpIdSuggestion, SIGNAL( clicked() ), this, SLOT( slotMoveUpIdSuggestion() ) );
m_buttonMoveDownIdSuggestion = new KPushButton( i18n( "id suggestion", "Down" ), this );
m_buttonMoveDownIdSuggestion->setIconSet( TQIconSet( SmallIcon( "down" ) ) );
12 years ago
gridLayout->addWidget( m_buttonMoveDownIdSuggestion, 4, 1 );
connect( m_buttonMoveDownIdSuggestion, SIGNAL( clicked() ), this, SLOT( slotMoveDownIdSuggestion() ) );
m_buttonToggleDefault = new KPushButton( i18n( "Toogle default" ), this );
m_buttonToggleDefault->setIconSet( TQIconSet( SmallIcon( "favorites" ) ) );
12 years ago
gridLayout->addWidget( m_buttonToggleDefault, 5, 1 );
connect( m_buttonToggleDefault, SIGNAL( clicked() ), this, SLOT( slotToggleDefault() ) );
m_checkBoxForceDefault = new TQCheckBox( i18n( "Use default id suggestion when editing new entries" ), this );
12 years ago
gridLayout->addMultiCellWidget( m_checkBoxForceDefault, 7, 7, 0, 1 );
TQLabel *label = new TQLabel( i18n( "Small Words:" ), this );
12 years ago
gridLayout->addMultiCellWidget( label, 9, 9, 0, 1 );
m_lineEditSmallWords = new KLineEdit( this );
label->setBuddy( m_lineEditSmallWords );
gridLayout->addWidget( m_lineEditSmallWords, 10, 0 );
connect( m_lineEditSmallWords, SIGNAL( textChanged( const TQString & ) ), this, SLOT( slotEditSmallWordsChanged( const TQString & ) ) );
12 years ago
m_buttonAddSmallWord = new KPushButton( i18n( "Add" ), this );
m_buttonAddSmallWord->setEnabled( false );
m_buttonAddSmallWord->setIconSet( TQIconSet( SmallIcon( "add" ) ) );
12 years ago
gridLayout->addWidget( m_buttonAddSmallWord, 10, 1 );
connect( m_buttonAddSmallWord, SIGNAL( clicked() ), this, SLOT( slotAddSmallWord() ) );
m_listSmallWords = new KListView( this );
m_listSmallWords->addColumn( i18n( "Small Words" ) );
m_listSmallWords->header()->setClickEnabled( true );
m_listSmallWords->setFullWidth( true );
connect( m_listSmallWords, SIGNAL( selectionChanged() ), this, SLOT( slotListSmallWordsChanged() ) );
connect( m_listSmallWords, SIGNAL( currentChanged( TQListViewItem * ) ), this, SLOT( slotListSmallWordsChanged() ) );
12 years ago
gridLayout->addMultiCellWidget( m_listSmallWords, 11, 12, 0, 0 );
m_buttonDeleteSmallWord = new KPushButton( i18n( "Delete" ), this );
m_buttonDeleteSmallWord->setEnabled( false );
m_buttonDeleteSmallWord->setIconSet( TQIconSet( SmallIcon( "editdelete" ) ) );
12 years ago
gridLayout->addWidget( m_buttonDeleteSmallWord, 11, 1 );
connect( m_buttonDeleteSmallWord, SIGNAL( clicked() ), this, SLOT( slotDeleteSmallWord() ) );
}
}
#include "settingsidsuggestions.moc"