view widgets/PropertyBox.cpp @ 33:651e4e868bcc

* Implement play mute, level and pan controls and a layer visibility control * Handle swapping the buffers in AudioCallbackPlaySource more gracefully, so that in many cases it can be done inaudibly. Still gets it wrong when playing in a noncontiguous selection. * Fix to SV file save for non-2d sparse models * Fixes to LED button drawing and AudioDial mouse functionality * Add progress bar for Ogg file import * Reshuffle PropertyContainer and its subclasses so it can be a QObject * Add layer dormancy (invisible layer permitted to free its cache space) * Optimisations to SpectrogramLayer, removing locks when reading/writing individual pixels in the cache (should be unnecessary there) -- there's still an issue here as we need a lock when reading from the model in case the model is replaced, and we don't currently have one * Several munlock() calls to make it harder to exhaust real memory if running in an RT mode with mlockall() active
author Chris Cannam
date Fri, 17 Feb 2006 18:04:26 +0000
parents 37b110168acf
children c43f2c4f66f2
line wrap: on
line source
/* -*- c-basic-offset: 4 -*-  vi:set ts=8 sts=4 sw=4: */

/*
    A waveform viewer and audio annotation editor.
    Chris Cannam, Queen Mary University of London, 2005-2006
    
    This is experimental software.  Not for distribution.
*/

#include "PropertyBox.h"

#include "base/PropertyContainer.h"
#include "base/PlayParameters.h"
#include "base/Layer.h"

#include "AudioDial.h"
#include "LEDButton.h"

#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QComboBox>
#include <QLabel>
#include <QFrame>

#include <cassert>
#include <iostream>

//#define DEBUG_PROPERTY_BOX 1

PropertyBox::PropertyBox(PropertyContainer *container) :
    m_container(container)
{
#ifdef DEBUG_PROPERTY_BOX
    std::cerr << "PropertyBox[" << this << "(\"" <<
	container->getPropertyContainerName().toStdString() << "\")]::PropertyBox" << std::endl;
#endif

    QVBoxLayout *vbox = new QVBoxLayout;
    setLayout(vbox);

    bool needViewPlayBox = false;

    if (container->getPlayParameters() || dynamic_cast<Layer *>(container)) {
	needViewPlayBox = true;
    }

    if (needViewPlayBox) {
#ifdef DEBUG_PROPERTY_BOX
	std::cerr << "Adding view play box" << std::endl;
#endif
	QFrame *frame = new QFrame;
	frame->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
	QHBoxLayout *hbox = new QHBoxLayout;
	frame->setLayout(hbox);
	vbox->addWidget(frame);
	populateViewPlayBox(container, hbox);
	hbox->insertStretch(-1, 10);
    }

    m_mainWidget = new QWidget;
    vbox->addWidget(m_mainWidget);

    vbox->insertStretch(-1, 10);

    m_layout = new QGridLayout;
    m_mainWidget->setLayout(m_layout);

    PropertyContainer::PropertyList properties = container->getProperties();

    blockSignals(true);

    size_t i;

    for (i = 0; i < properties.size(); ++i) {
	updatePropertyEditor(properties[i]);
    }

    blockSignals(false);

    m_layout->setRowStretch(m_layout->rowCount(), 10);

#ifdef DEBUG_PROPERTY_BOX
    std::cerr << "PropertyBox[" << this << "]::PropertyBox returning" << std::endl;
#endif
}

PropertyBox::~PropertyBox()
{
#ifdef DEBUG_PROPERTY_BOX
    std::cerr << "PropertyBox[" << this << "]::~PropertyBox" << std::endl;
#endif
}

void
PropertyBox::populateViewPlayBox(PropertyContainer *container, QLayout *layout)
{
    Layer *layer = dynamic_cast<Layer *>(container);
    PlayParameters *params = container->getPlayParameters();
    if (!params && !layer) return;

    std::cerr << "PropertyBox::populateViewPlayBox: container " << container << " (name " << container->getPropertyContainerName().toStdString() << ") params " << params << std::endl;
    
    if (layer) {
	LEDButton *showButton = new LEDButton(Qt::blue);
	layout->addWidget(showButton);
	connect(showButton, SIGNAL(stateChanged(bool)),
		layer, SLOT(showLayer(bool)));
    }
    
    if (params) {
	LEDButton *playButton = new LEDButton(Qt::darkGreen);
	layout->addWidget(playButton);
	connect(playButton, SIGNAL(stateChanged(bool)),
		params, SLOT(setPlayAudible(bool)));
    }
}

