Kate
katecompletiondelegate.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries and the Kate part. 00002 * 00003 * Copyright (C) 2007 David Nolden <david.nolden.kdevelop@art-master.de> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "katecompletiondelegate.h" 00022 00023 #include <ktexteditor/codecompletionmodel.h> 00024 00025 #include "katerenderer.h" 00026 #include "katetextline.h" 00027 #include "katedocument.h" 00028 #include "kateview.h" 00029 #include "katehighlight.h" 00030 #include "katerenderrange.h" 00031 00032 #include "katecompletionwidget.h" 00033 #include "katecompletionmodel.h" 00034 #include "katecompletiontree.h" 00035 00036 //Currently disable because it doesn't work 00037 #define DISABLE_INTERNAL_HIGHLIGHTING 00038 00039 KateCompletionDelegate::KateCompletionDelegate(ExpandingWidgetModel* model, KateCompletionWidget* parent) : 00040 ExpandingDelegate(model, parent), m_cachedRow(-1) 00041 { 00042 } 00043 00044 void KateCompletionDelegate::adjustStyle( const QModelIndex& index, QStyleOptionViewItem & option ) const { 00045 if(index.column() == 0) { 00046 //We always want to use the match-color if available, also for highlighted items. 00048 uint color = model()->matchColor(index); 00049 if(color != 0) { 00050 QColor match(color); 00051 00052 for(int a = 0; a <=2; a++ ) 00053 option.palette.setColor( (QPalette::ColorGroup)a, QPalette::Highlight, match ); 00054 } 00055 } 00056 } 00057 00058 00059 KateRenderer * KateCompletionDelegate::renderer( ) const 00060 { 00061 return widget()->view()->renderer(); 00062 } 00063 00064 KateCompletionWidget * KateCompletionDelegate::widget( ) const 00065 { 00066 return static_cast<KateCompletionWidget*>(const_cast<QObject*>(parent())); 00067 } 00068 00069 KateDocument * KateCompletionDelegate::document( ) const 00070 { 00071 return widget()->view()->doc(); 00072 } 00073 00074 void KateCompletionDelegate::heightChanged() const { 00075 if(parent()) 00076 widget()->updateHeight(); 00077 } 00078 00079 QList<QTextLayout::FormatRange> KateCompletionDelegate::createHighlighting(const QModelIndex& index, QStyleOptionViewItem& option) const { 00080 00081 QVariant highlight = model()->data(index, KTextEditor::CodeCompletionModel::HighlightingMethod); 00082 00083 // TODO: config enable specifying no highlight as default 00084 int highlightMethod = KTextEditor::CodeCompletionModel::InternalHighlighting; 00085 if (highlight.canConvert(QVariant::Int)) 00086 highlightMethod = highlight.toInt(); 00087 00088 if (highlightMethod & KTextEditor::CodeCompletionModel::CustomHighlighting) { 00089 m_currentColumnStart = 0; 00090 return highlightingFromVariantList(model()->data(index, KTextEditor::CodeCompletionModel::CustomHighlight).toList()); 00091 } 00092 00093 #ifdef DISABLE_INTERNAL_HIGHLIGHTING 00094 return QList<QTextLayout::FormatRange>(); 00095 #endif 00096 00097 if( index.row() == m_cachedRow && highlightMethod & KTextEditor::CodeCompletionModel::InternalHighlighting ) { 00098 00099 if( index.column() < m_cachedColumnStarts.size() ) { 00100 m_currentColumnStart = m_cachedColumnStarts[index.column()]; 00101 } else { 00102 kWarning() << "Column-count does not match"; 00103 } 00104 00105 return m_cachedHighlights; 00106 } 00107 00109 m_cachedRow = index.row(); 00110 00111 KTextEditor::Cursor completionStart = widget()->completionRange()->start(); 00112 00113 QString startText = document()->text(KTextEditor::Range(completionStart.line(), 0, completionStart.line(), completionStart.column())); 00114 00115 QString lineContent = startText; 00116 00117 int len = completionStart.column(); 00118 m_cachedColumnStarts.clear(); 00119 00120 for (int i = 0; i < KTextEditor::CodeCompletionModel::ColumnCount; ++i) { 00121 m_cachedColumnStarts.append(len); 00122 QString text = model()->data(model()->index(index.row(), i, index.parent()), Qt::DisplayRole).toString(); 00123 lineContent += text; 00124 len += text.length(); 00125 } 00126 00127 Kate::TextLine thisLine = Kate::TextLine (new Kate::TextLineData(lineContent)); 00128 00129 //kDebug( 13035 ) << "About to highlight with mode " << highlightMethod << " text [" << thisLine->string() << "]"; 00130 00131 if (highlightMethod & KTextEditor::CodeCompletionModel::InternalHighlighting) { 00132 Kate::TextLine previousLine; 00133 if (completionStart.line()) 00134 previousLine = document()->kateTextLine(completionStart.line() - 1); 00135 else 00136 previousLine = Kate::TextLine (new Kate::TextLineData()); 00137 00138 QVector<int> foldingList; 00139 bool ctxChanged = false; 00140 document()->highlight()->doHighlight(previousLine.data(), thisLine.data(), foldingList, ctxChanged); 00141 } 00142 00143 m_currentColumnStart = m_cachedColumnStarts[index.column()]; 00144 00145 NormalRenderRange rr; 00146 QList<QTextLayout::FormatRange> ret = renderer()->decorationsForLine(thisLine, 0, false, &rr, option.state & QStyle::State_Selected); 00147 00148 //Remove background-colors 00149 for( QList<QTextLayout::FormatRange>::iterator it = ret.begin(); it != ret.end(); ++it ) 00150 (*it).format.clearBackground(); 00151 00152 return ret; 00153 } 00154 00155
KDE 4.6 API Reference