document.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by David Saxton                                    *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "document.h"
00012 #include "documentiface.h"
00013 #include "ktechlab.h"
00014 #include "projectmanager.h"
00015 #include "view.h"
00016 #include "viewcontainer.h"
00017 
00018 #include <kfiledialog.h>
00019 #include <klocale.h>
00020 #include <kmessagebox.h>
00021 #include <ktabwidget.h>
00022 
00023 Document::Document( const QString &caption, KTechlab *ktechlab, const char *name )
00024         : QObject( ktechlab, name ),
00025         b_modified(false),
00026         p_ktechlab(ktechlab),
00027         p_activeView(0),
00028         m_caption(caption),
00029         m_bAddToProjectOnSave(false),
00030         m_pDocumentIface(0),
00031         m_dcopID(0),
00032         m_nextViewID(0),
00033         m_bDeleted(false)
00034 {
00035         if (p_ktechlab)
00036                 connect( p_ktechlab, SIGNAL(configurationChanged()), this, SLOT(slotUpdateConfiguration()) );
00037 }
00038 
00039 
00040 Document::~Document()
00041 {
00042         m_bDeleted = true;
00043         
00044         ViewList viewsToDelete = m_viewList;
00045         const ViewList::iterator end = viewsToDelete.end();
00046         for ( ViewList::iterator it = viewsToDelete.begin(); it != end; ++it )
00047                 (*it)->deleteLater();
00048 }
00049 
00050 
00051 void Document::handleNewView( View *view )
00052 {
00053         if ( !view || m_viewList.contains(view) )
00054                 return;
00055         
00056         m_viewList.append(view);
00057         view->setDCOPID(m_nextViewID++);
00058         view->setCaption(m_caption);
00059         connect( view, SIGNAL(destroyed(QObject* )), this, SLOT(slotViewDestroyed(QObject* )) );
00060         connect( view, SIGNAL(viewFocused(View* )), this, SLOT(slotViewFocused(View* )) );
00061         connect( view, SIGNAL(viewUnfocused()), this, SIGNAL(viewUnfocused()) );
00062         view->show();
00063         view->setFocused();
00064 }
00065 
00066 
00067 void Document::slotViewDestroyed( QObject *obj )
00068 {
00069         View *view = static_cast<View*>(obj);
00070         
00071         m_viewList.remove(view);
00072         
00073         if ( p_activeView == (QGuardedPtr<View>)view )
00074         {
00075                 p_activeView = 0;
00076                 emit viewUnfocused();
00077         }
00078         
00079         if ( m_viewList.isEmpty() )
00080                 deleteLater();
00081 }
00082 
00083 
00084 void Document::slotViewFocused(View *view)
00085 {
00086         if (!view)
00087                 return;
00088         
00089         p_activeView = view;
00090         emit viewFocused(view);
00091 }
00092 
00093 
00094 void Document::setCaption( const QString &caption )
00095 {
00096         m_caption = caption;
00097         const ViewList::iterator end = m_viewList.end();
00098         for ( ViewList::iterator it = m_viewList.begin(); it != end; ++it )
00099                 (*it)->setCaption(caption);
00100 }
00101 
00102 
00103 bool Document::getURL( const QString &types )
00104 {
00105         KURL url = KFileDialog::getSaveURL( QString::null, types, p_ktechlab, i18n("Save Location"));
00106         
00107         if ( url.isEmpty() )
00108                 return false;
00109         
00110         if ( QFile::exists( url.path() ) )
00111         {
00112                 int query = KMessageBox::warningYesNo( p_ktechlab,
00113                                                                                            i18n( "A file named \"%1\" already exists. Are you sure you want to overwrite it?" ).arg( url.fileName() ),
00114                                                                                            i18n( "Overwrite File?" ),
00115                                                                                            i18n( "Overwrite" ),
00116                                                                                            KStdGuiItem::cancel() );
00117                 if ( query == KMessageBox::No )
00118                         return false;
00119         }
00120         
00121         setURL(url);
00122         
00123         return true;
00124 }
00125 
00126 
00127 bool Document::fileClose()
00128 {
00129         if ( isModified() )
00130         {
00131                 // If the filename is empty then it must  be an untitled file.
00132                 QString name = m_url.fileName().isEmpty() ? caption() : m_url.fileName();
00133                 
00134                 if ( ViewContainer * viewContainer = (activeView() ? activeView()->viewContainer() : 0) )
00135                         p_ktechlab->tabWidget()->setCurrentPage( p_ktechlab->tabWidget()->indexOf(viewContainer) );
00136                 
00137                 int choice = KMessageBox::warningYesNoCancel( p_ktechlab,
00138                                 i18n("The document \'%1\' has been modified.\nDo you want to save it?").arg(name),
00139                                 i18n("Save Document?"),
00140                                 i18n("Save"),
00141                                 i18n("Discard") );
00142                 
00143                 if ( choice == KMessageBox::Cancel )
00144                         return false;
00145                 if ( choice == KMessageBox::Yes )
00146                         fileSave();
00147         }
00148 
00149         deleteLater();
00150         return true;
00151 }
00152 
00153 
00154 void Document::setModified( bool modified )
00155 {
00156         if ( b_modified == modified )
00157                 return;
00158         
00159         b_modified = modified;
00160         
00161         if (!m_bDeleted)
00162                 emit modifiedStateChanged();
00163 }
00164 
00165 
00166 void Document::setURL( const KURL &url )
00167 {
00168         if ( m_url == url )
00169                 return;
00170         
00171         bool wasEmpty = m_url.isEmpty();
00172         m_url = url;
00173         
00174         if ( wasEmpty && m_bAddToProjectOnSave && ProjectManager::self()->currentProject() )
00175                 ProjectManager::self()->currentProject()->addFile(m_url);
00176         
00177         emit fileNameChanged(url);
00178         
00179         if (p_ktechlab)
00180         {
00181                 p_ktechlab->addRecentFile(url);
00182                 p_ktechlab->requestUpdateCaptions();
00183         }
00184 }
00185 
00186 DCOPObject * Document::dcopObject( ) const
00187 {
00188         return m_pDocumentIface;
00189 }
00190 
00191 void Document::setDCOPID( unsigned id )
00192 {
00193         if ( m_dcopID == id )
00194                 return;
00195         
00196         m_dcopID = id;
00197         if ( m_pDocumentIface )
00198         {
00199                 QCString docID;
00200                 docID.setNum( dcopID() );
00201                 m_pDocumentIface->setObjId( "Document#" + docID );
00202         }
00203 }
00204 
00205 #include "document.moc"

Generated on Tue May 8 17:05:29 2007 for KTechLab by  doxygen 1.5.1