kspread

link.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Ariya Hidayat <ariya@kde.org>
3 (C) 2003 Norbert Andres <nandres@web.de>
4 (C) 2002 Philipp Mueller <philipp.mueller@gmx.de>
5 (C) 1999-2002 Laurent Montel <montel@kde.org>
6 (C) 1999 Stephan Kulow <coolo@kde.org>
7 (C) 1998-1999 Torben Weis <weis@kde.org>
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
18
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23*/
24
25#include "link.h"
26
27#include <tqcombobox.h>
28#include <tqframe.h>
29#include <tqlabel.h>
30#include <tqlayout.h>
31
32#include <kdesktopfile.h>
33#include <kdialogbase.h>
34#include <kiconloader.h>
35#include <klineedit.h>
36#include <tdemessagebox.h>
37#include <tdelocale.h>
38#include <tderecentdocument.h>
39#include <kurlrequester.h>
40
41using namespace KSpread;
42
43class LinkDialog::Private
44{
45public:
46 TQString text;
47 TQFrame* internetPage;
48 KLineEdit* internetText;
49 KLineEdit* internetLink;
50 TQFrame* mailPage;
51 KLineEdit* mailText;
52 KLineEdit* mailLink;
53 TQFrame* filePage;
54 KLineEdit* fileText;
55 KURLRequester* fileLink;
56 TQFrame* cellPage;
57 KLineEdit* cellText;
58 KLineEdit* cellLink;
59};
60
61LinkDialog::LinkDialog( TQWidget*, const char* )
62 : KDialogBase( KDialogBase::IconList,i18n( "Insert Link") ,
63 KDialogBase::Ok | KDialogBase::Cancel,
64 KDialogBase::Ok )
65{
66 d = new Private;
67
68 // link for web or ftp
69 d->internetPage = addPage( i18n( "Internet" ), TQString(),
70 BarIcon( "text-html",TDEIcon::SizeMedium ) );
71 TQVBoxLayout* iLayout = new TQVBoxLayout( d->internetPage, marginHint(), spacingHint() );
72 iLayout->add( new TQLabel( i18n("Text to display:" ), d->internetPage ) );
73 d->internetText = new KLineEdit( d->internetPage );
74 iLayout->add( d->internetText );
75 iLayout->add( new TQLabel( i18n("Internet address:" ), d->internetPage ) );
76 d->internetLink = new KLineEdit( d->internetPage );
77 iLayout->add( d->internetLink );
78 iLayout->addItem( new TQSpacerItem( 0, 0, TQSizePolicy::Minimum, TQSizePolicy::Expanding ) );
79 connect( d->internetText, TQ_SIGNAL( textChanged( const TQString& ) ), this,
80 TQ_SLOT( setText( const TQString& ) ) );
81
82 // link for e-mail
83 d->mailPage = addPage( i18n( "Mail" ), TQString(),
84 BarIcon( "mail_generic",TDEIcon::SizeMedium ) );
85 TQVBoxLayout* mLayout = new TQVBoxLayout( d->mailPage, marginHint(), spacingHint() );
86 mLayout->add( new TQLabel( i18n("Text to display:" ), d->mailPage ) );
87 d->mailText = new KLineEdit( d->mailPage );
88 mLayout->add( d->mailText );
89 mLayout->add( new TQLabel( i18n("Email:" ), d->mailPage ) );
90 d->mailLink = new KLineEdit( d->mailPage );
91 mLayout->add( d->mailLink );
92 mLayout->addItem( new TQSpacerItem( 0, 0, TQSizePolicy::Minimum, TQSizePolicy::Expanding ) );
93 connect( d->mailText, TQ_SIGNAL( textChanged( const TQString& ) ), this,
94 TQ_SLOT( setText( const TQString& ) ) );
95
96 // link for external file
97 d->filePage = addPage( i18n( "File" ), TQString(),
98 BarIcon( "document-new",TDEIcon::SizeMedium ) );
99 TQVBoxLayout* fLayout = new TQVBoxLayout( d->filePage, marginHint(), spacingHint() );
100 fLayout->add( new TQLabel( i18n("Text to display:" ), d->filePage ) );
101 d->fileText = new KLineEdit( d->filePage );
102 fLayout->add( d->fileText );
103 fLayout->add( new TQLabel( i18n("File location:" ), d->filePage ) );
104 d->fileLink = new KURLRequester( d->filePage );
105 fLayout->add( d->fileLink );
106 fLayout->add( new TQLabel( i18n("Recent file:" ), d->filePage ) );
107 TQComboBox* recentFile = new TQComboBox( d->filePage );
108 recentFile->setSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed );
109 fLayout->add( recentFile );
110 fLayout->addItem( new TQSpacerItem( 0, 40, TQSizePolicy::Minimum, TQSizePolicy::MinimumExpanding ) );
111 connect( d->fileText, TQ_SIGNAL( textChanged( const TQString& ) ), this,
112 TQ_SLOT( setText( const TQString& ) ) );
113 TQObject::connect( recentFile, TQ_SIGNAL( highlighted ( const TQString &) ),
114 d->fileLink->lineEdit(), TQ_SLOT( setText( const TQString & ) ) );
115
116
117 // populate recent files
118 TQStringList fileList = TDERecentDocument::recentDocuments();
119 for( TQStringList::ConstIterator it = fileList.begin();it != fileList.end(); ++it )
120 {
121 KDesktopFile f(*it, true /* read only */);
122 if ( !f.readURL().isEmpty() )
123 recentFile->insertItem( f.readURL() );
124 }
125 if( recentFile->count()==0 )
126 {
127 recentFile->insertItem( i18n("No Entries") );
128 recentFile->setEnabled( false );
129 }
130
131 // link to another cell
132 d->cellPage = addPage( i18n( "Cell" ), TQString(),
133 BarIcon( "misc",TDEIcon::SizeMedium ) );
134 TQVBoxLayout* cLayout = new TQVBoxLayout( d->cellPage, marginHint(), spacingHint() );
135 cLayout->add( new TQLabel( i18n("Text to display:" ), d->cellPage ) );
136 d->cellText = new KLineEdit( d->cellPage );
137 cLayout->add( d->cellText );
138 cLayout->add( new TQLabel( i18n("Cell:" ), d->cellPage ) );
139 d->cellLink = new KLineEdit( d->cellPage );
140 cLayout->add( d->cellLink );
141 cLayout->addItem( new TQSpacerItem( 0, 0, TQSizePolicy::Minimum, TQSizePolicy::Expanding ) );
142 connect( d->cellText, TQ_SIGNAL( textChanged( const TQString& ) ), this,
143 TQ_SLOT( setText( const TQString& ) ) );
144
145 enableButtonSeparator( true );
146 d->internetText->setFocus();
147 resize( 400,300 );
148}
149
150LinkDialog::~LinkDialog()
151{
152 delete d;
153}
154
155TQString LinkDialog::text() const
156{
157 return d->text;
158}
159
160TQString LinkDialog::link() const
161{
162 TQString str;
163 switch( activePageIndex() )
164 {
165 case 0:
166 str = d->internetLink->text();
167 if( !str.isEmpty() )
168 if( str.find( "http://" )==-1 )
169 if( str.find( "https://" )==-1 )
170 if( str.find( "ftp://" )==-1 )
171 str.prepend( "http://" );
172 break;
173
174 case 1:
175 str = d->mailLink->text();
176 if( !str.isEmpty() )
177 if( str.find( "mailto:" )==-1 )
178 str.prepend( "mailto:" );
179 break;
180
181 case 2:
182 str = d->fileLink->lineEdit()->text();
183 if( !str.isEmpty() )
184 if( str.find( "file:/" )==-1 )
185 str.prepend( "file://" );
186 break;
187
188 case 3:
189 str = d->cellLink->text();
190 break;
191
192 break;
193 }
194 return str;
195}
196
197void LinkDialog::setText( const TQString& text )
198{
199 d->text = text;
200
201 d->internetText->blockSignals( true );
202 d->internetText->setText( text );
203 d->internetText->blockSignals( false );
204
205 d->mailText->blockSignals( true );
206 d->mailText->setText( text );
207 d->mailText->blockSignals( false );
208
209 d->fileText->blockSignals( true );
210 d->fileText->setText( text );
211 d->fileText->blockSignals( false );
212
213 d->cellText->blockSignals( true );
214 d->cellText->setText( text );
215 d->cellText->blockSignals( false );
216}
217
218// link must be complete, e.g. "http://www.koffice.org" instead of
219// "www.koffice.org" only, since protocol is used to decide which page to show
220void LinkDialog::setLink( const TQString& link )
221{
222 if( link.startsWith( "https://" ) )
223 {
224 d->internetLink->setText( link.mid( TQString("https://").length() ) );
225 showPage( 0 );
226 return;
227 }
228
229 if( link.startsWith( "http://" ) )
230 {
231 d->internetLink->setText( link.mid( TQString("http://").length() ) );
232 showPage( 0 );
233 return;
234 }
235
236 if( link.startsWith( "ftp://" ) )
237 {
238 d->internetLink->setText( link.mid( TQString("ftp://").length() ) );
239 showPage( 0 );
240 return;
241 }
242
243 if( link.startsWith( "mailto:" ) )
244 {
245 d->mailLink->setText( link.mid( TQString("mailto:").length() ) );
246 showPage( 1 );
247 return;
248 }
249
250 if( link.startsWith( "file:/" ) )
251 {
252 TQString s = link.mid( TQString("file:/").length() );
253 while(s.startsWith("//")) s.remove(0,1);
254 d->fileLink->lineEdit()->setText(s);
255 showPage( 2 );
256 return;
257 }
258
259 // assume cell reference
260 d->cellLink->setText( link );
261 showPage( 3 );
262}
263
264void LinkDialog::slotOk()
265{
266 TQString str;
267 switch( activePageIndex() )
268 {
269 case 0: str = i18n( "Internet address is empty" ); break;
270 case 1: str = i18n( "Mail address is empty" ); break;
271 case 2: str = i18n( "File name is empty" ); break;
272 case 3: str = i18n( "Destination cell is empty" ); break;
273 break;
274 }
275
276 if( link().isEmpty() )
277 {
278 KMessageBox::error( this, str );
279 return;
280 }
281
282 if( d->text.isEmpty() )
283 d->text = link();
284
285 accept();
286}
287
288#include "link.moc"
The KSpread namespace.
Definition commands.h:39