annotate widgets/PaneStack.cpp @ 121:7363cacf7de0

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