• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Kate

kateargumenthintmodel.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2007 David Nolden <david.nolden.kdevelop@art-master.de>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "kateargumenthintmodel.h"
00020 
00021 #include <QTextFormat>
00022 #include <QGridLayout>
00023 #include <kapplication.h>
00024 
00025 #include <ktexteditor/codecompletionmodel.h>
00026 #include "katecompletionwidget.h"
00027 #include "kateargumenthinttree.h"
00028 #include "katecompletiontree.h"
00029 
00030 using namespace KTextEditor;
00031 
00032 void KateArgumentHintModel::clear() {
00033   m_rows.clear();
00034   clearExpanding();
00035 }
00036 
00037 QModelIndex KateArgumentHintModel::mapToSource( const QModelIndex & index ) const {
00038   if( index.row() <  0 || index.row() >= m_rows.count() )
00039     return QModelIndex();
00040     
00041   if( m_rows[index.row()] <  0 || m_rows[index.row()] >= group()->filtered.count() )
00042     return QModelIndex();
00043   
00044   KateCompletionModel::ModelRow source = group()->filtered[m_rows[index.row()]].sourceRow();
00045   if( !source.first ) {
00046     kDebug( 13035 ) << "KateArgumentHintModel::data: Row does not exist in source";
00047     return QModelIndex();
00048   }
00049 
00050   QModelIndex  sourceIndex = source.second.sibling(source.second.row(), index.column());
00051   
00052   return sourceIndex;
00053 }
00054 
00055 void KateArgumentHintModel::parentModelReset() {
00056   clear();
00057   buildRows();
00058 }
00059 
00060 void KateArgumentHintModel::buildRows() {
00061   m_rows.clear();
00062   QMap<int, QList<int> > m_depths; //Map each hint-depth to a list of functions of that depth
00063   for( int a = 0; a < group()->filtered.count(); a++ ) {
00064     KateCompletionModel::ModelRow source = group()->filtered[a].sourceRow();
00065     QModelIndex  sourceIndex = source.second.sibling(source.second.row(), 0);
00066     QVariant v = sourceIndex.data(CodeCompletionModel::ArgumentHintDepth);
00067     if( v.type() == QVariant::Int ) {
00068       QList<int>& lst( m_depths[v.toInt()] );
00069       lst << a;
00070     }
00071   }
00072 
00073   for( QMap<int, QList<int> >::const_iterator it = m_depths.constBegin(); it != m_depths.constEnd(); ++it ) {
00074     foreach( int row, *it )
00075       m_rows.push_front(row);//Insert filtered in reversed order
00076     m_rows.push_front( -it.key() );
00077   }
00078   
00079   reset();
00080   emit contentStateChanged(!m_rows.isEmpty());
00081 }
00082 
00083 KateArgumentHintModel::KateArgumentHintModel( KateCompletionWidget* parent ) : ExpandingWidgetModel(parent), m_parent(parent) {
00084   connect(parent->model(), SIGNAL(modelReset()), this, SLOT(parentModelReset()));
00085   connect(parent->model(), SIGNAL(argumentHintsChanged()), this, SLOT(parentModelReset()));
00086 }
00087 
00088 QVariant KateArgumentHintModel::data ( const QModelIndex & index, int role ) const {
00089   if( index.row() <  0 || index.row() >= m_rows.count() ) {
00090     //kDebug( 13035 ) << "KateArgumentHintModel::data: index out of bound: " << index.row() << " total filtered: " << m_rows.count();
00091     return QVariant();
00092   }
00093 
00094   if( m_rows[index.row()] < 0 ) {
00095     //Show labels
00096     if( role == Qt::DisplayRole && index.column() == 0 ) {
00097       return QString(); //QString("Depth %1").arg(-m_rows[index.row()]);
00098     } else if( role == Qt::BackgroundRole ) {
00099       return KApplication::kApplication()->palette().toolTipBase().color();
00100     }else if( role == Qt::ForegroundRole ) {
00101       return KApplication::kApplication()->palette().toolTipText().color();
00102     }else{
00103       return QVariant();
00104     }
00105   }
00106 
00107   if( m_rows[index.row()] <  0 || m_rows[index.row()] >= group()->filtered.count() ) {
00108     kDebug( 13035 ) << "KateArgumentHintModel::data: index out of bound: " << m_rows[index.row()] << " total filtered: " << group()->filtered.count();
00109     return QVariant();
00110   }
00111   
00112   KateCompletionModel::ModelRow source = group()->filtered[m_rows[index.row()]].sourceRow();
00113   if( !source.first ) {
00114     kDebug( 13035 ) << "KateArgumentHintModel::data: Row does not exist in source";
00115     return QVariant();
00116   }
00117 
00118   if( index.column() == 0 ) {
00119     switch( role ) {
00120       case Qt::DecorationRole:
00121       {
00122         //Show the expand-handle
00123         model()->cacheIcons();
00124 
00125         if( !isExpanded(index ) )
00126           return QVariant( model()->m_collapsedIcon );
00127         else
00128           return QVariant( model()->m_expandedIcon );
00129       }
00130       case Qt::DisplayRole:
00131         //Ignore text in the first column(we create our own compound text in the second)
00132         return QVariant();
00133     }
00134   }
00135 
00136   QModelIndex  sourceIndex = source.second.sibling(source.second.row(), index.column());
00137  
00138   if( !sourceIndex.isValid() ) {
00139     kDebug( 13035 ) << "KateArgumentHintModel::data: Source-index is not valid";
00140     return QVariant();
00141   }
00142 
00143   switch( role ) {
00144     case Qt::DisplayRole:
00145     {
00146       //Construct the text
00147       QString totalText;
00148       for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ )
00149         if( a != CodeCompletionModel::Scope ) //Skip the scope
00150           totalText += source.second.sibling(source.second.row(), a).data(Qt::DisplayRole).toString() + ' ';
00151     
00152       
00153       return QVariant(totalText);
00154     }
00155     case CodeCompletionModel::HighlightingMethod:
00156     {
00157       //Return that we are doing custom-highlighting of one of the sub-strings does it
00158       for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ ) {
00159           QVariant method = source.second.sibling(source.second.row(), a).data(CodeCompletionModel::HighlightingMethod);
00160           if( method.type() == QVariant::Int && method.toInt() ==  CodeCompletionModel::CustomHighlighting)
00161             return QVariant(CodeCompletionModel::CustomHighlighting);
00162       }
00163     
00164       return QVariant();
00165     }
00166     case CodeCompletionModel::CustomHighlight:
00167     {
00168       QStringList strings;
00169       
00170       //Collect strings
00171       for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ )
00172           strings << source.second.sibling(source.second.row(), a).data(Qt::DisplayRole).toString();
00173 
00174       QList<QVariantList> highlights;
00175 
00176       //Collect custom-highlightings
00177       for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ )
00178           highlights << source.second.sibling(source.second.row(), a).data(CodeCompletionModel::CustomHighlight).toList();
00179 
00180       //Replace invalid QTextFormats with match-quality color or yellow.
00181       for( QList<QVariantList>::iterator it = highlights.begin(); it != highlights.end(); ++it )
00182       {
00183         QVariantList& list( *it );
00184         
00185         for( int a = 2; a < list.count(); a += 3 )
00186         {
00187           if( list[a].canConvert<QTextFormat>() )
00188           {
00189             QTextFormat f = list[a].value<QTextFormat>();
00190             
00191             if(!f.isValid())
00192             {
00193               f = QTextFormat( QTextFormat::CharFormat );
00194               uint color = matchColor( index );
00195 
00196               if( color )
00197                 f.setBackground( QBrush(color) );
00198               else
00199                 f.setBackground( Qt::yellow );
00200               
00201               list[a] = QVariant( f );
00202             }
00203           }
00204         }
00205       }
00206       
00207       
00208       return mergeCustomHighlighting( strings, highlights, 1 );
00209     }
00210     case Qt::DecorationRole:
00211     {
00212       //Redirect the decoration to the decoration of the item-column
00213       return source.second.sibling(source.second.row(), CodeCompletionModel::Icon).data(role);
00214     }
00215   }
00216   
00217   QVariant v = ExpandingWidgetModel::data( index, role );
00218   if( v.isValid() )
00219     return v;
00220   else
00221     return sourceIndex.data( role );
00222 }
00223 
00224 int KateArgumentHintModel::rowCount ( const QModelIndex & parent ) const {
00225   if( !parent.isValid() )
00226     return m_rows.count();
00227   else
00228     return 0;
00229 }
00230 
00231 int KateArgumentHintModel::columnCount ( const QModelIndex & /*parent*/ ) const {
00232   return 2; //2 Columns, one for the expand-handle, one for the signature
00233 }
00234 
00235 KateCompletionModel::Group* KateArgumentHintModel::group() const {
00236   return model()->m_argumentHints;
00237 }
00238 
00239 KateCompletionModel* KateArgumentHintModel::model() const {
00240   return m_parent->model();
00241 }
00242 
00243 QTreeView* KateArgumentHintModel::treeView() const {
00244   return m_parent->argumentHintTree();
00245 }
00246 
00247 void KateArgumentHintModel::emitDataChanged( const QModelIndex& start, const QModelIndex& end ) {
00248   emit dataChanged(start, end);
00249 }
00250 
00251 bool KateArgumentHintModel::indexIsItem(const QModelIndex& index) const {
00252   return index.row() >= 0 && index.row() < m_rows.count() && m_rows[index.row()] >= 0;
00253 }
00254 
00255 int KateArgumentHintModel::contextMatchQuality(const QModelIndex& index) const {
00256   int row=index.row();
00257   if( row <  0 || row >= m_rows.count() )
00258     return -1;
00259 
00260   if( m_rows[row] <  0 || m_rows[row] >= group()->filtered.count() )
00261     return -1; //Probably a label
00262   
00263   KateCompletionModel::ModelRow source = group()->filtered[m_rows[row]].sourceRow();
00264   if( !source.first )
00265     return -1;
00266   
00267    QModelIndex  sourceIndex = source.second.sibling(source.second.row(), 0);
00268  
00269   if( !sourceIndex.isValid() )
00270     return -1;
00271 
00272   int depth = sourceIndex.data(CodeCompletionModel::ArgumentHintDepth).toInt();
00273 
00274   switch(depth) {
00275     case 1:
00276     {
00277       //This argument-hint is on the lowest level, match it with the currently selected item in the completion-widget
00278       QModelIndex row = m_parent->treeView()->currentIndex();
00279       if( !row.isValid() )
00280         return -1;
00281 
00282       QModelIndex selectedIndex = m_parent->model()->mapToSource( row );
00283       if( !selectedIndex.isValid() )
00284         return -1;
00285 
00286       if( selectedIndex.model() != sourceIndex.model() )
00287         return -1; //We can only match items from the same source-model
00288 
00289       sourceIndex.data( CodeCompletionModel::SetMatchContext );
00290 
00291       QVariant v = selectedIndex.data( CodeCompletionModel::MatchQuality );
00292       if( v.type() == QVariant::Int ) 
00293         return v.toInt();
00294     }
00295     break;
00296     default:
00297       //Do some other nice matching here in future
00298       break;
00299   }
00300 
00301   return -1;
00302 }
00303 
00304 #include "kateargumenthintmodel.moc"

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal