Menu

[r560]: / src / songwidget.cpp  Maximize  Restore  History

Download this file

825 lines (713 with data), 25.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/***************************************************************************
//
// softProjector - an open source media projection software
// Copyright (C) 2011 Vladislav Kobzar, Matvey Adzhigirey and Ilya Spivakov
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
***************************************************************************/
#include <QDebug>
#include "songwidget.h"
#include "ui_songwidget.h"
SongWidget::SongWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::SongWidget)
{
ui->setupUi(this);
songs_model = new SongsModel;
proxy_model = new SongProxyModel(this);
proxy_model->setSourceModel(songs_model);
proxy_model->setDynamicSortFilter(true);
ui->songs_view->setModel(proxy_model);
//ui->songs_view->sortByColumn(1, Qt::AscendingOrder);
connect(ui->songs_view->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex&, const QModelIndex&)),
this, SLOT(songsViewRowChanged(const QModelIndex&, const QModelIndex&)));
playlist_model = new SongsModel;
ui->playlist_view->setModel(playlist_model);
connect(ui->playlist_view->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex&, const QModelIndex&)),
this, SLOT(playlistViewRowChanged(const QModelIndex&, const QModelIndex&)));
// Decrease the row height:
ui->songs_view->resizeRowsToContents();
ui->playlist_view->resizeRowsToContents();
// IS this needed?
// Modify the column widths:
ui->songs_view->setColumnWidth(0, 0);//Category
ui->songs_view->setColumnWidth(1, 60);//Song Number
ui->songs_view->setColumnWidth(2, 150);//Song Title
ui->songs_view->setColumnWidth(3, 70);//Songbook
ui->playlist_view->setColumnWidth(0, 0);//Category
ui->playlist_view->setColumnWidth(1, 60);//Song Number
ui->playlist_view->setColumnWidth(2, 150);//Song Title
ui->playlist_view->setColumnWidth(3, 70);//Songbook
proxy_model->setSongbookFilter("ALL");
proxy_model->setCategoryFilter(-1);
loadSongbooks();
loadCategories(false);
isSpinboxEditing = false;
playlistSongWasEdited = false;
// set highligher
highlight = new HighlighterDelegate(ui->listPreview);
ui->listWidgetDummy->setVisible(false);
}
SongWidget::~SongWidget()
{
delete ui;
delete songs_model;
}
void SongWidget::songsViewRowChanged(const QModelIndex &current, const QModelIndex &previous)
{
if( current.isValid() )
{
// Called when a new song is selected in the songs table
int row = proxy_model->mapToSource(current).row();
Song song = songs_model->getSong(row);
sendToPreview(song);
focusInPlaylistTable = false;
}
updateButtonStates();
}
void SongWidget::updateButtonStates()
{
bool enable_live;
bool enable_add;
bool enable_remove;
bool enable_up;
bool enable_down;
if(focusInPlaylistTable)
{
enable_live = ui->playlist_view->currentIndex().isValid();
enable_add = false;
enable_remove = ( playlist_model->rowCount() > 0 );
int selrow = ui->playlist_view->currentIndex().row();
enable_up = (playlist_model->rowCount() > 1) && (selrow != 0);
enable_down = (playlist_model->rowCount() > 1) && (selrow+1 != playlist_model->rowCount());
}
else
{
// focus in songs table
if( proxy_model->rowCount() == 0 )
enable_live = false;
else
enable_live = ui->songs_view->currentIndex().isValid();
enable_add = enable_live;
enable_remove = false;
enable_up = false;
enable_down = false;
}
ui->btnLive->setEnabled(enable_live);
ui->btnAddToPlaylist->setEnabled(enable_add);
ui->btnRemoveFromPlaylist->setEnabled(enable_remove);
ui->btnUpInPlaylist->setEnabled(enable_up);
ui->btnDownInPlaylist->setEnabled(enable_down);
}
void SongWidget::playlistViewRowChanged(const QModelIndex &current, const QModelIndex &previous)
{
if( current.isValid() )
{
// Called when a new song is selected in the playlist table
Song song = playlist_model->getSong(current.row());
sendToPreview(song);
}
else
{
//FIXME clear the preview? Or not...
}
updateButtonStates();
}
void SongWidget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void SongWidget::loadSongbooks()
{
QSqlQuery sq;
QStringList sbor;
sq.exec("SELECT id, name FROM Songbooks");
while (sq.next())
{
songbookList << sq.value(0).toString();
sbor << sq.value(1).toString();
}
ui->songbook_menu->addItem(tr("All songbooks"));
ui->songbook_menu->addItems(sbor);
// QList<Song> song_list = song_database.getSongs();
allSongs = song_database.getSongs();
//ui->song_num_spinbox->setEnabled(false);
songs_model->setSongs(allSongs);
// Hide song search items
ui->comboBoxSearchType->setVisible(false);
if(ui->pushButtonClearResults->isVisible())
ui->listPreview->setItemDelegate(ui->listWidgetDummy->itemDelegate());
ui->pushButtonClearResults->setVisible(false);
ui->labelSearchType->setVisible(false);
// update the song spin box and redraw the table:
on_songbook_menu_currentIndexChanged( ui->songbook_menu->currentIndex() );
}
Song SongWidget::currentSong()
{
// Returns the selected song
QModelIndex current_index;
int current_row;
if (focusInPlaylistTable)
current_row = ui->playlist_view->currentIndex().row();
else
{
current_index = proxy_model->mapToSource(ui->songs_view->currentIndex());
current_row = current_index.row();
}
Song return_song;
if(current_row>=0)
{
if(focusInPlaylistTable)
return_song = playlist_model->getSong(current_row);
else
return_song = songs_model->getSong(current_row);
}
return return_song;
}
void SongWidget::selectMatchingSong(QString text)
{
bool startonly = ui->begins_rbutton->isChecked();
// Look for a song matching <text>. Select it and scroll to show it.
for (int i = 0; i < songs_model->song_list.size(); i++)
{
QString s = songs_model->song_list.at(i).title;
bool matches;
if( startonly )
matches = s.startsWith(text);
else
matches = s.contains(text);
if( matches )
{
// Select the row <i>:
ui->songs_view->selectRow(i);
// Scroll the songs table to the row <i>:
ui->songs_view->scrollTo( songs_model->index(i, 0) );
return;
}
}
}
void SongWidget::sendToPreview(Song song)
{
QStringList song_list = song.getSongTextList();
ui->listPreview->clear();
ui->listPreview->addItems(song_list);
ui->listPreview->setCurrentRow(0);
ui->preview_label->setText(song.title);
if(song.notes.isEmpty())
ui->label_notes->setVisible(false);
else
{
ui->label_notes->setText(QString("%1\n%2").arg(tr("Notes:","Notes to songs")).arg(song.notes));
ui->label_notes->setVisible(true);
}
preview_song = song;
}
void SongWidget::sendToProjector(Song song, int row)
{
// Display the specified song text in the right-most column of softProjector:
emit sendSong(song, row);
// Add a count to a song
counter.addSongCount(song);
}
void SongWidget::on_songbook_menu_currentIndexChanged(int index)
{
// Called when a different songbook is selected from the pull-down menu
songs_model->emitLayoutAboutToBeChanged(); //prepeare to chage layout
if( index == 0 )
{
proxy_model->setSongbookFilter("ALL");
ui->song_num_spinbox->setEnabled(false);
}
else
{
QString songbook_name = ui->songbook_menu->currentText();
proxy_model->setSongbookFilter(songbook_name);
ui->song_num_spinbox->setEnabled(true);
}
updateButtonStates();
songs_model->emitLayoutChanged(); // forces the view to redraw
}
void SongWidget::on_song_num_spinbox_valueChanged(int value)
{
// checks if spinbox just got into eding mode, it yes, then it reset searchbox and sorts song table view
if (!isSpinboxEditing)
{
isSpinboxEditing = true;
ui->lineEditSearch->clear();
on_lineEditSearch_textEdited("");
ui->songs_view->sortByColumn(0,Qt::AscendingOrder);
}
//int max_num = 0;
// Look for a song with number <value>. Select it and scroll to show it.
for (int i = 0; i < songs_model->song_list.size(); i++)
{
Song s = songs_model->song_list.at(i);
if( s.number == value && s.songbook_name == ui->songbook_menu->currentText() )
{
// Found a song with this song number
QModelIndex source_index = songs_model->index(i, 0);
if( proxy_model->filterAcceptsRow(source_index.row(), source_index) )
{
// If this row is visible
QModelIndex proxy_index = proxy_model->mapFromSource(source_index);
// Select the row <i>:
ui->songs_view->selectRow(proxy_index.row());
// Scroll the songs table to the row <i>:
ui->songs_view->scrollTo(proxy_index);
}
else
{
// This song is filtered out using text filter, so can't select
// it in the table. Just show it:
sendToPreview(s);
}
return;
}
//if( s.num > max_num )
// max_num = s.num;
}
// If got here, then no such song
//if( value > max_num )
// ui->song_num_spinbox->setMaximum();
QMessageBox mb;
mb.setText(tr("Could not find song with number ") + QString::number(value) );
mb.setWindowTitle(tr("No such song"));
mb.setIcon(QMessageBox::Warning);
mb.exec();
}
void SongWidget::on_song_num_spinbox_editingFinished()
{
// Called when the user presses enter after editing the song number
// At this point, the song is already selected in the songs table
//on_btnAddToPlaylist_clicked();
// Resets spin box to non eding mode
isSpinboxEditing = false;
}
void SongWidget::on_btnLive_clicked()
{
sendToProjector(preview_song, ui->listPreview->currentRow()); // Send current selected
}
void SongWidget::on_btnAddToPlaylist_clicked()
{
Song song;
if( false ) //ui->song_num_spinbox->hasFocus() )
{
int value = ui->song_num_spinbox->text().toInt();
for (int i = 0; i < songs_model->song_list.size(); i++)
{
Song s = songs_model->song_list.at(i);
if( s.number == value )
{
song = s;
break;
}
}
}
else
{
song = currentSong();
}
playlist_model->addSong(song);
emit sendPlaylistChanged(true);
// Select the row that was just added:
ui->playlist_view->selectRow(playlist_model->rowCount()-1);
//ui->playlist_view->setFocus();
//focusInPlaylistTable = true;
//ui->btnRemoveFromPlaylist->setEnabled(true);
sendToPreview(song);
updateButtonStates();
}
void SongWidget::on_btnRemoveFromPlaylist_clicked()
{
int row = ui->playlist_view->currentIndex().row();
playlist_model->removeRow(row);
emit sendPlaylistChanged(true);
updateButtonStates();
}
void SongWidget::on_btnDownInPlaylist_clicked()
{
playlist_model->emitLayoutAboutToBeChanged();
int row = ui->playlist_view->currentIndex().row();
Song song = playlist_model->song_list.takeAt(row);
playlist_model->song_list.insert(row+1, song);
playlist_model->emitLayoutChanged();
ui->playlist_view->selectRow(row+1);
emit sendPlaylistChanged(true);
updateButtonStates();
}
void SongWidget::on_btnUpInPlaylist_clicked()
{
playlist_model->emitLayoutAboutToBeChanged();
int row = ui->playlist_view->currentIndex().row();
Song song = playlist_model->song_list.takeAt(row);
playlist_model->song_list.insert(row-1, song);
playlist_model->emitLayoutChanged();
ui->playlist_view->selectRow(row-1);
emit sendPlaylistChanged(true);
updateButtonStates();
}
void SongWidget::on_lineEditSearch_textEdited(QString text)
{
// Check if full-text search is in progress
// If no full-text search is in progress, then filter
if(!ui->pushButtonClearResults->isVisible())
{
// These two options are mutually exclusive:
bool match_beginning = ui->begins_rbutton->isChecked();
bool exact_match = ui->exact_match_rbutton->isChecked();
songs_model->emitLayoutAboutToBeChanged(); // prepares view to be redrawn
proxy_model->setFilterString(text, match_beginning, exact_match);
songs_model->emitLayoutChanged(); // forces the view to redraw
// Select the first row that matches the new filter:
ui->songs_view->selectRow(0);
ui->songs_view->scrollToTop();
// Load Preview on song changes
int row = proxy_model->mapToSource(ui->songs_view->currentIndex()).row();
if( row>=0)
{
Song song = songs_model->getSong(row);
sendToPreview(song);
focusInPlaylistTable = false;
}
}
updateButtonStates();
}
Song SongWidget::getSongToEdit()
{
playlistSongWasEdited = focusInPlaylistTable;
// Check if edited song is also in the playlist table (exact match)
if (playlist_model->isInTable(preview_song.songID))
playlistSongWasEdited = true;
return preview_song;
}
void SongWidget::on_songs_view_doubleClicked(QModelIndex index)
{
// Called when a song is double-clicked
int row = proxy_model->mapToSource(index).row();
Song song = songs_model->getSong(row);
playlist_model->addSong(song);
emit sendPlaylistChanged(true);
ui->playlist_view->selectRow(playlist_model->rowCount()-1);
//ui->playlist_view->setFocus();
//focusInPlaylistTable = true;
sendToPreview(song);
}
void SongWidget::on_playlist_view_doubleClicked(QModelIndex index)
{
Song song = playlist_model->getSong(index.row());
//sendToProjector(song, ui->listPreview->currentRow());
sendToProjector(song, 0); // Send the first verse
}
void SongWidget::on_playlist_view_clicked(QModelIndex index)
{
// This method is implemented for the case where the use clicks
// in the playlist table without changing the previous selection.
Song song = playlist_model->getSong(index);
focusInPlaylistTable = true;
sendToPreview(song);
updateButtonStates();
}
void SongWidget::on_songs_view_clicked(QModelIndex index)
{
// This method is implemented for the case where the use clicks
// in the playlist table without changing the previous selection.
Song song = songs_model->getSong(proxy_model->mapToSource(index));
focusInPlaylistTable = false;
sendToPreview(song);
updateButtonStates();
}
void SongWidget::on_listPreview_doubleClicked(QModelIndex index)
{
sendToProjector(preview_song, index.row());
}
void SongWidget::updateSongbooks()
{
emit setWaitCursor();
// Reload the songbook and reselect the one that used to be selected
// if it's still available, otherwise show all songbooks
QString current_songbook = ui->songbook_menu->currentText();
QString item0 = ui->songbook_menu->itemText(0);
ui->songbook_menu->clear();
loadSongbooks();
int new_index = ui->songbook_menu->findText(current_songbook);
if( new_index == -1 )
new_index = 0; // All songbooks
ui->songbook_menu->setCurrentIndex(new_index);
emit setArrowCursor();
}
void SongWidget::updateSongFromDatabase(int songid, int initial_sid)
{
songs_model->updateSongFromDatabase(songid, initial_sid);
// Update in allSongs list
for(int i(0);i<allSongs.count();++i)
{
if(allSongs.at(i).songID == songid)
{
Song s;
s = allSongs.at(i);
s.readData();
allSongs.removeAt(i);
allSongs.append(s);
break;
}
}
// Updated playlist song if song was edited comes from playlist table
if (playlistSongWasEdited)
{
playlist_model->updateSongFromDatabase(songid, initial_sid);
emit sendPlaylistChanged(true);
}
// Update the preview table:
sendToPreview( currentSong() );
}
void SongWidget::deleteSong()
{
song_database.deleteSong(currentSong().songID);
int row = ui->songs_view->currentIndex().row();
proxy_model->removeRow(row);
}
void SongWidget::addNewSong(Song song, int initial_sid)
{
songs_model->addSong(song);
ui->songs_view->selectRow(songs_model->rowCount()-1);
// Updated playlist song if song was edited comes from playlist table
if (playlistSongWasEdited)
{
playlist_model->updateSongFromDatabase(song.songID,initial_sid);
emit sendPlaylistChanged(true);
}
sendToPreview(song);
}
void SongWidget::filterModeChanged()
{
// Re-apply the filter:
QString new_text = ui->lineEditSearch->text();
on_lineEditSearch_textEdited(new_text);
}
void SongWidget::on_contains_rbutton_clicked()
{
filterModeChanged();
}
void SongWidget::on_begins_rbutton_clicked()
{
filterModeChanged();
}
void SongWidget::on_exact_match_rbutton_clicked()
{
filterModeChanged();
}
QByteArray SongWidget::getSplitterState()
{
return ui->splitter->saveState();
}
void SongWidget::setSplitterState(QByteArray& state)
{
ui->splitter->restoreState(state);
}
void SongWidget::retranslateUis()
{
ui->songbook_menu->setItemText(0,tr("All songbooks"));
loadCategories(true);
}
bool SongWidget::isSongSelected()
{
return preview_song.isValid();
}
void SongWidget::loadCategories(bool ui_update)
{
EditWidget e;
// retrieve current category id
int cur_cat_id(-1);
int cur_index = ui->comboBoxCategory->currentIndex();
if(cur_index>0)
cur_cat_id = cat_ids.at(cur_index-1);
// get categories
QStringList cat_list;
cat_list = e.categories();
// create sorting by name and refrance categories id
QMap<QString,int> cmap;
for(int i(0); i< cat_list.count(); ++i)
cmap.insert(cat_list.at(i),i);
cat_ids.clear();
cat_list.clear();
cat_ids.append(cmap.values());
cat_list.append(cmap.keys());
if(ui_update) // update categories to retranslate
{
ui->comboBoxCategory->setItemText(0,tr("All song categories"));
for(int i(1); i <= ui->comboBoxCategory->count()-1;++i)
{
ui->comboBoxCategory->setItemText(i,cat_list.at(i-1));
}
// reset to selected category
if(cur_cat_id==-1)
cur_index=0;
else
cur_index= cat_ids.indexOf(cur_cat_id)+1;
ui->comboBoxCategory->setCurrentIndex(cur_index);
}
else if(!ui_update) // initialize categories
{
ui->comboBoxCategory->addItem(tr("All song categories"));
ui->comboBoxCategory->addItems(cat_list);
}
}
void SongWidget::on_comboBoxCategory_currentIndexChanged(int index)
{
if(index!=-1)
{
songs_model->emitLayoutAboutToBeChanged(); //prepeare to chage layout
if(index==0)
proxy_model->setCategoryFilter(index-1);
else
proxy_model->setCategoryFilter(cat_ids.at(index-1));
songs_model->emitLayoutChanged();
}
}
QList<Song> SongWidget::getPlaylistSongs()
{
QList<Song> playlist_songs;
for(int i(0); i<playlist_model->rowCount(); ++i)
{
playlist_songs.append(playlist_model->getSong(i));
}
return playlist_songs;
}
void SongWidget::loadPlaylistFromFile(QList<Song> songs)
{
if(playlist_model->rowCount()>0)
playlist_model->removeRows(0,playlist_model->rowCount());
int count = songs.count();
if(count>0)
{
for(int i(0); i<count; ++i)
playlist_model->addSong(songs.at(i));
}
updateButtonStates();
}
void SongWidget::on_pushButtonSearch_clicked()
{
QString search_text = ui->lineEditSearch->text();
search_text.replace(QRegExp(QString::fromUtf8("[+:;\\]\\[{}().,?/\\\"!'—*-]"))," ");
search_text = search_text.simplified();
QList<Song> search_results;
int type = ui->comboBoxSearchType->currentIndex();
// Make sure that there is some text to do a search for, if non, then return
if(search_text.count()<1)
{
ui->lineEditSearch->setPlaceholderText(tr("Please enter search text"));
return;
}
// set filter
QRegExp rx;
rx.setCaseSensitivity(Qt::CaseInsensitive);
search_text.replace(" ","\\W*");
if(type == 1 )
// Search whole word exsact phrase only
rx.setPattern("\\b"+search_text+"\\b");
else if(type == 2) // contains all words
// Search begining of every line
rx.setPattern("@%"+search_text);
else if(type == 3 || type == 4) // line begins
{
// Search for any of the search words
search_text.replace("\\W*","|");
rx.setPattern("\\b("+search_text+")\\b");
}
else
// Search text phrase
rx.setPattern(search_text);
// perform search
for(int i(0);i<allSongs.count();++i)
{
if(type == 4)
{
QStringList stl = search_text.split("|");
bool hasAll = false;
for(int j(0);j<stl.count();++j)
{
hasAll = allSongs.at(i).songText.contains(QRegExp("\\b"+stl.at(j)+"\\b",Qt::CaseInsensitive));
if(!hasAll)
break;
}
if(hasAll)
search_results.append(allSongs.at(i));
}
else
{
if(allSongs.at(i).songText.contains(rx))
search_results.append(allSongs.at(i));
}
}
// Hide song filter items and show search items
//ui->pushButtonSearch->setText(tr("Search"));
ui->begins_rbutton->setVisible(false);
ui->contains_rbutton->setVisible(false);
ui->exact_match_rbutton->setVisible(false);
ui->labelSearchType->setVisible(true);
ui->comboBoxSearchType->setVisible(true);
ui->pushButtonClearResults->setVisible(true);
// set new songs_model with search relusts
songs_model->setSongs(search_results);
// setup higligher
ui->listPreview->setItemDelegate(highlight);
if(type == 2)
highlight->highlighter->setHighlightText(search_text);
else
highlight->highlighter->setHighlightText(rx.pattern());
// reset filter on song table to show all results
songs_model->emitLayoutAboutToBeChanged(); // prepares view to be redrawn
proxy_model->setFilterString("", false, false);
songs_model->emitLayoutChanged(); // forces the view to redraw
ui->songs_view->selectRow(0);
ui->songs_view->scrollToTop();
int row = proxy_model->mapToSource(ui->songs_view->currentIndex()).row();
if( row>=0)
{
sendToPreview(songs_model->getSong(row));
}
else
{
Song s;
sendToPreview(s);
}
updateButtonStates();
}
void SongWidget::on_pushButtonClearResults_clicked()
{
// try to reset highliting settings on preview list
ui->listPreview->setItemDelegate(ui->listWidgetDummy->itemDelegate());
ui->lineEditSearch->setPlaceholderText("");
// Hide song filter items and show search items
//ui->pushButtonSearch->setText("");
ui->begins_rbutton->setVisible(true);
ui->contains_rbutton->setVisible(true);
ui->exact_match_rbutton->setVisible(true);
ui->comboBoxSearchType->setVisible(false);
ui->pushButtonClearResults->setVisible(false);
ui->labelSearchType->setVisible(false);
songs_model->setSongs(allSongs);
ui->lineEditSearch->clear();
Song s;
sendToPreview(s);
// ui->listPreview->clear();
updateButtonStates();
}