From f0402e69c6073175b37932a90056700f0a064753 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Tue, 2 Oct 2007 14:39:48 +0000 Subject: [PATCH] Fix some issues with buffer closing. * Switch WorkAreaManager to std::list instead of vector. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20664 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Buffer.cpp | 6 ++++-- src/Buffer.h | 2 +- src/frontends/WorkAreaManager.cpp | 26 ++++++++++++++++++++++---- src/frontends/WorkAreaManager.h | 13 +++++++++++-- src/frontends/qt4/GuiView.cpp | 13 ++++++++++++- 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index c7dd9cdebe..53ee61775e 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -276,11 +276,13 @@ void Buffer::changed() } -frontend::WorkAreaManager * Buffer::workAreaManager() const +frontend::WorkAreaManager & Buffer::workAreaManager() const { - return pimpl_->wa_; + BOOST_ASSERT(pimpl_->wa_); + return *pimpl_->wa_; } + Text & Buffer::text() const { return const_cast(pimpl_->inset.text_); diff --git a/src/Buffer.h b/src/Buffer.h index 81e52d2517..c0b2648366 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -146,7 +146,7 @@ public: void changed(); /// - frontend::WorkAreaManager * workAreaManager() const; + frontend::WorkAreaManager & workAreaManager() const; /// This signal is emitted when the buffer structure is changed. boost::signal structureChanged; diff --git a/src/frontends/WorkAreaManager.cpp b/src/frontends/WorkAreaManager.cpp index e846cc1aff..a8c90309fd 100644 --- a/src/frontends/WorkAreaManager.cpp +++ b/src/frontends/WorkAreaManager.cpp @@ -15,6 +15,8 @@ #include "WorkAreaManager.h" +using std::list; + namespace lyx { namespace frontend { @@ -23,16 +25,32 @@ void WorkAreaManager::add(WorkArea * wa) work_areas_.push_back(wa); } + +void WorkAreaManager::remove(WorkArea * wa) +{ + work_areas_.remove(wa); +} + + void WorkAreaManager::redrawAll() { - for (size_t i = 0; i != work_areas_.size(); ++i) - work_areas_[i]->redraw(); + for (list::iterator it = work_areas_.begin(); + it != work_areas_.end(); ) { + (*it)->redraw(); + ++it; + } } + void WorkAreaManager::closeAll() { - for (size_t i = 0; i != work_areas_.size(); ++i) - work_areas_[i]->close(); + for (list::iterator it = work_areas_.begin(); + it != work_areas_.end(); ) { + (*it)->close(); + if (work_areas_.empty()) + break; + ++it; + } } } // namespace frontend diff --git a/src/frontends/WorkAreaManager.h b/src/frontends/WorkAreaManager.h index 7ff32127bb..f2c93be019 100644 --- a/src/frontends/WorkAreaManager.h +++ b/src/frontends/WorkAreaManager.h @@ -12,13 +12,19 @@ #ifndef WORKAREA_MANAGER_H #define WORKAREA_MANAGER_H -#include +#include namespace lyx { namespace frontend { class WorkArea; +/// \c WorkArea Manager. +/** + * This is a helper class designed to avoid signal/slot connections + * between a \c Buffer and the potentially multiple \c WorkArea(s) + * used to visualize this Buffer contents. + */ class WorkAreaManager { public: @@ -27,6 +33,9 @@ public: /// void add(WorkArea * wa); + /// + void remove(WorkArea * wa); + /// void redrawAll(); @@ -34,7 +43,7 @@ public: void closeAll(); private: - std::vector work_areas_; + std::list work_areas_; }; } // namespace frontend diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp index 24c82ffa07..b8f489c09d 100644 --- a/src/frontends/qt4/GuiView.cpp +++ b/src/frontends/qt4/GuiView.cpp @@ -295,6 +295,14 @@ GuiViewBase::~GuiViewBase() void GuiViewBase::close() { quitting_by_menu_ = true; + for (int i = 0; i != d.tab_widget_->count(); ++i) { + GuiWorkArea * wa = dynamic_cast(d.tab_widget_->widget(i)); + BOOST_ASSERT(wa); + Buffer & buffer = wa->bufferView().buffer(); + buffer.workAreaManager().remove(wa); + d.tab_widget_->removeTab(i); + delete wa; + } QMainWindow::close(); quitting_by_menu_ = false; } @@ -860,7 +868,7 @@ WorkArea * GuiViewBase::addWorkArea(Buffer & buffer) // Hide tabbar if there's only one tab. d.tab_widget_->showBar(d.tab_widget_->count() > 1); /// - buffer.workAreaManager()->add(wa); + buffer.workAreaManager().add(wa); return wa; } @@ -911,6 +919,9 @@ void GuiViewBase::removeWorkArea(WorkArea * work_area) disconnectBufferView(); } + Buffer & buffer = work_area->bufferView().buffer(); + buffer.workAreaManager().remove(work_area); + // removing a work area often results from closing a file so // update the toc in any case. updateToc();