Salome HOME
ee9dbcfdf05ca2fd6df7516114946e178905c48e
[modules/gui.git] / src / Qtx / QtxWorkspace.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:      QtxWorkspace.cxx
21 // Author:    Sergey TELKOV
22 //
23 #include "QtxWorkspace.h"
24
25 #include <QMdiSubWindow>
26
27 /*!
28   \class QtxWorkspace
29   \brief A workspace widget which can be used in the MDI application
30          as top-level widget in the application main window.
31
32   Provides methods to tile child windows in horizontal or vertical
33   direction.
34 */
35
36 /*!
37   \brief Constructor.
38   \param parent parent widget
39 */
40 QtxWorkspace::QtxWorkspace( QWidget* parent )
41 : QMdiArea( parent )
42 {
43 }
44
45 /*!
46   \brief Destructor.
47 */
48 QtxWorkspace::~QtxWorkspace()
49 {
50 }
51
52 /*!
53   \brief Tiles child windows vertically.
54 */
55 void QtxWorkspace::tileVertical()
56 {
57   QList<QMdiSubWindow *> winList = subWindowList();
58   if ( winList.isEmpty() )
59     return;
60   
61   int count = 0;
62   for ( QList<QMdiSubWindow *>::const_iterator itr = winList.begin(); itr != winList.end(); ++itr )
63     if ( !( (*itr)->windowState() & Qt::WindowMinimized ) )
64       count++;
65
66   if ( !count )
67     return;
68
69   if ( activeSubWindow() && ( activeSubWindow()->windowState() & Qt::WindowMaximized ) )
70     activeSubWindow()->showNormal();
71
72   int y = 0;
73   int heightForEach = height() / count;
74   for ( QList<QMdiSubWindow *>::iterator it = winList.begin(); it != winList.end(); ++it )
75   {
76     QWidget* win = *it;
77     if ( win->windowState() & Qt::WindowMinimized )
78       continue;
79     
80     if ( win->windowState() & Qt::WindowMaximized )
81     {
82       win->hide();
83       win->showNormal();
84     }
85     
86 //    QApplication::sendPostedEvents( 0, QEvent::ShowNormal );
87
88     int prefH = win->minimumHeight() + win->parentWidget()->baseSize().height();
89     int actualH = qMax( heightForEach, prefH );
90
91     win->parentWidget()->setGeometry( 0, y, width(), actualH );
92     y += actualH;
93   }
94 }
95
96 /*!
97   \brief Tiles child windows horizontally.
98 */
99 void QtxWorkspace::tileHorizontal()
100 {
101   QList<QMdiSubWindow *> winList = subWindowList();
102   if ( winList.isEmpty() )
103     return;
104
105   int count = 0;
106   for ( QList<QMdiSubWindow *>::const_iterator itr = winList.begin(); itr != winList.end(); ++itr )
107     if ( !( (*itr)->windowState() & Qt::WindowMinimized ) )
108       count++;
109
110   if ( !count )
111     return;
112
113   if ( activeSubWindow() && activeSubWindow()->windowState() & Qt::WindowMaximized )
114     activeSubWindow()->showNormal();
115   
116   int x = 0;
117   int widthForEach = width() / count;
118   for ( QList<QMdiSubWindow *>::iterator it = winList.begin(); it != winList.end(); ++it )
119   {
120     QWidget* win = *it;
121     if ( win->windowState() & Qt::WindowMinimized )
122       continue;
123     
124     if ( win->windowState() & Qt::WindowMaximized )
125     {
126       win->hide();
127       win->showNormal();
128     }
129
130 //    QApplication::sendPostedEvents( 0, QEvent::ShowNormal );
131
132     int prefW = win->minimumWidth();
133     int actualW = qMax( widthForEach, prefW );
134     
135     win->parentWidget()->setGeometry( x, 0, actualW, height() );
136     x += actualW;
137   }
138 }
139
140 void QtxWorkspace::onSubWindowActivated( QMdiSubWindow* subWindow )
141 {
142   QWidget* w = 0;
143   if ( subWindow )
144     w = subWindow->widget();
145   emit windowActivated( w );
146 }