29 #include <QPushButton>
30 #include <QTreeWidget>
34 class KMimeTypeChooserPrivate
46 void _k_editMimeType();
47 void _k_slotCurrentChanged(QTreeWidgetItem*);
48 void _k_slotSycocaDatabaseChanged(
const QStringList&);
68 d(new KMimeTypeChooserPrivate(this))
70 d->defaultgroup = defaultGroup;
71 d->groups = groupsToShow;
75 if ( !text.isEmpty() )
82 headerLabels.append(
i18n(
"Mime Type") );
85 headerLabels.append(
i18n(
"Comment") );
88 headerLabels.append(
i18n(
"Patterns") );
91 d->mimeTypeTree->setColumnCount(headerLabels.count());
92 d->mimeTypeTree->setHeaderLabels(headerLabels);
93 QFontMetrics fm(d->mimeTypeTree->fontMetrics());
94 d->mimeTypeTree->setColumnWidth(0, 20 * fm.height());
96 d->loadMimeTypes( selMimeTypes );
101 ((QBoxLayout*)btns->layout())->addStretch(1);
104 connect( d->btnEditMimeType, SIGNAL(clicked()),
this, SLOT(_k_editMimeType()) );
105 d->btnEditMimeType->setEnabled(
false );
106 connect( d->mimeTypeTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,
int)),
107 this, SLOT(_k_editMimeType()));
108 connect( d->mimeTypeTree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
109 this, SLOT(_k_slotCurrentChanged(QTreeWidgetItem*)) );
111 d->btnEditMimeType->setWhatsThis(
i18n(
112 "Click this button to display the familiar KDE mime type editor.") );
121 void KMimeTypeChooserPrivate::loadMimeTypes(
const QStringList &_selectedMimeTypes )
125 if ( !_selectedMimeTypes.isEmpty() )
126 selMimeTypes = _selectedMimeTypes;
128 selMimeTypes = q->mimeTypes();
130 mimeTypeTree->clear();
133 const KMimeType::List mimetypes = KMimeType::allMimeTypes();
135 bool agroupisopen =
false;
136 QTreeWidgetItem *idefault = 0;
137 QTreeWidgetItem *firstChecked = 0;
139 foreach (
const KMimeType::Ptr& mt, mimetypes)
142 const int index = mimetype.indexOf(
'/');
143 const QString maj = mimetype.left(index);
145 if ( !groups.isEmpty() && !groups.contains( maj ) )
148 QTreeWidgetItem *groupItem;
150 if ( mit == groupItems.end() )
152 groupItem =
new QTreeWidgetItem( mimeTypeTree,
QStringList(maj) );
153 groupItems.insert( maj, groupItem );
154 if ( maj == defaultgroup )
155 idefault = groupItem;
158 groupItem = mit.value();
160 const QString min = mimetype.mid(index+1);
161 QTreeWidgetItem *item =
new QTreeWidgetItem( groupItem,
QStringList(min) );
162 item->setIcon( 0,
KIcon( mt->iconName() ) );
168 item->setText( cl, mt->comment() );
173 item->setText( cl, mt->patterns().join(
"; ") );
175 if ( selMimeTypes.contains(mimetype) ) {
176 item->setCheckState( 0, Qt::Checked );
177 groupItem->setExpanded(
true );
182 item->setCheckState( 0, Qt::Unchecked );
187 mimeTypeTree->scrollToItem( firstChecked );
189 if ( !agroupisopen && idefault )
191 idefault->setExpanded(
true );
192 mimeTypeTree->scrollToItem( idefault );
194 mimeTypeTree->resizeColumnToContents(1);
197 void KMimeTypeChooserPrivate::_k_editMimeType()
199 QTreeWidgetItem* item = mimeTypeTree->currentItem();
200 if ( !item || !item->parent() )
202 QString mt = (item->parent())->text(0) +
'/' + item->text(0);
205 q, SLOT(_k_slotSycocaDatabaseChanged(
QStringList)) );
206 QString keditfiletype = QString::fromLatin1(
"keditfiletype");
213 keditfiletype, keditfiletype , q->topLevelWidget());
216 void KMimeTypeChooserPrivate::_k_slotCurrentChanged(QTreeWidgetItem* item)
218 if ( btnEditMimeType )
219 btnEditMimeType->setEnabled(item && item->parent());
222 void KMimeTypeChooserPrivate::_k_slotSycocaDatabaseChanged(
const QStringList& changedResources)
224 if (changedResources.contains(
"xdgdata-mime"))
231 for (
int i = 0; i < parent->childCount(); ++i ) {
232 QTreeWidgetItem* item = parent->child(i);
233 if (item->checkState(0) == Qt::Checked)
241 for (
int i = 0; i < tree->topLevelItemCount(); ++i ) {
242 QTreeWidgetItem* topItem = tree->topLevelItem(i);
254 foreach(QTreeWidgetItem* item, checkedItems) {
255 mimeList.append( item->parent()->text(0) +
'/' + item->text(0) );
265 foreach(QTreeWidgetItem* item, checkedItems) {
266 KMimeType::Ptr p = KMimeType::mimeType( item->parent()->text(0) +
'/' + item->text(0) );
268 patternList += p->patterns();
276 class KMimeTypeChooserDialog::Private
301 :
KDialog( parent ), d(new Private(this))
303 setCaption( caption );
307 defaultGroup, groupsToShow, visuals,
309 setMainWidget(d->m_chooser);
318 :
KDialog( parent ), d(new Private(this))
320 setCaption( caption );
327 setMainWidget(d->m_chooser);
335 void KMimeTypeChooserDialog::Private::init()
337 q->setButtons( Cancel | Ok );
339 q->setDefaultButton( Ok );
342 q->resize(
group.readEntry(
"size",
QSize(600,500)));
356 #include "kmimetypechooser.moc"
QString i18n(const char *text)
MimetypeJob * mimetype(const KUrl &url, JobFlags flags=DefaultFlags)
Find mimetype for one file or directory.
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
Show the Mimetypes Comment field in a column ("HTML Document").
Show the Mimetypes Patterns field in a column ("*.html;*.htm").
KSharedConfigPtr config()
static bool runCommand(const QString &cmd, QWidget *window)
Run the given shell command and notifies KDE of the starting of the application.
static void getCheckedItems(QList< QTreeWidgetItem * > &lst, QTreeWidgetItem *parent)
A Dialog to choose some mimetypes.
QString quoteArg(const QString &arg)
QStringList mimeTypes() const
Show the "Edit" button, allowing to edit the selected type.
KMimeTypeChooser(const QString &text=QString(), const QStringList &selectedMimeTypes=QStringList(), const QString &defaultGroup=QString(), const QStringList &groupsToShow=QStringList(), int visuals=Comments|Patterns|EditButton, QWidget *parent=0)
Create a new KMimeTypeChooser.
void setSpacing(int space)
KMimeTypeChooser * chooser()
QStringList patterns() const
This widget provides a checkable list of all available mimetypes, and a list of selected ones...
KMimeTypeChooserDialog(const QString &caption=QString(), const QString &text=QString(), const QStringList &selectedMimeTypes=QStringList(), const QString &defaultGroup=QString(), const QStringList &groupsToShow=QStringList(), int visuals=KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton, QWidget *parent=0)
Create a KMimeTypeChooser dialog.
QString number(KIO::filesize_t size)
Converts a size to a string representation Not unlike QString::number(...)
~KMimeTypeChooserDialog()