void
PropertyBox::updatePropertyEditor(PropertyContainer::PropertyName name)
{
    PropertyContainer::PropertyType type = m_container->getPropertyType(name);
    int row = m_layout->rowCount();

    int min = 0, max = 0, value = 0;
    value = m_container->getPropertyRangeAndValue(name, &min, &max);

    bool have = (m_propertyControllers.find(name) !=
		 m_propertyControllers.end());

    QString groupName = m_container->getPropertyGroupName(name);

#ifdef DEBUG_PROPERTY_BOX
    std::cerr << "PropertyBox[" << this
	      << "(\"" << m_container->getPropertyContainerName().toStdString()
	      << "\")]";
    std::cerr << "::updatePropertyEditor(\"" << name.toStdString() << "\"):";
    std::cerr << " value " << value << ", have " << have << ", group \""
	      << groupName.toStdString() << "\"" << std::endl;
#endif

    bool inGroup = (groupName != QString());

    if (!have) {
	if (inGroup) {
	    if (m_groupLayouts.find(groupName) == m_groupLayouts.end()) {
#ifdef DEBUG_PROPERTY_BOX
		std::cerr << "PropertyBox: adding label \"" << groupName.toStdString() << "\" and frame for group for \"" << name.toStdString() << "\"" << std::endl;
#endif
		m_layout->addWidget(new QLabel(groupName, m_mainWidget), row, 0);
		QFrame *frame = new QFrame(m_mainWidget);
		m_layout->addWidget(frame, row, 1, 1, 2);
		m_groupLayouts[groupName] = new QHBoxLayout;
		m_groupLayouts[groupName]->setMargin(0);
		frame->setLayout(m_groupLayouts[groupName]);
	    }
	} else {
#ifdef DEBUG_PROPERTY_BOX 
	    std::cerr << "PropertyBox: adding label \"" << name.toStdString() << "\"" << std::endl;
#endif
	    m_layout->addWidget(new QLabel(name, m_mainWidget), row, 0);
	}
    }

    switch (type) {

    case PropertyContainer::ToggleProperty:
    {
	QCheckBox *cb;

	if (have) {
	    cb = dynamic_cast<QCheckBox *>(m_propertyControllers[name]);
	    assert(cb);
	} else {
#ifdef DEBUG_PROPERTY_BOX 
	    std::cerr << "PropertyBox: creating new checkbox" << std::endl;
#endif
	    cb = new QCheckBox();
	    cb->setObjectName(name);
	    connect(cb, SIGNAL(stateChanged(int)),
		    this, SLOT(propertyControllerChanged(int)));
	    if (inGroup) {
		cb->setToolTip(name);
		m_groupLayouts[groupName]->addWidget(cb);
	    } else {
		m_layout->addWidget(cb, row, 1, 1, 2);
	    }
	    m_propertyControllers[name] = cb;
	}

	if (cb->isChecked() != (value > 0)) cb->setChecked(value > 0);
	break;
    }

    case PropertyContainer::RangeProperty:
    {
	AudioDial *dial;

	if (have) {
	    dial = dynamic_cast<AudioDial *>(m_propertyControllers[name]);
	    assert(dial);
	} else {
#ifdef DEBUG_PROPERTY_BOX 
	    std::cerr << "PropertyBox: creating new dial" << std::endl;
#endif
	    dial = new AudioDial();
	    dial->setObjectName(name);
	    dial->setMinimum(min);
	    dial->setMaximum(max);
	    dial->setPageStep(1);
	    dial->setNotchesVisible(true);
	    connect(dial, SIGNAL(valueChanged(int)),
		    this, SLOT(propertyControllerChanged(int)));

	    if (inGroup) {
		dial->setFixedWidth(24);
		dial->setFixedHeight(24);
		dial->setToolTip(name);
		m_groupLayouts[groupName]->addWidget(dial);
	    } else {
		dial->setFixedWidth(32);
		dial->setFixedHeight(32);
		m_layout->addWidget(dial, row, 1);
		QLabel *label = new QLabel(m_mainWidget);
		connect(dial, SIGNAL(valueChanged(int)),
			label, SLOT(setNum(int)));
		label->setNum(value);
		m_layout->addWidget(label, row, 2);
	    }

	    m_propertyControllers[name] = dial;
	}

	if (dial->value() != value) dial->setValue(value);
	break;
    }

    case PropertyContainer::ValueProperty:
    {
	QComboBox *cb;

	if (have) {
	    cb = dynamic_cast<QComboBox *>(m_propertyControllers[name]);
	    assert(cb);
	} else {
#ifdef DEBUG_PROPERTY_BOX 
	    std::cerr << "PropertyBox: creating new combobox" << std::endl;
#endif

	    cb = new QComboBox();
	    cb->setObjectName(name);
	    for (int i = min; i <= max; ++i) {
		cb->addItem(m_container->getPropertyValueLabel(name, i));
	    }
	    connect(cb, SIGNAL(activated(int)),
		    this, SLOT(propertyControllerChanged(int)));
	    if (inGroup) {
		cb->setToolTip(name);
		m_groupLayouts[groupName]->addWidget(cb);
	    } else {
		m_layout->addWidget(cb, row, 1, 1, 2);
	    }
	    m_propertyControllers[name] = cb;
	}

	if (cb->currentIndex() != value) cb->setCurrentIndex(value);

#ifdef Q_WS_MAC
	// Crashes on startup without this, for some reason
	cb->setMinimumSize(QSize(10, 10));
#endif

	break;
    }

    default:
	break;
    }
}

void
PropertyBox::propertyContainerPropertyChanged(PropertyContainer *pc)
{
    if (pc != m_container) return;
    
    PropertyContainer::PropertyList properties = m_container->getProperties();
    size_t i;

    blockSignals(true);

    for (i = 0; i < properties.size(); ++i) {
	updatePropertyEditor(properties[i]);
    }

    blockSignals(false);
}

void
PropertyBox::propertyControllerChanged(int value)
{
    QObject *obj = sender();
    QString name = obj->objectName();

    std::cerr << "PropertyBox::propertyControllerChanged(" << name.toStdString()
	      << ", " << value << ")" << std::endl;
    
    PropertyContainer::PropertyType type = m_container->getPropertyType(name);
    
    if (type != PropertyContainer::InvalidProperty) {
	m_container->setProperty(name, value);
    }

    if (type == PropertyContainer::RangeProperty) {
	AudioDial *dial = dynamic_cast<AudioDial *>(m_propertyControllers[name]);
	if (dial) {
	    dial->setToolTip(QString("%1: %2").arg(name).arg(value));
	    //!!! unfortunately this doesn't update an already-visible tooltip
	}
    }
}
    
	    

#ifdef INCLUDE_MOCFILES
#include "PropertyBox.moc.cpp"
#endif