fw4spl
FrameLayoutManager.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwGuiQt/layoutManager/FrameLayoutManager.hpp"
8 
9 #include "fwGuiQt/QtMainFrame.hpp"
10 
11 #include <fwCore/base.hpp>
12 
13 #include <fwGui/registry/macros.hpp>
14 
15 #include <boost/lambda/lambda.hpp>
16 
17 #include <QApplication>
18 #include <QDesktopWidget>
19 #include <QIcon>
20 #include <QLayout>
21 #include <QMainWindow>
22 
23 fwGuiRegisterMacro( ::fwGui::FrameLayoutManager, ::fwGui::layoutManager::IFrameLayoutManager::REGISTRY_KEY );
24 
25 namespace fwGui
26 {
27 
28 //-----------------------------------------------------------------------------
29 
30 FrameLayoutManager::FrameLayoutManager(::fwGui::GuiBaseObject::Key key)
31 {
32 }
33 
34 //-----------------------------------------------------------------------------
35 
36 FrameLayoutManager::~FrameLayoutManager()
37 {
38 }
39 
40 //-----------------------------------------------------------------------------
41 
43 {
45  FrameInfo frameInfo = this->getFrameInfo();
46 
47  ::fwGuiQt::QtMainFrame* mainframe = new ::fwGuiQt::QtMainFrame();
48  m_qtWindow = mainframe;
49 
50  ::fwGuiQt::QtMainFrame::CloseCallback fct = std::bind( &::fwGui::FrameLayoutManager::onCloseFrame, this);
51  mainframe->setCloseCallback(fct);
52 
53  m_qtWindow->setWindowTitle(QString::fromStdString(frameInfo.m_name));
54  m_qtWindow->setMinimumSize(std::max(frameInfo.m_minSize.first, 0), std::max(frameInfo.m_minSize.second, 0));
55 
56  if(!frameInfo.m_iconPath.empty())
57  {
58  QIcon icon(QString::fromStdString(frameInfo.m_iconPath.string()));
59  OSLM_ASSERT("Unable to create an icon instance from " << frameInfo.m_iconPath.string(), !icon.isNull());
60  m_qtWindow->setWindowIcon(icon);
61  }
62  if(!qApp->activeWindow())
63  {
64  qApp->setActiveWindow(m_qtWindow);
65  }
66  if (frameInfo.m_style == ::fwGui::layoutManager::IFrameLayoutManager::STAY_ON_TOP)
67  {
68  m_qtWindow->setWindowFlags(Qt::WindowStaysOnTopHint);
69  }
70  else if(frameInfo.m_style == ::fwGui::layoutManager::IFrameLayoutManager::MODAL)
71  {
72  m_qtWindow->setWindowModality(Qt::ApplicationModal);
73  }
74 
75  int sizeX = (frameInfo.m_size.first > 0) ? frameInfo.m_size.first : m_qtWindow->size().width();
76  int sizeY = (frameInfo.m_size.second > 0) ? frameInfo.m_size.second : m_qtWindow->size().height();
77 
78  int posX = frameInfo.m_position.first;
79  int posY = frameInfo.m_position.second;
80  QPoint pos(posX, posY);
81  if(!this->isOnScreen(pos))
82  {
83  QRect frect(0, 0, sizeX, sizeY);
84  frect.moveCenter(QDesktopWidget().screenGeometry().center());
85  pos = frect.topLeft();
86  }
87  m_qtWindow->setGeometry(pos.x(), pos.y(), sizeX, sizeY);
88 
89  this->setState(frameInfo.m_state);
90 
91  QWidget* qwidget = new QWidget(m_qtWindow);
92  m_qtWindow->setCentralWidget(qwidget);
93 
94  QObject::connect(m_qtWindow, SIGNAL(destroyed(QObject*)), this, SLOT(onCloseFrame()));
95 
96  ::fwGuiQt::container::QtContainer::sptr container = ::fwGuiQt::container::QtContainer::New();
97  container->setQtContainer(qwidget);
98  m_container = container;
99 
100  ::fwGuiQt::container::QtContainer::sptr frameContainer = ::fwGuiQt::container::QtContainer::New();
101  frameContainer->setQtContainer(m_qtWindow);
102  m_frame = frameContainer;
103 }
104 
105 //-----------------------------------------------------------------------------
106 
108 {
109 
110  this->getFrameInfo().m_state = this->getState();
111  this->getFrameInfo().m_size.first = m_qtWindow->size().width();
112  this->getFrameInfo().m_size.second = m_qtWindow->size().height();
113  this->getFrameInfo().m_position.first = m_qtWindow->geometry().x();
114  this->getFrameInfo().m_position.second = m_qtWindow->geometry().y();
115  this->writeConfig();
116 
117  QObject::disconnect(m_qtWindow, SIGNAL(destroyed(QObject*)), this, SLOT(onCloseFrame()));
118 
119  m_container->destroyContainer();
120 
121  // m_qtWindow is cleaned/destroyed by m_frame
122  m_frame->destroyContainer();
123 }
124 
125 //-----------------------------------------------------------------------------
126 
127 void FrameLayoutManager::onCloseFrame()
128 {
129  SLM_TRACE_FUNC();
130  this->m_closeCallback();
131 }
132 
133 //-----------------------------------------------------------------------------
134 
135 void FrameLayoutManager::setState( FrameState state )
136 {
137  // Updates the window state.
138  switch( state )
139  {
140  case ICONIZED:
141  m_qtWindow->showMinimized();
142  break;
143 
144  case MAXIMIZED:
145  m_qtWindow->showMaximized();
146  break;
147 
148  case FULL_SCREEN:
149  m_qtWindow->showFullScreen();
150  break;
151  default:
152  m_qtWindow->showNormal();
153  }
154 }
155 
156 //-----------------------------------------------------------------------------
157 
158 ::fwGui::layoutManager::IFrameLayoutManager::FrameState FrameLayoutManager::getState()
159 {
160  FrameState state( UNKNOWN );
161 
162  if( m_qtWindow->isMinimized() )
163  {
164  state = ICONIZED;
165  }
166  else if( m_qtWindow->isMaximized() )
167  {
168  state = MAXIMIZED;
169  }
170  else if( m_qtWindow->isFullScreen() )
171  {
172  state = FULL_SCREEN;
173  }
174  return state;
175 }
176 
177 //-----------------------------------------------------------------------------
178 
179 bool FrameLayoutManager::isOnScreen(const QPoint& pos)
180 {
181  bool isVisible = false;
182  for(int i = 0; i < QDesktopWidget().screenCount() && !isVisible; ++i)
183  {
184  isVisible = QDesktopWidget().screenGeometry(i).contains(pos, false);
185  }
186  return isVisible;
187 }
188 
189 //-----------------------------------------------------------------------------
190 
191 } // namespace fwGui
const FrameInfo & getFrameInfo() const
Configuration definition.
virtual FWGUIQT_API void createFrame() override
Instantiate frame.
#define OSLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:310
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Defines the frame manager.
std::pair< int, int > m_minSize
Frame minimum size (min width and min height)
A qt panel used to control a VTK 2D Negatoscope view.
Definition: QtMainFrame.hpp:24
virtual FWGUIQT_API void destroyFrame() override
Destroy local frame with sub containers.
std::pair< int, int > m_position
Frame position.
::boost::filesystem::path m_iconPath
Frame icon.
FrameState m_state
Frame state (maximize, minized, full screen)