| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2009 Nikolaj Hald Nielsen <[email protected]> * |
| 3 |
* * |
| 4 |
* This program is free software; you can redistribute it and/or modify it under * |
| 5 |
* the terms of the GNU General Public License as published by the Free Software * |
| 6 |
* Foundation; either version 2 of the License, or (at your option) any later * |
| 7 |
* version. * |
| 8 |
* * |
| 9 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 10 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 11 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 12 |
* * |
| 13 |
* You should have received a copy of the GNU General Public License along with * |
| 14 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 15 |
****************************************************************************************/ |
| 16 |
|
| 17 |
#include "LayoutEditWidget.h" |
| 18 |
|
| 19 |
#include "core/support/Debug.h" |
| 20 |
#include "playlist/PlaylistDefines.h" |
| 21 |
#include "widgets/TokenDropTarget.h" |
| 22 |
|
| 23 |
#include <KLocalizedString> |
| 24 |
#include <KMessageBox> |
| 25 |
|
| 26 |
#include <QBoxLayout> |
| 27 |
#include <QCheckBox> |
| 28 |
|
| 29 |
using namespace Playlist; |
| 30 |
|
| 31 |
LayoutEditWidget::LayoutEditWidget( QWidget *parent ) |
| 32 |
: QWidget( parent ) |
| 33 |
{ |
| 34 |
QVBoxLayout* mainLayout = new QVBoxLayout( this ); |
| 35 |
|
| 36 |
m_dragstack = new TokenDropTarget( this ); |
| 37 |
m_dragstack->setCustomTokenFactory( new TokenWithLayoutFactory() ); |
| 38 |
mainLayout->addWidget( m_dragstack, 1 ); |
| 39 |
|
| 40 |
// connect ( m_dragstack, SIGNAL(focusReceived(QWidget*)), this, SIGNAL(focusReceived(QWidget*)) ); |
| 41 |
connect ( m_dragstack, &TokenDropTarget::changed, this, &LayoutEditWidget::changed ); |
| 42 |
|
| 43 |
m_showCoverCheckBox = new QCheckBox( i18n( "Show cover" ), this ); |
| 44 |
connect ( m_showCoverCheckBox, &QCheckBox::checkStateChanged, this, &LayoutEditWidget::changed ); |
| 45 |
mainLayout->addWidget( m_showCoverCheckBox, 0 ); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
LayoutEditWidget::~LayoutEditWidget() |
| 50 |
{ } |
| 51 |
|
| 52 |
void LayoutEditWidget::readLayout( const Playlist::LayoutItemConfig &config ) |
| 53 |
{ |
| 54 |
DEBUG_BLOCK |
| 55 |
int rowCount = config.rows(); |
| 56 |
|
| 57 |
disconnect ( m_showCoverCheckBox, &QCheckBox::checkStateChanged, this, &LayoutEditWidget::changed ); |
| 58 |
m_showCoverCheckBox->setChecked( config.showCover() ); |
| 59 |
connect ( m_showCoverCheckBox, &QCheckBox::checkStateChanged, this, &LayoutEditWidget::changed ); |
| 60 |
|
| 61 |
m_dragstack->clear(); |
| 62 |
|
| 63 |
for( int i = 0; i < rowCount; i++ ) |
| 64 |
{ |
| 65 |
//get the row config |
| 66 |
Playlist::LayoutItemConfigRow rowConfig = config.row( i ); |
| 67 |
|
| 68 |
int elementCount = rowConfig.count(); |
| 69 |
|
| 70 |
//FIXME! for now, each element get the same size. This needs extensions to the token stuff |
| 71 |
//qreal size = 1.0 / (qreal) elementCount; |
| 72 |
|
| 73 |
for( int j = 0; j < elementCount; j++ ) |
| 74 |
{ |
| 75 |
Playlist::LayoutItemConfigRowElement element = rowConfig.element( j ); |
| 76 |
//check if the element has a valid value, will crash if trying to use it otherwise |
| 77 |
debug() << "value: " << element.value(); |
| 78 |
if ( element.value() == -1 ) |
| 79 |
{ |
| 80 |
|
| 81 |
error() << "Invalid element value '" << element.value() << "' in playlist layout."; |
| 82 |
KMessageBox::detailedError( this, |
| 83 |
i18n( "Invalid playlist layout." ), |
| 84 |
i18n( "Encountered an unknown element name while reading layout." ) ); |
| 85 |
m_dragstack->clear(); |
| 86 |
return; |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
Column col = static_cast<Column>(element.value()); |
| 91 |
TokenWithLayout *token = new TokenWithLayout( columnName( col ), |
| 92 |
iconName( col ), |
| 93 |
element.value() ); |
| 94 |
token->setBold( element.bold() ); |
| 95 |
token->setItalic( element.italic() ); |
| 96 |
token->setUnderline( element.underline() ); |
| 97 |
token->setAlignment( element.alignment() ); |
| 98 |
token->setWidth( element.size() * 100.0 ); |
| 99 |
token->setPrefix( element.prefix() ); |
| 100 |
token->setSuffix( element.suffix() ); |
| 101 |
m_dragstack->insertToken( token, i, j ); |
| 102 |
// Do all modifications on the token above that line, otherwise the dialog will think it's been modified by the user |
| 103 |
connect ( token, &TokenWithLayout::changed, this, &LayoutEditWidget::changed ); |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
Playlist::LayoutItemConfig LayoutEditWidget::config() |
| 110 |
{ |
| 111 |
|
| 112 |
LayoutItemConfig config; |
| 113 |
config.setShowCover( m_showCoverCheckBox->isChecked() ); |
| 114 |
|
| 115 |
int noOfRows = m_dragstack->rows(); |
| 116 |
|
| 117 |
for( int i = 0; i < noOfRows; i++ ) |
| 118 |
{ |
| 119 |
|
| 120 |
LayoutItemConfigRow currentRowConfig; |
| 121 |
|
| 122 |
QList<Token *> tokens = m_dragstack->tokensAtRow( i ); |
| 123 |
|
| 124 |
for( Token * token : tokens ) |
| 125 |
{ |
| 126 |
if ( TokenWithLayout *twl = dynamic_cast<TokenWithLayout *>( token ) ) |
| 127 |
{ |
| 128 |
currentRowConfig.addElement( LayoutItemConfigRowElement( twl->value(), twl->width(), |
| 129 |
twl->bold(), twl->italic(), twl->underline(), |
| 130 |
twl->alignment(), twl->prefix(), twl->suffix() ) ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
config.addRow( currentRowConfig ); |
| 135 |
|
| 136 |
} |
| 137 |
return config; |
| 138 |
} |
| 139 |
|
| 140 |
void LayoutEditWidget::clear() |
| 141 |
{ |
| 142 |
m_dragstack->clear(); |
| 143 |
} |
| 144 |
|
| 145 |
|