kword

KWCreateBookmarkDia.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C)  2002 Montel Laurent <lmontel@mandrakesoft.com>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <klocale.h>
00021 #include "KWDocument.h"
00022 #include <qvbox.h>
00023 #include <qlayout.h>
00024 #include <qlineedit.h>
00025 #include <qpushbutton.h>
00026 #include <qlistbox.h>
00027 #include <kmessagebox.h>
00028 #include "KWCommand.h"
00029 #include "KWCreateBookmarkDia.h"
00030 #include "KWCreateBookmarkDiaBase.h"
00031 #include "KWFrame.h"
00032 #include "KWFrameSet.h"
00033 
00034 KWCreateBookmarkDia::KWCreateBookmarkDia( const QStringList & _list, QWidget *parent, const char *name )
00035     : KDialogBase( parent, name , true, "", Ok|Cancel, Ok, true )
00036 {
00037     setCaption( i18n("Create New Bookmark") );
00038     listBookMark = _list;
00039     init();
00040 }
00041 
00042 KWCreateBookmarkDia::KWCreateBookmarkDia( const QStringList & _list, const QString & _name, QWidget *parent, const char *name )
00043     : KDialogBase( parent, name , true, "", Ok|Cancel, Ok, true )
00044 {
00045     setCaption( i18n("Rename Bookmark") );
00046     listBookMark = _list;
00047     init();
00048     m_bookmarkName->setText(_name);
00049 }
00050 
00051 void KWCreateBookmarkDia::init()
00052 {
00053     KWCreateBookmarkDiaBase *dia = new KWCreateBookmarkDiaBase( this );
00054     m_bookmarkName = dia->m_bookmarkName;
00055     enableButtonOK( false );
00056     connect( m_bookmarkName, SIGNAL(textChanged ( const QString & )), this, SLOT(nameChanged( const QString &)));
00057     setMainWidget( dia );
00058     m_bookmarkName->setFocus();
00059 }
00060 
00061 void KWCreateBookmarkDia::slotOk()
00062 {
00063     if ( listBookMark.findIndex(m_bookmarkName->text() ) != -1 )
00064     {
00065         KMessageBox::error(this, i18n("That name already exists, please choose another name."));
00066     }
00067     else
00068         KDialogBase::slotOk();
00069 }
00070 
00071 QString KWCreateBookmarkDia::bookmarkName()const
00072 {
00073     return m_bookmarkName->text();
00074 }
00075 
00076 void KWCreateBookmarkDia::nameChanged( const QString &text)
00077 {
00078     enableButtonOK( !text.isEmpty() );
00079 }
00080 
00081 
00082 /* ****************************  */
00083 KWSelectBookmarkDia::KWSelectBookmarkDia( const QStringList & _list, KWDocument *_doc, QWidget *parent, const char *name )
00084     : KDialogBase( parent, name , true, "", Ok|Cancel, Ok, true )
00085 {
00086     m_doc=_doc;
00087     setCaption( i18n("Select Bookmark") );
00088     QWidget *page = new QWidget( this );
00089     setMainWidget(page);
00090 
00091     QGridLayout * grid = new QGridLayout(page, 5, 2, KDialog::marginHint(), KDialog::spacingHint());
00092     m_bookmarkList = new QListBox( page );
00093     grid->addMultiCellWidget(m_bookmarkList, 0, 4, 0, 0);
00094     m_bookmarkList->insertStringList(_list);
00095 
00096     connect(m_bookmarkList,  SIGNAL( selectionChanged ()), this, SLOT(slotSelectionChanged()));
00097     connect(m_bookmarkList,  SIGNAL(doubleClicked ( QListBoxItem * )), this, SLOT(slotOk()));
00098     connect(m_bookmarkList,  SIGNAL(returnPressed ( QListBoxItem * )), this, SLOT(slotOk()));
00099 
00100     m_pbRename = new QPushButton( i18n("Rename Bookmark"), page );
00101     grid->addWidget( m_pbRename, 0, 1);
00102     connect( m_pbRename, SIGNAL(clicked()), this, SLOT(slotRenameBookmark()));
00103 
00104     m_pbDelete = new QPushButton( i18n("Delete Bookmark"), page );
00105     grid->addWidget( m_pbDelete, 1, 1);
00106 
00107     connect( m_pbDelete, SIGNAL(clicked()), this, SLOT(slotDeleteBookmark()));
00108 
00109     m_bookmarkList->setFocus();
00110     slotSelectionChanged();
00111 }
00112 
00113 void KWSelectBookmarkDia::slotRenameBookmark()
00114 {
00115     QString tmp =m_bookmarkList->currentText();
00116     if ( tmp.isEmpty() )
00117         return;
00118     //all bookmark name
00119     QStringList lst =m_doc->listOfBookmarkName(0L);
00120     lst.remove( tmp );
00121     KWCreateBookmarkDia dia( lst, tmp, this, 0 );
00122     if ( dia.exec() ) {
00123         QString newName = dia.bookmarkName();
00124         KWRenameBookmarkCommand *cmd = new KWRenameBookmarkCommand( i18n("Rename Bookmark"), tmp, newName, m_doc);
00125         m_doc->addCommand( cmd );
00126         cmd->execute();
00127         m_bookmarkList->changeItem ( newName, m_bookmarkList->currentItem() );
00128     }
00129 }
00130 
00131 void KWSelectBookmarkDia::slotDeleteBookmark()
00132 {
00133     QString tmp =m_bookmarkList->currentText();
00134     if ( !tmp.isEmpty())
00135     {
00136         m_doc->deleteBookmark(tmp);
00137         m_bookmarkList->removeItem(m_bookmarkList->currentItem());
00138     }
00139 }
00140 
00141 
00142 QString KWSelectBookmarkDia::bookmarkSelected()const
00143 {
00144     return m_bookmarkList->currentText();
00145 }
00146 
00147 void KWSelectBookmarkDia::slotSelectionChanged()
00148 {
00149     bool state =!m_bookmarkList->currentText().isEmpty();
00150     enableButtonOK( state );
00151     m_pbRename->setEnabled( state);
00152     m_pbDelete->setEnabled( state && m_doc->isReadWrite());
00153 }
00154 
00155 #include "KWCreateBookmarkDia.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys