kpresenter

KPrImportStyleDia.cpp

00001 // -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
00002 /* This file is part of the KDE project
00003    Copyright (C)  2002 Montel Laurent <lmontel@mandrakesoft.com>
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 <klocale.h>
00022 #include "KPrDocument.h"
00023 #include <qvbox.h>
00024 #include <qlayout.h>
00025 #include <qlineedit.h>
00026 #include <qpushbutton.h>
00027 #include <qlistbox.h>
00028 #include <kmessagebox.h>
00029 #include "KPrImportStyleDia.h"
00030 #include <KoStore.h>
00031 #include <qfile.h>
00032 #include <kfiledialog.h>
00033 #include <kdebug.h>
00034 #include <qlabel.h>
00035 #include "KPrTextObject.h"
00036 
00037 KPrImportStyleDia::KPrImportStyleDia( KPrDocument *_doc, KoStyleCollection* currentCollection,
00038                                       QWidget *parent, const char *name )
00039     :KoImportStyleDia( currentCollection, parent, name ),
00040      m_doc(_doc)
00041 {
00042 }
00043 
00044 KPrImportStyleDia::~KPrImportStyleDia()
00045 {
00046 }
00047 
00048 void KPrImportStyleDia::loadFile()
00049 {
00050     KFileDialog fd( QString::null, QString::null, 0, 0, TRUE );
00051     QStringList lst = "application/x-kpresenter";
00052 #if 0 //For the future
00053     lst << "application/vnd.oasis.opendocument.presentation";
00054 #endif
00055     fd.setMimeFilter( lst );
00056     fd.setCaption(i18n("Import Style"));
00057     KURL url;
00058     if ( fd.exec() != QDialog::Accepted )
00059         return;
00060     url = fd.selectedURL();
00061     if( url.isEmpty() )
00062     {
00063         KMessageBox::sorry( this,
00064                             i18n("File name is empty."),
00065                             i18n("Import Style"));
00066         return;
00067     }
00068     QMap<QString, QString>insertStyle;
00069     KoStore* store=KoStore::createStore( this, url.path(), KoStore::Read );
00070     if (store )
00071     {
00072         if (store->open("maindoc.xml") )
00073         {
00074             clear();
00075             m_listStyleName->clear();
00076 
00077             QDomDocument doc;
00078             doc.setContent( store->device() );
00079             QDomElement word = doc.documentElement();
00080 
00081             QDomElement stylesElem = word.namedItem( "STYLES" ).toElement();
00082             if ( !stylesElem.isNull() )
00083             {
00084                 //todo
00085                 //duplicate code try to remove it !
00086                 QValueList<QString> followingStyles;
00087                 QDomNodeList listStyles = stylesElem.elementsByTagName( "STYLE" );
00088                 for (unsigned int item = 0; item < listStyles.count(); item++)
00089                 {
00090                     QDomElement styleElem = listStyles.item( item ).toElement();
00091 
00092                     KoParagStyle *sty = new KoParagStyle( QString::null );
00093                     // Load the style from the <STYLE> element
00094                     sty->loadStyle( styleElem );
00095 
00096                     if ( currentCollection()->findStyle( sty->name() ) )
00097                         sty->setName(generateStyleName(sty->name() + "-%1"));
00098                     QString name = sty->displayName();
00099                     // ### TODO: we should offer the option of updating the
00100                     // existing style instead of creating a foo-1 style. Any ideas for a GUI?
00101                     if ( currentCollection()->findStyleByDisplayName( name ) )
00102                         sty->setDisplayName(generateStyleDisplayName(sty->displayName() + "-%1"));
00103                     insertStyle.insert( name, sty->name() );
00104 
00105                     QDomElement formatElem = styleElem.namedItem( "FORMAT" ).toElement();
00106                     if ( !formatElem.isNull() )
00107                         sty->format() = KPrTextObject::loadFormat( formatElem, 0L, m_doc->defaultFont(),
00108                                                                   m_doc->globalLanguage(), m_doc->globalHyphenation() );
00109                     else
00110                         kdWarning(33001) << "No FORMAT tag in <STYLE>" << endl; // This leads to problems in applyStyle().
00111 
00112 
00113                     // Style created, now let's try to add it
00114                     sty = m_styleList.addStyle(sty);
00115 
00116                     if( m_styleList.count() >= 0 && uint( m_styleList.count() ) > followingStyles.count() )
00117                     {
00118                         QString following = styleElem.namedItem("FOLLOWING").toElement().attribute("name");
00119                         followingStyles.append( following );
00120                     }
00121                     else
00122                         kdWarning(33001) << "Found duplicate style declaration, overwriting former " << sty->name() << endl;
00123                 }
00124 
00125                 Q_ASSERT( m_styleList.count() >= 0 && followingStyles.count() == uint( m_styleList.count() ) );
00126 
00127                 unsigned int i=0;
00128                 for( QValueList<QString>::Iterator it = followingStyles.begin(); it != followingStyles.end(); ++it ) {
00129                     QString newName =*it;
00130                     if ( insertStyle.contains( *it ) )
00131                         newName = (insertStyle)[ *it ];
00132 
00133                     KoParagStyle * style = m_styleList.findStyle(newName);
00134                     if ( style )
00135                         m_styleList.styleAt(i++)->setFollowingStyle( style );
00136                 }
00137 
00138             }
00139             initList();
00140         }
00141         else if ( store->hasFile( "content.xml" ) )
00142         {
00143             //oasis file format (for koffice-1.5)
00144         }
00145         else
00146         {
00147             KMessageBox::error( this,
00148                                 i18n("File is not a KPresenter file!"),
00149                                 i18n("Import Style"));
00150         }
00151         store->close();
00152     }
00153     delete store;
00154 }
00155 
00156 
00157 #include "KPrImportStyleDia.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys