F:/KPlato/koffice/libs/kofficecore/KoFileDialog.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2002-2005 David Faure <faure@kde.org>
00003    Copyright (C) 2002-2004 Clarence Dang <dang@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "KoFileDialog.h"
00022 #include "KoDocument.h"
00023 #include <kfilefiltercombo.h>
00024 #include <klocale.h>
00025 #include <kdiroperator.h>
00026 #include <kdebug.h>
00027 
00028 KoFileDialog::KoFileDialog(const QString& startDir, const QString& filter,
00029                            QWidget *parent, const char* /*name*/,
00030                            bool /*modal*/ )
00031     : KFileDialog( startDir, filter, parent )
00032 {
00033     connect( filterWidget, SIGNAL( activated( int) ),
00034              this, SLOT( slotChangedfilter( int ) ) );
00035 }
00036 
00037 void KoFileDialog::slotChangedfilter( int index )
00038 {
00039     // Switch to "directory selection" mode for SaveAsDirectoryStore,
00040     // switch back to "file selection" mode otherwise.
00041     KFile::Mode newMode = KFile::File;
00042     if ( index >= 1 && index <= (int)m_specialFormats.count()
00043          && m_specialFormats[index-1] == KoDocument::SaveAsDirectoryStore ) {
00044         newMode = KFile::Directory;
00045     }
00046     if ( newMode != mode() )
00047     {
00048         ops->setMode( newMode );
00049         updateAutoSelectExtension();
00050     }
00051 }
00052 
00053 void KoFileDialog::setSpecialMimeFilter( QStringList& mimeFilter,
00054                                          const QString& currentFormat, const int specialOutputFlag,
00055                                          const QString& nativeFormat,
00056                                          int supportedSpecialFormats )
00057 {
00058     Q_ASSERT( !mimeFilter.isEmpty() );
00059     Q_ASSERT( mimeFilter[0] == nativeFormat );
00060 
00061     bool addUncompressed = supportedSpecialFormats & KoDocument::SaveAsDirectoryStore;
00062     bool addFlatXML = supportedSpecialFormats & KoDocument::SaveAsFlatXML;
00063 
00064     int idxSpecialOutputFlag = 0;
00065     int numSpecialEntries = 0;
00066     if ( addUncompressed ) {
00067         ++numSpecialEntries;
00068         m_specialFormats.append( KoDocument::SaveAsDirectoryStore );
00069         if ( specialOutputFlag == KoDocument::SaveAsDirectoryStore )
00070             idxSpecialOutputFlag = numSpecialEntries;
00071     }
00072     if ( addFlatXML ) {
00073         ++numSpecialEntries;
00074         m_specialFormats.append( KoDocument::SaveAsFlatXML );
00075         if ( specialOutputFlag == KoDocument::SaveAsFlatXML )
00076             idxSpecialOutputFlag = numSpecialEntries;
00077     }
00078 
00079     // Insert numSpecialEntries entries with native mimetypes, for the special entries.
00080     for ( int i=0; i<numSpecialEntries; ++i )
00081       mimeFilter.insert( 1, nativeFormat );
00082 
00083     // Fill in filter combo
00084     // Note: if currentFormat doesn't exist in mimeFilter, filterWidget
00085     //       will default to the first item (native format)
00086     setMimeFilter( mimeFilter, currentFormat.isEmpty() ? nativeFormat : currentFormat );
00087 
00088     // To get a different description in the combo, we need to change its entries afterwards
00089     KMimeType::Ptr type = KMimeType::mimeType( nativeFormat );
00090     if ( ! type )
00091         type = KMimeType::defaultMimeTypePtr();
00092     int idx = 1; // 0 is the native format
00093 
00094     if ( addUncompressed )
00095         filterWidget->setItemText( idx++, i18n("%1 (Uncompressed XML Files)", type->comment() ) );
00096     if ( addFlatXML )
00097         filterWidget->setItemText( idx++, i18n("%1 (Flat XML File)", type->comment() ) );
00098     // if you add an entry here, update numSpecialEntries above and specialEntrySelected() below
00099 
00100     // For native format...
00101     if (currentFormat == nativeFormat || currentFormat.isEmpty())
00102     {
00103         // KFileFilterCombo selected the _last_ "native mimetype" entry, select the correct one
00104         filterWidget->setCurrentIndex( idxSpecialOutputFlag );
00105         slotChangedfilter( filterWidget->currentIndex() );
00106     }
00107     // [Mainly KWord] Tell MS Office users that they can save in RTF!
00108     int i = 0;
00109     QString tmpString;
00110     QString compatString;
00111     foreach( tmpString, mimeFilter )
00112     {
00113         KMimeType::Ptr mime = KMimeType::mimeType( tmpString );
00114         if ( ! mime )
00115             mime = KMimeType::defaultMimeTypePtr();
00116         compatString = mime->property ("X-KDE-CompatibleApplication").toString ();
00117         if (!compatString.isEmpty ())
00118             filterWidget->setItemText( i, i18n ("%1 (%2 Compatible)", mime->comment (), compatString) );
00119         i++;
00120     }
00121 }
00122 
00123 int KoFileDialog::specialEntrySelected()
00124 {
00125     int i = filterWidget->currentIndex();
00126     // Item 0 is the native format, the following ones are the special formats
00127     if ( i >= 1 && i <= (int)m_specialFormats.count() )
00128         return m_specialFormats[i-1];
00129     return 0;
00130 }
00131 
00132 #include "KoFileDialog.moc"

Généré le Wed Nov 22 23:41:02 2006 pour KPlato par  doxygen 1.5.1-p1