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

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Laurent Montel <montel@kde.org>
00003    Copyright (C) 2006 Fredrik Edemar <f_edemar@linux.se>
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 
00022 #include <QFile>
00023 #include <QGridLayout>
00024 #include <QHBoxLayout>
00025 #include <QLabel>
00026 #include <QLayout>
00027 #include <QList>
00028 #include <QPushButton>
00029 #include <QToolButton>
00030 #include <QTreeWidget>
00031 
00032 #include <kbuttonbox.h>
00033 #include <kdebug.h>
00034 #include <kiconloader.h>
00035 #include <klocale.h>
00036 #include <kmessagebox.h>
00037 #include <ktemporaryfile.h>
00038 
00039 #include <q3multilineedit.h>
00040 
00041 #include "KoMainWindow.h"
00042 #include "KoQueryTrader.h"
00043 
00044 #include "KoVersionDialog.h"
00045 
00046 
00047 KoVersionDialog::KoVersionDialog( QWidget* parent, KoDocument *doc  )
00048     : KDialog( parent )
00049 {
00050   setCaption( i18n("Version") );
00051   setButtons( Close );
00052   setDefaultButton( Close );
00053   m_doc = doc;
00054 
00055   QWidget* page = new QWidget( this );
00056   setMainWidget( page );
00057   setModal( true );
00058 
00059   QGridLayout* grid1 = new QGridLayout( page );
00060   grid1->setMargin(KDialog::marginHint());
00061   grid1->setSpacing(KDialog::spacingHint());
00062 
00063   list=new QTreeWidget(page);
00064   list->setColumnCount( 3 );
00065   QStringList h;
00066   h.append( i18n("Date & Time") );
00067   h.append( i18n("Saved By") );
00068   h.append( i18n("Comment") );
00069   list->setHeaderLabels( h );
00070 
00071   updateVersionList();
00072 
00073   grid1->addWidget(list,0,0,9,1);
00074 
00075   m_pAdd=new QPushButton(i18n("&Add"),page);
00076   grid1->addWidget(m_pAdd,1,2);
00077 
00078   m_pRemove=new QPushButton(i18n("&Remove"),page);
00079   grid1->addWidget(m_pRemove,2,2);
00080 
00081   m_pModify=new QPushButton(i18n("&Modify"),page);
00082   grid1->addWidget(m_pModify,3,2);
00083 
00084   m_pOpen=new QPushButton(i18n("&Open"),page);
00085   grid1->addWidget(m_pOpen,4,2);
00086 
00087 
00088   connect( m_pRemove, SIGNAL( clicked() ), this, SLOT( slotRemove() ) );
00089   connect( m_pAdd, SIGNAL( clicked() ), this, SLOT( slotAdd() ) );
00090   connect( m_pOpen, SIGNAL( clicked() ), this, SLOT( slotOpen() ) );
00091   connect( m_pModify, SIGNAL( clicked() ), this, SLOT( slotModify() ) );
00092   connect( list, SIGNAL( itemActivated( QTreeWidgetItem *, int ) ), this, SLOT( slotOpen() ) );
00093 
00094   updateButton();
00095 
00096   resize( 600, 250 );
00097 
00098 }
00099 
00100 KoVersionDialog::~KoVersionDialog()
00101 {
00102 }
00103 
00104 void KoVersionDialog::updateVersionList()
00105 {
00106   list->clear();
00107   // add all versions to the tree widget
00108   QList<KoVersionInfo> versions = m_doc->versionList();
00109   QList<QTreeWidgetItem *> items;
00110   for (int i = 0; i < versions.size(); ++i)
00111   {
00112     QStringList l;
00113     l.append( versions.at(i).date.toString() );
00114     l.append( versions.at(i).saved_by );
00115     l.append( versions.at(i).comment );
00116     items.append( new QTreeWidgetItem( l ) );
00117   }
00118   list->insertTopLevelItems(0, items );
00119 }
00120 
00121 
00122 void KoVersionDialog::updateButton()
00123 {
00124 #if 0
00125     bool state = ( list->currentItem() >= 0 );
00126     m_pRemove->setEnabled( state );
00127 #endif
00128 }
00129 
00130 void KoVersionDialog::slotAdd()
00131 {
00132   KoVersionModifyDialog * dlg = new KoVersionModifyDialog( this, 0 );
00133   if ( !dlg->exec() )
00134   {
00135     delete dlg;
00136     return;
00137   }
00138 
00139   if ( !m_doc->addVersion( dlg->comment() ) )
00140     KMessageBox::error( this, i18n("A new version could not be added") );
00141 
00142   delete dlg;
00143 
00144   updateVersionList();
00145 }
00146 
00147 void KoVersionDialog::slotRemove()
00148 {
00149   if ( !list->currentItem() )
00150     return;
00151 
00152   for (int i = 0; i < m_doc->versionList().size(); ++i) {
00153     if ( m_doc->versionList().at(i).date.toString() == list->currentItem()->text(0) )
00154     {
00155       m_doc->versionList().takeAt(i);
00156       delete list->currentItem();
00157       return;
00158     }
00159   }
00160 }
00161 
00162 void KoVersionDialog::slotModify()
00163 {
00164     if ( !list->currentItem() )
00165         return;
00166 
00167     KoVersionInfo *version = 0;
00168     for (int i = 0; i < m_doc->versionList().size(); ++i) {
00169       if ( m_doc->versionList().at(i).date.toString() == list->currentItem()->text(0) )
00170       {
00171         version = &m_doc->versionList()[i];
00172         break;
00173       }
00174     }
00175     if ( !version )
00176       return;
00177 
00178     KoVersionModifyDialog * dlg = new KoVersionModifyDialog( this, version );
00179     if ( dlg->exec() )
00180     {
00181       version->comment = dlg->comment();
00182       list->currentItem()->setText(2, version->comment );
00183     }
00184     delete dlg;
00185 
00186 }
00187 
00188 void KoVersionDialog::slotOpen()
00189 {
00190   if ( !list->currentItem() )
00191     return;
00192 
00193   KoVersionInfo *version = 0;
00194   for (int i = 0; i < m_doc->versionList().size(); ++i) {
00195     if ( m_doc->versionList().at(i).date.toString() == list->currentItem()->text(0) )
00196     {
00197       version = &m_doc->versionList()[i];
00198       break;
00199     }
00200   }
00201   if ( !version )
00202     return;
00203 
00204     KTemporaryFile tmp;
00205     tmp.setAutoRemove(false);
00206     tmp.open();
00207     tmp.write( version->data );
00208     tmp.flush();
00209     tmp.setPermissions( QFile::ReadUser );
00210     tmp.flush();
00211 
00212     if ( !m_doc->shells().isEmpty() ) //open the version in a new window if possible
00213     {
00214         KoDocumentEntry entry = KoDocumentEntry( KoDocument::readNativeService() );
00215         QString errorMsg;
00216         KoDocument* doc = entry.createDoc( &errorMsg );
00217         if ( !doc ) {
00218           if ( !errorMsg.isEmpty() )
00219             KMessageBox::error( 0, errorMsg );
00220           return;
00221         }
00222         KoMainWindow *shell = new KoMainWindow( doc->instance() );
00223         shell->openDocument( tmp.fileName() );
00224         shell->show();
00225     }
00226     else
00227         m_doc->openURL( tmp.fileName() );
00228 
00229     tmp.setAutoRemove(true);
00230     slotButtonClicked( Close );
00231 }
00232 
00233 KoVersionModifyDialog::KoVersionModifyDialog(  QWidget* parent, KoVersionInfo *info )
00234     : KDialog( parent  )
00235 {
00236     setCaption( i18n("Comment") );
00237     setButtons( Ok | Cancel );
00238     setDefaultButton( Ok );
00239     setModal( true );
00240 
00241     QWidget* page = new QWidget( this );
00242     setMainWidget( page );
00243 
00244     QVBoxLayout *grid1 = new QVBoxLayout( page );
00245     grid1->setMargin(KDialog::marginHint());
00246     grid1->setSpacing(KDialog::spacingHint());
00247 
00248     QLabel *l = new QLabel( page );
00249     if ( info )
00250         l->setText( i18n("Date: %1", info->date.toString() ) );
00251     else
00252         l->setText( i18n("Date: %1", QDateTime::currentDateTime().toString( Qt::ISODate ) ) );
00253     grid1->addWidget( l );
00254 
00255     m_multiline = new Q3MultiLineEdit( page );
00256     if ( info )
00257         m_multiline->setText( info->comment );
00258     grid1->addWidget( m_multiline );
00259 
00260 }
00261 
00262 QString KoVersionModifyDialog::comment() const
00263 {
00264     return m_multiline->text();
00265 }
00266 
00267 
00268 #include "KoVersionDialog.moc"

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