Blokkal
an Extendable KDE Blogging Client
SourceForge.net Logo

viewmanager.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Martin Mueller                                  *
00003  *   orvio@orvio.de                                                        *
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  *   This program 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         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "viewmanager.h"
00021 #include "viewmanager.moc"
00022 
00023 #include "../blokkalentry.h"
00024 #include "../blokkalpluginmanager.h"
00025 #include "editentryviewfactory.h"
00026 #include "view.h"
00027 
00028 #include <kglobal.h>
00029 
00030 #include <QWidget>
00031 
00032 class Blokkal::Ui::ViewManagerPrivate {
00033 public:
00034         ViewManagerPrivate( void ) {}
00035 
00036         ViewManager instance;
00037 
00038         QMap<Blokkal::Entry *, Blokkal::Ui::View *> entryViewMap;
00039         QMap<Blokkal::Ui::View *, QWidget* > viewWidgetMap;
00040 };
00041 
00042 K_GLOBAL_STATIC( Blokkal::Ui::ViewManagerPrivate, _buvmp );
00043 
00044 Blokkal::Ui::ViewManager::ViewManager( void )
00045 {
00046         setObjectName( "ViewManager" );
00047 }
00048 
00049 Blokkal::Ui::ViewManager::~ViewManager( void )
00050 {}
00051 
00052 Blokkal::Ui::ViewManager * Blokkal::Ui::ViewManager::self( void )
00053 {
00054         return &_buvmp->instance;
00055 }
00056 
00057 Blokkal::Ui::View * Blokkal::Ui::ViewManager::view( Blokkal::Entry * entry )
00058 {
00059         if( !hasView( entry ) ) {
00060                 entry->ref();
00061                 View * const view = Blokkal::PluginManager::self()->editEntryViewFactory()->createView( entry );
00062                 _buvmp->entryViewMap.insert( entry, view );
00063                 _buvmp->viewWidgetMap.insert( view, view->mainWidget() );
00064                 connect( view->mainWidget(),
00065                          SIGNAL( destroyed( void ) ),
00066                          self(),
00067                          SLOT( slotViewDestroyed( void ) ) );
00068                 connect( entry,
00069                          SIGNAL( entryDestroyed( Blokkal::Entry * ) ),
00070                          self(),
00071                          SLOT( slotEntryDestroyed( Blokkal::Entry * ) ) );
00072         }
00073 
00074         return _buvmp->entryViewMap[entry];
00075 }
00076 
00077 bool Blokkal::Ui::ViewManager::hasView( Blokkal::Entry * entry )
00078 {
00079         return _buvmp->entryViewMap.contains( entry );
00080 }
00081 
00082 void Blokkal::Ui::ViewManager::slotViewDestroyed( void )
00083 {
00084         for( QMap<Blokkal::Entry *, Blokkal::Ui::View *>::ConstIterator it = _buvmp->entryViewMap.begin();
00085              it != _buvmp->entryViewMap.end();
00086              ++it )
00087         {
00088                 if( _buvmp->viewWidgetMap.contains( it.value() ) &&
00089                     sender() == _buvmp->viewWidgetMap[it.value()] )
00090                 {
00091                         it.key()->deref();
00092                         _buvmp->viewWidgetMap.remove( it.value() );
00093                         _buvmp->entryViewMap.remove( it.key() );
00094                         return;
00095                 }
00096         }
00097 }
00098 
00099 void Blokkal::Ui::ViewManager::slotEntryDestroyed( Blokkal::Entry * entry )
00100 {
00101         if( !_buvmp->entryViewMap.contains( entry ) ) {
00102                 return;
00103         }
00104 
00105         _buvmp->viewWidgetMap.remove( _buvmp->entryViewMap[entry] );
00106         _buvmp->entryViewMap.remove( entry );
00107 }