Logo Search packages:      
Sourcecode: qasmixer version File versions  Download package

main_view_mixer_ctl.cpp

//
// C++ Implementation:
//
// Description:
//
//
// Author: Sebastian Holtermann <sebholt@xwmw.org>, (C) 2010-2011
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include "main_view_mixer_ctl.hpp"

#include "qsnd/mixer_events.hpp"

#include <QCoreApplication>
#include <QHeaderView>

#include <iostream>


Main_View_Mixer_CTL::Main_View_Mixer_CTL (
      Mixer_Settings & settings_n,
      QSnd::Snd_Control_Address & ctl_n,
      QWidget * parent_n ) :
Main_View ( ctl_n, parent_n ),
_settings ( settings_n )
{
      _default_iface_type_idx =
            _snd_mixer.data().iface_type_idx ( SND_CTL_ELEM_IFACE_MIXER );

      connect ( &_snd_mixer, SIGNAL ( sig_mixer_reload_request() ),
            this, SLOT ( reload_delayed() ) );


      _mixer_ctl.set_mixer_data ( &_snd_mixer.data() );

      // Tree model
      _tree_model.set_snd_mixer ( &_snd_mixer );
      {
            QFont fnt ( _tree_view.font() );
            fnt.setBold ( true );
            _tree_model.base_item()->setFont ( fnt );
      }

      // Tree view
      _tree_view.setHeaderHidden ( true );
      _tree_view.setTextElideMode ( Qt::ElideMiddle );
      _tree_view.setModel ( &_tree_model );

      connect ( &_tree_view, SIGNAL ( activated ( const QModelIndex & ) ),
            this, SLOT ( tree_element_selected ( const QModelIndex & ) ) );


      // Table view
      _table_view.setModel ( &_table_model );
      {
            QHeaderView * vhv ( _table_view.verticalHeader() );
            if ( vhv != 0 ) {
                  vhv->hide();
            }
      }
      _table_view.setSelectionMode ( QAbstractItemView::SingleSelection );
      _table_view.setSelectionBehavior ( QAbstractItemView::SelectRows );

      connect ( &_table_view, SIGNAL ( activated ( const QModelIndex & ) ),
            this, SLOT ( table_element_selected ( const QModelIndex & ) ) );


      // Tree view container widget
      {
            QVBoxLayout * lay_wdg_tree ( new QVBoxLayout );
            lay_wdg_tree->setContentsMargins ( 0, 0, 0, 0 );
            lay_wdg_tree->addWidget ( &_tree_view );
            _wdg_tree.setLayout ( lay_wdg_tree );
      }

      {
            // Set parent in advance to avoid flicker
            _table_view.setParent ( &_wdg_center);
            _mixer_ctl.setParent ( &_wdg_center);

            _lay_center_stack = new QStackedLayout;
            _lay_center_stack->setContentsMargins ( 0, 0, 0, 0 );
            _lay_center_stack->addWidget ( &_table_view );
            _lay_center_stack->addWidget ( &_mixer_ctl );

            QVBoxLayout * lay_center ( new QVBoxLayout );
            lay_center->addLayout ( _lay_center_stack );
            _wdg_center.setLayout ( lay_center );
      }


      // Horizontal splitter
      {
            _hsplit.setOrientation ( Qt::Horizontal );
            _hsplit.addWidget ( &_wdg_tree );
            _hsplit.addWidget ( &_wdg_center );

            _hsplit.setCollapsible ( 0, false );
            _hsplit.setCollapsible ( 1, false );
            _hsplit.setStretchFactor ( 1, 8 );

            lay_stack()->addWidget ( &_hsplit );
      }


      // Adjust margins
      {
            QLayout * lay;

            lay = _wdg_center.layout();
            if ( lay != 0 ) {
                  QMargins mgs ( lay->contentsMargins() );
                  mgs.setRight ( 0 );
                  mgs.setTop ( 0 );
                  mgs.setBottom ( 0 );
                  lay->setContentsMargins ( mgs );
            }

            lay = _mixer_ctl.layout();
            if ( lay != 0 ) {
                  lay->setContentsMargins ( 0, 0, 0, 0 );
            }
      }

      _tree_view.setCurrentIndex ( _tree_model.base_item()->index() );
}


Main_View_Mixer_CTL::~Main_View_Mixer_CTL ( )
{
      _mixer_ctl.set_snd_elem_group ( 0 );
      _snd_mixer.close();
}


void
Main_View_Mixer_CTL::set_mixer_style (
      QSnd::Mixer_Style * mstyle_n )
{
      _mixer_ctl.set_mixer_style ( mstyle_n );
}


