45
|
1 |
#include "PLLayout.h"
|
|
2 |
|
|
3 |
#include <QDebug>
|
|
4 |
#include <QApplication>
|
|
5 |
#include <QDesktopWidget>
|
|
6 |
|
|
7 |
PLLayout::PLLayout()
|
|
8 |
{
|
|
9 |
m_numOfClients=0;
|
|
10 |
m_currentLayoutIdx=-1;
|
|
11 |
m_portraitLayoutIdx=-1;
|
|
12 |
m_landscapeLayoutIdx=-1;
|
|
13 |
m_hasLLayout=false;
|
|
14 |
m_hasPLayout=false;
|
|
15 |
m_isPLayout=false;
|
|
16 |
m_isLLayout=false;
|
|
17 |
}
|
|
18 |
|
|
19 |
PLLayout::~PLLayout()
|
|
20 |
{
|
|
21 |
//take ownership and responsibility to clean up the child layouts
|
|
22 |
while(m_numOfClients>0) {
|
|
23 |
delete(m_layoutList[--m_numOfClients]);
|
|
24 |
}
|
|
25 |
}
|
|
26 |
|
|
27 |
/*
|
|
28 |
FIRST, some basic methods that implement the quite
|
|
29 |
universal engine of this portrait landscape manager.
|
|
30 |
*/
|
|
31 |
int PLLayout::newLayout(QLayout *clientlayout)
|
|
32 |
{
|
|
33 |
m_layoutList.append(clientlayout);
|
|
34 |
if(m_currentLayoutIdx==-1)
|
|
35 |
m_currentLayoutIdx=0;
|
|
36 |
return m_numOfClients++;
|
|
37 |
}
|
|
38 |
|
|
39 |
void PLLayout::activateLayout(int idx) {
|
|
40 |
if((idx>-1)&&(idx<m_numOfClients)) {
|
|
41 |
if(m_currentLayoutIdx != idx) {
|
|
42 |
m_currentLayoutIdx = idx;
|
|
43 |
redrawParent();
|
|
44 |
}
|
|
45 |
} else
|
|
46 |
qDebug() << "ERROR: selecting unassigned MultLayoutClient. IDX=" << idx
|
|
47 |
<< " numOfClients=" << m_numOfClients;
|
|
48 |
}
|
|
49 |
|
|
50 |
int PLLayout::getLayoutIdx() const
|
|
51 |
{
|
|
52 |
return m_currentLayoutIdx;
|
|
53 |
}
|
|
54 |
|
|
55 |
void PLLayout::redrawParent()
|
|
56 |
{
|
|
57 |
activate(); //inherited from QLayout
|
|
58 |
update(); //inherited from QLayout
|
|
59 |
QWidget *w = parentWidget();
|
|
60 |
if(!(w == 0))
|
|
61 |
w->updateGeometry();
|
|
62 |
}
|
|
63 |
|
|
64 |
/*
|
|
65 |
NOW, implement all required methods of a QLayout Subclass, and point
|
|
66 |
them to the actual activated layout
|
|
67 |
*/
|
|
68 |
void PLLayout::addItem(QLayoutItem *item)
|
|
69 |
{
|
|
70 |
if (m_numOfClients<0) {
|
|
71 |
qDebug() << "ERROR: using MultLayout before assigning client.";
|
|
72 |
return;
|
|
73 |
}
|
|
74 |
m_layoutList[getLayoutIdx()]->addItem(item);
|
|
75 |
}
|
|
76 |
|
|
77 |
QSize PLLayout::sizeHint() const {
|
|
78 |
if (m_numOfClients<0) {
|
|
79 |
qDebug() << "ERROR: using MultLayout before assigning client.";
|
|
80 |
return QSize(0,0);
|
|
81 |
}
|
|
82 |
return (m_layoutList[getLayoutIdx()]->sizeHint());
|
|
83 |
}
|
|
84 |
|
|
85 |
QSize PLLayout::minimumSize() const
|
|
86 |
{
|
|
87 |
if (m_numOfClients<0) {
|
|
88 |
qDebug() << "ERROR: using MultLayout before assigning client.";
|
|
89 |
return QSize(0,0);
|
|
90 |
}
|
|
91 |
return( m_layoutList[getLayoutIdx()]->minimumSize());
|
|
92 |
}
|
|
93 |
|
|
94 |
void PLLayout::setGeometry(const QRect &rect)
|
|
95 |
{
|
|
96 |
if (m_numOfClients<0) {
|
|
97 |
qDebug() << "ERROR: using MultLayout before assigning client.";
|
|
98 |
return ;
|
|
99 |
}
|
|
100 |
m_layoutList[getLayoutIdx()]->setGeometry(rect);
|
|
101 |
}
|
|
102 |
|
|
103 |
bool PLLayout::hasHeightForWidth() const
|
|
104 |
{
|
|
105 |
if (m_numOfClients<0) {
|
|
106 |
qDebug() << "ERROR: using MultLayout before assigning client.";
|
|
107 |
return false;
|
|
108 |
}
|
|
109 |
return m_layoutList[getLayoutIdx()]->hasHeightForWidth();
|
|
110 |
}
|
|
111 |
|
|
112 |
int PLLayout::heightForWidth(int n) const
|
|
113 |
{
|
|
114 |
if (m_numOfClients<0) {
|
|
115 |
qDebug() << "ERROR: using MultLayout before assigning client.";
|
|
116 |
return 0;
|
|
117 |
}
|
|
118 |
return m_layoutList[getLayoutIdx()]->heightForWidth(n);
|
|
119 |
}
|
|
120 |
|
|
121 |
|
|
122 |
QLayoutItem * PLLayout::itemAt(int index) const
|
|
123 |
{
|
|
124 |
if (m_numOfClients<0) {
|
|
125 |
qDebug() << "ERROR: using MultLayout before assigning client.";
|
|
126 |
return 0;
|
|
127 |
}
|
|
128 |
return m_layoutList[getLayoutIdx()]->itemAt(index);
|
|
129 |
}
|
|
130 |
|
|
131 |
|
|
132 |
QLayoutItem * PLLayout::takeAt(int index)
|
|
133 |
{
|
|
134 |
if (m_numOfClients<0) {
|
|
135 |
qDebug() << "ERROR: using MultLayout before assigning client.";
|
|
136 |
return 0;
|
|
137 |
}
|
|
138 |
return m_layoutList[getLayoutIdx()]->takeAt(index);
|
|
139 |
}
|
|
140 |
|
|
141 |
int PLLayout::count() const
|
|
142 |
{
|
|
143 |
if (m_numOfClients<0) {
|
|
144 |
qDebug() << "ERROR: using MultLayout before assigning client.";
|
|
145 |
return 0;
|
|
146 |
}
|
|
147 |
return m_layoutList[getLayoutIdx()]->count();
|
|
148 |
}
|
|
149 |
|
|
150 |
/*
|
|
151 |
Now we add some convenience API methods to handle portrait/landscape comfortable
|
|
152 |
While the above mechanism is quite universal and may be re-used beyond portrait
|
|
153 |
landscape use case, these implement the dedicated API for portrait / landscape use.
|
|
154 |
*/
|
|
155 |
int PLLayout::setLLayout(QLayout *clientlayout)
|
|
156 |
{
|
|
157 |
if(m_hasLLayout) {
|
|
158 |
qDebug() << "ERROR: re-assigning Layout to FthLPMultiLayout is not allowed";
|
|
159 |
return -1;
|
|
160 |
}
|
|
161 |
int i = newLayout(clientlayout);
|
|
162 |
m_hasLLayout = true;
|
|
163 |
m_landscapeLayoutIdx = i;
|
|
164 |
activateLayout(m_landscapeLayoutIdx);
|
|
165 |
m_isLLayout = true;
|
|
166 |
return i;
|
|
167 |
}
|
|
168 |
|
|
169 |
int PLLayout::setPLayout(QLayout *clientlayout)
|
|
170 |
{
|
|
171 |
if(m_hasPLayout) {
|
|
172 |
qDebug() << "ERROR: re-assigning Layout to FthLPMultiLayout is not allowed";
|
|
173 |
return -1;
|
|
174 |
}
|
|
175 |
int i = newLayout(clientlayout);
|
|
176 |
m_hasPLayout = true;
|
|
177 |
m_portraitLayoutIdx = i;
|
|
178 |
activateLayout(m_portraitLayoutIdx);
|
|
179 |
m_isPLayout = true;
|
|
180 |
return i;
|
|
181 |
}
|
|
182 |
|
|
183 |
void PLLayout::activatePLayout()
|
|
184 |
{
|
|
185 |
if((m_isLLayout)&&(m_hasPLayout)) {
|
|
186 |
activateLayout(m_portraitLayoutIdx);
|
|
187 |
m_isPLayout = true;
|
|
188 |
} else {
|
|
189 |
qDebug() << "ERROR: first assign portrait and landscape layouts to FthLPMultiLayout";
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
void PLLayout::activateLLayout()
|
|
194 |
{
|
|
195 |
if((m_isPLayout)&&(m_hasLLayout)) {
|
|
196 |
activateLayout(m_landscapeLayoutIdx);
|
|
197 |
m_isLLayout = true;
|
|
198 |
} else {
|
|
199 |
qDebug() << "ERROR: first assign portrait and landscape layouts to FthLPMultiLayout";
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
/*
|
|
204 |
and finally a convenience function for the parent widgets event handler
|
|
205 |
*/
|
|
206 |
|
|
207 |
void PLLayout::resizeEvent(QResizeEvent* event)
|
|
208 |
{
|
|
209 |
QRect qs=QApplication::desktop()->availableGeometry();
|
|
210 |
if(qs.height() > qs.width())
|
|
211 |
activatePLayout();
|
|
212 |
else
|
|
213 |
activateLLayout();
|
|
214 |
}
|
|
215 |
|
|
216 |
/*
|
|
217 |
REFERENCES
|
|
218 |
http://lists.trolltech.com/qt-interest/2004-01/thread00746-0.html
|
|
219 |
http://wiki.forum.nokia.com/index.php/CS001437_-_Listening_for_screen_orientation_changes_in_Qt
|
|
220 |
http://wiki.forum.nokia.com/index.php/Dynamic_Layout_handling_with_QWidget
|
|
221 |
*/
|