kspread

link.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Ariya Hidayat <ariya@kde.org>
00003              (C) 2003 Norbert Andres <nandres@web.de>
00004              (C) 2002 Philipp Mueller <philipp.mueller@gmx.de>
00005              (C) 1999-2002 Laurent Montel <montel@kde.org>
00006              (C) 1999 Stephan Kulow <coolo@kde.org>
00007              (C) 1998-1999 Torben Weis <weis@kde.org>
00008    
00009    This library is free software; you can redistribute it and/or
00010    modify it under the terms of the GNU Library General Public
00011    License as published by the Free Software Foundation; either
00012    version 2 of the License, or (at your option) any later version.
00013 
00014    This library is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017    Library General Public License for more details.
00018 
00019    You should have received a copy of the GNU Library General Public License
00020    along with this library; see the file COPYING.LIB.  If not, write to
00021    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00022  * Boston, MA 02110-1301, USA.
00023 */
00024 
00025 #include "link.h"
00026 
00027 #include <qcombobox.h>
00028 #include <qframe.h>
00029 #include <qlabel.h>
00030 #include <qlayout.h>
00031 
00032 #include <kdesktopfile.h>
00033 #include <kdialogbase.h>
00034 #include <kiconloader.h>
00035 #include <klineedit.h>
00036 #include <kmessagebox.h>
00037 #include <klocale.h>
00038 #include <krecentdocument.h>
00039 #include <kurlrequester.h>
00040 
00041 using namespace KSpread;
00042 
00043 class LinkDialog::Private
00044 {
00045 public:
00046     QString text;
00047     QFrame* internetPage;
00048     KLineEdit* internetText;
00049     KLineEdit* internetLink;
00050     QFrame* mailPage;
00051     KLineEdit* mailText;
00052     KLineEdit* mailLink;
00053     QFrame* filePage;
00054     KLineEdit* fileText;
00055     KURLRequester* fileLink;
00056     QFrame* cellPage;
00057     KLineEdit* cellText;
00058     KLineEdit* cellLink;
00059 };
00060 
00061 LinkDialog::LinkDialog( QWidget*, const char* )
00062   :  KDialogBase( KDialogBase::IconList,i18n( "Insert Link") ,
00063                   KDialogBase::Ok | KDialogBase::Cancel,
00064                   KDialogBase::Ok )
00065 {
00066     d = new Private;
00067     
00068     // link for web or ftp
00069     d->internetPage = addPage( i18n( "Internet" ), QString::null, 
00070         BarIcon( "html",KIcon::SizeMedium ) );
00071     QVBoxLayout* iLayout = new QVBoxLayout( d->internetPage, marginHint(), spacingHint() );
00072     iLayout->add( new QLabel( i18n("Text to display:" ), d->internetPage ) );
00073     d->internetText = new KLineEdit( d->internetPage );
00074     iLayout->add( d->internetText );
00075     iLayout->add( new QLabel( i18n("Internet address:" ), d->internetPage ) );
00076     d->internetLink = new KLineEdit( d->internetPage );
00077     iLayout->add( d->internetLink );
00078     iLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding ) );
00079     connect( d->internetText, SIGNAL( textChanged( const QString& ) ), this,
00080         SLOT( setText( const QString& ) ) );
00081     
00082     // link for e-mail
00083     d->mailPage = addPage( i18n( "Mail" ), QString::null, 
00084         BarIcon( "mail_generic",KIcon::SizeMedium ) );
00085     QVBoxLayout* mLayout = new QVBoxLayout( d->mailPage, marginHint(), spacingHint() );
00086     mLayout->add( new QLabel( i18n("Text to display:" ), d->mailPage ) );
00087     d->mailText = new KLineEdit( d->mailPage );
00088     mLayout->add( d->mailText );
00089     mLayout->add( new QLabel( i18n("Email:" ), d->mailPage ) );
00090     d->mailLink = new KLineEdit( d->mailPage );
00091     mLayout->add( d->mailLink );
00092     mLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding ) );
00093     connect( d->mailText, SIGNAL( textChanged( const QString& ) ), this,
00094         SLOT( setText( const QString& ) ) );
00095     
00096     // link for external file
00097     d->filePage = addPage( i18n( "File" ), QString::null, 
00098         BarIcon( "filenew",KIcon::SizeMedium ) );
00099     QVBoxLayout* fLayout = new QVBoxLayout( d->filePage, marginHint(), spacingHint() );
00100     fLayout->add( new QLabel( i18n("Text to display:" ), d->filePage ) );
00101     d->fileText = new KLineEdit( d->filePage );
00102     fLayout->add( d->fileText );
00103     fLayout->add( new QLabel( i18n("File location:" ), d->filePage ) );
00104     d->fileLink = new KURLRequester( d->filePage );
00105     fLayout->add( d->fileLink );
00106     fLayout->add( new QLabel( i18n("Recent file:" ), d->filePage ) );
00107     QComboBox* recentFile = new QComboBox( d->filePage );
00108     recentFile->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00109     fLayout->add( recentFile );
00110     fLayout->addItem( new QSpacerItem( 0, 40, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding ) );
00111     connect( d->fileText, SIGNAL( textChanged( const QString& ) ), this,
00112         SLOT( setText( const QString& ) ) );
00113     QObject::connect( recentFile, SIGNAL( highlighted ( const QString &) ),
00114         d->fileLink->lineEdit(), SLOT( setText( const QString & ) ) );
00115     
00116     
00117     // populate recent files
00118     QStringList fileList = KRecentDocument::recentDocuments();
00119     for( QStringList::ConstIterator it = fileList.begin();it != fileList.end(); ++it )
00120     {
00121         KDesktopFile f(*it, true /* read only */);
00122         if ( !f.readURL().isEmpty() )
00123             recentFile->insertItem( f.readURL() );
00124     }
00125     if( recentFile->count()==0 )
00126     {
00127         recentFile->insertItem( i18n("No Entries") );
00128         recentFile->setEnabled( false );
00129     }    
00130     
00131     // link to another cell
00132     d->cellPage =  addPage( i18n( "Cell" ), QString::null, 
00133         BarIcon( "misc",KIcon::SizeMedium ) );
00134     QVBoxLayout* cLayout = new QVBoxLayout( d->cellPage, marginHint(), spacingHint() );
00135     cLayout->add( new QLabel( i18n("Text to display:" ), d->cellPage ) );
00136     d->cellText = new KLineEdit( d->cellPage );
00137     cLayout->add( d->cellText );
00138     cLayout->add( new QLabel( i18n("Cell:" ), d->cellPage ) );
00139     d->cellLink = new KLineEdit( d->cellPage );
00140     cLayout->add( d->cellLink );
00141     cLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding ) );
00142     connect( d->cellText, SIGNAL( textChanged( const QString& ) ), this,
00143         SLOT( setText( const QString& ) ) );
00144     
00145     enableButtonSeparator( true );
00146     d->internetText->setFocus();
00147     resize( 400,300 );
00148 }
00149 
00150 LinkDialog::~LinkDialog()
00151 {
00152     delete d;
00153 }
00154 
00155 QString LinkDialog::text() const
00156 {
00157     return d->text;
00158 }
00159 
00160 QString LinkDialog::link() const
00161 {
00162     QString str;
00163     switch( activePageIndex() )
00164     {
00165         case 0:  
00166           str = d->internetLink->text();  
00167           if( !str.isEmpty() )
00168           if( str.find( "http://" )==-1 )
00169           if( str.find( "https://" )==-1 )
00170           if( str.find( "ftp://" )==-1 )
00171               str.prepend( "http://" );
00172           break;
00173           
00174         case 1:  
00175            str = d->mailLink->text(); 
00176            if( !str.isEmpty() )
00177            if( str.find( "mailto:" )==-1 )
00178                str.prepend( "mailto:" );
00179            break;
00180            
00181         case 2:  
00182            str = d->fileLink->lineEdit()->text(); 
00183            if( !str.isEmpty() )
00184            if( str.find( "file:/" )==-1 )
00185                str.prepend( "file://" );
00186            break;
00187            
00188         case 3:  
00189             str = d->cellLink->text(); 
00190             break;
00191             
00192         break;
00193     }
00194     return str;
00195 }
00196 
00197 void LinkDialog::setText( const QString& text )
00198 {
00199     d->text = text;
00200     
00201     d->internetText->blockSignals( true );
00202     d->internetText->setText( text );
00203     d->internetText->blockSignals( false );
00204     
00205     d->mailText->blockSignals( true );
00206     d->mailText->setText( text );
00207     d->mailText->blockSignals( false );
00208 
00209     d->fileText->blockSignals( true );
00210     d->fileText->setText( text );
00211     d->fileText->blockSignals( false );
00212     
00213     d->cellText->blockSignals( true );
00214     d->cellText->setText( text );
00215     d->cellText->blockSignals( false );
00216 }
00217 
00218 // link must be complete, e.g. "http://www.koffice.org" instead of 
00219 // "www.koffice.org" only, since protocol is used to decide which page to show
00220 void LinkDialog::setLink( const QString& link )
00221 {
00222     if( link.startsWith( "https://" ) )
00223     {
00224       d->internetLink->setText( link.mid( QString("https://").length() ) );
00225       showPage( 0 );
00226       return;
00227     }
00228     
00229     if( link.startsWith( "http://" ) )
00230     {
00231       d->internetLink->setText( link.mid( QString("http://").length() ) );
00232       showPage( 0 );
00233       return;
00234     }
00235     
00236     if( link.startsWith( "ftp://" ) )
00237     {
00238       d->internetLink->setText( link.mid( QString("ftp://").length() ) );
00239       showPage( 0 );
00240       return;
00241     }
00242     
00243     if( link.startsWith( "mailto:" ) )
00244     {
00245       d->mailLink->setText( link.mid( QString("mailto:").length() ) );
00246       showPage( 1 );
00247       return;
00248     }
00249     
00250     if( link.startsWith( "file:/" ) )
00251     {
00252       QString s = link.mid( QString("file:/").length() );
00253       while(s.startsWith("//")) s.remove(0,1);
00254       d->fileLink->lineEdit()->setText(s);
00255       showPage( 2 );
00256       return;
00257     }
00258     
00259     // assume cell reference
00260     d->cellLink->setText( link );
00261     showPage( 3 );
00262 }
00263 
00264 void LinkDialog::slotOk()
00265 {
00266     QString str;
00267     switch( activePageIndex() )
00268     {
00269         case 0:  str = i18n( "Internet address is empty" );  break;
00270         case 1:  str = i18n( "Mail address is empty" ); break;
00271         case 2:  str = i18n( "File name is empty" ); break;
00272         case 3:  str = i18n( "Destination cell is empty" ); break;
00273         break;
00274     }
00275     
00276     if( link().isEmpty() )
00277     {
00278        KMessageBox::error( this, str );
00279        return;
00280     } 
00281     
00282     if( d->text.isEmpty() )
00283         d->text = link();
00284     
00285     accept();
00286 }
00287 
00288 #include "link.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys