PropertyStack.cpp

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "PropertyStack.h"
00017 #include "PropertyBox.h"
00018 #include "base/PropertyContainer.h"
00019 #include "view/View.h"
00020 #include "layer/Layer.h"
00021 #include "layer/LayerFactory.h"
00022 #include "widgets/NotifyingTabBar.h"
00023 #include "widgets/IconLoader.h"
00024 
00025 #include <QIcon>
00026 #include <QTabWidget>
00027 
00028 #include <iostream>
00029 
00030 //#define DEBUG_PROPERTY_STACK 1
00031 
00032 PropertyStack::PropertyStack(QWidget *parent, View *client) :
00033     QTabWidget(parent),
00034     m_client(client)
00035 {
00036     NotifyingTabBar *bar = new NotifyingTabBar();
00037     bar->setDrawBase(false);
00038 
00039     connect(bar, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredTabBar()));
00040     connect(bar, SIGNAL(mouseLeft()), this, SLOT(mouseLeftTabBar()));
00041     connect(bar, SIGNAL(activeTabClicked()), this, SLOT(activeTabClicked()));
00042 
00043     setTabBar(bar);
00044 
00045 #if (QT_VERSION >= 0x0402)
00046     setElideMode(Qt::ElideNone); 
00047     tabBar()->setUsesScrollButtons(true); 
00048     tabBar()->setIconSize(QSize(16, 16));
00049 #endif
00050     
00051     repopulate();
00052 
00053     connect(this, SIGNAL(currentChanged(int)),
00054             this, SLOT(selectedContainerChanged(int)));
00055 
00056     connect(m_client, SIGNAL(propertyContainerAdded(PropertyContainer *)),
00057             this, SLOT(propertyContainerAdded(PropertyContainer *)));
00058 
00059     connect(m_client, SIGNAL(propertyContainerRemoved(PropertyContainer *)),
00060             this, SLOT(propertyContainerRemoved(PropertyContainer *)));
00061 
00062     connect(m_client, SIGNAL(propertyContainerPropertyChanged(PropertyContainer *)),
00063             this, SLOT(propertyContainerPropertyChanged(PropertyContainer *)));
00064 
00065     connect(m_client, SIGNAL(propertyContainerPropertyRangeChanged(PropertyContainer *)),
00066             this, SLOT(propertyContainerPropertyRangeChanged(PropertyContainer *)));
00067 
00068     connect(m_client, SIGNAL(propertyContainerNameChanged(PropertyContainer *)),
00069             this, SLOT(propertyContainerNameChanged(PropertyContainer *)));
00070 
00071     connect(this, SIGNAL(propertyContainerSelected(View *, PropertyContainer *)),
00072             m_client, SLOT(propertyContainerSelected(View *, PropertyContainer *)));
00073 }
00074 
00075 void
00076 PropertyStack::repopulate()
00077 {
00078     blockSignals(true);
00079 
00080 #ifdef DEBUG_PROPERTY_STACK
00081     std::cerr << "PropertyStack::repopulate" << std::endl;
00082 #endif
00083     
00084     while (count() > 0) {
00085         removeTab(0);
00086     }
00087     for (size_t i = 0; i < m_boxes.size(); ++i) {
00088         delete m_boxes[i];
00089     }
00090     m_boxes.clear();
00091     
00092     for (size_t i = 0; i < m_client->getPropertyContainerCount(); ++i) {
00093 
00094         PropertyContainer *container = m_client->getPropertyContainer(i);
00095         QString name = container->getPropertyContainerName();
00096         
00097         PropertyBox *box = new PropertyBox(container);
00098 
00099         connect(box, SIGNAL(showLayer(bool)), this, SLOT(showLayer(bool)));
00100         connect(box, SIGNAL(contextHelpChanged(const QString &)),
00101                 this, SIGNAL(contextHelpChanged(const QString &)));
00102 
00103         Layer *layer = dynamic_cast<Layer *>(container);
00104         if (layer) {
00105             box->layerVisibilityChanged(!layer->isLayerDormant(m_client));
00106         }
00107 
00108         QString shortName = name;
00109 
00110         if (layer) {
00111             shortName = LayerFactory::getInstance()->getLayerPresentationName
00112                 (LayerFactory::getInstance()->getLayerType(layer));
00113         }
00114 
00115         shortName = QString("&%1 %2").arg(i + 1).arg(shortName);
00116 
00117         QString iconName = container->getPropertyContainerIconName();
00118 
00119         QIcon icon(IconLoader().load(iconName));
00120         if (icon.isNull()) {
00121             addTab(box, shortName);
00122         } else {
00123             addTab(box, icon, QString("&%1").arg(i + 1));
00124             setTabToolTip(i, name);
00125         }
00126 
00127         m_boxes.push_back(box);
00128     }    
00129 
00130     blockSignals(false);
00131 }
00132 
00133 bool
00134 PropertyStack::containsContainer(PropertyContainer *pc) const
00135 {
00136     for (size_t i = 0; i < m_client->getPropertyContainerCount(); ++i) {
00137         PropertyContainer *container = m_client->getPropertyContainer(i);
00138         if (pc == container) return true;
00139     }
00140 
00141     return false;
00142 }
00143 
00144 int
00145 PropertyStack::getContainerIndex(PropertyContainer *pc) const
00146 {
00147     for (size_t i = 0; i < m_client->getPropertyContainerCount(); ++i) {
00148         PropertyContainer *container = m_client->getPropertyContainer(i);
00149         if (pc == container) return i;
00150     }
00151 
00152     return false;
00153 }
00154 
00155 void
00156 PropertyStack::propertyContainerAdded(PropertyContainer *)
00157 {
00158     if (sender() != m_client) return;
00159     repopulate();
00160 }
00161 
00162 void
00163 PropertyStack::propertyContainerRemoved(PropertyContainer *)
00164 {
00165     if (sender() != m_client) return;
00166     repopulate();
00167 }
00168 
00169 void
00170 PropertyStack::propertyContainerPropertyChanged(PropertyContainer *pc)
00171 {
00172     Layer *layer = dynamic_cast<Layer *>(pc);
00173     for (unsigned int i = 0; i < m_boxes.size(); ++i) {
00174         if (pc == m_boxes[i]->getContainer()) {
00175             m_boxes[i]->propertyContainerPropertyChanged(pc);
00176             if (layer) {
00177                 m_boxes[i]->layerVisibilityChanged
00178                     (!layer->isLayerDormant(m_client));
00179             }
00180         }
00181     }
00182 }
00183 
00184 void
00185 PropertyStack::propertyContainerPropertyRangeChanged(PropertyContainer *pc)
00186 {
00187     for (unsigned int i = 0; i < m_boxes.size(); ++i) {
00188         if (pc == m_boxes[i]->getContainer()) {
00189             m_boxes[i]->propertyContainerPropertyRangeChanged(pc);
00190         }
00191     }
00192 }
00193 
00194 void
00195 PropertyStack::propertyContainerNameChanged(PropertyContainer *)
00196 {
00197     if (sender() != m_client) return;
00198     repopulate();
00199 }
00200 
00201 void
00202 PropertyStack::showLayer(bool show)
00203 {
00204     QObject *obj = sender();
00205     
00206     for (unsigned int i = 0; i < m_boxes.size(); ++i) {
00207         if (obj == m_boxes[i]) {
00208             Layer *layer = dynamic_cast<Layer *>(m_boxes[i]->getContainer());
00209             if (layer) {
00210                 layer->showLayer(m_client, show);
00211                 return;
00212             }
00213         }
00214     }
00215 }
00216 
00217 void
00218 PropertyStack::selectedContainerChanged(int n)
00219 {
00220     if (n >= int(m_boxes.size())) return;
00221     emit propertyContainerSelected(m_client, m_boxes[n]->getContainer());
00222 }
00223 
00224 void
00225 PropertyStack::mouseEnteredTabBar()
00226 {
00227     emit contextHelpChanged(tr("Click to change the current active layer"));
00228 }
00229 
00230 void
00231 PropertyStack::mouseLeftTabBar()
00232 {
00233     emit contextHelpChanged("");
00234 }
00235 
00236 void
00237 PropertyStack::activeTabClicked()
00238 {
00239     emit viewSelected(m_client);
00240 }
00241 

Generated on Wed Feb 20 15:45:27 2008 for SonicVisualiser by  doxygen 1.5.1