Kate
katecompletiondelegate.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 "katecompletiondelegate.h"
00020
00021 #include <ktexteditor/codecompletionmodel.h>
00022
00023 #include "katerenderer.h"
00024 #include "katetextline.h"
00025 #include "katedocument.h"
00026 #include "kateview.h"
00027 #include "katehighlight.h"
00028 #include "katerenderrange.h"
00029 #include "katesmartrange.h"
00030
00031 #include "katecompletionwidget.h"
00032 #include "katecompletionmodel.h"
00033 #include "katecompletiontree.h"
00034
00035 KateCompletionDelegate::KateCompletionDelegate(ExpandingWidgetModel* model, KateCompletionWidget* parent) :
00036 ExpandingDelegate(model, parent), m_cachedRow(-1)
00037 {
00038 }
00039
00040 void KateCompletionDelegate::adjustStyle( const QModelIndex& index, QStyleOptionViewItem & option ) const {
00041 if(index.column() == 0) {
00042
00044 uint color = model()->matchColor(index);
00045 if(color != 0) {
00046 QColor match(color);
00047
00048 for(int a = 0; a <=2; a++ )
00049 option.palette.setColor( (QPalette::ColorGroup)a, QPalette::Highlight, match );
00050 }
00051 }
00052 }
00053
00054
00055 KateRenderer * KateCompletionDelegate::renderer( ) const
00056 {
00057 return widget()->view()->renderer();
00058 }
00059
00060 KateCompletionWidget * KateCompletionDelegate::widget( ) const
00061 {
00062 return static_cast<KateCompletionWidget*>(const_cast<QObject*>(parent()));
00063 }
00064
00065 KateDocument * KateCompletionDelegate::document( ) const
00066 {
00067 return widget()->view()->doc();
00068 }
00069
00070 void KateCompletionDelegate::heightChanged() const {
00071 if(parent())
00072 widget()->updateHeight();
00073 }
00074
00075 QList<QTextLayout::FormatRange> KateCompletionDelegate::createHighlighting(const QModelIndex& index, QStyleOptionViewItem& option) const {
00076
00077 QVariant highlight = model()->data(index, KTextEditor::CodeCompletionModel::HighlightingMethod);
00078
00079
00080 int highlightMethod = KTextEditor::CodeCompletionModel::InternalHighlighting;
00081 if (highlight.canConvert(QVariant::Int))
00082 highlightMethod = highlight.toInt();
00083
00084 if( index.row() == m_cachedRow && highlightMethod & KTextEditor::CodeCompletionModel::InternalHighlighting ) {
00085
00086 if( index.column() < m_cachedColumnStarts.size() ) {
00087 m_currentColumnStart = m_cachedColumnStarts[index.column()];
00088 } else {
00089 kWarning() << "Column-count does not match";
00090 }
00091
00092 return m_cachedHighlights;
00093 }
00094
00096 m_cachedRow = index.row();
00097
00098 KTextEditor::Cursor completionStart = widget()->completionRange()->start();
00099
00100 QString startText = document()->text(KTextEditor::Range(completionStart.line(), 0, completionStart.line(), completionStart.column()));
00101
00102 KateTextLine::Ptr thisLine(new KateTextLine());
00103 thisLine->insertText(0, startText);
00104
00105 int len = completionStart.column();
00106 m_cachedColumnStarts.clear();
00107
00108 if (highlightMethod & KTextEditor::CodeCompletionModel::CustomHighlighting) {
00109 m_currentColumnStart = 0;
00110 return highlightingFromVariantList(model()->data(index, KTextEditor::CodeCompletionModel::CustomHighlight).toList());
00111 }
00112
00113 for (int i = 0; i < KTextEditor::CodeCompletionModel::ColumnCount; ++i) {
00114 m_cachedColumnStarts.append(len);
00115 QString text = model()->data(model()->index(index.row(), i, index.parent()), Qt::DisplayRole).toString();
00116 thisLine->insertText(thisLine->length(), text);
00117 len += text.length();
00118 }
00119
00120
00121
00122 if (highlightMethod & KTextEditor::CodeCompletionModel::InternalHighlighting) {
00123 KateTextLine::Ptr previousLine;
00124 if (completionStart.line())
00125 previousLine = document()->kateTextLine(completionStart.line() - 1);
00126 else
00127 previousLine = new KateTextLine();
00128
00129 QVector<int> foldingList;
00130 bool ctxChanged = false;
00131 document()->highlight()->doHighlight(previousLine.data(), thisLine.data(), foldingList, ctxChanged);
00132 }
00133
00134 m_currentColumnStart = m_cachedColumnStarts[index.column()];
00135
00136 NormalRenderRange rr;
00137 QList<QTextLayout::FormatRange> ret = renderer()->decorationsForLine(thisLine, 0, false, &rr, option.state & QStyle::State_Selected);
00138
00139
00140 for( QList<QTextLayout::FormatRange>::iterator it = ret.begin(); it != ret.end(); ++it )
00141 (*it).format.clearBackground();
00142
00143 return ret;
00144 }
00145
00146