comparison view/PaneStack.cpp @ 127:89c625dda204

* Reorganising code base. This revision will not compile.
author Chris Cannam
date Mon, 31 Jul 2006 11:44:37 +0000
parents
children 33929e0c3c6b
comparison
equal deleted inserted replaced
126:0e95c127bb53 127:89c625dda204
1
2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
3
4 /*
5 Sonic Visualiser
6 An audio file viewer and annotation editor.
7 Centre for Digital Music, Queen Mary, University of London.
8 This file copyright 2006 Chris Cannam.
9
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version. See the file
14 COPYING included with this distribution for more information.
15 */
16
17 #include "PaneStack.h"
18
19 #include "widgets/Pane.h"
20 #include "widgets/PropertyStack.h"
21 #include "base/Layer.h"
22 #include "base/ViewManager.h"
23
24 #include <QApplication>
25 #include <QHBoxLayout>
26 #include <QPainter>
27 #include <QPalette>
28 #include <QLabel>
29 #include <QSplitter>
30 #include <QStackedWidget>
31
32 #include <iostream>
33
34 PaneStack::PaneStack(QWidget *parent, ViewManager *viewManager) :
35 QFrame(parent),
36 m_currentPane(0),
37 m_splitter(new QSplitter),
38 m_propertyStackStack(new QStackedWidget),
39 m_viewManager(viewManager),
40 m_layoutStyle(PropertyStackPerPaneLayout)
41 {
42 QHBoxLayout *layout = new QHBoxLayout;
43 layout->setMargin(0);
44 layout->setSpacing(0);
45
46 m_splitter->setOrientation(Qt::Vertical);
47 m_splitter->setOpaqueResize(false);
48
49 layout->addWidget(m_splitter);
50 layout->setStretchFactor(m_splitter, 1);
51 layout->addWidget(m_propertyStackStack);
52 m_propertyStackStack->hide();
53
54 setLayout(layout);
55 }
56
57 Pane *
58 PaneStack::addPane(bool suppressPropertyBox)
59 {
60 QFrame *frame = new QFrame;
61 QHBoxLayout *layout = new QHBoxLayout;
62 layout->setMargin(0);
63 layout->setSpacing(2);
64
65 QLabel *currentIndicator = new QLabel(frame);
66 currentIndicator->setFixedWidth(QPainter(this).fontMetrics().width("x"));
67 layout->addWidget(currentIndicator);
68 layout->setStretchFactor(currentIndicator, 1);
69 currentIndicator->setScaledContents(true);
70
71 Pane *pane = new Pane(frame);
72 pane->setViewManager(m_viewManager);
73 layout->addWidget(pane);
74 layout->setStretchFactor(pane, 10);
75
76 QWidget *properties = 0;
77 if (suppressPropertyBox) {
78 properties = new QFrame();
79 } else {
80 properties = new PropertyStack(frame, pane);
81 connect(properties, SIGNAL(propertyContainerSelected(View *, PropertyContainer *)),
82 this, SLOT(propertyContainerSelected(View *, PropertyContainer *)));
83 }
84 if (m_layoutStyle == PropertyStackPerPaneLayout) {
85 layout->addWidget(properties);
86 } else {
87 properties->setParent(m_propertyStackStack);
88 m_propertyStackStack->addWidget(properties);
89 }
90 layout->setStretchFactor(properties, 1);
91
92 PaneRec rec;
93 rec.pane = pane;
94 rec.propertyStack = properties;
95 rec.currentIndicator = currentIndicator;
96 rec.frame = frame;
97 rec.layout = layout;
98 m_panes.push_back(rec);
99
100 frame->setLayout(layout);
101 m_splitter->addWidget(frame);
102
103 connect(pane, SIGNAL(propertyContainerAdded(PropertyContainer *)),
104 this, SLOT(propertyContainerAdded(PropertyContainer *)));
105 connect(pane, SIGNAL(propertyContainerRemoved(PropertyContainer *)),
106 this, SLOT(propertyContainerRemoved(PropertyContainer *)));
107 connect(pane, SIGNAL(paneInteractedWith()),
108 this, SLOT(paneInteractedWith()));
109 connect(pane, SIGNAL(rightButtonMenuRequested(QPoint)),
110 this, SLOT(rightButtonMenuRequested(QPoint)));
111
112 if (!m_currentPane) {
113 setCurrentPane(pane);
114 }
115
116 return pane;
117 }
118
119 void
120 PaneStack::setLayoutStyle(LayoutStyle style)
121 {
122 if (style == m_layoutStyle) return;
123 m_layoutStyle = style;
124
125 std::vector<PaneRec>::iterator i;
126
127 switch (style) {
128
129 case SinglePropertyStackLayout:
130
131 for (i = m_panes.begin(); i != m_panes.end(); ++i) {
132 i->layout->removeWidget(i->propertyStack);
133 i->propertyStack->setParent(m_propertyStackStack);
134 m_propertyStackStack->addWidget(i->propertyStack);
135 }
136 m_propertyStackStack->show();
137 break;
138
139 case PropertyStackPerPaneLayout:
140
141 for (i = m_panes.begin(); i != m_panes.end(); ++i) {
142 m_propertyStackStack->removeWidget(i->propertyStack);
143 i->propertyStack->setParent(i->frame);
144 i->layout->addWidget(i->propertyStack);
145 i->propertyStack->show();
146 }
147 m_propertyStackStack->hide();
148 break;
149 }
150 }
151
152 Pane *
153 PaneStack::getPane(int n)
154 {
155 return m_panes[n].pane;
156 }
157
158 Pane *
159 PaneStack::getHiddenPane(int n)
160 {
161 return m_hiddenPanes[n].pane;
162 }
163
164 void
165 PaneStack::deletePane(Pane *pane)
166 {
167 std::vector<PaneRec>::iterator i;
168 bool found = false;
169
170 for (i = m_panes.begin(); i != m_panes.end(); ++i) {
171 if (i->pane == pane) {
172 m_panes.erase(i);
173 found = true;
174 break;
175 }
176 }
177
178 if (!found) {
179
180 for (i = m_hiddenPanes.begin(); i != m_hiddenPanes.end(); ++i) {
181 if (i->pane == pane) {
182 m_hiddenPanes.erase(i);
183 found = true;
184 break;
185 }
186 }
187
188 if (!found) {
189 std::cerr << "WARNING: PaneStack::deletePane(" << pane << "): Pane not found in visible or hidden panes, not deleting" << std::endl;
190 return;
191 }
192 }
193
194 delete pane->parent();
195
196 if (m_currentPane == pane) {
197 if (m_panes.size() > 0) {
198 setCurrentPane(m_panes[0].pane);
199 } else {
200 setCurrentPane(0);
201 }
202 }
203 }
204
205 int
206 PaneStack::getPaneCount() const
207 {
208 return m_panes.size();
209 }
210
211 int
212 PaneStack::getHiddenPaneCount() const
213 {
214 return m_hiddenPanes.size();
215 }
216
217 void
218 PaneStack::hidePane(Pane *pane)
219 {
220 std::vector<PaneRec>::iterator i = m_panes.begin();
221
222 while (i != m_panes.end()) {
223 if (i->pane == pane) {
224
225 m_hiddenPanes.push_back(*i);
226 m_panes.erase(i);
227
228 QWidget *pw = dynamic_cast<QWidget *>(pane->parent());
229 if (pw) pw->hide();
230
231 if (m_currentPane == pane) {
232 if (m_panes.size() > 0) {
233 setCurrentPane(m_panes[0].pane);
234 } else {
235 setCurrentPane(0);
236 }
237 }
238
239 return;
240 }
241 ++i;
242 }
243
244 std::cerr << "WARNING: PaneStack::hidePane(" << pane << "): Pane not found in visible panes" << std::endl;
245 }
246
247 void
248 PaneStack::showPane(Pane *pane)
249 {
250 std::vector<PaneRec>::iterator i = m_hiddenPanes.begin();
251
252 while (i != m_hiddenPanes.end()) {
253 if (i->pane == pane) {
254 m_panes.push_back(*i);
255 m_hiddenPanes.erase(i);
256 QWidget *pw = dynamic_cast<QWidget *>(pane->parent());
257 if (pw) pw->show();
258
259 //!!! update current pane
260
261 return;
262 }
263 ++i;
264 }
265
266 std::cerr << "WARNING: PaneStack::showPane(" << pane << "): Pane not found in hidden panes" << std::endl;
267 }
268
269 void
270 PaneStack::setCurrentPane(Pane *pane) // may be null
271 {
272 if (m_currentPane == pane) return;
273
274 std::vector<PaneRec>::iterator i = m_panes.begin();
275
276 // We used to do this by setting the foreground and background
277 // role, but it seems the background role is ignored and the
278 // background drawn transparent in Qt 4.1 -- I can't quite see why
279
280 QPixmap selectedMap(1, 1);
281 selectedMap.fill(QApplication::palette().color(QPalette::Foreground));
282
283 QPixmap unselectedMap(1, 1);
284 unselectedMap.fill(QApplication::palette().color(QPalette::Background));
285
286 bool found = false;
287
288 while (i != m_panes.end()) {
289 if (i->pane == pane) {
290 i->currentIndicator->setPixmap(selectedMap);
291 if (m_layoutStyle == SinglePropertyStackLayout) {
292 m_propertyStackStack->setCurrentWidget(i->propertyStack);
293 }
294 found = true;
295 } else {
296 i->currentIndicator->setPixmap(unselectedMap);
297 }
298 ++i;
299 }
300
301 if (found || pane == 0) {
302 m_currentPane = pane;
303 emit currentPaneChanged(m_currentPane);
304 } else {
305 std::cerr << "WARNING: PaneStack::setCurrentPane(" << pane << "): pane is not a visible pane in this stack" << std::endl;
306 }
307 }
308
309 void
310 PaneStack::setCurrentLayer(Pane *pane, Layer *layer) // may be null
311 {
312 setCurrentPane(pane);
313
314 if (m_currentPane) {
315
316 std::vector<PaneRec>::iterator i = m_panes.begin();
317
318 while (i != m_panes.end()) {
319
320 if (i->pane == pane) {
321 PropertyStack *stack = dynamic_cast<PropertyStack *>
322 (i->propertyStack);
323 if (stack) {
324 if (stack->containsContainer(layer)) {
325 stack->setCurrentIndex(stack->getContainerIndex(layer));
326 emit currentLayerChanged(pane, layer);
327 } else {
328 stack->setCurrentIndex
329 (stack->getContainerIndex
330 (pane->getPropertyContainer(0)));
331 emit currentLayerChanged(pane, 0);
332 }
333 }
334 break;
335 }
336 ++i;
337 }
338 }
339 }
340
341 Pane *
342 PaneStack::getCurrentPane()
343 {
344 return m_currentPane;
345 }
346
347 void
348 PaneStack::propertyContainerAdded(PropertyContainer *)
349 {
350 sizePropertyStacks();
351 }
352
353 void
354 PaneStack::propertyContainerRemoved(PropertyContainer *)
355 {
356 sizePropertyStacks();
357 }
358
359 void
360 PaneStack::propertyContainerSelected(View *client, PropertyContainer *pc)
361 {
362 std::vector<PaneRec>::iterator i = m_panes.begin();
363
364 while (i != m_panes.end()) {
365 PropertyStack *stack = dynamic_cast<PropertyStack *>(i->propertyStack);
366 if (stack &&
367 stack->getClient() == client &&
368 stack->containsContainer(pc)) {
369 setCurrentPane(i->pane);
370 break;
371 }
372 ++i;
373 }
374
375 Layer *layer = dynamic_cast<Layer *>(pc);
376 if (layer) emit currentLayerChanged(m_currentPane, layer);
377 else emit currentLayerChanged(m_currentPane, 0);
378 }
379
380 void
381 PaneStack::paneInteractedWith()
382 {
383 Pane *pane = dynamic_cast<Pane *>(sender());
384 if (!pane) return;
385 setCurrentPane(pane);
386 }
387
388 void
389 PaneStack::rightButtonMenuRequested(QPoint position)
390 {
391 Pane *pane = dynamic_cast<Pane *>(sender());
392 if (!pane) return;
393 emit rightButtonMenuRequested(pane, position);
394 }
395
396 void
397 PaneStack::sizePropertyStacks()
398 {
399 int maxMinWidth = 0;
400
401 for (size_t i = 0; i < m_panes.size(); ++i) {
402 if (!m_panes[i].propertyStack) continue;
403 // std::cerr << "PaneStack::sizePropertyStacks: " << i << ": min "
404 // << m_panes[i].propertyStack->minimumSizeHint().width() << ", current "
405 // << m_panes[i].propertyStack->width() << std::endl;
406
407 if (m_panes[i].propertyStack->minimumSizeHint().width() > maxMinWidth) {
408 maxMinWidth = m_panes[i].propertyStack->minimumSizeHint().width();
409 }
410 }
411
412 // std::cerr << "PaneStack::sizePropertyStacks: max min width " << maxMinWidth << std::endl;
413
414 #ifdef Q_WS_MAC
415 // This is necessary to compensate for cb->setMinimumSize(10, 10)
416 // in PropertyBox in the Mac version (to avoid a mysterious crash)
417 int setWidth = maxMinWidth * 3 / 2;
418 #else
419 int setWidth = maxMinWidth;
420 #endif
421
422 m_propertyStackStack->setMaximumWidth(setWidth + 10);
423
424 for (size_t i = 0; i < m_panes.size(); ++i) {
425 if (!m_panes[i].propertyStack) continue;
426 m_panes[i].propertyStack->setMinimumWidth(setWidth);
427 }
428 }
429
430
431 #ifdef INCLUDE_MOCFILES
432 #include "PaneStack.moc.cpp"
433 #endif
434