void
Main_View_Mixer_CTL::reload ( )
{
      //std::cout << "Main_View_Mixer_CTL::reload" << "\n";
      Main_View::reload();

      // Close mixer and clean up
      _mixer_ctl.set_snd_elem_group ( 0 );
      _tree_model.set_snd_mixer ( 0 );
      _table_model.set_snd_mixer ( 0 );
      _snd_mixer.close();

      int stack_idx ( 0 );

      // Open mixer
      if ( !current_ctl().is_clear() ) {
            _snd_mixer.open ( current_ctl().ctl_id() );
            if ( _snd_mixer.is_open() ) {
                  stack_idx = 1;
            } else {
                  message_wdg().set_mixer_open_fail (
                        current_ctl().ctl_id(),
                        _snd_mixer.err_message(),
                        _snd_mixer.err_func() );
            }
      } else {
            message_wdg().set_no_device();
      }

      // Set interface type in tree and table model
      if ( _snd_mixer.iface_types_avail() > 0 ) {

            _tree_model.set_snd_mixer ( &_snd_mixer );
            //_table_model.set_iface_type_idx ( iface_type_idx );
            _table_model.set_snd_mixer ( &_snd_mixer );
      }

      expand_tree_items();
      adjust_table_columns();

      lay_stack()->setCurrentIndex ( stack_idx );

      apply_settings();
}


void
Main_View_Mixer_CTL::apply_settings ( )
{
      _mixer_ctl.set_wheel_degrees ( _settings.wheel_degrees );

      QModelIndex idx (
            _tree_model.elem_desc_index (
                  _settings.ctl_mixer.interface_name,
                  _settings.ctl_mixer.element_name,
                  _settings.ctl_mixer.element_index ) );

      if ( !idx.isValid() ) {
            idx = _tree_model.index ( 0, 0 );
      }
      if ( idx.isValid() ) {
            _tree_view.setCurrentIndex  ( idx );
      }
}


void
Main_View_Mixer_CTL::expand_tree_items ( )
{
      const QModelIndex base_idx ( _tree_model.base_item()->index() );

      _tree_view.expand ( base_idx );

      const unsigned int num_rows ( _tree_model.rowCount ( base_idx ) );
      if ( num_rows == 1 ) {
            for ( unsigned int ii=0; ii < num_rows; ++ii ) {
                  QModelIndex mod_idx ( _tree_model.index ( ii, 0, base_idx ) );
                  _tree_view.expand ( mod_idx );
            }
      }
}


void
Main_View_Mixer_CTL::adjust_table_columns ( )
{
      _table_view.resizeColumnsToContents();
      _table_view.resizeRowsToContents();
}


void
Main_View_Mixer_CTL::tree_element_selected (
      const QModelIndex & idx_n )
{
      if ( idx_n.isValid() ) {

            QSnd::Snd_Mixer_CTL_Elem_Group * elem_grp ( 0 );
            unsigned int elem_index ( 0 );
            unsigned int iface_type_index ( 0 );

            unsigned int found = _tree_model.index_data (
                  idx_n, &elem_grp, &elem_index, &iface_type_index );

            if ( found == 1 ) {
                  //std::cout << "Element type selected\n";
                  _table_model.set_iface_type_idx ( iface_type_index );
                  _lay_center_stack->setCurrentIndex ( 0 );
                  adjust_table_columns();
            } else if ( found == 2 ) {
                  //std::cout << "Element group selected\n";
                  _mixer_ctl.set_snd_elem_group ( elem_grp, elem_index );
                  _lay_center_stack->setCurrentIndex ( 1 );
            } else if ( found == 3 ) {
                  //std::cout << "Element selected\n";
                  _mixer_ctl.set_snd_elem_group ( elem_grp, elem_index );
                  _lay_center_stack->setCurrentIndex ( 1 );
            }

            // Update settings
            if ( found > 0 ) {
                  _settings.ctl_mixer.interface_name =
                        _snd_mixer.data().iface_name ( iface_type_index );
                  _settings.ctl_mixer.element_name.clear();
                  if ( found > 1 ) {
                        _settings.ctl_mixer.element_name =
                              elem_grp->elem ( 0 )->elem_name();
                        _settings.ctl_mixer.element_index = elem_index;
                  }
            }
      }
}


void
Main_View_Mixer_CTL::table_element_selected (
      const QModelIndex & idx_n )
{
      QSnd::Snd_Mixer_CTL_Elem * elem ( _table_model.elem ( idx_n ) );
      if ( elem != 0 ) {
            QModelIndex tree_idx ( _tree_model.elem_index ( elem ) );
            if ( tree_idx.isValid() ) {
                  _tree_view.setCurrentIndex ( tree_idx );
            }
      }
}


Generated by  Doxygen 1.6.0   Back to index