• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • completion
kateargumenthintmodel.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2007 David Nolden <david.nolden.kdevelop@art-master.de>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kateargumenthintmodel.h"
22 
23 #include <QTextFormat>
24 #include <QGridLayout>
25 #include <kapplication.h>
26 
27 #include <ktexteditor/codecompletionmodel.h>
28 #include "katecompletionwidget.h"
29 #include "kateargumenthinttree.h"
30 #include "katecompletiontree.h"
31 
32 using namespace KTextEditor;
33 
34 void KateArgumentHintModel::clear() {
35  m_rows.clear();
36  clearExpanding();
37 }
38 
39 QModelIndex KateArgumentHintModel::mapToSource( const QModelIndex & index ) const {
40  if( index.row() < 0 || index.row() >= m_rows.count() )
41  return QModelIndex();
42 
43  if( m_rows[index.row()] < 0 || m_rows[index.row()] >= group()->filtered.count() )
44  return QModelIndex();
45 
46  KateCompletionModel::ModelRow source = group()->filtered[m_rows[index.row()]].sourceRow();
47  if( !source.first ) {
48  kDebug( 13035 ) << "KateArgumentHintModel::data: Row does not exist in source";
49  return QModelIndex();
50  }
51 
52  QModelIndex sourceIndex = source.second.sibling(source.second.row(), index.column());
53 
54  return sourceIndex;
55 }
56 
57 void KateArgumentHintModel::parentModelReset() {
58  clear();
59  buildRows();
60 }
61 
62 void KateArgumentHintModel::buildRows() {
63  m_rows.clear();
64  QMap<int, QList<int> > m_depths; //Map each hint-depth to a list of functions of that depth
65  for( int a = 0; a < group()->filtered.count(); a++ ) {
66  KateCompletionModel::ModelRow source = group()->filtered[a].sourceRow();
67  QModelIndex sourceIndex = source.second.sibling(source.second.row(), 0);
68  QVariant v = sourceIndex.data(CodeCompletionModel::ArgumentHintDepth);
69  if( v.type() == QVariant::Int ) {
70  QList<int>& lst( m_depths[v.toInt()] );
71  lst << a;
72  }
73  }
74 
75  for( QMap<int, QList<int> >::const_iterator it = m_depths.constBegin(); it != m_depths.constEnd(); ++it ) {
76  foreach( int row, *it )
77  m_rows.push_front(row);//Insert filtered in reversed order
78  m_rows.push_front( -it.key() );
79  }
80 
81  reset();
82  emit contentStateChanged(!m_rows.isEmpty());
83 }
84 
85 KateArgumentHintModel::KateArgumentHintModel( KateCompletionWidget* parent ) : ExpandingWidgetModel(parent), m_parent(parent) {
86  connect(parent->model(), SIGNAL(modelReset()), this, SLOT(parentModelReset()));
87  connect(parent->model(), SIGNAL(argumentHintsChanged()), this, SLOT(parentModelReset()));
88 }
89 
90 QVariant KateArgumentHintModel::data ( const QModelIndex & index, int role ) const {
91  if( index.row() < 0 || index.row() >= m_rows.count() ) {
92  //kDebug( 13035 ) << "KateArgumentHintModel::data: index out of bound: " << index.row() << " total filtered: " << m_rows.count();
93  return QVariant();
94  }
95 
96  if( m_rows[index.row()] < 0 ) {
97  //Show labels
98  if( role == Qt::DisplayRole && index.column() == 0 ) {
99  return QString(); //QString("Depth %1").arg(-m_rows[index.row()]);
100  } else if( role == Qt::BackgroundRole ) {
101  return KApplication::kApplication()->palette().toolTipBase().color();
102  }else if( role == Qt::ForegroundRole ) {
103  return KApplication::kApplication()->palette().toolTipText().color();
104  }else{
105  return QVariant();
106  }
107  }
108 
109  if( m_rows[index.row()] < 0 || m_rows[index.row()] >= group()->filtered.count() ) {
110  kDebug( 13035 ) << "KateArgumentHintModel::data: index out of bound: " << m_rows[index.row()] << " total filtered: " << group()->filtered.count();
111  return QVariant();
112  }
113 
114  KateCompletionModel::ModelRow source = group()->filtered[m_rows[index.row()]].sourceRow();
115  if( !source.first ) {
116  kDebug( 13035 ) << "KateArgumentHintModel::data: Row does not exist in source";
117  return QVariant();
118  }
119 
120  if( index.column() == 0 ) {
121  switch( role ) {
122  case Qt::DecorationRole:
123  {
124  //Show the expand-handle
125  model()->cacheIcons();
126 
127  if( !isExpanded(index ) )
128  return QVariant( model()->m_collapsedIcon );
129  else
130  return QVariant( model()->m_expandedIcon );
131  }
132  case Qt::DisplayRole:
133  //Ignore text in the first column(we create our own compound text in the second)
134  return QVariant();
135  }
136  }
137 
138  QModelIndex sourceIndex = source.second.sibling(source.second.row(), index.column());
139 
140  if( !sourceIndex.isValid() ) {
141  kDebug( 13035 ) << "KateArgumentHintModel::data: Source-index is not valid";
142  return QVariant();
143  }
144 
145  switch( role ) {
146  case Qt::DisplayRole:
147  {
148  //Construct the text
149  QString totalText;
150  for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ )
151  if( a != CodeCompletionModel::Scope ) //Skip the scope
152  totalText += source.second.sibling(source.second.row(), a).data(Qt::DisplayRole).toString() + ' ';
153 
154 
155  return QVariant(totalText);
156  }
157  case CodeCompletionModel::HighlightingMethod:
158  {
159  //Return that we are doing custom-highlighting of one of the sub-strings does it
160  for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ ) {
161  QVariant method = source.second.sibling(source.second.row(), a).data(CodeCompletionModel::HighlightingMethod);
162  if( method.type() == QVariant::Int && method.toInt() == CodeCompletionModel::CustomHighlighting)
163  return QVariant(CodeCompletionModel::CustomHighlighting);
164  }
165 
166  return QVariant();
167  }
168  case CodeCompletionModel::CustomHighlight:
169  {
170  QStringList strings;
171 
172  //Collect strings
173  for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ )
174  strings << source.second.sibling(source.second.row(), a).data(Qt::DisplayRole).toString();
175 
176  QList<QVariantList> highlights;
177 
178  //Collect custom-highlightings
179  for( int a = CodeCompletionModel::Prefix; a <= CodeCompletionModel::Postfix; a++ )
180  highlights << source.second.sibling(source.second.row(), a).data(CodeCompletionModel::CustomHighlight).toList();
181 
182  //Replace invalid QTextFormats with match-quality color or yellow.
183  for( QList<QVariantList>::iterator it = highlights.begin(); it != highlights.end(); ++it )
184  {
185  QVariantList& list( *it );
186 
187  for( int a = 2; a < list.count(); a += 3 )
188  {
189  if( list[a].canConvert<QTextFormat>() )
190  {
191  QTextFormat f = list[a].value<QTextFormat>();
192 
193  if(!f.isValid())
194  {
195  f = QTextFormat( QTextFormat::CharFormat );
196  uint color = matchColor( index );
197 
198  if( color )
199  f.setBackground( QBrush(color) );
200  else
201  f.setBackground( Qt::yellow );
202 
203  list[a] = QVariant( f );
204  }
205  }
206  }
207  }
208 
209 
210  return mergeCustomHighlighting( strings, highlights, 1 );
211  }
212  case Qt::DecorationRole:
213  {
214  //Redirect the decoration to the decoration of the item-column
215  return source.second.sibling(source.second.row(), CodeCompletionModel::Icon).data(role);
216  }
217  }
218 
219  QVariant v = ExpandingWidgetModel::data( index, role );
220  if( v.isValid() )
221  return v;
222  else
223  return sourceIndex.data( role );
224 }
225 
226 int KateArgumentHintModel::rowCount ( const QModelIndex & parent ) const {
227  if( !parent.isValid() )
228  return m_rows.count();
229  else
230  return 0;
231 }
232 
233 int KateArgumentHintModel::columnCount ( const QModelIndex & /*parent*/ ) const {
234  return 2; //2 Columns, one for the expand-handle, one for the signature
235 }
236 
237 KateCompletionModel::Group* KateArgumentHintModel::group() const {
238  return model()->m_argumentHints;
239 }
240 
241 KateCompletionModel* KateArgumentHintModel::model() const {
242  return m_parent->model();
243 }
244 
245 QTreeView* KateArgumentHintModel::treeView() const {
246  return m_parent->argumentHintTree();
247 }
248 
249 void KateArgumentHintModel::emitDataChanged( const QModelIndex& start, const QModelIndex& end ) {
250  emit dataChanged(start, end);
251 }
252 
253 bool KateArgumentHintModel::indexIsItem(const QModelIndex& index) const {
254  return index.row() >= 0 && index.row() < m_rows.count() && m_rows[index.row()] >= 0;
255 }
256 
257 int KateArgumentHintModel::contextMatchQuality(const QModelIndex& index) const {
258  int row=index.row();
259  if( row < 0 || row >= m_rows.count() )
260  return -1;
261 
262  if( m_rows[row] < 0 || m_rows[row] >= group()->filtered.count() )
263  return -1; //Probably a label
264 
265  KateCompletionModel::ModelRow source = group()->filtered[m_rows[row]].sourceRow();
266  if( !source.first )
267  return -1;
268 
269  QModelIndex sourceIndex = source.second.sibling(source.second.row(), 0);
270 
271  if( !sourceIndex.isValid() )
272  return -1;
273 
274  int depth = sourceIndex.data(CodeCompletionModel::ArgumentHintDepth).toInt();
275 
276  switch(depth) {
277  case 1:
278  {
279  //This argument-hint is on the lowest level, match it with the currently selected item in the completion-widget
280  QModelIndex row = m_parent->treeView()->currentIndex();
281  if( !row.isValid() )
282  return -1;
283 
284  QModelIndex selectedIndex = m_parent->model()->mapToSource( row );
285  if( !selectedIndex.isValid() )
286  return -1;
287 
288  if( selectedIndex.model() != sourceIndex.model() )
289  return -1; //We can only match items from the same source-model
290 
291  sourceIndex.data( CodeCompletionModel::SetMatchContext );
292 
293  QVariant v = selectedIndex.data( CodeCompletionModel::MatchQuality );
294  if( v.type() == QVariant::Int )
295  return v.toInt();
296  }
297  break;
298  default:
299  //Do some other nice matching here in future
300  break;
301  }
302 
303  return -1;
304 }
305 
306 #include "kateargumenthintmodel.moc"
QModelIndex
KateArgumentHintModel::columnCount
virtual int columnCount(const QModelIndex &) const
Definition: kateargumenthintmodel.cpp:233
KateArgumentHintModel::mapToSource
QModelIndex mapToSource(const QModelIndex &proxyIndex) const
Definition: kateargumenthintmodel.cpp:39
KateCompletionModel::mapToSource
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
Maps from this display-model into the appropriate source code-completion model.
Definition: katecompletionmodel.cpp:866
QVariant::toList
QList< QVariant > toList() const
katecompletiontree.h
mergeCustomHighlighting
QList< QVariant > mergeCustomHighlighting(int leftSize, const QList< QVariant > &left, int rightSize, const QList< QVariant > &right)
Definition: expandingwidgetmodel.cpp:449
QMap::constBegin
const_iterator constBegin() const
QMap
KateArgumentHintModel::KateArgumentHintModel
KateArgumentHintModel(KateCompletionWidget *parent)
Definition: kateargumenthintmodel.cpp:85
QList::push_front
void push_front(const T &value)
ExpandingWidgetModel::m_collapsedIcon
static QIcon m_collapsedIcon
Definition: expandingwidgetmodel.h:136
kateargumenthintmodel.h
QBrush
KateCompletionModel
This class has the responsibility for filtering, sorting, and manipulating code completion data provi...
Definition: katecompletionmodel.h:48
QAbstractItemModel::modelReset
void modelReset()
KateCompletionWidget::model
const KateCompletionModel * model() const
Definition: katecompletionwidget.cpp:252
KateArgumentHintModel::contextMatchQuality
virtual int contextMatchQuality(const QModelIndex &row) const
Definition: kateargumenthintmodel.cpp:257
QModelIndex::isValid
bool isValid() const
QList::count
int count(const T &value) const
KateCompletionWidget
This is the code completion's main widget, and also contains the core interface logic.
Definition: katecompletionwidget.h:55
KateCompletionWidget::argumentHintTree
KateArgumentHintTree * argumentHintTree() const
Definition: katecompletionwidget.cpp:244
ExpandingWidgetModel::data
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Does not request data from index, this only returns local data like highlighting for expanded rows an...
Definition: expandingwidgetmodel.cpp:88
QVariant::toInt
int toInt(bool *ok) const
kateargumenthinttree.h
QAbstractItemModel::dataChanged
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
KateArgumentHintModel::buildRows
void buildRows()
Definition: kateargumenthintmodel.cpp:62
KateArgumentHintModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
Definition: kateargumenthintmodel.cpp:226
QMap::constEnd
const_iterator constEnd() const
QModelIndex::row
int row() const
QTextFormat::setBackground
void setBackground(const QBrush &brush)
ExpandingWidgetModel::matchColor
uint matchColor(const QModelIndex &index) const
Returns the match-color for the given index, or zero if match-quality could not be computed...
Definition: expandingwidgetmodel.cpp:58
KateArgumentHintModel::parentModelReset
void parentModelReset()
Definition: kateargumenthintmodel.cpp:57
katecompletionwidget.h
KateArgumentHintModel::clear
void clear()
Definition: kateargumenthintmodel.cpp:34
QString
QList< int >
QList::iterator
QStringList
ExpandingWidgetModel::isExpanded
bool isExpanded(const QModelIndex &row) const
Definition: expandingwidgetmodel.cpp:305
QPair
QTextFormat
QList::end
iterator end()
KateArgumentHintModel::emitDataChanged
void emitDataChanged(const QModelIndex &start, const QModelIndex &end)
Definition: kateargumenthintmodel.cpp:249
KateCompletionModel::Group
Definition: katecompletionmodel.h:274
QModelIndex::model
const QAbstractItemModel * model() const
QModelIndex::data
QVariant data(int role) const
KateArgumentHintModel::data
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Does not request data from index, this only returns local data like highlighting for expanded rows an...
Definition: kateargumenthintmodel.cpp:90
QTreeView
KateArgumentHintModel::indexIsItem
virtual bool indexIsItem(const QModelIndex &index) const
Should return true if the given row should be painted like a contained item(as opposed to label-rows ...
Definition: kateargumenthintmodel.cpp:253
KateArgumentHintModel::treeView
virtual QTreeView * treeView() const
Definition: kateargumenthintmodel.cpp:245
QModelIndex::column
int column() const
QVariant::isValid
bool isValid() const
QVariant::type
Type type() const
QAbstractItemView::currentIndex
QModelIndex currentIndex() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KateCompletionWidget::treeView
KateCompletionTree * treeView() const
Definition: katecompletionwidget.cpp:923
ExpandingWidgetModel::m_expandedIcon
static QIcon m_expandedIcon
Definition: expandingwidgetmodel.h:135
KateCompletionModel::Group::filtered
QList< Item > filtered
Definition: katecompletionmodel.h:302
QVariant::toString
QString toString() const
QTextFormat::isValid
bool isValid() const
QList::begin
iterator begin()
ExpandingWidgetModel::cacheIcons
void cacheIcons() const
Definition: expandingwidgetmodel.cpp:441
QVariant
ExpandingWidgetModel
Cares about expanding/un-expanding items in a tree-view together with ExpandingDelegate.
Definition: expandingwidgetmodel.h:36
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

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

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal