• Skip to content
  • Skip to link menu
KDE 4.1 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 void KateArgumentHintModel::parentModelReset() {
00038   clear();
00039   buildRows();
00040 }
00041 
00042 void KateArgumentHintModel::buildRows() {
00043   m_rows.clear();
00044   QMap<int, QList<int> > m_depths; //Map each hint-depth to a list of functions of that depth
00045   for( int a = 0; a < group()->rows.count(); a++ ) {
00046     KateCompletionModel::ModelRow source = group()->rows[a];
00047     QModelIndex  sourceIndex = source.second.sibling(source.second.row(), 0);
00048     QVariant v = sourceIndex.data(CodeCompletionModel::ArgumentHintDepth);
00049     if( v.type() == QVariant::Int ) {
00050       QList<int>& lst( m_depths[v.toInt()] );
00051       lst << a;
00052     }
00053   }
00054 
00055   for( QMap<int, QList<int> >::const_iterator it = m_depths.begin(); it != m_depths.end(); ++it ) {
00056     foreach( int row, *it )
00057       m_rows.push_front(row);//Insert rows in reversed order
00058     m_rows.push_front( -it.key() );
00059   }
00060   
00061   reset();
00062   emit contentStateChanged(!m_rows.isEmpty());
00063 }
00064 
00065 KateArgumentHintModel::KateArgumentHintModel( KateCompletionWidget* parent ) : ExpandingWidgetModel(parent), m_parent(parent) {
00066   connect(parent->model(), SIGNAL(modelReset()), this, SLOT(parentModelReset()));
00067   connect(parent->model(), SIGNAL(argumentHintsChanged()), this, SLOT(parentModelReset()));
00068 }
00069 
00070 QVariant KateArgumentHintModel::data ( const QModelIndex & index, int role ) const {
00071   if( index.row() <  0 || index.row() >= m_rows.count() ) {
00072     //kDebug( 13035 ) << "KateArgumentHintModel::data: index out of bound: " << index.row() << " total rows: " << m_rows.count();
00073     return QVariant();
00074   }
00075 
00076   if( m_rows[index.row()] < 0 ) {
00077     //Show labels
00078     if( role == Qt::DisplayRole && index.column() == 0 ) {
00079       return QString("Depth %1").arg(-m_rows[index.row()]);
00080     } else if( role == Qt::BackgroundRole ) {
00081       return KApplication::kApplication()->palette().toolTipBase().color();
00082     }else if( role == Qt::ForegroundRole ) {
00083       return KApplication::kApplication()->palette().toolTipText().color();
00084     }else{
00085       return QVariant();
00086     }
00087   }
00088 
00089   if( m_rows[index.row()] <  0 || m_rows[index.row()] >= group()->rows.count() ) {
00090     kDebug( 13035 ) << "KateArgumentHintModel::data: index out of bound: " << m_rows[index.row()] << " total rows: " << group()->rows.count();
00091     return QVariant();
00092   }
00093   
00094   KateCompletionModel::ModelRow source = group()->rows[m_rows[index.row()]];
00095   if( !source.first ) {
00096     kDebug( 13035 ) << "KateArgumentHintModel::data: Row does not exist in source";
00097     return QVariant();
00098   }
00099 
00100   if( index.column() == 0 ) {
00101     switch( role ) {
00102       case Qt::DecorationRole:
00103       {
00104         //Show the expand-handle
00105         model()->cacheIcons();
00106 
00107         if( !isExpanded(index ) )
00108           return QVariant( model()->m_collapsedIcon );
00109         else
00110           return QVariant( model()->m_expandedIcon );
00111       }
00112       case Qt::DisplayRole:
00113         //Ignore text in the first column(we create our own compound text in the second)
00114         return QVariant();
00115     }
00116   }
00117 
00118   QModelIndex  sourceIndex = source.second.sibling(source.second.row(), index.column());
00119  
00120   if( !sourceIndex.isValid() ) {
00121     kDebug( 13035 ) << "KateArgumentHintModel::data: Source-index is not valid";
00122     return QVariant();
00123   }
00124 
00125   switch( role ) {
00126     case Qt::DisplayRole:
00127     {
00128       //Construct the text
00129       QString totalText;
00130       for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ )
00131         if( a != CodeCompletionModel::Scope ) //Skip the scope
00132           totalText += source.second.sibling(source.second.row(), a).data(Qt::DisplayRole).toString() + ' ';
00133     
00134       
00135       return QVariant(totalText);
00136     }
00137     case CodeCompletionModel::HighlightingMethod:
00138     {
00139       //Return that we are doing custom-highlighting of one of the sub-strings does it
00140       for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ ) {
00141           QVariant method = source.second.sibling(source.second.row(), a).data(CodeCompletionModel::HighlightingMethod);
00142           if( method.type() == QVariant::Int && method.toInt() ==  CodeCompletionModel::CustomHighlighting)
00143             return QVariant(CodeCompletionModel::CustomHighlighting);
00144       }
00145     
00146       return QVariant();
00147     }
00148     case CodeCompletionModel::CustomHighlight:
00149     {
00150       QStringList strings;
00151       
00152       //Collect strings
00153       for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ )
00154           strings << source.second.sibling(source.second.row(), a).data(Qt::DisplayRole).toString();
00155 
00156       QList<QVariantList> highlights;
00157 
00158       //Collect custom-highlightings
00159       for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ )
00160           highlights << source.second.sibling(source.second.row(), a).data(CodeCompletionModel::CustomHighlight).toList();
00161 
00162       //Replace invalid QTextFormats with match-quality color or yellow.
00163       for( QList<QVariantList>::iterator it = highlights.begin(); it != highlights.end(); ++it )
00164       {
00165         QVariantList& list( *it );
00166         
00167         for( int a = 2; a < list.count(); a += 3 )
00168         {
00169           if( list[a].canConvert<QTextFormat>() )
00170           {
00171             QTextFormat f = list[a].value<QTextFormat>();
00172             
00173             if(!f.isValid())
00174             {
00175               f = QTextFormat( QTextFormat::CharFormat );
00176               uint color = matchColor( index );
00177 
00178               if( color )
00179                 f.setBackground( QBrush(color) );
00180               else
00181                 f.setBackground( Qt::yellow );
00182               
00183               list[a] = QVariant( f );
00184             }
00185           }
00186         }
00187       }
00188       
00189       
00190       return mergeCustomHighlighting( strings, highlights, 1 );
00191     }
00192     case Qt::DecorationRole:
00193     {
00194       //Redirect the decoration to the decoration of the item-column
00195       return source.second.sibling(source.second.row(), CodeCompletionModel::Icon).data(role);
00196     }
00197   }
00198   
00199   QVariant v = ExpandingWidgetModel::data( index, role );
00200   if( v.isValid() )
00201     return v;
00202   else
00203     return sourceIndex.data( role );
00204 }
00205 
00206 int KateArgumentHintModel::rowCount ( const QModelIndex & parent ) const {
00207   if( !parent.isValid() )
00208     return m_rows.count();
00209   else
00210     return 0;
00211 }
00212 
00213 int KateArgumentHintModel::columnCount ( const QModelIndex & /*parent*/ ) const {
00214   return 2; //2 Columns, one for the expand-handle, one for the signature
00215 }
00216 
00217 KateCompletionModel::Group* KateArgumentHintModel::group() const {
00218   return model()->m_argumentHints;
00219 }
00220 
00221 KateCompletionModel* KateArgumentHintModel::model() const {
00222   return m_parent->model();
00223 }
00224 
00225 QTreeView* KateArgumentHintModel::treeView() const {
00226   return m_parent->argumentHintTree();
00227 }
00228 
00229 void KateArgumentHintModel::emitDataChanged( const QModelIndex& start, const QModelIndex& end ) {
00230   emit dataChanged(start, end);
00231 }
00232 
00233 bool KateArgumentHintModel::indexIsItem(const QModelIndex& index) const {
00234   return index.row() >= 0 && index.row() < m_rows.count() && m_rows[index.row()] >= 0;
00235 }
00236 
00237 int KateArgumentHintModel::contextMatchQuality(const QModelIndex& index) const {
00238   int row=index.row();
00239   if( row <  0 || row >= m_rows.count() )
00240     return -1;
00241 
00242   if( m_rows[row] <  0 || m_rows[row] >= group()->rows.count() )
00243     return -1; //Probably a label
00244   
00245   KateCompletionModel::ModelRow source = group()->rows[m_rows[row]];
00246   if( !source.first )
00247     return -1;
00248   
00249    QModelIndex  sourceIndex = source.second.sibling(source.second.row(), 0);
00250  
00251   if( !sourceIndex.isValid() )
00252     return -1;
00253 
00254   int depth = sourceIndex.data(CodeCompletionModel::ArgumentHintDepth).toInt();
00255 
00256   switch(depth) {
00257     case 1:
00258     {
00259       //This argument-hint is on the lowest level, match it with the currently selected item in the completion-widget
00260       QModelIndex row = m_parent->treeView()->currentIndex();
00261       if( !row.isValid() )
00262         return -1;
00263 
00264       QModelIndex selectedIndex = m_parent->model()->mapToSource( row );
00265       if( !selectedIndex.isValid() )
00266         return -1;
00267 
00268       if( selectedIndex.model() != sourceIndex.model() )
00269         return -1; //We can only match items from the same source-model
00270 
00271       sourceIndex.data( CodeCompletionModel::SetMatchContext );
00272 
00273       QVariant v = selectedIndex.data( CodeCompletionModel::MatchQuality );
00274       if( v.type() == QVariant::Int ) 
00275         return v.toInt();
00276     }
00277     break;
00278     default:
00279       //Do some other nice matching here in future
00280       break;
00281   }
00282 
00283   return -1;
00284 }
00285 
00286 #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
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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