File indexing completed on 2024-05-19 04:49:49

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "LayoutEditWidget.h"
0018 
0019 #include "core/support/Debug.h"
0020 #include "playlist/PlaylistDefines.h"
0021 #include "widgets/TokenDropTarget.h"
0022 
0023 #include <KLocalizedString>
0024 #include <KMessageBox>
0025 
0026 #include <QBoxLayout>
0027 #include <QCheckBox>
0028 #include <QLayout>
0029 
0030 using namespace Playlist;
0031 
0032 LayoutEditWidget::LayoutEditWidget( QWidget *parent )
0033     : QWidget( parent )
0034 {
0035     QVBoxLayout* mainLayout = new QVBoxLayout( this );
0036 
0037     m_dragstack = new TokenDropTarget( this );
0038     m_dragstack->setCustomTokenFactory( new TokenWithLayoutFactory() );
0039     mainLayout->addWidget( m_dragstack, 1 );
0040 
0041     // connect ( m_dragstack, SIGNAL(focusReceived(QWidget*)), this, SIGNAL(focusReceived(QWidget*)) );
0042     connect ( m_dragstack, &TokenDropTarget::changed, this, &LayoutEditWidget::changed );
0043 
0044     m_showCoverCheckBox = new QCheckBox( i18n( "Show cover" ), this );
0045     connect ( m_showCoverCheckBox, &QCheckBox::stateChanged, this, &LayoutEditWidget::changed );
0046     mainLayout->addWidget( m_showCoverCheckBox, 0 );
0047 }
0048 
0049 
0050 LayoutEditWidget::~LayoutEditWidget()
0051 { }
0052 
0053 void LayoutEditWidget::readLayout( const Playlist::LayoutItemConfig &config )
0054 {
0055     DEBUG_BLOCK
0056     int rowCount = config.rows();
0057 
0058     disconnect ( m_showCoverCheckBox, &QCheckBox::stateChanged, this, &LayoutEditWidget::changed );
0059     m_showCoverCheckBox->setChecked( config.showCover() );
0060     connect ( m_showCoverCheckBox, &QCheckBox::stateChanged, this, &LayoutEditWidget::changed );
0061 
0062     m_dragstack->clear();
0063 
0064     for( int i = 0; i < rowCount; i++ )
0065     {
0066         //get the row config
0067         Playlist::LayoutItemConfigRow rowConfig = config.row( i );
0068 
0069         int elementCount = rowConfig.count();
0070 
0071         //FIXME! for now, each element get the same size. This needs extensions to the token stuff
0072         //qreal size = 1.0 / (qreal) elementCount;
0073 
0074         for( int j = 0; j < elementCount; j++ )
0075         {
0076             Playlist::LayoutItemConfigRowElement element = rowConfig.element( j );
0077             //check if the element has a valid value, will crash if trying to use it otherwise
0078             debug() << "value: " << element.value();
0079             if ( element.value()  == -1 )
0080             {
0081 
0082                 error() << "Invalid element value '" << element.value() << "' in playlist layout.";
0083                 KMessageBox::detailedError( this,
0084                                             i18n( "Invalid playlist layout." ),
0085                                             i18n( "Encountered an unknown element name while reading layout." ) );
0086                 m_dragstack->clear();
0087                 return;
0088 
0089             }
0090 
0091             Column col = static_cast<Column>(element.value());
0092             TokenWithLayout *token =  new TokenWithLayout( columnName( col ),
0093                                                            iconName( col ),
0094                                                            element.value() );
0095             token->setBold( element.bold() );
0096             token->setItalic( element.italic() );
0097             token->setUnderline( element.underline() );
0098             token->setAlignment( element.alignment() );
0099             token->setWidth( element.size() * 100.0 );
0100             token->setPrefix( element.prefix() );
0101             token->setSuffix( element.suffix() );
0102             m_dragstack->insertToken( token, i, j );
0103             // Do all modifications on the token above that line, otherwise the dialog will think it's been modified by the user
0104             connect ( token, &TokenWithLayout::changed, this, &LayoutEditWidget::changed );
0105         }
0106 
0107     }
0108 }
0109 
0110 Playlist::LayoutItemConfig LayoutEditWidget::config()
0111 {
0112 
0113     LayoutItemConfig config;
0114     config.setShowCover( m_showCoverCheckBox->isChecked() );
0115 
0116     int noOfRows = m_dragstack->rows();
0117 
0118     for( int i = 0; i < noOfRows; i++ )
0119     {
0120 
0121         LayoutItemConfigRow currentRowConfig;
0122 
0123         QList<Token *> tokens = m_dragstack->tokensAtRow( i );
0124 
0125         foreach( Token * token, tokens ) {
0126             if ( TokenWithLayout *twl = dynamic_cast<TokenWithLayout *>( token ) )
0127             {
0128                 currentRowConfig.addElement( LayoutItemConfigRowElement( twl->value(), twl->width(),
0129                                                                          twl->bold(), twl->italic(), twl->underline(),
0130                                                                          twl->alignment(), twl->prefix(), twl->suffix() ) );
0131             }
0132         }
0133 
0134         config.addRow( currentRowConfig );
0135 
0136     }
0137     return config;
0138 }
0139 
0140 void LayoutEditWidget::clear()
0141 {
0142     m_dragstack->clear();
0143 }
0144 
0145