Debian Codegraph @

src/Gui/View.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2004 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include "View.h"
25 #include "Application.h"
26 #include "Document.h"
27
28 using namespace Gui;
29
30 //**************************************************************************
31 // BaseView
32 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
34 TYPESYSTEM_SOURCE_ABSTRACT(Gui::BaseView, Base::BaseClass)
35
36
37 BaseView::BaseView(Gui::Document* pcDocument)
38 : _pcDocument(pcDocument)
39 {
40 if (pcDocument) {
41 pcDocument->attachView(this);
42 bIsPassive = false;
43 }
44 else {
45 Application::Instance->attachView(this);
46 bIsPassive = true;
47 }
48 }
49
50 BaseView::~BaseView()
51 {
52 onClose();
53 }
54
55 void BaseView::onClose()
56 {
57 if (bIsDetached) {
58 return;
59 }
60
61 if (bIsPassive) {
62 Application::Instance->detachView(this);
63 if (_pcDocument) {
64 _pcDocument->detachView(this, true);
65 }
66 }
67 else {
68 if (_pcDocument) {
69 _pcDocument->detachView(this);
70 }
71 }
72
73 _pcDocument = nullptr;
74 bIsDetached = true;
75 }
76
77 void BaseView::deleteSelf()
78 {
79 delete this;
80 }
81
82 void BaseView::setDocument(Gui::Document* pcDocument)
83 {
84 if (_pcDocument == pcDocument) {
85 return;
86 }
87 // detaches and attaches the observer
88 if (_pcDocument) {
89 _pcDocument->detachView(this, true);
90 }
91 if (pcDocument) {
92 pcDocument->attachView(this, true);
93 }
94
95 // set the new document as the active one
96 _pcDocument = pcDocument;
97 }
98
99 /// returns the document the view is attached to
100 App::Document* BaseView::getAppDocument() const
101 {
102 return _pcDocument ? _pcDocument->getDocument() : nullptr;
103 }