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

http://github.com/tomahawk-player/tomahawk · C++ · 205 lines · 121 code · 40 blank · 44 comment · 14 complexity · 30c51b9f2dc1cf4c474bfa067883c565 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 "DynamicControlWrapper.h"
  19. #include "playlist/dynamic/DynamicControl.h"
  20. #include "utils/TomahawkUtilsGui.h"
  21. #include "utils/Logger.h"
  22. #include <QHBoxLayout>
  23. #include <QComboBox>
  24. #include <QLayout>
  25. #include <QToolButton>
  26. #include <QPaintEvent>
  27. #include <QPainter>
  28. #include <QStackedLayout>
  29. #include <QApplication>
  30. using namespace Tomahawk;
  31. DynamicControlWrapper::DynamicControlWrapper( const Tomahawk::dyncontrol_ptr& control, QGridLayout* layout, int row, QWidget* parent )
  32. : QObject( parent )
  33. , m_parent( parent )
  34. , m_row( row )
  35. , m_minusButton( 0 )
  36. , m_control( control )
  37. , m_typeSelector( 0 )
  38. , m_layout( QPointer< QGridLayout >( layout ) )
  39. {
  40. m_typeSelector = new QComboBox( m_parent );
  41. m_matchSelector = QPointer<QWidget>( control->matchSelector() );
  42. m_entryWidget = QPointer<QWidget>( control->inputField() );
  43. m_minusButton = initButton( m_parent );
  44. m_minusButton->setIcon( TomahawkUtils::defaultPixmap( TomahawkUtils::ListRemove ) );
  45. connect( m_minusButton, SIGNAL( clicked( bool ) ), this, SIGNAL( removeControl() ) );
  46. m_plusL = new QStackedLayout();
  47. m_plusL->setContentsMargins( 0, 0, 0, 0 );
  48. m_plusL->setMargin( 0 );
  49. m_plusL->addWidget( m_minusButton );
  50. m_plusL->addWidget( createDummy( m_minusButton, m_parent ) ); // :-(
  51. connect( m_typeSelector, SIGNAL( activated( int ) ), SLOT( typeSelectorChanged( int ) ) );
  52. connect( m_control.data(), SIGNAL( changed() ), this, SIGNAL( changed() ) );
  53. m_layout.data()->addWidget( m_typeSelector, row, 0, Qt::AlignLeft );
  54. if( !control.isNull() ) {
  55. foreach( const QString& type, control->typeSelectors() )
  56. m_typeSelector->addItem( qApp->translate( "Type selector", type.toUtf8() ), type );
  57. }
  58. int typeIndex = 0;
  59. if ( !m_control.isNull() ) {
  60. typeIndex = m_typeSelector->findData( m_control->selectedType() );
  61. if (typeIndex == -1) typeIndex = 0;
  62. }
  63. typeSelectorChanged( typeIndex, true );
  64. m_layout.data()->addLayout( m_plusL, m_row, 3, Qt::AlignCenter );
  65. m_plusL->setCurrentIndex( 0 );
  66. }
  67. DynamicControlWrapper::~DynamicControlWrapper()
  68. {
  69. // remove the controls widgets from our layout so they are not parented
  70. // we don't want to auto-delete them since the control should own them
  71. // if we delete them, then the control will be holding on to null ptrs
  72. removeFromLayout();
  73. if( !m_entryWidget.isNull() )
  74. m_control->inputField()->setParent( 0 );
  75. if( !m_matchSelector.isNull() )
  76. m_control->matchSelector()->setParent( 0 );
  77. delete m_typeSelector;
  78. delete m_minusButton;
  79. }
  80. dyncontrol_ptr
  81. DynamicControlWrapper::control() const
  82. {
  83. return m_control;
  84. }
  85. void
  86. DynamicControlWrapper::removeFromLayout()
  87. {
  88. if( m_layout.isNull() )
  89. return;
  90. if( !m_matchSelector.isNull() )
  91. m_layout.data()->removeWidget( m_matchSelector.data() );
  92. if( !m_entryWidget.isNull() )
  93. m_layout.data()->removeWidget( m_entryWidget.data() );
  94. m_layout.data()->removeWidget( m_typeSelector );
  95. m_layout.data()->removeItem( m_plusL );
  96. }
  97. QToolButton*
  98. DynamicControlWrapper::initButton( QWidget* parent )
  99. {
  100. QToolButton* btn = new QToolButton( parent );
  101. btn->setAttribute( Qt::WA_LayoutUsesWidgetRect );
  102. btn->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
  103. btn->setIconSize( QSize( 16, 16 ) );
  104. btn->setToolButtonStyle( Qt::ToolButtonIconOnly );
  105. btn->setAutoRaise( true );
  106. btn->setContentsMargins( 0, 0, 0, 0 );
  107. return btn;
  108. }
  109. QWidget*
  110. DynamicControlWrapper::createDummy( QWidget* fromW, QWidget* parent )
  111. {
  112. QWidget* dummy = new QWidget( parent );
  113. dummy->setAttribute( Qt::WA_LayoutUsesWidgetRect );
  114. dummy->setContentsMargins( 0, 0, 0, 0 );
  115. dummy->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
  116. dummy->setMinimumSize( fromW->sizeHint() );
  117. dummy->setMaximumSize( fromW->sizeHint() );
  118. return dummy;
  119. }
  120. void
  121. DynamicControlWrapper::typeSelectorChanged( int typeIndex, bool firstLoad )
  122. {
  123. Q_ASSERT( !m_layout.isNull() );
  124. m_layout.data()->removeWidget( m_matchSelector.data() );
  125. m_layout.data()->removeWidget( m_entryWidget.data() );
  126. // Get the untranslated type name
  127. QString type = m_typeSelector->itemData(typeIndex).toString();
  128. if( m_control->selectedType() != type && !firstLoad )
  129. m_control->setSelectedType( type );
  130. m_typeSelector->setCurrentIndex( typeIndex );
  131. if( m_control->matchSelector() ) {
  132. m_matchSelector = QPointer<QWidget>( m_control->matchSelector() );
  133. m_layout.data()->addWidget( m_matchSelector.data(), m_row, 1, Qt::AlignCenter );
  134. m_matchSelector.data()->show();
  135. }
  136. if( m_control->inputField() ) {
  137. m_entryWidget = QPointer<QWidget>( m_control->inputField() );
  138. m_layout.data()->addWidget( m_entryWidget.data(), m_row, 2 );
  139. m_entryWidget.data()->show();
  140. }
  141. emit changed();
  142. }
  143. /*
  144. void
  145. DynamicControlWrapper::enterEvent(QEvent* ev)
  146. {
  147. m_mouseOver = true;
  148. if( m_isLocal )
  149. m_plusL->setCurrentIndex( 0 );
  150. if( ev )
  151. QObject::enterEvent( ev );
  152. }
  153. void
  154. DynamicControlWrapper::leaveEvent(QEvent* ev)
  155. {
  156. m_mouseOver = true;
  157. if( m_isLocal )
  158. m_plusL->setCurrentIndex( 1 );
  159. if( ev )
  160. QWidget::leaveEvent( ev );
  161. }
  162. */