File indexing completed on 2024-05-19 04:48:46

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Peter ZHOU <peterzhoulei@gmail.com>                               *
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 "ScriptSelector.h"
0018 
0019 #include "core/support/Debug.h"
0020 
0021 #include <KCategorizedView>
0022 #include <KLocalizedString>
0023 #include <QLineEdit>
0024 #include <KPluginInfo>
0025 #include <QScrollBar>
0026 
0027 #include <algorithm>
0028 
0029 // uber-hacky, this whole thing, make our own script selector?
0030 ScriptSelector::ScriptSelector( QWidget * parent )
0031     : KPluginSelector( parent )
0032     , m_scriptCount( 0 )
0033 {
0034     m_lineEdit = this->findChild<QLineEdit*>();
0035     if( m_lineEdit )
0036     {
0037         m_lineEdit->setPlaceholderText( i18n( "Search Scripts" ) );
0038         connect( m_lineEdit, &QLineEdit::textChanged, this, &ScriptSelector::slotFiltered );
0039     }
0040 
0041     m_listView = this->findChild<KCategorizedView*>();
0042 }
0043 
0044 ScriptSelector::~ScriptSelector()
0045 {}
0046 
0047 void
0048 ScriptSelector::setVerticalPosition( int position )
0049 {
0050     m_listView->verticalScrollBar()->setSliderPosition( position );
0051 }
0052 
0053 int
0054 ScriptSelector::verticalPosition()
0055 {
0056     return m_listView->verticalScrollBar()->sliderPosition();
0057 }
0058 
0059 QString
0060 ScriptSelector::filter()
0061 {
0062     return m_lineEdit->text();
0063 }
0064 
0065 void
0066 ScriptSelector::setFilter( const QString &filter )
0067 {
0068     m_lineEdit->setText( filter );
0069 }
0070 
0071 void
0072 ScriptSelector::addScripts( QList<KPluginInfo> pluginInfoList,
0073                             PluginLoadMethod pluginLoadMethod,
0074                             const QString &categoryName,
0075                             const QString &categoryKey,
0076                             const KSharedConfig::Ptr &config )
0077 {
0078     DEBUG_BLOCK
0079 
0080     std::sort( pluginInfoList.begin(), pluginInfoList.end()
0081          , []( const KPluginInfo &left, const KPluginInfo &right ){ return left.name() < right.name(); } );
0082     addPlugins( pluginInfoList, pluginLoadMethod, categoryName, categoryKey, config );
0083     foreach( const KPluginInfo &plugin, pluginInfoList )
0084     {
0085         m_scriptCount++;
0086         m_scripts[m_scriptCount] = plugin.pluginName();
0087     }
0088 }
0089 
0090 QString
0091 ScriptSelector::currentItem() const
0092 {
0093     DEBUG_BLOCK
0094 
0095     QItemSelectionModel *selModel = m_listView->selectionModel();
0096     const QModelIndexList selIndexes = selModel->selectedIndexes();
0097 
0098     if( !selIndexes.isEmpty() )
0099     {
0100         QModelIndex currentIndex = selIndexes[0];
0101         if( currentIndex.isValid() )
0102         {
0103             debug() << "row: " << currentIndex.row() + 1; //the index start from 1
0104             debug() << "name: "<< m_scripts[currentIndex.row() + 1];
0105             return m_scripts[currentIndex.row() + 1];
0106         }
0107     }
0108 
0109     return QString();
0110 }
0111 
0112 void
0113 ScriptSelector::slotFiltered( const QString &filter )
0114 {
0115     if( filter.isEmpty() )
0116         Q_EMIT filtered( false );
0117     else
0118         Q_EMIT filtered( true );
0119 }
0120