/src/libtomahawk/playlist/dynamic/widgets/DynamicControlList.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 189 lines · 138 code · 34 blank · 17 comment · 10 complexity · ead54d4fe055fdf5c261c024c6481a77 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
  4. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "DynamicControlList.h"
  19. #include "DynamicControlWrapper.h"
  20. #include "playlist/dynamic/GeneratorInterface.h"
  21. #include "utils/ImageRegistry.h"
  22. #include "utils/TomahawkUtils.h"
  23. #include "utils/Logger.h"
  24. #include "Source.h"
  25. #include <QLayout>
  26. #include <QLabel>
  27. #include <QPaintEvent>
  28. #include <QPushButton>
  29. #include <QToolButton>
  30. #include <QPainter>
  31. #include <QGridLayout>
  32. #include <QHBoxLayout>
  33. using namespace Tomahawk;
  34. DynamicControlList::DynamicControlList( QWidget* parent )
  35. : QWidget( parent )
  36. , m_layout( new QGridLayout )
  37. {
  38. init();
  39. }
  40. DynamicControlList::DynamicControlList( const geninterface_ptr& generator, const QList< dyncontrol_ptr >& controls, QWidget* parent )
  41. : QWidget( parent )
  42. , m_generator( generator )
  43. , m_layout( new QGridLayout )
  44. {
  45. init();
  46. setControls( generator, controls );
  47. }
  48. DynamicControlList::~DynamicControlList()
  49. {
  50. }
  51. void
  52. DynamicControlList::init()
  53. {
  54. qDebug() << "GRIDLAYOUT: " << m_layout->rowCount();
  55. setContentsMargins( 0, 0, 0, 0 );
  56. setLayout( m_layout );
  57. m_layout->setColumnStretch( 2, 1 );
  58. m_layout->setMargin( 0 );
  59. m_layout->setVerticalSpacing( 0 );
  60. #ifdef Q_OS_MAC // on OS X we don't want the right edge of the toolbuttons against the window
  61. m_layout->setContentsMargins( 0, 0, 3, 0 );
  62. #else
  63. m_layout->setContentsMargins( 0, 0, 0, 0 );
  64. #endif
  65. m_layout->setSizeConstraint( QLayout::SetMinimumSize );
  66. m_collapseLayout = new QHBoxLayout();
  67. m_collapseLayout->setContentsMargins( 0, 0, 0, 0 );
  68. m_collapseLayout->setMargin( 0 );
  69. m_collapseLayout->setSpacing( 0 );
  70. m_collapse = new QPushButton( tr( "Save Settings" ), this );
  71. m_collapse->setAttribute( Qt::WA_LayoutUsesWidgetRect );
  72. m_collapseLayout->addWidget( m_collapse );
  73. m_addControl = new QToolButton( this );
  74. m_addControl->setAttribute( Qt::WA_LayoutUsesWidgetRect );
  75. m_addControl->setIcon( ImageRegistry::instance()->icon( RESPATH "images/list-add.svg" ) );
  76. m_addControl->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
  77. m_addControl->setIconSize( QSize( 16, 16 ) );
  78. m_addControl->setToolButtonStyle( Qt::ToolButtonIconOnly );
  79. m_addControl->setAutoRaise( true );
  80. m_addControl->setContentsMargins( 0, 0, 0, 0 );
  81. m_collapseLayout->addWidget( m_addControl );
  82. m_collapse->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
  83. connect( m_collapse, SIGNAL( clicked() ), this, SIGNAL( toggleCollapse() ) );
  84. connect( m_addControl, SIGNAL( clicked() ), this, SLOT( addNewControl() ) );
  85. setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
  86. }
  87. void
  88. DynamicControlList::setControls( const geninterface_ptr& generator, const QList< dyncontrol_ptr >& controls )
  89. {
  90. if( m_controls.size() == controls.size() && controls.size() > 0 ) { // check if we're setting the same controls we already have, and exit if we are
  91. bool different = false;
  92. for( int i = 0; i < m_controls.size(); i++ ) {
  93. if( m_controls.value( i )->control().data() != controls.value( i ).data() ) {
  94. different = true;
  95. break;
  96. }
  97. }
  98. if( !different ) { // no work to do
  99. return;
  100. }
  101. }
  102. if( !m_controls.isEmpty() ) {
  103. qDeleteAll( m_controls );
  104. m_controls.clear();
  105. }
  106. m_layout->removeItem( m_collapseLayout );
  107. m_generator = generator;
  108. if( controls.isEmpty() ) {
  109. qDebug() << "CREATING DEFAULT CONTROL";
  110. DynamicControlWrapper* ctrlW = new DynamicControlWrapper( generator->createControl(), m_layout, m_controls.size(), this );
  111. connect( ctrlW, SIGNAL( removeControl() ), this, SLOT( removeControl() ) );
  112. connect( ctrlW, SIGNAL( changed() ), this, SLOT( controlChanged() ) );
  113. m_controls << ctrlW;
  114. } else
  115. {
  116. foreach( const dyncontrol_ptr& control, controls ) {
  117. DynamicControlWrapper* ctrlW = new DynamicControlWrapper( control, m_layout, m_controls.size(), this );
  118. connect( ctrlW, SIGNAL( removeControl() ), this, SLOT( removeControl() ) );
  119. connect( ctrlW, SIGNAL( changed() ), this, SLOT( controlChanged() ) );
  120. m_controls << ctrlW;
  121. }
  122. }
  123. m_layout->addItem( m_collapseLayout, m_layout->rowCount(), 0, 1, 4, Qt::AlignCenter );
  124. }
  125. void
  126. DynamicControlList::addNewControl()
  127. {
  128. m_layout->removeItem( m_collapseLayout );
  129. dyncontrol_ptr control = m_generator->createControl();
  130. m_controls.append( new DynamicControlWrapper( control, m_layout, m_layout->rowCount(), this ) );
  131. connect( m_controls.last(), SIGNAL( removeControl() ), this, SLOT( removeControl() ) );
  132. connect( m_controls.last(), SIGNAL( changed() ), this, SLOT( controlChanged() ) );
  133. m_layout->addItem( m_collapseLayout, m_layout->rowCount(), 0, 1, 4, Qt::AlignCenter );
  134. emit controlsChanged( true );
  135. }
  136. void
  137. DynamicControlList::removeControl()
  138. {
  139. DynamicControlWrapper* w = qobject_cast<DynamicControlWrapper*>( sender() );
  140. w->removeFromLayout();
  141. m_controls.removeAll( w );
  142. m_generator->removeControl( w->control() );
  143. w->deleteLater();
  144. emit controlsChanged( false );
  145. }
  146. void
  147. DynamicControlList::controlChanged()
  148. {
  149. Q_ASSERT( sender() && qobject_cast<DynamicControlWrapper*>(sender()) );
  150. DynamicControlWrapper* widget = qobject_cast<DynamicControlWrapper*>(sender());
  151. qDebug() << "control changed!";
  152. foreach( DynamicControlWrapper* c, m_controls )
  153. qDebug() << c->control()->id() << c->control()->selectedType() << c->control()->match() << c->control()->input();
  154. emit controlChanged( widget->control() );
  155. }