Kate
kateargumenthintmodel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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;
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);
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
00091 return QVariant();
00092 }
00093
00094 if( m_rows[index.row()] < 0 ) {
00095
00096 if( role == Qt::DisplayRole && index.column() == 0 ) {
00097 return 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
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
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
00147 QString totalText;
00148 for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ )
00149 if( a != CodeCompletionModel::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
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
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
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
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
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 & ) const {
00232 return 2;
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;
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
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;
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
00298 break;
00299 }
00300
00301 return -1;
00302 }
00303
00304 #include "kateargumenthintmodel.moc"