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

tdeio/tdefile

  • tdeio
  • tdefile
tdefilefiltercombo.cpp
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 <tdelocale.h>
21#include <kdebug.h>
22#include <kstaticdeleter.h>
23#include <config-tdefile.h>
24
25#include "tdefilefiltercombo.h"
26
27class KFileFilterCombo::KFileFilterComboPrivate
28{
29public:
30 KFileFilterComboPrivate() {
31 hasAllSupportedFiles = false;
32 defaultFilter = i18n("*|All Files");
33 isMimeFilter = false;
34 }
35
36 // when we have more than 3 mimefilters and no default-filter,
37 // we don't show the comments of all mimefilters in one line,
38 // instead we show "All supported files". We have to translate
39 // that back to the list of mimefilters in currentFilter() tho.
40 bool hasAllSupportedFiles;
41 // true when setMimeFilter was called
42 bool isMimeFilter;
43 TQString lastFilter;
44 TQString defaultFilter;
45};
46
47KFileFilterCombo::KFileFilterCombo( TQWidget *parent, const char *name)
48 : KComboBox(true, parent, name), d( new KFileFilterComboPrivate )
49{
50 setTrapReturnKey( true );
51 setInsertionPolicy(NoInsertion);
52 connect( this, TQ_SIGNAL( activated( int )), this, TQ_SIGNAL( filterChanged() ));
53 connect( this, TQ_SIGNAL( returnPressed() ), this, TQ_SIGNAL( filterChanged() ));
54 connect( this, TQ_SIGNAL( filterChanged() ), TQ_SLOT( slotFilterChanged() ));
55 m_allTypes = false;
56}
57
58KFileFilterCombo::~KFileFilterCombo()
59{
60 delete d;
61}
62
63void KFileFilterCombo::setFilter(const TQString& filter)
64{
65 clear();
66 filters.clear();
67 d->hasAllSupportedFiles = false;
68
69 if (!filter.isEmpty()) {
70 TQString tmp = filter;
71 int index = tmp.find('\n');
72 while (index > 0) {
73 filters.append(tmp.left(index));
74 tmp = tmp.mid(index + 1);
75 index = tmp.find('\n');
76 }
77 filters.append(tmp);
78 }
79 else
80 filters.append( d->defaultFilter );
81
82 TQStringList::ConstIterator it;
83 TQStringList::ConstIterator end(filters.end());
84 for (it = filters.begin(); it != end; ++it) {
85 int tab = (*it).find('|');
86 insertItem((tab < 0) ? *it :
87 (*it).mid(tab + 1));
88 }
89
90 d->lastFilter = currentText();
91 d->isMimeFilter = false;
92}
93
94TQString KFileFilterCombo::currentFilter() const
95{
96 TQString f = currentText();
97 if (f == text(currentItem())) { // user didn't edit the text
98 f = *filters.at(currentItem());
99 if ( d->isMimeFilter || (currentItem() == 0 && d->hasAllSupportedFiles) ) {
100 return f; // we have a mimetype as filter
101 }
102 }
103
104 int tab = f.find('|');
105 if (tab < 0)
106 return f;
107 else
108 return f.left(tab);
109}
110
111void KFileFilterCombo::setCurrentFilter( const TQString& filter )
112{
113 int pos = 0;
114 for( TQStringList::ConstIterator it = filters.begin();
115 it != filters.end();
116 ++it, ++pos ) {
117 if( *it == filter ) {
118 setCurrentItem( pos );
119 filterChanged();
120 return;
121 }
122 }
123 setCurrentText( filter );
124 filterChanged();
125}
126
127void KFileFilterCombo::setMimeFilter( const TQStringList& types,
128 const TQString& defaultType )
129{
130 clear();
131 filters.clear();
132 TQString delim = TQString::fromLatin1(", ");
133 d->hasAllSupportedFiles = false;
134
135 m_allTypes = defaultType.isEmpty() && (types.count() > 1);
136
137 TQString allComments, allTypes;
138 int i = 0;
139 for(TQStringList::ConstIterator it = types.begin(); it != types.end(); ++it, ++i)
140 {
141 if ( m_allTypes && it != types.begin() ) {
142 allComments += delim;
143 allTypes += ' ';
144 }
145
146 kdDebug(tdefile_area) << *it << endl;
147 KMimeType::Ptr type = KMimeType::mimeType( *it );
148 filters.append( type->name() );
149 if ( m_allTypes )
150 {
151 allTypes += type->name();
152 allComments += type->comment();
153 }
154 insertItem( type->comment() );
155 if ( type->name() == defaultType )
156 setCurrentItem( i );
157 }
158
159 if ( m_allTypes )
160 {
161 if ( i < 3 ) // show the mime-comments of at max 3 types
162 insertItem( allComments, 0 );
163 else {
164 insertItem( i18n("All Supported Files"), 0 );
165 d->hasAllSupportedFiles = true;
166 }
167
168 filters.prepend( allTypes );
169 }
170
171 d->lastFilter = currentText();
172 d->isMimeFilter = true;
173}
174
175void KFileFilterCombo::slotFilterChanged()
176{
177 d->lastFilter = currentText();
178}
179
180bool KFileFilterCombo::eventFilter( TQObject *o, TQEvent *e )
181{
182 if ( o == lineEdit() && e->type() == TQEvent::FocusOut ) {
183 if ( currentText() != d->lastFilter )
184 emit filterChanged();
185 }
186
187 return KComboBox::eventFilter( o, e );
188}
189
190void KFileFilterCombo::setDefaultFilter( const TQString& filter )
191{
192 d->defaultFilter = filter;
193}
194
195TQString KFileFilterCombo::defaultFilter() const
196{
197 return d->defaultFilter;
198}
199
200void KFileFilterCombo::virtual_hook( int id, void* data )
201{ KComboBox::virtual_hook( id, data ); }
202
203#include "tdefilefiltercombo.moc"

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
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdefile by doxygen 1.9.4
This website is maintained by Timothy Pearson.