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

KFile

  • sources
  • kde-4.14
  • kdelibs
  • kfile
kfilefiltercombo.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) Stephan Kulow <coolo@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 as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kfilefiltercombo.h"
21 
22 #include <kdebug.h>
23 #include <klocale.h>
24 #include <kmimetype.h>
25 #include <config-kfile.h>
26 #include <QtCore/QEvent>
27 #include <QtGui/QLineEdit>
28 
29 class KFileFilterCombo::Private
30 {
31 public:
32  Private( KFileFilterCombo *_parent )
33  : parent(_parent),
34  hasAllSupportedFiles(false),
35  isMimeFilter(false),
36  defaultFilter(i18n("*|All Files"))
37  {
38  }
39 
40  void _k_slotFilterChanged();
41 
42  KFileFilterCombo *parent;
43  // when we have more than 3 mimefilters and no default-filter,
44  // we don't show the comments of all mimefilters in one line,
45  // instead we show "All supported files". We have to translate
46  // that back to the list of mimefilters in currentFilter() tho.
47  bool hasAllSupportedFiles;
48  // true when setMimeFilter was called
49  bool isMimeFilter;
50  QString lastFilter;
51  QString defaultFilter;
52 
53  QStringList m_filters;
54  bool m_allTypes;
55 };
56 
57 KFileFilterCombo::KFileFilterCombo( QWidget *parent)
58  : KComboBox(true, parent), d( new Private(this) )
59 {
60  setTrapReturnKey( true );
61  setInsertPolicy(QComboBox::NoInsert);
62  connect( this, SIGNAL(activated(int)), this, SIGNAL(filterChanged()));
63  connect( this, SIGNAL(returnPressed()), this, SIGNAL(filterChanged()));
64  connect( this, SIGNAL(filterChanged()), SLOT(_k_slotFilterChanged()));
65  d->m_allTypes = false;
66 }
67 
68 KFileFilterCombo::~KFileFilterCombo()
69 {
70  delete d;
71 }
72 
73 void KFileFilterCombo::setFilter(const QString& filter)
74 {
75  clear();
76  d->m_filters.clear();
77  d->hasAllSupportedFiles = false;
78 
79  if (!filter.isEmpty()) {
80  QString tmp = filter;
81  int index = tmp.indexOf('\n');
82  while (index > 0) {
83  d->m_filters.append(tmp.left(index));
84  tmp = tmp.mid(index + 1);
85  index = tmp.indexOf('\n');
86  }
87  d->m_filters.append(tmp);
88  }
89  else
90  d->m_filters.append( d->defaultFilter );
91 
92  QStringList::ConstIterator it;
93  QStringList::ConstIterator end(d->m_filters.constEnd());
94  for (it = d->m_filters.constBegin(); it != end; ++it) {
95  int tab = (*it).indexOf('|');
96  addItem((tab < 0) ? *it :
97  (*it).mid(tab + 1));
98  }
99 
100  d->lastFilter = currentText();
101  d->isMimeFilter = false;
102 }
103 
104 QString KFileFilterCombo::currentFilter() const
105 {
106  QString f = currentText();
107  if (f == itemText(currentIndex())) { // user didn't edit the text
108  f = d->m_filters.value(currentIndex());
109  if ( d->isMimeFilter || (currentIndex() == 0 && d->hasAllSupportedFiles) ) {
110  return f; // we have a mimetype as filter
111  }
112  }
113 
114  int tab = f.indexOf('|');
115  if (tab < 0)
116  return f;
117  else
118  return f.left(tab);
119 }
120 
121 bool KFileFilterCombo::showsAllTypes() const
122 {
123  return d->m_allTypes;
124 }
125 
126 QStringList KFileFilterCombo::filters() const
127 {
128  return d->m_filters;
129 }
130 
131 void KFileFilterCombo::setCurrentFilter( const QString& filter )
132 {
133  setCurrentIndex(d->m_filters.indexOf(filter));
134  filterChanged();
135 }
136 
137 void KFileFilterCombo::setMimeFilter( const QStringList& types,
138  const QString& defaultType )
139 {
140  clear();
141  d->m_filters.clear();
142  QString delim = QLatin1String(", ");
143  d->hasAllSupportedFiles = false;
144  bool hasAllFilesFilter = false;
145 
146  d->m_allTypes = defaultType.isEmpty() && (types.count() > 1);
147 
148  QString allComments, allTypes;
149  for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it)
150  {
151  kDebug(kfile_area) << *it;
152  KMimeType::Ptr type = KMimeType::mimeType( *it );
153 
154  if (!type) {
155  kDebug(kfile_area) << "Could not create mimetype!\n";
156  continue;
157  }
158 
159  if ( type->name().startsWith( QLatin1String( "all/" ) ) ) {
160  hasAllFilesFilter = true;
161  continue;
162  }
163 
164  if ( d->m_allTypes && it != types.begin() ) {
165  allComments += delim;
166  allTypes += ' ';
167  }
168 
169  d->m_filters.append( type->name() );
170  if ( d->m_allTypes )
171  {
172  allTypes += type->name();
173  allComments += type->comment();
174  }
175  addItem( type->comment() );
176  if ( type->name() == defaultType )
177  setCurrentIndex( count() - 1 );
178  }
179 
180  if ( d->m_allTypes )
181  {
182  if ( count() <= 3 ) // show the mime-comments of at max 3 types
183  insertItem(0, allComments);
184  else {
185  insertItem(0, i18n("All Supported Files"));
186  d->hasAllSupportedFiles = true;
187  }
188  setCurrentIndex( 0 );
189 
190  d->m_filters.prepend( allTypes );
191  }
192 
193  if ( hasAllFilesFilter ) {
194  addItem(i18n("All Files"));
195  d->m_filters.append( QLatin1String("all/allfiles") );
196  }
197 
198  d->lastFilter = currentText();
199  d->isMimeFilter = true;
200 }
201 
202 void KFileFilterCombo::Private::_k_slotFilterChanged()
203 {
204  lastFilter = parent->currentText();
205 }
206 
207 bool KFileFilterCombo::eventFilter( QObject *o, QEvent *e )
208 {
209  if ( o == lineEdit() && e->type() == QEvent::FocusOut ) {
210  if ( currentText() != d->lastFilter )
211  emit filterChanged();
212  }
213 
214  return KComboBox::eventFilter( o, e );
215 }
216 
217 void KFileFilterCombo::setDefaultFilter( const QString& filter )
218 {
219  d->defaultFilter = filter;
220 }
221 
222 QString KFileFilterCombo::defaultFilter() const
223 {
224  return d->defaultFilter;
225 }
226 
227 bool KFileFilterCombo::isMimeFilter() const
228 {
229  return d->isMimeFilter;
230 }
231 
232 #include "kfilefiltercombo.moc"
i18n
QString i18n(const char *text)
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QEvent
QWidget
QEvent::type
Type type() const
kdebug.h
kmimetype.h
KFileFilterCombo::isMimeFilter
bool isMimeFilter() const
Returns true if the filter has been set using setMimeFilter().
Definition: kfilefiltercombo.cpp:227
KFileFilterCombo::setFilter
void setFilter(const QString &filter)
Sets the filter string.
Definition: kfilefiltercombo.cpp:73
QComboBox::clear
void clear()
KFileFilterCombo::currentFilter
QString currentFilter() const
Definition: kfilefiltercombo.cpp:104
QComboBox::itemText
QString itemText(int index) const
KFileFilterCombo::setCurrentFilter
void setCurrentFilter(const QString &filter)
Sets the current filter.
Definition: kfilefiltercombo.cpp:131
KFileFilterCombo::defaultFilter
QString defaultFilter() const
Definition: kfilefiltercombo.cpp:222
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
klocale.h
QComboBox::insertItem
void insertItem(int index, const QString &text, const QVariant &userData)
config-kfile.h
KFileFilterCombo::KFileFilterCombo
KFileFilterCombo(QWidget *parent=0)
Creates a new filter combo box.
Definition: kfilefiltercombo.cpp:57
QComboBox::addItem
void addItem(const QString &text, const QVariant &userData)
KFileFilterCombo::~KFileFilterCombo
~KFileFilterCombo()
Destroys the filter combo box.
Definition: kfilefiltercombo.cpp:68
QList::count
int count(const T &value) const
QComboBox::count
int count() const
KFileFilterCombo::filters
QStringList filters() const
Definition: kfilefiltercombo.cpp:126
KFileFilterCombo::filterChanged
void filterChanged()
This signal is emitted whenever the filter has been changed.
QObject
QString::isEmpty
bool isEmpty() const
KFileFilterCombo::showsAllTypes
bool showsAllTypes() const
Definition: kfilefiltercombo.cpp:121
QComboBox::activated
void activated(int index)
KComboBox::returnPressed
void returnPressed()
QString
QStringList
QList::end
iterator end()
KComboBox::setTrapReturnKey
void setTrapReturnKey(bool trap)
KFileFilterCombo
Definition: kfilefiltercombo.h:29
KFileFilterCombo::eventFilter
virtual bool eventFilter(QObject *, QEvent *)
Definition: kfilefiltercombo.cpp:207
QComboBox::lineEdit
QLineEdit * lineEdit() const
KFileFilterCombo::setMimeFilter
void setMimeFilter(const QStringList &types, const QString &defaultType)
Sets a list of mimetypes.
Definition: kfilefiltercombo.cpp:137
QString::mid
QString mid(int position, int n) const
QLatin1String
QComboBox::setInsertPolicy
void setInsertPolicy(InsertPolicy policy)
QList::ConstIterator
typedef ConstIterator
KComboBox
QComboBox::currentIndex
int currentIndex() const
QString::left
QString left(int n) const
KFileFilterCombo::setDefaultFilter
void setDefaultFilter(const QString &filter)
This method allows you to set a default-filter, that is used when an empty filter is set...
Definition: kfilefiltercombo.cpp:217
QComboBox::currentText
QString currentText() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
end
const KShortcut & end()
KComboBox::eventFilter
virtual bool eventFilter(QObject *, QEvent *)
kfile_area
const int kfile_area
QList::begin
iterator begin()
kfilefiltercombo.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:27:26 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KFile

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

kdelibs API Reference

Skip menu "kdelibs 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

Search



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