• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kfile
kurlcombobox.cpp
Go to the documentation of this file.
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 "kurlcombobox.h"
20
21#include <QtCore/QDir>
22#include <QtGui/QMouseEvent>
23#include <QtGui/QDrag>
24
25#include <kdebug.h>
26#include <kglobalsettings.h>
27#include <kicon.h>
28#include <klocale.h>
29#include <kmimetype.h>
30#include <kiconloader.h>
31
32class KUrlComboBox::KUrlComboBoxPrivate
33{
34public:
35 KUrlComboBoxPrivate(KUrlComboBox *parent)
36 : m_parent(parent),
37 dirIcon(QLatin1String("folder"))
38 {}
39
40 ~KUrlComboBoxPrivate()
41 {
42 qDeleteAll( itemList );
43 qDeleteAll( defaultList );
44 }
45
46 typedef struct {
47 QString text;
48 KUrl url;
49 QIcon icon;
50 } KUrlComboItem;
51
52 void init( Mode mode );
53 void insertUrlItem( const KUrlComboItem * );
54 QIcon getIcon( const KUrl& url ) const;
55 void updateItem( const KUrlComboItem *item, int index, const QIcon& icon );
56
57 void _k_slotActivated( int );
58
59 KUrlComboBox *m_parent;
60 KIcon dirIcon;
61 bool urlAdded;
62 int myMaximum;
63 Mode myMode; // can be used as parameter to KUR::path( int ) or url( int )
64 // to specify if we want a trailing slash or not
65 QPoint m_dragPoint;
66
67 QList<const KUrlComboItem*> itemList;
68 QList<const KUrlComboItem*> defaultList;
69 QMap<int,const KUrlComboItem*> itemMapper;
70
71 QIcon opendirIcon;
72};
73
74
75KUrlComboBox::KUrlComboBox( Mode mode, QWidget *parent)
76 : KComboBox( parent),d(new KUrlComboBoxPrivate(this))
77{
78 d->init( mode );
79}
80
81
82KUrlComboBox::KUrlComboBox( Mode mode, bool rw, QWidget *parent)
83 : KComboBox( rw, parent),d(new KUrlComboBoxPrivate(this))
84{
85 d->init( mode );
86}
87
88
89KUrlComboBox::~KUrlComboBox()
90{
91 delete d;
92}
93
94
95void KUrlComboBox::KUrlComboBoxPrivate::init( Mode mode )
96{
97 myMode = mode;
98 urlAdded = false;
99 myMaximum = 10; // default
100 m_parent->setInsertPolicy( NoInsert );
101 m_parent->setTrapReturnKey( true );
102 m_parent->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ));
103 m_parent->setLayoutDirection( Qt::LeftToRight );
104 if ( m_parent->completionObject() ) {
105 m_parent->completionObject()->setOrder( KCompletion::Sorted );
106 }
107
108 opendirIcon = KIcon(QLatin1String("folder-open"));
109
110 m_parent->connect( m_parent, SIGNAL(activated(int)), SLOT(_k_slotActivated(int)));
111}
112
113
114QStringList KUrlComboBox::urls() const
115{
116 kDebug(250) << "::urls()";
117 //static const QString &fileProt = KGlobal::staticQString("file:");
118 QStringList list;
119 QString url;
120 for ( int i = d->defaultList.count(); i < count(); i++ ) {
121 url = itemText( i );
122 if ( !url.isEmpty() ) {
123 //if ( url.at(0) == '/' )
124 // list.append( url.prepend( fileProt ) );
125 //else
126 list.append( url );
127 }
128 }
129
130 return list;
131}
132
133
134void KUrlComboBox::addDefaultUrl( const KUrl& url, const QString& text )
135{
136 addDefaultUrl( url, d->getIcon( url ), text );
137}
138
139
140void KUrlComboBox::addDefaultUrl( const KUrl& url, const QIcon& icon,
141 const QString& text )
142{
143 KUrlComboBoxPrivate::KUrlComboItem *item = new KUrlComboBoxPrivate::KUrlComboItem;
144 item->url = url;
145 item->icon = icon;
146 if ( text.isEmpty() )
147 item->text = url.pathOrUrl(d->myMode == Directories
148 ? KUrl::AddTrailingSlash
149 : KUrl::RemoveTrailingSlash);
150 else
151 item->text = text;
152
153 d->defaultList.append( item );
154}
155
156
157void KUrlComboBox::setDefaults()
158{
159 clear();
160 d->itemMapper.clear();
161
162 const KUrlComboBoxPrivate::KUrlComboItem *item;
163 for ( int id = 0; id < d->defaultList.count(); id++ ) {
164 item = d->defaultList.at( id );
165 d->insertUrlItem( item );
166 }
167}
168
169void KUrlComboBox::setUrls( const QStringList &urls )
170{
171 setUrls( urls, RemoveBottom );
172}
173
174void KUrlComboBox::setUrls( const QStringList &_urls, OverLoadResolving remove )
175{
176 setDefaults();
177 qDeleteAll( d->itemList );
178 d->itemList.clear();
179 d->urlAdded = false;
180
181 if ( _urls.isEmpty() )
182 return;
183
184 QStringList urls;
185 QStringList::ConstIterator it = _urls.constBegin();
186
187 // kill duplicates
188 while ( it != _urls.constEnd() ) {
189 if ( !urls.contains( *it ) )
190 urls += *it;
191 ++it;
192 }
193
194 // limit to myMaximum items
195 /* Note: overload is an (old) C++ keyword, some compilers (KCC) choke
196 on that, so call it Overload (capital 'O'). (matz) */
197 int Overload = urls.count() - d->myMaximum + d->defaultList.count();
198 while ( Overload > 0) {
199 if (remove == RemoveBottom) {
200 if (!urls.isEmpty())
201 urls.removeLast();
202 }
203 else {
204 if (!urls.isEmpty())
205 urls.removeFirst();
206 }
207 Overload--;
208 }
209
210 it = urls.constBegin();
211
212 KUrlComboBoxPrivate::KUrlComboItem *item = 0L;
213
214 while ( it != urls.constEnd() ) {
215 if ( (*it).isEmpty() ) {
216 ++it;
217 continue;
218 }
219 KUrl u = *it;
220
221 // Don't restore if file doesn't exist anymore
222 if (u.isLocalFile() && !QFile::exists(u.toLocalFile())) {
223 ++it;
224 continue;
225 }
226
227 item = new KUrlComboBoxPrivate::KUrlComboItem;
228 item->url = u;
229 item->icon = d->getIcon( u );
230 item->text = u.pathOrUrl(d->myMode == Directories
231 ? KUrl::AddTrailingSlash
232 : KUrl::RemoveTrailingSlash);
233
234 d->insertUrlItem( item );
235 d->itemList.append( item );
236 ++it;
237 }
238}
239
240
241void KUrlComboBox::setUrl( const KUrl& url )
242{
243 if ( url.isEmpty() )
244 return;
245
246 bool blocked = blockSignals( true );
247
248 // check for duplicates
249 QMap<int,const KUrlComboBoxPrivate::KUrlComboItem*>::ConstIterator mit = d->itemMapper.constBegin();
250 QString urlToInsert = url.url(KUrl::RemoveTrailingSlash);
251 while ( mit != d->itemMapper.constEnd() ) {
252 Q_ASSERT( mit.value() );
253
254 if ( urlToInsert == mit.value()->url.url(KUrl::RemoveTrailingSlash) ) {
255 setCurrentIndex( mit.key() );
256
257 if (d->myMode == Directories)
258 d->updateItem( mit.value(), mit.key(), d->opendirIcon );
259
260 blockSignals( blocked );
261 return;
262 }
263 ++mit;
264 }
265
266 // not in the combo yet -> create a new item and insert it
267
268 // first remove the old item
269 if (d->urlAdded) {
270 Q_ASSERT(!d->itemList.isEmpty());
271 d->itemList.removeLast();
272 d->urlAdded = false;
273 }
274
275 setDefaults();
276
277 int offset = qMax (0, d->itemList.count() - d->myMaximum + d->defaultList.count());
278 for ( int i = offset; i < d->itemList.count(); i++ )
279 d->insertUrlItem( d->itemList[i] );
280
281 KUrlComboBoxPrivate::KUrlComboItem *item = new KUrlComboBoxPrivate::KUrlComboItem;
282 item->url = url;
283 item->icon = d->getIcon( url );
284 item->text = url.pathOrUrl(d->myMode == Directories
285 ? KUrl::AddTrailingSlash
286 : KUrl::RemoveTrailingSlash);
287 kDebug(250) << "setURL: text=" << item->text;
288
289 int id = count();
290 QString text = /*isEditable() ? item->url.prettyUrl( (KUrl::AdjustPathOption)myMode ) : */ item->text;
291
292 if (d->myMode == Directories)
293 KComboBox::insertItem( id, d->opendirIcon, text);
294 else
295 KComboBox::insertItem( id,item->icon, text);
296
297 d->itemMapper.insert( id, item );
298 d->itemList.append( item );
299
300 setCurrentIndex( id );
301 Q_ASSERT(!d->itemList.isEmpty());
302 d->urlAdded = true;
303 blockSignals( blocked );
304}
305
306
307void KUrlComboBox::KUrlComboBoxPrivate::_k_slotActivated( int index )
308{
309 const KUrlComboItem *item = itemMapper.value(index);
310
311 if ( item ) {
312 m_parent->setUrl( item->url );
313 emit m_parent->urlActivated( item->url );
314 }
315}
316
317
318void KUrlComboBox::KUrlComboBoxPrivate::insertUrlItem( const KUrlComboBoxPrivate::KUrlComboItem *item )
319{
320 Q_ASSERT( item );
321
322// kDebug(250) << "insertURLItem " << item->text;
323 int id = m_parent->count();
324 m_parent->KComboBox::insertItem(id, item->icon, item->text);
325 itemMapper.insert( id, item );
326}
327
328
329void KUrlComboBox::setMaxItems( int max )
330{
331 d->myMaximum = max;
332
333 if (count() > d->myMaximum) {
334 int oldCurrent = currentIndex();
335
336 setDefaults();
337
338 int offset = qMax (0, d->itemList.count() - d->myMaximum + d->defaultList.count());
339 for ( int i = offset; i < d->itemList.count(); i++ )
340 d->insertUrlItem( d->itemList[i] );
341
342 if ( count() > 0 ) { // restore the previous currentItem
343 if ( oldCurrent >= count() )
344 oldCurrent = count() -1;
345 setCurrentIndex( oldCurrent );
346 }
347 }
348}
349
350int KUrlComboBox::maxItems() const
351{
352 return d->myMaximum;
353}
354
355void KUrlComboBox::removeUrl( const KUrl& url, bool checkDefaultURLs )
356{
357 QMap<int,const KUrlComboBoxPrivate::KUrlComboItem*>::ConstIterator mit = d->itemMapper.constBegin();
358 while ( mit != d->itemMapper.constEnd() ) {
359 if ( url.url(KUrl::RemoveTrailingSlash) == mit.value()->url.url(KUrl::RemoveTrailingSlash) ) {
360 if ( !d->itemList.removeAll( mit.value() ) && checkDefaultURLs )
361 d->defaultList.removeAll( mit.value() );
362 }
363 ++mit;
364 }
365
366 bool blocked = blockSignals( true );
367 setDefaults();
368 QListIterator<const KUrlComboBoxPrivate::KUrlComboItem*> it( d->itemList );
369 while ( it.hasNext() ) {
370 d->insertUrlItem( it.next() );
371 }
372 blockSignals( blocked );
373}
374
375void KUrlComboBox::setCompletionObject(KCompletion* compObj, bool hsig)
376{
377 if ( compObj ) {
378 // on a url combo box we want completion matches to be sorted. This way, if we are given
379 // a suggestion, we match the "best" one. For instance, if we have "foo" and "foobar",
380 // and we write "foo", the match is "foo" and never "foobar". (ereslibre)
381 compObj->setOrder( KCompletion::Sorted );
382 }
383 KComboBox::setCompletionObject( compObj, hsig );
384}
385
386void KUrlComboBox::mousePressEvent(QMouseEvent *event)
387{
388 QStyleOptionComboBox comboOpt;
389 comboOpt.initFrom(this);
390 const int x0 = QStyle::visualRect(layoutDirection(), rect(),
391 style()->subControlRect(QStyle::CC_ComboBox, &comboOpt, QStyle::SC_ComboBoxEditField, this)).x();
392 const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &comboOpt, this);
393
394 if (event->x() < (x0 + KIconLoader::SizeSmall + frameWidth)) {
395 d->m_dragPoint = event->pos();
396 } else {
397 d->m_dragPoint = QPoint();
398 }
399
400 KComboBox::mousePressEvent(event);
401}
402
403void KUrlComboBox::mouseMoveEvent(QMouseEvent *event)
404{
405 const int index = currentIndex();
406
407 if (!itemIcon(index).isNull() && !d->m_dragPoint.isNull() && event->buttons() & Qt::LeftButton &&
408 (event->pos() - d->m_dragPoint).manhattanLength() > KGlobalSettings::dndEventDelay()) {
409 QDrag *drag = new QDrag(this);
410 QMimeData *mime = new QMimeData();
411 mime->setUrls(QList<QUrl>() << KUrl(itemText(index)));
412 mime->setText(itemText(index));
413 drag->setPixmap(itemIcon(index).pixmap(KIconLoader::SizeMedium));
414 drag->setMimeData(mime);
415 drag->exec();
416 }
417
418 KComboBox::mouseMoveEvent(event);
419}
420
421QIcon KUrlComboBox::KUrlComboBoxPrivate::getIcon( const KUrl& url ) const
422{
423 if (myMode == Directories)
424 return dirIcon;
425 else
426 return KIcon(KMimeType::iconNameForUrl(url, 0));
427}
428
429
430// updates "item" with icon "icon" and sets the URL instead of text
431void KUrlComboBox::KUrlComboBoxPrivate::updateItem( const KUrlComboBoxPrivate::KUrlComboItem *item,
432 int index, const QIcon& icon)
433{
434 m_parent->setItemIcon(index,icon);
435
436 if ( m_parent->isEditable() ) {
437 m_parent->setItemText(index, item->url.pathOrUrl(myMode == Directories
438 ? KUrl::AddTrailingSlash
439 : KUrl::RemoveTrailingSlash));
440 }
441 else {
442 m_parent->setItemText(index,item->text);
443 }
444}
445
446
447#include "kurlcombobox.moc"
KComboBox
KCompletionBase::setCompletionObject
virtual void setCompletionObject(KCompletion *compObj, bool hsig=true)
KCompletion
KCompletion::setOrder
virtual void setOrder(CompOrder order)
KCompletion::Sorted
Sorted
KGlobalSettings::dndEventDelay
static int dndEventDelay()
KIconLoader::SizeMedium
SizeMedium
KIconLoader::SizeSmall
SizeSmall
KIcon
KMimeType::iconNameForUrl
static QString iconNameForUrl(const KUrl &url, mode_t mode=0)
KUrlComboBox
This combobox shows a number of recent URLs/directories, as well as some default directories.
Definition: kurlcombobox.h:48
KUrlComboBox::setUrls
void setUrls(const QStringList &urls)
Inserts urls into the combobox below the "default urls" (see addDefaultUrl).
Definition: kurlcombobox.cpp:169
KUrlComboBox::mousePressEvent
virtual void mousePressEvent(QMouseEvent *event)
Definition: kurlcombobox.cpp:386
KUrlComboBox::OverLoadResolving
OverLoadResolving
This Enumeration is used in setUrl() to determine which items will be removed when the given list is ...
Definition: kurlcombobox.h:65
KUrlComboBox::RemoveBottom
@ RemoveBottom
Definition: kurlcombobox.h:65
KUrlComboBox::KUrlComboBox
KUrlComboBox(Mode mode, QWidget *parent=0)
Constructs a KUrlComboBox.
Definition: kurlcombobox.cpp:75
KUrlComboBox::setUrl
void setUrl(const KUrl &url)
Sets the current url.
Definition: kurlcombobox.cpp:241
KUrlComboBox::setCompletionObject
virtual void setCompletionObject(KCompletion *compObj, bool hsig=true)
Reimplemented from KComboBox (from KCompletion)
Definition: kurlcombobox.cpp:375
KUrlComboBox::urls
QStringList urls
Definition: kurlcombobox.h:50
KUrlComboBox::removeUrl
void removeUrl(const KUrl &url, bool checkDefaultURLs=true)
Removes any occurrence of url.
Definition: kurlcombobox.cpp:355
KUrlComboBox::~KUrlComboBox
~KUrlComboBox()
Destructs the combo box.
Definition: kurlcombobox.cpp:89
KUrlComboBox::maxItems
int maxItems
Definition: kurlcombobox.h:51
KUrlComboBox::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *event)
Definition: kurlcombobox.cpp:403
KUrlComboBox::setMaxItems
void setMaxItems(int)
Sets how many items should be handled and displayed by the combobox.
Definition: kurlcombobox.cpp:329
KUrlComboBox::Mode
Mode
This enum describes which kind of items is shown in the combo box.
Definition: kurlcombobox.h:57
KUrlComboBox::Directories
@ Directories
Definition: kurlcombobox.h:57
KUrlComboBox::setDefaults
void setDefaults()
Clears all items and inserts the default urls into the combo.
Definition: kurlcombobox.cpp:157
KUrlComboBox::addDefaultUrl
void addDefaultUrl(const KUrl &url, const QString &text=QString())
Adds a url that will always be shown in the combobox, it can't be "rotated away".
Definition: kurlcombobox.cpp:134
KUrl
KUrl::pathOrUrl
QString pathOrUrl() const
KUrl::RemoveTrailingSlash
RemoveTrailingSlash
KUrl::AddTrailingSlash
AddTrailingSlash
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
KUrl::isLocalFile
bool isLocalFile() const
KUrl::toLocalFile
QString toLocalFile(AdjustPathOption trailing=LeaveTrailingSlash) const
QList
QMap
QWidget
kDebug
#define kDebug
kdebug.h
kglobalsettings.h
kicon.h
kiconloader.h
klocale.h
kmimetype.h
kurlcombobox.h
clear
KAction * clear(const QObject *recvr, const char *slot, QObject *parent)
remove
KGuiItem remove()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal