• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdefile
 

tdeio/tdefile

  • tdeio
  • tdefile
kurlcombobox.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000,2001 Carsten Pfeiffer <pfeiffer@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2, as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include <tqdir.h>
20 #include <tqlistbox.h>
21 
22 #include <kdebug.h>
23 #include <tdeglobal.h>
24 #include <kiconloader.h>
25 #include <tdelocale.h>
26 #include <kmimetype.h>
27 
28 #include <kurlcombobox.h>
29 
30 class KURLComboBox::KURLComboBoxPrivate
31 {
32 public:
33  KURLComboBoxPrivate() {
34  dirpix = SmallIcon(TQString::fromLatin1("folder"));
35  }
36 
37  TQPixmap dirpix;
38 };
39 
40 
41 KURLComboBox::KURLComboBox( Mode mode, TQWidget *parent, const char *name )
42  : KComboBox( parent, name )
43 {
44  init( mode );
45 }
46 
47 
48 KURLComboBox::KURLComboBox( Mode mode, bool rw, TQWidget *parent,
49  const char *name )
50  : KComboBox( rw, parent, name )
51 {
52  init( mode );
53 }
54 
55 
56 KURLComboBox::~KURLComboBox()
57 {
58  delete d;
59 }
60 
61 
62 void KURLComboBox::init( Mode mode )
63 {
64  d = new KURLComboBoxPrivate();
65 
66  myMode = mode;
67  urlAdded = false;
68  myMaximum = 10; // default
69  itemList.setAutoDelete( true );
70  defaultList.setAutoDelete( true );
71  setInsertionPolicy( NoInsertion );
72  setTrapReturnKey( true );
73  setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed ));
74 
75  opendirPix = SmallIcon(TQString::fromLatin1("folder_open"));
76 
77  connect( this, TQT_SIGNAL( activated( int )), TQT_SLOT( slotActivated( int )));
78 }
79 
80 
81 TQStringList KURLComboBox::urls() const
82 {
83  kdDebug(250) << "::urls()" << endl;
84  //static const TQString &fileProt = TDEGlobal::staticQString("file:");
85  TQStringList list;
86  TQString url;
87  for ( int i = defaultList.count(); i < count(); i++ ) {
88  url = text( i );
89  if ( !url.isEmpty() ) {
90  //if ( url.at(0) == '/' )
91  // list.append( url.prepend( fileProt ) );
92  //else
93  list.append( url );
94  }
95  }
96 
97  return list;
98 }
99 
100 
101 void KURLComboBox::addDefaultURL( const KURL& url, const TQString& text )
102 {
103  addDefaultURL( url, getPixmap( url ), text );
104 }
105 
106 
107 void KURLComboBox::addDefaultURL( const KURL& url, const TQPixmap& pix,
108  const TQString& text )
109 {
110  KURLComboItem *item = new KURLComboItem;
111  item->url = url;
112  item->pixmap = pix;
113  if ( text.isEmpty() )
114  if ( url.isLocalFile() )
115  item->text = url.path( myMode );
116  else
117  item->text = url.prettyURL( myMode );
118  else
119  item->text = text;
120 
121  defaultList.append( item );
122 }
123 
124 
125 void KURLComboBox::setDefaults()
126 {
127  clear();
128  itemMapper.clear();
129 
130  KURLComboItem *item;
131  for ( unsigned int id = 0; id < defaultList.count(); id++ ) {
132  item = defaultList.at( id );
133  insertURLItem( item );
134  }
135 }
136 
137 void KURLComboBox::setURLs( TQStringList urls )
138 {
139  setURLs( urls, RemoveBottom );
140 }
141 
142 void KURLComboBox::setURLs( TQStringList urls, OverLoadResolving remove )
143 {
144  setDefaults();
145  itemList.clear();
146 
147  if ( urls.isEmpty() )
148  return;
149 
150  TQStringList::Iterator it = urls.begin();
151 
152  // kill duplicates
153  TQString text;
154  while ( it != urls.end() ) {
155  while ( urls.contains( *it ) > 1 ) {
156  it = urls.remove( it );
157  continue;
158  }
159  ++it;
160  }
161 
162  // limit to myMaximum items
163  /* Note: overload is an (old) C++ keyword, some compilers (KCC) choke
164  on that, so call it Overload (capital 'O'). (matz) */
165  int Overload = urls.count() - myMaximum + defaultList.count();
166  while ( Overload > 0 ) {
167  urls.remove((remove == RemoveBottom) ? urls.fromLast() : urls.begin());
168  Overload--;
169  }
170 
171  it = urls.begin();
172 
173  KURLComboItem *item = 0L;
174  KURL u;
175 
176  while ( it != urls.end() ) {
177  if ( (*it).isEmpty() ) {
178  ++it;
179  continue;
180  }
181  u = KURL::fromPathOrURL( *it );
182 
183  // Don't restore if file doesn't exist anymore
184  if (u.isLocalFile() && !TQFile::exists(u.path())) {
185  ++it;
186  continue;
187  }
188 
189  item = new KURLComboItem;
190  item->url = u;
191  item->pixmap = getPixmap( u );
192 
193  if ( u.isLocalFile() )
194  item->text = u.path( myMode ); // don't show file:/
195  else
196  item->text = *it;
197 
198  insertURLItem( item );
199  itemList.append( item );
200  ++it;
201  }
202 }
203 
204 
205 void KURLComboBox::setURL( const KURL& url )
206 {
207  if ( url.isEmpty() )
208  return;
209 
210  blockSignals( true );
211 
212  // check for duplicates
213  TQMap<int,const KURLComboItem*>::ConstIterator mit = itemMapper.begin();
214  TQString urlToInsert = url.url(-1);
215  while ( mit != itemMapper.end() ) {
216  if ( urlToInsert == mit.data()->url.url(-1) ) {
217  setCurrentItem( mit.key() );
218 
219  if ( myMode == Directories )
220  updateItem( mit.data(), mit.key(), opendirPix );
221 
222  blockSignals( false );
223  return;
224  }
225  ++mit;
226  }
227 
228  // not in the combo yet -> create a new item and insert it
229 
230  // first remove the old item
231  if ( urlAdded ) {
232  itemList.removeLast();
233  urlAdded = false;
234  }
235 
236  setDefaults();
237 
238  TQPtrListIterator<KURLComboItem> it( itemList );
239  for( ; it.current(); ++it )
240  insertURLItem( it.current() );
241 
242  KURLComboItem *item = new KURLComboItem;
243  item->url = url;
244  item->pixmap = getPixmap( url );
245  if ( url.isLocalFile() )
246  item->text = url.path( myMode );
247  else
248  item->text = url.prettyURL( myMode );
249  kdDebug(250) << "setURL: text=" << item->text << endl;
250 
251  int id = count();
252  TQString text = /*isEditable() ? item->url.prettyURL( myMode ) : */ item->text;
253 
254  if ( myMode == Directories )
255  KComboBox::insertItem( opendirPix, text, id );
256  else
257  KComboBox::insertItem( item->pixmap, text, id );
258  itemMapper.insert( id, item );
259  itemList.append( item );
260 
261  setCurrentItem( id );
262  urlAdded = true;
263  blockSignals( false );
264 }
265 
266 
267 void KURLComboBox::slotActivated( int index )
268 {
269  const KURLComboItem *item = itemMapper[ index ];
270 
271  if ( item ) {
272  setURL( item->url );
273  emit urlActivated( item->url );
274  }
275 }
276 
277 
278 void KURLComboBox::insertURLItem( const KURLComboItem *item )
279 {
280 // kdDebug(250) << "insertURLItem " << item->text << endl;
281  int id = count();
282  KComboBox::insertItem( item->pixmap, item->text, id );
283  itemMapper.insert( id, item );
284 }
285 
286 
287 void KURLComboBox::setMaxItems( int max )
288 {
289  myMaximum = max;
290 
291  if ( count() > myMaximum ) {
292  int oldCurrent = currentItem();
293 
294  setDefaults();
295 
296  TQPtrListIterator<KURLComboItem> it( itemList );
297  int Overload = itemList.count() - myMaximum + defaultList.count();
298  for ( int i = 0; i <= Overload; i++ )
299  ++it;
300 
301  for( ; it.current(); ++it )
302  insertURLItem( it.current() );
303 
304  if ( count() > 0 ) { // restore the previous currentItem
305  if ( oldCurrent >= count() )
306  oldCurrent = count() -1;
307  setCurrentItem( oldCurrent );
308  }
309  }
310 }
311 
312 
313 void KURLComboBox::removeURL( const KURL& url, bool checkDefaultURLs )
314 {
315  TQMap<int,const KURLComboItem*>::ConstIterator mit = itemMapper.begin();
316  while ( mit != itemMapper.end() ) {
317  if ( url.url(-1) == mit.data()->url.url(-1) ) {
318  if ( !itemList.remove( mit.data() ) && checkDefaultURLs )
319  defaultList.remove( mit.data() );
320  }
321  ++mit;
322  }
323 
324  blockSignals( true );
325  setDefaults();
326  TQPtrListIterator<KURLComboItem> it( itemList );
327  while ( it.current() ) {
328  insertURLItem( *it );
329  ++it;
330  }
331  blockSignals( false );
332 }
333 
334 
335 TQPixmap KURLComboBox::getPixmap( const KURL& url ) const
336 {
337  if ( myMode == Directories )
338  return d->dirpix;
339  else
340  return KMimeType::pixmapForURL( url, 0, TDEIcon::Small );
341 }
342 
343 
344 // updates "item" with pixmap "pixmap" and sets the URL instead of text
345 // works around a Qt bug.
346 void KURLComboBox::updateItem( const KURLComboItem *item,
347  int index, const TQPixmap& pixmap )
348 {
349  // TQComboBox::changeItem() doesn't honor the pixmap when
350  // using an editable combobox, so we just remove and insert
351  if ( editable() ) {
352  removeItem( index );
353  insertItem( pixmap,
354  item->url.isLocalFile() ? item->url.path( myMode ) :
355  item->url.prettyURL( myMode ),
356  index );
357  }
358  else
359  changeItem( pixmap, item->text, index );
360 }
361 
362 
363 #include "kurlcombobox.moc"
KURLComboBox::urls
TQStringList urls() const
Definition: kurlcombobox.cpp:81
KURLComboBox::urlActivated
void urlActivated(const KURL &url)
Emitted when an item was clicked at.
KURLComboBox::updateItem
void updateItem(const KURLComboItem *item, int index, const TQPixmap &pix)
Updates item with pixmap and sets the url instead of the text of the KURLComboItem.
Definition: kurlcombobox.cpp:346
KURLComboBox::KURLComboBox
KURLComboBox(Mode mode, TQWidget *parent=0, const char *name=0)
Constructs a KURLComboBox.
Definition: kurlcombobox.cpp:41
KURLComboBox::OverLoadResolving
OverLoadResolving
This Enumeration is used in setURL() to determine which items will be removed when the given list is ...
Definition: kurlcombobox.h:59
KURLComboBox::setMaxItems
void setMaxItems(int)
Sets how many items should be handled and displayed by the combobox.
Definition: kurlcombobox.cpp:287
KURLComboBox::removeURL
void removeURL(const KURL &url, bool checkDefaultURLs=true)
Removes any occurrence of url.
Definition: kurlcombobox.cpp:313
KURLComboBox::setURL
void setURL(const KURL &url)
Sets the current url.
Definition: kurlcombobox.cpp:205
KURLComboBox::setDefaults
void setDefaults()
Clears all items and inserts the default urls into the combo.
Definition: kurlcombobox.cpp:125
KURLComboBox::setURLs
void setURLs(TQStringList urls)
Inserts urls into the combobox below the "default urls" (see addDefaultURL).
Definition: kurlcombobox.cpp:137
KURLComboBox::~KURLComboBox
~KURLComboBox()
Destructs the combo box.
Definition: kurlcombobox.cpp:56
KURLComboBox::addDefaultURL
void addDefaultURL(const KURL &url, const TQString &text=TQString::null)
Adds a url that will always be shown in the combobox, it can't be "rotated away". ...
Definition: kurlcombobox.cpp:101
KURLComboBox::getPixmap
TQPixmap getPixmap(const KURL &url) const
Uses KMimeType::pixmapForURL() to return a proper pixmap for url.
Definition: kurlcombobox.cpp:335
KURLComboBox::Mode
Mode
This enum describes which kind of items is shown in the combo box.
Definition: kurlcombobox.h:51

tdeio/tdefile

Skip menu "tdeio/tdefile"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeio/tdefile

Skip menu "tdeio/tdefile"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdefile by doxygen 1.8.8
This website is maintained by Timothy Pearson.