]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiImplementation.cpp
Make GuiRef and GuiInclude subclasses of GuiCommand.
[lyx.git] / src / frontends / qt4 / GuiImplementation.cpp
1 // -*- C++ -*-
2 /**
3  * \file GuiImplementation.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiImplementation.h"
16
17 #include "GuiView.h"
18
19 #include <QApplication>
20
21
22 namespace
23 {
24         template<class T>
25         void updateIds(std::map<int, T*> const & stdmap, std::vector<int> & ids)
26         {
27                 ids.clear();
28                 typename std::map<int, T*>::const_iterator it;
29                 for (it = stdmap.begin(); it != stdmap.end(); ++it)
30                         ids.push_back(it->first);
31         }
32 }
33
34
35 namespace lyx {
36 namespace frontend {
37
38
39 GuiImplementation::GuiImplementation()
40 {
41         view_ids_.clear();
42 }
43
44
45 LyXView& GuiImplementation::createRegisteredView()
46 {
47         updateIds(views_, view_ids_);
48         int id = 0;
49         while (views_.find(id) != views_.end())
50                 id++;
51         views_.insert(std::pair<int, GuiViewBase *>(id, new GuiViewBase(id)));
52         updateIds(views_, view_ids_);
53         return *views_[id];
54 }
55
56
57 bool GuiImplementation::unregisterView(int id)
58 {
59         updateIds(views_, view_ids_);
60         BOOST_ASSERT(views_.find(id) != views_.end());
61         BOOST_ASSERT(views_[id]);
62
63         std::map<int, GuiViewBase *>::iterator it;
64         for (it = views_.begin(); it != views_.end(); ++it) {
65                 if (it->first == id) {
66                         views_.erase(id);
67                         break;
68                 }
69         }
70         updateIds(views_, view_ids_);
71         return true;
72 }
73
74
75 bool GuiImplementation::closeAllViews()
76 {
77         updateIds(views_, view_ids_);
78         if (views_.empty())
79         {
80                 // quit in CloseEvent will not be triggert
81                 qApp->quit();
82                 return true;
83         }
84
85         std::map<int, GuiViewBase*> const cmap = views_;
86         std::map<int, GuiViewBase*>::const_iterator it;
87         for (it = cmap.begin(); it != cmap.end(); ++it)
88         {
89                 // TODO: return false when close event was ignored
90                 //       e.g. quitWriteAll()->'Cancel'
91                 //       maybe we need something like 'bool closeView()'
92                 it->second->close();
93                 // unregisterd by the CloseEvent
94         }
95
96         views_.clear();
97         view_ids_.clear();
98         return true;
99 }
100
101
102 LyXView& GuiImplementation::view(int id) const
103 {
104         BOOST_ASSERT(views_.find(id) != views_.end());
105         return *views_.find(id)->second;
106 }
107
108
109 } // namespace frontend
110 } // namespace lyx
111
112 #include "GuiImplementation_moc.cpp"