1 // Copyright (c) 2016 The SigViewer Development Team
2 // Licensed under the GNU General Public License (GPL)
3 // https://www.gnu.org/licenses/gpl
4 
5 
6 #include "application_context_impl.h"
7 #include "base/exception.h"
8 
9 #include "gui_impl/main_window_model_impl.h"
10 
11 #include <QDebug>
12 
13 
14 namespace sigviewer
15 {
16 
17 //-----------------------------------------------------------------------------
getInstance(bool cleanup)18 QSharedPointer<ApplicationContextImpl> ApplicationContextImpl::getInstance (bool cleanup)
19 {
20     static QSharedPointer<ApplicationContextImpl> instance (new ApplicationContextImpl);
21     if (cleanup)
22         instance.clear();
23     if (instance.isNull ())
24     {
25         instance = QSharedPointer<ApplicationContextImpl> (new ApplicationContextImpl);
26         qDebug () << "ApplicationContextImpl::getInstance() created new instance = " << instance.data();
27     }
28     return instance;
29 }
30 
31 //-------------------------------------------------------------------------
init(std::set<ApplicationMode> activated_modes)32 void ApplicationContextImpl::init (std::set<ApplicationMode> activated_modes)
33 {
34     qDebug () << "ApplicationContextImpl::init() instance = " << getInstance();
35 
36     getInstance()->activated_modes_ = activated_modes;
37     getInstance()->setState (APP_STATE_NO_FILE_OPEN);
38     if (activated_modes.count (APPLICATION_NON_GUI_MODE) == 0)
39     {
40         getInstance()->color_manager_ = QSharedPointer<ColorManager> (new ColorManager);
41         getInstance()->main_window_model_ = QSharedPointer<MainWindowModel> (new MainWindowModelImpl (getInstance()));
42     }
43 }
44 
45 //-------------------------------------------------------------------------
cleanup()46 void ApplicationContextImpl::cleanup ()
47 {
48     qDebug () << "ApplicationContextImpl::cleanup()";
49     getInstance (true);
50 }
51 
52 
53 //-----------------------------------------------------------------------------
~ApplicationContextImpl()54 ApplicationContextImpl::~ApplicationContextImpl ()
55 {
56     qDebug () << "deleting ApplicationContextImpl";
57 }
58 
59 //-------------------------------------------------------------------------
modeActivated(ApplicationMode mode) const60 bool ApplicationContextImpl::modeActivated (ApplicationMode mode) const
61 {
62     return activated_modes_.count (mode) > 0;
63 }
64 
65 //-------------------------------------------------------------------------
getCurrentFileContext()66 QSharedPointer<FileContext> ApplicationContextImpl::getCurrentFileContext ()
67 {
68     return current_file_context_;
69 }
70 
71 //-------------------------------------------------------------------------
setCurrentTabContext(QSharedPointer<TabContext> tab_context)72 void ApplicationContextImpl::setCurrentTabContext (QSharedPointer<TabContext> tab_context)
73 {
74     if (!current_tab_context_.isNull())
75         current_tab_context_->disconnect (this);
76     current_tab_context_ = tab_context;
77     if (!connect (current_tab_context_.data(), SIGNAL(selectionStateChanged(TabSelectionState)), SIGNAL(currentTabSelectionStateChanged(TabSelectionState))))
78         throw (Exception ("ApplicationContext::setCurrentTabContext failed to connect to selectionStateChanged"));
79     if (!connect (current_tab_context_.data(), SIGNAL(editStateChanged(TabEditState)), SIGNAL(currentTabEditStateChanged(TabEditState))))
80         throw (Exception ("ApplicationContext::setCurrentTabContext failed to connect to editStateChanged"));
81 }
82 
83 //-------------------------------------------------------------------------
getCurrentCommandExecuter()84 QSharedPointer<CommandExecuter> ApplicationContextImpl::getCurrentCommandExecuter ()
85 {
86     return current_tab_context_;
87 }
88 
89 //-------------------------------------------------------------------------
addFileContext(QSharedPointer<FileContext> file_context)90 void ApplicationContextImpl::addFileContext (QSharedPointer<FileContext>file_context)
91 {
92     current_file_context_ = file_context;
93     if (!connect (current_file_context_.data(), SIGNAL(stateChanged(FileState)), SIGNAL(currentFileStateChanged(FileState))))
94         throw (Exception ("ApplicationContext::addFileContext faild to connect stateChanged(FileState)"));
95     setState (APP_STATE_FILE_OPEN);
96 }
97 
98 //-------------------------------------------------------------------------
removeCurrentFileContext()99 void ApplicationContextImpl::removeCurrentFileContext ()
100 {
101     if (!current_file_context_.isNull())
102         current_file_context_->disconnect ();
103     current_file_context_ = QSharedPointer<FileContext> (0);
104     setState (APP_STATE_NO_FILE_OPEN);
105 }
106 
107 //-----------------------------------------------------------------------------
getMainWindowModel()108 QSharedPointer<MainWindowModel> ApplicationContextImpl::getMainWindowModel ()
109 {
110     return main_window_model_;
111 }
112 
113 //-------------------------------------------------------------------------
getEventColorManager()114 QSharedPointer<ColorManager> ApplicationContextImpl::getEventColorManager ()
115 {
116     return color_manager_;
117 }
118 
119 //-----------------------------------------------------------------------------
setState(ApplicationState state)120 void ApplicationContextImpl::setState (ApplicationState state)
121 {
122     qDebug () << "ApplicationContextImpl::setState " << state << "; this = " << this;
123     state_ = state;
124     emit stateChanged (state_);
125 }
126 
127 }
128