1 //=========================================================
2 // MusE
3 // Linux Music Editor
4 // widget_stack.cpp
5 // (C) Copyright 1999-2004 Werner Schweer (ws@seh.de)
6 // (C) Copyright 2016 Tim E. Real (terminator356 on sourceforge)
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; version 2 of
11 // the License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 //
22 //=========================================================
23
24 #include "widget_stack.h"
25 #include "scrollbar.h"
26
27 #include <QWheelEvent>
28 #include <QVBoxLayout>
29
30 namespace MusEGui {
31
32 //---------------------------------------------------------
33 // WidgetStack
34 //---------------------------------------------------------
35
WidgetStack(QWidget * parent,const char * name,SizeHintMode sizeHintMode)36 WidgetStack::WidgetStack(QWidget* parent, const char* name, SizeHintMode sizeHintMode)
37 : QWidget(parent), _sizeHintMode(sizeHintMode)
38 {
39 setObjectName(name);
40 top = -1;
41 }
42
43 //---------------------------------------------------------
44 // raiseWidget
45 //---------------------------------------------------------
46
raiseWidget(int idx)47 void WidgetStack::raiseWidget(int idx)
48 {
49 if (top != -1) {
50 if (stack[top])
51 stack[top]->hide();
52 }
53 top = idx;
54 if (idx == -1)
55 return;
56 int n = stack.size();
57 if (idx >= n)
58 return;
59 if (stack[idx])
60 {
61 resizeStack(size());
62 stack[idx]->show();
63 }
64 }
65
66 //---------------------------------------------------------
67 // addWidget
68 //---------------------------------------------------------
69
addWidget(QWidget * w,unsigned int n)70 void WidgetStack::addWidget(QWidget* w, unsigned int n)
71 {
72 if (w)
73 w->hide();
74 if (stack.size() <= n )
75 stack.push_back(w);
76 else
77 {
78 stack[n] = w;
79 resizeStack(size());
80 }
81 }
82
getWidget(unsigned int n)83 QWidget* WidgetStack::getWidget(unsigned int n)
84 {
85 if (stack.size() <= n )
86 return 0;
87 return stack[n];
88 }
89
90 //---------------------------------------------------------
91 // visibleWidget
92 //---------------------------------------------------------
93
visibleWidget() const94 QWidget* WidgetStack::visibleWidget() const
95 {
96 if (top != -1)
97 return stack[top];
98 return 0;
99 }
100
101 //---------------------------------------------------------
102 // minimumSizeHint
103 //---------------------------------------------------------
104
minimumSizeHint() const105 QSize WidgetStack::minimumSizeHint() const
106 {
107 if (top == -1)
108 return (QSize(0, 0));
109
110 QSize s(0,0);
111
112 // Check if we want only the visible widget...
113 if(sizeHintMode() == VisibleHint && stack[top])
114 {
115 QSize ss = stack[top]->minimumSizeHint();
116 if (!ss.isValid())
117 {
118 // fprintf(stderr, "WidgetStack::minimumSizeHint: minimumSizeHint invalid, getting minimumSize\n");
119 ss = stack[top]->minimumSize();
120 }
121 // fprintf(stderr, "WidgetStack::minimumSizeHint w:%d h:%d\n", ss.width(), ss.height());
122 return ss;
123 }
124
125 for (unsigned int i = 0; i < stack.size(); ++i) {
126 if (stack[i]) {
127 QSize ss = stack[i]->minimumSizeHint();
128 if (!ss.isValid())
129 ss = stack[i]->minimumSize();
130 s = s.expandedTo(ss);
131 }
132 }
133
134 return s;
135 }
136
137 //---------------------------------------------------------
138 // wheelEvent
139 //---------------------------------------------------------
140
wheelEvent(QWheelEvent * ev)141 void WidgetStack::wheelEvent(QWheelEvent* ev)
142 {
143 emit redirectWheelEvent(ev);
144 }
145
resizeStack(const QSize & newSize)146 void WidgetStack::resizeStack(const QSize& newSize)
147 {
148 if(QWidget* widget = visibleWidget())
149 {
150 QSize wsz = widget->minimumSizeHint();
151 if(!wsz.isValid())
152 wsz = widget->minimumSize();
153
154 QSize sz(newSize);
155 if(sz.width() < wsz.width())
156 sz.setWidth(wsz.width());
157 if(sz.height() < wsz.height())
158 sz.setHeight(wsz.height());
159
160 widget->resize(sz);
161 }
162 }
163
resizeEvent(QResizeEvent * e)164 void WidgetStack::resizeEvent(QResizeEvent* e)
165 {
166 e->ignore();
167 QWidget::resizeEvent(e);
168 resizeStack(e->size());
169 }
170
sizeHint() const171 QSize WidgetStack::sizeHint() const
172 {
173 QSize s(0,0);
174 // Check if we want only the visible widget...
175 if(sizeHintMode() == VisibleHint)
176 {
177 if(top == -1 || !stack[top])
178 return s;
179
180 QSize ss = stack[top]->sizeHint();
181 if(ss.isValid())
182 return ss;
183 else
184 return s;
185 }
186
187 for(unsigned int i = 0; i < stack.size(); ++i)
188 {
189 if(stack[i])
190 {
191 QSize ss = stack[i]->sizeHint();
192 if(ss.isValid())
193 s = s.expandedTo(ss);
194 }
195 }
196 return s;
197 }
198
199
200 } // namespace MusEGui
201