kspread Library API Documentation

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., 59 Temple Place - Suite 330,
00022    Boston, MA 02111-1307, 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 class LinkDialogPrivate
00042 {
00043 public:
00044     QString text;
00045     QFrame* internetPage;
00046     KLineEdit* internetText;
00047     KLineEdit* internetLink;
00048     QFrame* mailPage;
00049     KLineEdit* mailText;
00050     KLineEdit* mailLink;
00051     QFrame* filePage;
00052     KLineEdit* fileText;
00053     KURLRequester* fileLink;
00054     QFrame* cellPage;
00055     KLineEdit* cellText;
00056     KLineEdit* cellLink;
00057 };
00058 
00059 LinkDialog::LinkDialog( QWidget* parent, const char* name )
00060   :  KDialogBase( KDialogBase::IconList,i18n( "Insert Link") ,
00061                   KDialogBase::Ok | KDialogBase::Cancel,
00062                   KDialogBase::Ok )
00063 {
00064     d = new LinkDialogPrivate;
00065     
00066     // link for web or ftp
00067     d->internetPage = addPage( i18n( "Internet" ), QString::null, 
00068         BarIcon( "html",KIcon::SizeMedium ) );
00069     QVBoxLayout* iLayout = new QVBoxLayout( d->internetPage, marginHint(), spacingHint() );
00070     iLayout->add( new QLabel( i18n("Text to display:" ), d->internetPage ) );
00071     d->internetText = new KLineEdit( d->internetPage );
00072     iLayout->add( d->internetText );
00073     iLayout->add( new QLabel( i18n("Internet address:" ), d->internetPage ) );
00074     d->internetLink = new KLineEdit( d->internetPage );
00075     iLayout->add( d->internetLink );
00076     iLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding ) );
00077     connect( d->internetText, SIGNAL( textChanged( const QString& ) ), this,
00078         SLOT( setText( const QString& ) ) );
00079     
00080     // link for e-mail
00081     d->mailPage = addPage( i18n( "Mail" ), QString::null, 
00082         BarIcon( "mail_generic",KIcon::SizeMedium ) );
00083     QVBoxLayout* mLayout = new QVBoxLayout( d->mailPage, marginHint(), spacingHint() );
00084     mLayout->add( new QLabel( i18n("Text to display:" ), d->mailPage ) );
00085     d->mailText = new KLineEdit( d->mailPage );
00086     mLayout->add( d->mailText );
00087     mLayout->add( new QLabel( i18n("Email:" ), d->mailPage ) );
00088     d->mailLink = new KLineEdit( d->mailPage );
00089     mLayout->add( d->mailLink );
00090     mLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding ) );
00091     connect( d->mailText, SIGNAL( textChanged( const QString& ) ), this,
00092         SLOT( setText( const QString& ) ) );
00093     
00094     // link for external file
00095     d->filePage = addPage( i18n( "File" ), QString::null, 
00096         BarIcon( "filenew",KIcon::SizeMedium ) );
00097     QVBoxLayout* fLayout = new QVBoxLayout( d->filePage, marginHint(), spacingHint() );
00098     fLayout->add( new QLabel( i18n("Text to display:" ), d->filePage ) );
00099     d->fileText = new KLineEdit( d->filePage );
00100     fLayout->add( d->fileText );
00101     fLayout->add( new QLabel( i18n("File location:" ), d->filePage ) );
00102     d->fileLink = new KURLRequester( d->filePage );
00103     fLayout->add( d->fileLink );
00104     fLayout->add( new QLabel( i18n("Recent file:" ), d->filePage ) );
00105     QComboBox* recentFile = new QComboBox( d->filePage );
00106     recentFile->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00107     fLayout->add( recentFile );
00108     fLayout->addItem( new QSpacerItem( 0, 40, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding ) );
00109     connect( d->mailText, SIGNAL( textChanged( const QString& ) ), this,
00110         SLOT( setText( const QString& ) ) );
00111     QObject::connect( recentFile, SIGNAL( highlighted ( const QString &) ),
00112         d->fileLink->lineEdit(), SLOT( setText( const QString & ) ) );
00113     
00114     
00115     // populate recent files
00116     QStringList fileList = KRecentDocument::recentDocuments();
00117     for( QStringList::ConstIterator it = fileList.begin();it != fileList.end(); ++it )
00118     {
00119         KDesktopFile f(*it, true /* read only */);
00120         if ( !f.readURL().isEmpty() )
00121             recentFile->insertItem( f.readURL() );
00122     }
00123     if( recentFile->count()==0 )
00124     {
00125         recentFile->insertItem( i18n("No Entries") );
00126         recentFile->setEnabled( false );
00127     }    
00128     
00129     // link to another cell
00130     d->cellPage =  addPage( i18n( "Cell" ), QString::null, 
00131         BarIcon( "misc",KIcon::SizeMedium ) );
00132     QVBoxLayout* cLayout = new QVBoxLayout( d->cellPage, marginHint(), spacingHint() );
00133     cLayout->add( new QLabel( i18n("Text to display:" ), d->cellPage ) );
00134     d->cellText = new KLineEdit( d->cellPage );
00135     cLayout->add( d->cellText );
00136     cLayout->add( new QLabel( i18n("Cell:" ), d->cellPage ) );
00137     d->cellLink = new KLineEdit( d->cellPage );
00138     cLayout->add( d->cellLink );
00139     cLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding ) );
00140     connect( d->cellText, SIGNAL( textChanged( const QString& ) ), this,
00141         SLOT( setText( const QString& ) ) );
00142     
00143     enableButtonSeparator( true );
00144     d->internetText->setFocus();
00145     resize( 400,300 );
00146 }
00147 
00148 LinkDialog::~LinkDialog()
00149 {
00150     delete d;
00151 }
00152 
00153 QString LinkDialog::text() const
00154 {
00155     return d->text;
00156 }
00157 
00158 QString LinkDialog::link() const
00159 {
00160     QString str;
00161     switch( activePageIndex() )
00162     {
00163         case 0:  
00164           str = d->internetLink->text();  
00165           if( !str.isEmpty() )
00166           if( str.find( "http://" )==-1 )
00167           if( str.find( "https://" )==-1 )
00168           if( str.find( "ftp://" )==-1 )
00169               str.prepend( "http://" );
00170           break;
00171           
00172         case 1:  
00173            str = d->mailLink->text(); 
00174            if( !str.isEmpty() )
00175            if( str.find( "mailto:" )==-1 )
00176                str.prepend( "mailto:" );
00177            break;
00178            
00179         case 2:  
00180            str = d->fileLink->lineEdit()->text(); 
00181            if( !str.isEmpty() )
00182            if( str.find( "file:/" )==-1 )
00183                str.prepend( "file:/" );
00184            break;
00185            
00186         case 3:  
00187             str = d->cellLink->text(); 
00188             break;
00189             
00190         break;
00191     }
00192     return str;
00193 }
00194 
00195 void LinkDialog::setText( const QString& text )
00196 {
00197     d->text = text;
00198     
00199     d->internetText->blockSignals( true );
00200     d->internetText->setText( text );
00201     d->internetText->blockSignals( false );
00202     
00203     d->mailText->blockSignals( true );
00204     d->mailText->setText( text );
00205     d->mailText->blockSignals( false );
00206 
00207     d->fileText->blockSignals( true );
00208     d->fileText->setText( text );
00209     d->fileText->blockSignals( false );
00210     
00211     d->cellText->blockSignals( true );
00212     d->cellText->setText( text );
00213     d->cellText->blockSignals( false );
00214 }
00215 
00216 // link must be complete, e.g. "http://www.koffice.org" instead of 
00217 // "www.koffice.org" only, since protocol is used to decide which page to show
00218 void LinkDialog::setLink( const QString& link )
00219 {
00220     if( link.startsWith( "https://" ) )
00221     {
00222       d->internetLink->setText( link.mid( QString("https://").length() ) );
00223       showPage( 0 );
00224       return;
00225     }
00226     
00227     if( link.startsWith( "http://" ) )
00228     {
00229       d->internetLink->setText( link.mid( QString("http://").length() ) );
00230       showPage( 0 );
00231       return;
00232     }
00233     
00234     if( link.startsWith( "ftp://" ) )
00235     {
00236       d->internetLink->setText( link.mid( QString("ftp://").length() ) );
00237       showPage( 0 );
00238       return;
00239     }
00240     
00241     if( link.startsWith( "mailto:" ) )
00242     {
00243       d->mailLink->setText( link.mid( QString("mailto:").length() ) );
00244       showPage( 1 );
00245       return;
00246     }
00247     
00248     if( link.startsWith( "file:/" ) )
00249     {
00250       d->fileLink->lineEdit()->setText( link.mid( QString("file:/").length() ) );
00251       showPage( 2 );
00252       return;
00253     }
00254     
00255     // assume cell reference
00256     d->cellLink->setText( link );
00257     showPage( 3 );
00258 }
00259 
00260 void LinkDialog::slotOk()
00261 {
00262     QString str;
00263     switch( activePageIndex() )
00264     {
00265         case 0:  str = i18n( "Internet address is empty" );  break;
00266         case 1:  str = i18n( "Mail address is empty" ); break;
00267         case 2:  str = i18n( "File name is empty" ); break;
00268         case 3:  str = i18n( "Destination cell is empty" ); break;
00269         break;
00270     }
00271     
00272     if( link().isEmpty() )
00273     {
00274        KMessageBox::error( this, str );
00275        return;
00276     } 
00277     
00278     if( d->text.isEmpty() )
00279         d->text = link();
00280     
00281     accept();
00282 }
00283 
00284 #include "link.moc"
KDE Logo
This file is part of the documentation for kspread Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:43:38 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003