File indexing completed on 2024-05-19 04:48:45
0001 /**************************************************************************************** 0002 * Copyright (c) 2012 Matěj Laitl <matej@laitl.cz> * 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 "ExcludedLabelsDialog.h" 0018 0019 #include "core/meta/Meta.h" 0020 #include "core/collections/QueryMaker.h" 0021 #include "core-impl/collections/support/CollectionManager.h" 0022 #include "statsyncing/Config.h" 0023 0024 #include <KConfigGroup> 0025 #include <KLocalizedString> 0026 0027 #include <QDialogButtonBox> 0028 #include <QGridLayout> 0029 #include <QLabel> 0030 #include <QLineEdit> 0031 #include <QListWidget> 0032 #include <QPushButton> 0033 #include <QToolButton> 0034 #include <QVBoxLayout> 0035 0036 ExcludedLabelsDialog::ExcludedLabelsDialog( StatSyncing::Config *config, QWidget *parent, 0037 Qt::WindowFlags flags ) 0038 : QDialog( parent, flags ) 0039 , m_statSyncingConfig( config ) 0040 { 0041 Q_ASSERT( m_statSyncingConfig ); 0042 QWidget *mainWidget = new QWidget(this); 0043 QVBoxLayout *mainLayout = new QVBoxLayout(this); 0044 mainLayout->addWidget(mainWidget); 0045 setupUi(mainWidget); 0046 0047 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, this); 0048 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0049 okButton->setDefault(true); 0050 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0051 connect(buttonBox, &QDialogButtonBox::accepted, this, &ExcludedLabelsDialog::accept); 0052 connect(buttonBox, &QDialogButtonBox::rejected, this, &ExcludedLabelsDialog::reject); 0053 setWindowTitle( i18n( "Excluded Labels" ) ); 0054 mainLayout->addWidget(buttonBox); 0055 0056 addLabels( config->excludedLabels(), true ); 0057 Collections::QueryMaker *qm = CollectionManager::instance()->queryMaker(); 0058 qm->setQueryType( Collections::QueryMaker::Label ); 0059 qm->setAutoDelete( true ); 0060 connect( qm, &Collections::QueryMaker::newLabelsReady, 0061 this, &ExcludedLabelsDialog::slowNewResultReady ); 0062 qm->run(); 0063 0064 connect( addButton, &QAbstractButton::clicked, this, &ExcludedLabelsDialog::slotAddExcludedLabel ); 0065 connect( addLabelLine, &QLineEdit::returnPressed, this, &ExcludedLabelsDialog::slotAddExcludedLabel ); 0066 connect( okButton, &QAbstractButton::clicked, this, &ExcludedLabelsDialog::slotSaveToConfig ); 0067 } 0068 0069 void 0070 ExcludedLabelsDialog::slowNewResultReady( const Meta::LabelList &labels ) 0071 { 0072 foreach( const Meta::LabelPtr &label, labels ) 0073 addLabel( label->name() ); 0074 } 0075 0076 void 0077 ExcludedLabelsDialog::slotAddExcludedLabel() 0078 { 0079 addLabel( addLabelLine->text(), true ); 0080 addLabelLine->setText( QString() ); 0081 } 0082 0083 void 0084 ExcludedLabelsDialog::slotSaveToConfig() 0085 { 0086 QSet<QString> excluded; 0087 foreach( const QListWidgetItem *item, listWidget->selectedItems() ) 0088 { 0089 excluded.insert( item->text() ); 0090 } 0091 m_statSyncingConfig->setExcludedLabels( excluded ); 0092 } 0093 0094 void 0095 ExcludedLabelsDialog::addLabel( const QString &label, bool selected ) 0096 { 0097 int count = listWidget->count(); 0098 for( int i = 0; i <= count; i++ ) 0099 { 0100 QModelIndex idx; 0101 if( i == count ) 0102 { 0103 // reached end of the list 0104 listWidget->addItem( label ); 0105 idx = listWidget->model()->index( i, 0 ); 0106 } 0107 else if( listWidget->item( i )->text() == label ) 0108 { 0109 // already in list 0110 return; 0111 } 0112 else if( QString::localeAwareCompare( listWidget->item( i )->text(), label ) > 0 ) 0113 { 0114 listWidget->insertItem( i, label ); 0115 idx = listWidget->model()->index( i, 0 ); 0116 } 0117 // else continue to next iteration 0118 0119 // just inserted 0120 if( idx.isValid() && selected ) 0121 listWidget->selectionModel()->select( idx, QItemSelectionModel::Select ); 0122 if( idx.isValid() ) 0123 return; 0124 } 0125 } 0126 0127 void 0128 ExcludedLabelsDialog::addLabels( const QSet<QString> &labels, bool selected ) 0129 { 0130 foreach( const QString &label, labels ) 0131 { 0132 addLabel( label, selected ); 0133 } 0134 }