Debian Codegraph @

src/playlist/layouts/PlaylistLayoutEditDialog.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2009 Nikolaj Hald Nielsen <[email protected]> *
3 * Copyright (c) 2009 Téo Mrnjavac <[email protected]> *
4 * Copyright (c) 2010 Oleksandr Khayrullin <[email protected]> *
5 * *
6 * This program is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU General Public License as published by the Free Software *
8 * Foundation; either version 2 of the License, or (at your option) any later *
9 * version. *
10 * *
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License along with *
16 * this program. If not, see <http://www.gnu.org/licenses/>. *
17 ****************************************************************************************/
18
19 #include "PlaylistLayoutEditDialog.h"
20
21 #include "core/support/Debug.h"
22
23 #include "playlist/layouts/LayoutManager.h"
24 #include "playlist/PlaylistDefines.h"
25
26 #include <KMessageBox>
27
28 #include <QInputDialog>
29 #include <QLineEdit>
30
31 Playlist::PlaylistLayoutEditDialog::PlaylistLayoutEditDialog( QWidget *parent )
32 : QDialog( parent )
33 {
34 setupUi( this );
35
36 // -- add tokens to the token pool
37 Column tokenValues[] = {
38 Album,
39 AlbumArtist,
40 Artist,
41 Bitrate,
42 Bpm,
43 Comment,
44 Composer,
45 Directory,
46 DiscNumber,
47 Divider,
48 Filename,
49 Filesize,
50 Genre,
51 GroupLength,
52 GroupTracks,
53 LastPlayed,
54 Labels,
55 Length,
56 Moodbar,
57 PlaceHolder,
58 PlayCount,
59 Rating,
60 SampleRate,
61 Score,
62 Source,
63 Title,
64 TitleWithTrackNum,
65 TrackNumber,
66 Type,
67 Year };
68
69 for( uint i = 0; i < sizeof( tokenValues ) / sizeof( tokenValues[0] ); i++ )
70 tokenPool->addToken( new Token( columnName( tokenValues[i] ),
71 iconName( tokenValues[i] ),
72 static_cast<qint64>(tokenValues[i]) ) );
73
74 resetFirstActiveLayout();
75
76 //add an editor to each tab
77 for( int part = 0; part < PlaylistLayout::NumParts; part++ )
78 m_partsEdit[part] = new Playlist::LayoutEditWidget( this );
79 m_layoutsMap = new QMap<QString, PlaylistLayout>();
80
81 elementTabs->addTab( m_partsEdit[PlaylistLayout::Head], i18n( "Head" ) );
82 elementTabs->addTab( m_partsEdit[PlaylistLayout::StandardBody], i18n( "Body" ) );
83 elementTabs->addTab( m_partsEdit[PlaylistLayout::VariousArtistsBody], i18n( "Body (Various artists)" ) );
84 elementTabs->addTab( m_partsEdit[PlaylistLayout::Single], i18n( "Single" ) );
85
86 QStringList layoutNames = LayoutManager::instance()->layouts();
87 for( const QString &layoutName : layoutNames )
88 {
89 PlaylistLayout layout = LayoutManager::instance()->layout( layoutName );
90 layout.setDirty( false );
91 m_layoutsMap->insert( layoutName, layout );
92 }
93
94 layoutListWidget->addItems( layoutNames );
95
96 layoutListWidget->setCurrentRow( LayoutManager::instance()->layouts().indexOf( LayoutManager::instance()->activeLayoutName() ) );
97
98 setupGroupByCombo();
99
100 if ( layoutListWidget->currentItem() )
101 setLayout( layoutListWidget->currentItem()->text() );
102
103 connect( previewButton, &QAbstractButton::clicked, this, &PlaylistLayoutEditDialog::preview );
104 connect( layoutListWidget, &QListWidget::currentTextChanged, this, &PlaylistLayoutEditDialog::setLayout );
105 connect( layoutListWidget, &QListWidget::currentRowChanged, this, &PlaylistLayoutEditDialog::toggleEditButtons );
106 connect( layoutListWidget, &QListWidget::currentRowChanged, this, &PlaylistLayoutEditDialog::toggleUpDownButtons );
107
108 connect( moveUpButton, &QAbstractButton::clicked, this, &PlaylistLayoutEditDialog::moveUp );
109 connect( moveDownButton, &QAbstractButton::clicked, this, &PlaylistLayoutEditDialog::moveDown );
110
111 buttonBox->button(QDialogButtonBox::Apply)->setIcon( QIcon::fromTheme( QStringLiteral("dialog-ok-apply") ) );
112 buttonBox->button(QDialogButtonBox::Ok)->setIcon( QIcon::fromTheme( QStringLiteral("dialog-ok") ) );
113 buttonBox->button(QDialogButtonBox::Cancel)->setIcon( QIcon::fromTheme( QStringLiteral("dialog-cancel") ) );
114 connect( buttonBox->button(QDialogButtonBox::Apply), &QAbstractButton::clicked, this, &PlaylistLayoutEditDialog::apply );
115
116 const QIcon newIcon = QIcon::fromTheme( QStringLiteral("document-new") );
117 newLayoutButton->setIcon( newIcon );
118 newLayoutButton->setToolTip( i18n( "New playlist layout" ) );
119 connect( newLayoutButton, &QAbstractButton::clicked, this, &PlaylistLayoutEditDialog::newLayout );
120
121 const QIcon copyIcon = QIcon::fromTheme( QStringLiteral("edit-copy") );
122 copyLayoutButton->setIcon( copyIcon );
123 copyLayoutButton->setToolTip( i18n( "Copy playlist layout" ) );
124 connect( copyLayoutButton, &QAbstractButton::clicked, this, &PlaylistLayoutEditDialog::copyLayout );
125
126 const QIcon deleteIcon = QIcon::fromTheme( QStringLiteral("edit-delete") );
127 deleteLayoutButton->setIcon( deleteIcon );
128 deleteLayoutButton->setToolTip( i18n( "Delete playlist layout" ) );
129 connect( deleteLayoutButton, &QAbstractButton::clicked, this, &PlaylistLayoutEditDialog::deleteLayout );
130
131 const QIcon renameIcon = QIcon::fromTheme( QStringLiteral("edit-rename") );
132 renameLayoutButton->setIcon( renameIcon );
133 renameLayoutButton->setToolTip( i18n( "Rename playlist layout" ) );
134 connect( renameLayoutButton, &QAbstractButton::clicked, this, &PlaylistLayoutEditDialog::renameLayout );
135
136 toggleEditButtons();
137 toggleUpDownButtons();
138
139 for( int part = 0; part < PlaylistLayout::NumParts; part++ )
140 connect( m_partsEdit[part], &Playlist::LayoutEditWidget::changed, this, &PlaylistLayoutEditDialog::setLayoutChanged );
141
142 connect( inlineControlsChekbox, &QCheckBox::checkStateChanged, this, &PlaylistLayoutEditDialog::setLayoutChanged );
143 connect( tooltipsCheckbox, &QCheckBox::checkStateChanged, this, &PlaylistLayoutEditDialog::setLayoutChanged );
144 connect( groupByComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
145 this, &PlaylistLayoutEditDialog::setLayoutChanged );
146 }
147
148
149 Playlist::PlaylistLayoutEditDialog::~PlaylistLayoutEditDialog()
150 {
151 }
152
153 void
154 Playlist::PlaylistLayoutEditDialog::resetFirstActiveLayout()
155 {
156 m_firstActiveLayout = LayoutManager::instance()->activeLayoutName();
157 }
158
159 void
160 Playlist::PlaylistLayoutEditDialog::newLayout() //SLOT
161 {
162 bool ok;
163 QString layoutName = QInputDialog::getText( this, i18n( "Choose a name for the new playlist layout" ),
164 i18n( "Please enter a name for the playlist layout you are about to define:" ),
165 QLineEdit::Normal, QString(), &ok );
166 if( !ok )
167 return;
168 if( layoutName.isEmpty() )
169 {
170 KMessageBox::error( this, i18n( "Cannot create a layout with no name." ), i18n( "Layout name error" ) );
171 return;
172 }
173 if( m_layoutsMap->keys().contains( layoutName ) )
174 {
175 KMessageBox::error( this, i18n( "Cannot create a layout with the same name as an existing layout." ), i18n( "Layout name error" ) );
176 return;
177 }
178 if( layoutName.contains( QLatin1Char('/') ) )
179 {
180 KMessageBox::error( this, i18n( "Cannot create a layout containing '/'." ), i18n( "Layout name error" ) );
181 return;
182 }
183
184 PlaylistLayout layout;
185 layout.setEditable( true ); //Should I use true, TRUE or 1?
186 layout.setDirty( true );
187
188 layoutListWidget->addItem( layoutName );
189 layoutListWidget->setCurrentItem( (layoutListWidget->findItems( layoutName, Qt::MatchExactly ) ).first() );
190 for( int part = 0; part < PlaylistLayout::NumParts; part++ )
191 {
192 m_partsEdit[part]->clear();
193 layout.setLayoutForPart( (PlaylistLayout::Part)part, m_partsEdit[part]->config() );
194 }
195 m_layoutsMap->insert( layoutName, layout );
196
197 LayoutManager::instance()->addUserLayout( layoutName, layout );
198
199 setLayout( layoutName );
200 }
201
202 void
203 Playlist::PlaylistLayoutEditDialog::copyLayout()
204 {
205 LayoutItemConfig configs[PlaylistLayout::NumParts];
206 for( int part = 0; part < PlaylistLayout::NumParts; part++ )
207 configs[part] = m_partsEdit[part]->config();
208
209 QString layoutName = layoutListWidget->currentItem()->text();
210
211 bool ok;
212 layoutName = QInputDialog::getText( this,
213 i18n( "Choose a name for the new playlist layout" ),
214 i18n( "Please enter a name for the playlist layout you are about to define as copy of the layout '%1':", layoutName ),
215 QLineEdit::Normal, layoutName, &ok );
216
217 if( !ok)
218 return;
219 if( layoutName.isEmpty() )
220 {
221 KMessageBox::error( this, i18n( "Cannot create a layout with no name." ), i18n( "Layout name error" ) );
222 return;
223 }
224 if( m_layoutsMap->keys().contains( layoutName ) )
225 {
226 KMessageBox::error( this, i18n( "Cannot create a layout with the same name as an existing layout." ), i18n( "Layout name error" ) );
227 return;
228 }
229 //layoutListWidget->addItem( layoutName );
230 PlaylistLayout layout;
231 layout.setEditable( true ); //Should I use true, TRUE or 1?
232 layout.setDirty( true );
233
234 configs[PlaylistLayout::Head].setActiveIndicatorRow( -1 );
235 for( int part = 0; part < PlaylistLayout::NumParts; part++ )
236 layout.setLayoutForPart( (PlaylistLayout::Part)part, configs[part] );
237
238 layout.setInlineControls( inlineControlsChekbox->isChecked() );
239 layout.setTooltips( tooltipsCheckbox->isChecked() );
240 layout.setGroupBy( groupByComboBox->itemData( groupByComboBox->currentIndex() ).toString() );
241
242 LayoutManager::instance()->addUserLayout( layoutName, layout );
243
244 //reload from manager:
245 layoutListWidget->clear();
246 layoutListWidget->addItems( LayoutManager::instance()->layouts() );
247
248 m_layoutsMap->insert( layoutName, layout );
249 layoutListWidget->setCurrentItem( ( layoutListWidget->findItems( layoutName, Qt::MatchExactly ) ).first() );
250 setLayout( layoutName );
251 }
252
253 void
254 Playlist::PlaylistLayoutEditDialog::deleteLayout() //SLOT
255 {
256 m_layoutsMap->remove( layoutListWidget->currentItem()->text() );
257 if( LayoutManager::instance()->layouts().contains( layoutListWidget->currentItem()->text() ) ) //if the layout is already saved in the LayoutManager
258 LayoutManager::instance()->deleteLayout( layoutListWidget->currentItem()->text() ); //delete it
259 delete layoutListWidget->currentItem();
260 }
261
262 void
263 Playlist::PlaylistLayoutEditDialog::renameLayout()
264 {
265 PlaylistLayout layout = m_layoutsMap->value( layoutListWidget->currentItem()->text() );
266
267 QString layoutName;
268 while( layoutName.isEmpty() || m_layoutsMap->keys().contains( layoutName ) )
269 {
270 bool ok;
271 layoutName = QInputDialog::getText( this,
272 i18n( "Choose a new name for the playlist layout" ),
273 i18n( "Please enter a new name for the playlist layout you are about to rename:" ),
274 QLineEdit::Normal, layoutListWidget->currentItem()->text(), &ok );
275 if ( !ok )
276 {
277 //Cancelled so just return
278 return;
279 }
280 if( layoutName.isEmpty() )
281 KMessageBox::error( this, i18n( "Cannot rename a layout to have no name." ), i18n( "Layout name error" ) );
282 if( m_layoutsMap->keys().contains( layoutName ) )
283 KMessageBox::error( this, i18n( "Cannot rename a layout to have the same name as an existing layout." ), i18n( "Layout name error" ) );
284 }
285 m_layoutsMap->remove( layoutListWidget->currentItem()->text() );
286 if( LayoutManager::instance()->layouts().contains( layoutListWidget->currentItem()->text() ) ) //if the layout is already saved in the LayoutManager
287 LayoutManager::instance()->deleteLayout( layoutListWidget->currentItem()->text() ); //delete it
288 LayoutManager::instance()->addUserLayout( layoutName, layout );
289 m_layoutsMap->insert( layoutName, layout );
290 layoutListWidget->currentItem()->setText( layoutName );
291
292 setLayout( layoutName );
293 }
294
295 void
296 Playlist::PlaylistLayoutEditDialog::setLayout( const QString &layoutName ) //SLOT
297 {
298 DEBUG_BLOCK
299 m_layoutName = layoutName;
300
301 if( m_layoutsMap->keys().contains( layoutName ) ) //is the layout exists in the list of loaded layouts
302 {
303 debug() << "loaded layout";
304 PlaylistLayout layout = m_layoutsMap->value( layoutName );
305 for( int part = 0; part < PlaylistLayout::NumParts; part++ )
306 m_partsEdit[part]->readLayout( layout.layoutForPart( (PlaylistLayout::Part)part ) );
307 inlineControlsChekbox->setChecked( layout.inlineControls() );
308 tooltipsCheckbox->setChecked( layout.tooltips() );
309 groupByComboBox->setCurrentIndex( groupByComboBox->findData( layout.groupBy() ) );
310
311 setEnabledTabs();
312 //make sure that it is not marked dirty (it will be because of the changed signal triggering when loading it)
313 //unless it is actually changed
314 debug() << "not dirty anyway!!";
315 (*m_layoutsMap)[m_layoutName].setDirty( false );
316 }
317 else
318 {
319 for( int part = 0; part < PlaylistLayout::NumParts; part++ )
320 m_partsEdit[part]->clear();
321 }
322 }
323
324 void
325 Playlist::PlaylistLayoutEditDialog::preview()
326 {
327 PlaylistLayout layout;
328
329 for( int part = 0; part < PlaylistLayout::NumParts; part++ )
330 {
331 LayoutItemConfig config = m_partsEdit[part]->config();
332 if( part == PlaylistLayout::Head )
333 config.setActiveIndicatorRow( -1 );
334 layout.setLayoutForPart( (PlaylistLayout::Part)part, config );
335 }
336
337 layout.setInlineControls( inlineControlsChekbox->isChecked() );
338 layout.setTooltips( tooltipsCheckbox->isChecked() );
339 layout.setGroupBy( groupByComboBox->itemData( groupByComboBox->currentIndex() ).toString() );
340
341 LayoutManager::instance()->setPreviewLayout( layout );
342 }
343
344 void
345 Playlist::PlaylistLayoutEditDialog::toggleEditButtons() //SLOT
346 {
347 if ( !layoutListWidget->currentItem() ) {
348 deleteLayoutButton->setEnabled( 0 );
349 renameLayoutButton->setEnabled( 0 );
350 } else if( LayoutManager::instance()->isDefaultLayout( layoutListWidget->currentItem()->text() ) ) {
351 deleteLayoutButton->setEnabled( 0 );
352 renameLayoutButton->setEnabled( 0 );
353 } else {
354 deleteLayoutButton->setEnabled( 1 );
355 renameLayoutButton->setEnabled( 1 );
356 }
357 }
358
359 void
360 Playlist::PlaylistLayoutEditDialog::toggleUpDownButtons()
361 {
362 if ( !layoutListWidget->currentItem() )
363 {
364 moveUpButton->setEnabled( 0 );
365 moveDownButton->setEnabled( 0 );
366 }
367 else if ( layoutListWidget->currentRow() == 0 )
368 {
369 moveUpButton->setEnabled( 0 );
370 if ( layoutListWidget->currentRow() >= m_layoutsMap->size() -1 )
371 moveDownButton->setEnabled( 0 );
372 else
373 moveDownButton->setEnabled( 1 );
374 }
375 else if ( layoutListWidget->currentRow() >= m_layoutsMap->size() -1 )
376 {
377 moveDownButton->setEnabled( 0 );
378 moveUpButton->setEnabled( 1 ); //we already checked that this is not row 0
379 }
380 else
381 {
382 moveDownButton->setEnabled( 1 );
383 moveUpButton->setEnabled( 1 );
384 }
385
386
387 }
388
389 void
390 Playlist::PlaylistLayoutEditDialog::apply() //SLOT
391 {
392 for( const QString &layoutName : m_layoutsMap->keys() )
393 {
394 PlaylistLayout layout = m_layoutsMap->value( layoutName );
395
396 if( layout.isDirty() )
397 {
398 // search a new name for changed default layouts
399 if( LayoutManager::instance()->isDefaultLayout( layoutName ) )
400 {
401 QString newLayoutName = i18n( "copy of %1", layoutName );
402 QString orgCopyName = newLayoutName;
403
404 int copyNumber = 1;
405 QStringList existingLayouts = LayoutManager::instance()->layouts();
406 while( existingLayouts.contains( newLayoutName ) )
407 {
408 copyNumber++;
409 newLayoutName = i18nc( "adds a copy number to a generated name if the name already exists, for instance 'copy of Foo 2' if 'copy of Foo' is taken", "%1 %2", orgCopyName, copyNumber );
410 }
411
412 const QString msg = i18n( "The layout '%1' you modified is one of the default layouts and cannot be overwritten. "
413 "Saved as new layout '%2'", layoutName, newLayoutName );
414 KMessageBox::error( this, msg, i18n( "Default Layout" ) );
415
416
417 layout.setDirty( false );
418 m_layoutsMap->insert( newLayoutName, layout );
419 LayoutManager::instance()->addUserLayout( newLayoutName, layout );
420 layoutListWidget->addItem( newLayoutName );
421
422 if( layoutName == m_layoutName )
423 layoutListWidget->setCurrentItem( ( layoutListWidget->findItems( newLayoutName, Qt::MatchExactly ) ).first() );
424
425 // restore the default layout
426 m_layoutsMap->insert( layoutName, LayoutManager::instance()->layout( layoutName ) );
427 }
428 else
429 {
430 layout.setDirty( false );
431
432 m_layoutsMap->insert( layoutName, layout );
433 LayoutManager::instance()->addUserLayout( layoutName, layout );
434 }
435 }
436 }
437 LayoutManager::instance()->setActiveLayout( layoutListWidget->currentItem()->text() ); //important to override the previewed layout if preview is used
438 }
439
440 void
441 Playlist::PlaylistLayoutEditDialog::accept() //SLOT
442 {
443 apply();
444 QDialog::accept();
445 }
446
447 void
448 Playlist::PlaylistLayoutEditDialog::reject() //SLOT
449 {
450 DEBUG_BLOCK
451
452 debug() << "Applying initial layout: " << m_firstActiveLayout;
453 if( layoutListWidget->findItems( m_firstActiveLayout, Qt::MatchExactly ).isEmpty() )
454 LayoutManager::instance()->setActiveLayout( QStringLiteral("Default") );
455 else
456 LayoutManager::instance()->setActiveLayout( m_firstActiveLayout );
457
458 QDialog::reject();
459 }
460
461 void
462 Playlist::PlaylistLayoutEditDialog::moveUp()
463 {
464 int newRow = LayoutManager::instance()->moveUp( m_layoutName );
465
466 layoutListWidget->clear();
467 layoutListWidget->addItems( LayoutManager::instance()->layouts() );
468
469 layoutListWidget->setCurrentRow( newRow );
470 }
471
472 void
473 Playlist::PlaylistLayoutEditDialog::moveDown()
474 {
475 int newRow = LayoutManager::instance()->moveDown( m_layoutName );
476
477 layoutListWidget->clear();
478 layoutListWidget->addItems( LayoutManager::instance()->layouts() );
479
480 layoutListWidget->setCurrentRow( newRow );
481 }
482
483 void
484 Playlist::PlaylistLayoutEditDialog::setEnabledTabs()
485 {
486 DEBUG_BLOCK
487
488 //Enable or disable tabs depending on whether grouping is allowed.
489 QString grouping = groupByComboBox->itemData( groupByComboBox->currentIndex() ).toString();
490 bool groupingEnabled = ( !grouping.isEmpty() && grouping != QLatin1String("None") );
491
492 if ( !groupingEnabled )
493 elementTabs->setCurrentWidget( m_partsEdit[PlaylistLayout::Single] );
494
495 debug() << groupByComboBox->itemData( groupByComboBox->currentIndex() ).toString();
496 debug() << groupingEnabled;
497
498 elementTabs->setTabEnabled( elementTabs->indexOf( m_partsEdit[PlaylistLayout::Head] ), groupingEnabled );
499 elementTabs->setTabEnabled( elementTabs->indexOf( m_partsEdit[PlaylistLayout::StandardBody] ), groupingEnabled );
500 elementTabs->setTabEnabled( elementTabs->indexOf( m_partsEdit[PlaylistLayout::VariousArtistsBody] ), groupingEnabled );
501 }
502
503 //Sets up a combo box that presents the possible grouping categories, as well as the option
504 //to perform no grouping.
505 //We'll use the "user data" to store the un-i18n-ized category name for internal use.
506 void
507 Playlist::PlaylistLayoutEditDialog::setupGroupByCombo()
508 {
509 for( const Playlist::Column &col : Playlist::groupableCategories() )
510 {
511 groupByComboBox->addItem( QIcon::fromTheme( iconName( col ) ),
512 columnName( col ),
513 QVariant( internalColumnName( col ) ) );
514 }
515
516 //Add the option to not perform grouping
517 //Use a null string to specify "no grouping"
518 groupByComboBox->addItem( i18n( "No Grouping" ), QVariant( QStringLiteral("None") ) );
519 }
520
521 void
522 Playlist::PlaylistLayoutEditDialog::setLayoutChanged()
523 {
524 DEBUG_BLOCK
525
526 setEnabledTabs();
527
528 for( int part = 0; part < PlaylistLayout::NumParts; part++ )
529 (*m_layoutsMap)[m_layoutName].setLayoutForPart( (PlaylistLayout::Part)part, m_partsEdit[part]->config() );
530
531 (*m_layoutsMap)[m_layoutName].setInlineControls( inlineControlsChekbox->isChecked() );
532 (*m_layoutsMap)[m_layoutName].setTooltips( tooltipsCheckbox->isChecked() );
533 (*m_layoutsMap)[m_layoutName].setGroupBy( groupByComboBox->itemData( groupByComboBox->currentIndex() ).toString() );
534 (*m_layoutsMap)[m_layoutName].setDirty( true );
535 }
536
537