File: kfilefiltercombo.cpp

package info (click to toggle)
kde4libs 4%3A4.14.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,316 kB
  • sloc: cpp: 761,810; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (232 lines) | stat: -rw-r--r-- 6,255 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* This file is part of the KDE libraries
    Copyright (C) Stephan Kulow <coolo@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kfilefiltercombo.h"

#include <kdebug.h>
#include <klocale.h>
#include <kmimetype.h>
#include <config-kfile.h>
#include <QtCore/QEvent>
#include <QtGui/QLineEdit>

class KFileFilterCombo::Private
{
public:
    Private( KFileFilterCombo *_parent )
        : parent(_parent),
          hasAllSupportedFiles(false),
          isMimeFilter(false),
          defaultFilter(i18n("*|All Files"))
    {
    }

    void _k_slotFilterChanged();

    KFileFilterCombo *parent;
    // when we have more than 3 mimefilters and no default-filter,
    // we don't show the comments of all mimefilters in one line,
    // instead we show "All supported files". We have to translate
    // that back to the list of mimefilters in currentFilter() tho.
    bool hasAllSupportedFiles;
    // true when setMimeFilter was called
    bool isMimeFilter;
    QString lastFilter;
    QString defaultFilter;

    QStringList m_filters;
    bool m_allTypes;
};

KFileFilterCombo::KFileFilterCombo( QWidget *parent)
    : KComboBox(true, parent), d( new Private(this) )
{
    setTrapReturnKey( true );
    setInsertPolicy(QComboBox::NoInsert);
    connect( this, SIGNAL(activated(int)), this, SIGNAL(filterChanged()));
    connect( this, SIGNAL(returnPressed()), this, SIGNAL(filterChanged()));
    connect( this, SIGNAL(filterChanged()), SLOT(_k_slotFilterChanged()));
    d->m_allTypes = false;
}

KFileFilterCombo::~KFileFilterCombo()
{
    delete d;
}

void KFileFilterCombo::setFilter(const QString& filter)
{
    clear();
    d->m_filters.clear();
    d->hasAllSupportedFiles = false;

    if (!filter.isEmpty()) {
	QString tmp = filter;
	int index = tmp.indexOf('\n');
	while (index > 0) {
	    d->m_filters.append(tmp.left(index));
	    tmp = tmp.mid(index + 1);
	    index = tmp.indexOf('\n');
	}
	d->m_filters.append(tmp);
    }
    else
	d->m_filters.append( d->defaultFilter );

    QStringList::ConstIterator it;
    QStringList::ConstIterator end(d->m_filters.constEnd());
    for (it = d->m_filters.constBegin(); it != end; ++it) {
	int tab = (*it).indexOf('|');
	addItem((tab < 0) ? *it :
		   (*it).mid(tab + 1));
    }

    d->lastFilter = currentText();
    d->isMimeFilter = false;
}

QString KFileFilterCombo::currentFilter() const
{
    QString f = currentText();
    if (f == itemText(currentIndex())) { // user didn't edit the text
	f = d->m_filters.value(currentIndex());
        if ( d->isMimeFilter || (currentIndex() == 0 && d->hasAllSupportedFiles) ) {
            return f; // we have a mimetype as filter
        }
    }

    int tab = f.indexOf('|');
    if (tab < 0)
	return f;
    else
	return f.left(tab);
}

bool KFileFilterCombo::showsAllTypes() const
{
    return d->m_allTypes;
}

QStringList KFileFilterCombo::filters() const
{
    return d->m_filters;
}

void KFileFilterCombo::setCurrentFilter( const QString& filter )
{
    setCurrentIndex(d->m_filters.indexOf(filter));
    filterChanged();
}

void KFileFilterCombo::setMimeFilter( const QStringList& types,
                                      const QString& defaultType )
{
    clear();
    d->m_filters.clear();
    QString delim = QLatin1String(", ");
    d->hasAllSupportedFiles = false;
    bool hasAllFilesFilter = false;

    d->m_allTypes = defaultType.isEmpty() && (types.count() > 1);

    QString allComments, allTypes;
    for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it)
    {
        kDebug(kfile_area) << *it;
        KMimeType::Ptr type = KMimeType::mimeType( *it );

        if (!type) {
            kDebug(kfile_area) << "Could not create mimetype!\n";
            continue;
        }

        if ( type->name().startsWith( QLatin1String( "all/" ) ) ) {
            hasAllFilesFilter = true;
            continue;
        }

        if ( d->m_allTypes && it != types.begin() ) {
            allComments += delim;
            allTypes += ' ';
        }

        d->m_filters.append( type->name() );
        if ( d->m_allTypes )
        {
            allTypes += type->name();
            allComments += type->comment();
        }
        addItem( type->comment() );
        if ( type->name() == defaultType )
            setCurrentIndex( count() - 1 );
    }

    if ( d->m_allTypes )
    {
        if ( count() <= 3 ) // show the mime-comments of at max 3 types
            insertItem(0, allComments);
        else {
            insertItem(0, i18n("All Supported Files"));
            d->hasAllSupportedFiles = true;
        }
        setCurrentIndex( 0 );

        d->m_filters.prepend( allTypes );
    }

    if ( hasAllFilesFilter ) {
        addItem(i18n("All Files"));
        d->m_filters.append( QLatin1String("all/allfiles") );
    }

    d->lastFilter = currentText();
    d->isMimeFilter = true;
}

void KFileFilterCombo::Private::_k_slotFilterChanged()
{
    lastFilter = parent->currentText();
}

bool KFileFilterCombo::eventFilter( QObject *o, QEvent *e )
{
    if ( o == lineEdit() && e->type() == QEvent::FocusOut ) {
        if ( currentText() != d->lastFilter )
            emit filterChanged();
    }

    return KComboBox::eventFilter( o, e );
}

void KFileFilterCombo::setDefaultFilter( const QString& filter )
{
    d->defaultFilter = filter;
}

QString KFileFilterCombo::defaultFilter() const
{
    return d->defaultFilter;
}

bool KFileFilterCombo::isMimeFilter() const
{
    return d->isMimeFilter;
}

#include "kfilefiltercombo.moc"