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
00036 #define DISABLE_INTERNAL_HIGHLIGHTING
00037
00038 KateCompletionDelegate::KateCompletionDelegate(ExpandingWidgetModel* model, KateCompletionWidget* parent) :
00039 ExpandingDelegate(model, parent), m_cachedRow(-1)
00040 {
00041 }
00042
00043 void KateCompletionDelegate::adjustStyle( const QModelIndex& index, QStyleOptionViewItem & option ) const {
00044 if(index.column() == 0) {
00045
00047 uint color = model()->matchColor(index);
00048 if(color != 0) {
00049 QColor match(color);
00050
00051 for(int a = 0; a <=2; a++ )
00052 option.palette.setColor( (QPalette::ColorGroup)a, QPalette::Highlight, match );
00053 }
00054 }
00055 }
00056
00057
00058 KateRenderer * KateCompletionDelegate::renderer( ) const
00059 {
00060 return widget()->view()->renderer();
00061 }
00062
00063 KateCompletionWidget * KateCompletionDelegate::widget( ) const
00064 {
00065 return static_cast<KateCompletionWidget*>(const_cast<QObject*>(parent()));
00066 }
00067
00068 KateDocument * KateCompletionDelegate::document( ) const
00069 {
00070 return widget()->view()->doc();
00071 }
00072
00073 void KateCompletionDelegate::heightChanged() const {
00074 if(parent())
00075 widget()->updateHeight();
00076 }
00077
00078 QList<QTextLayout::FormatRange> KateCompletionDelegate::createHighlighting(const QModelIndex& index, QStyleOptionViewItem& option) const {
00079
00080 QVariant highlight = model()->data(index, KTextEditor::CodeCompletionModel::HighlightingMethod);
00081
00082
00083 int highlightMethod = KTextEditor::CodeCompletionModel::InternalHighlighting;
00084 if (highlight.canConvert(QVariant::Int))
00085 highlightMethod = highlight.toInt();
00086
00087 if (highlightMethod & KTextEditor::CodeCompletionModel::CustomHighlighting) {
00088 m_currentColumnStart = 0;
00089 return highlightingFromVariantList(model()->data(index, KTextEditor::CodeCompletionModel::CustomHighlight).toList());
00090 }
00091
00092 #ifdef DISABLE_INTERNAL_HIGHLIGHTING
00093 return QList<QTextLayout::FormatRange>();
00094 #endif
00095
00096 if( index.row() == m_cachedRow && highlightMethod & KTextEditor::CodeCompletionModel::InternalHighlighting ) {
00097
00098 if( index.column() < m_cachedColumnStarts.size() ) {
00099 m_currentColumnStart = m_cachedColumnStarts[index.column()];
00100 } else {
00101 kWarning() << "Column-count does not match";
00102 }
00103
00104 return m_cachedHighlights;
00105 }
00106
00108 m_cachedRow = index.row();
00109
00110 KTextEditor::Cursor completionStart = widget()->completionRange()->start();
00111
00112 QString startText = document()->text(KTextEditor::Range(completionStart.line(), 0, completionStart.line(), completionStart.column()));
00113
00114 KateTextLine::Ptr thisLine(new KateTextLine());
00115 thisLine->insertText(0, 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 thisLine->insertText(thisLine->length(), text);
00124 len += text.length();
00125 }
00126
00127
00128
00129 if (highlightMethod & KTextEditor::CodeCompletionModel::InternalHighlighting) {
00130 KateTextLine::Ptr previousLine;
00131 if (completionStart.line())
00132 previousLine = document()->kateTextLine(completionStart.line() - 1);
00133 else
00134 previousLine = new KateTextLine();
00135
00136 QVector<int> foldingList;
00137 bool ctxChanged = false;
00138 document()->highlight()->doHighlight(previousLine.data(), thisLine.data(), foldingList, ctxChanged);
00139 }
00140
00141 m_currentColumnStart = m_cachedColumnStarts[index.column()];
00142
00143 NormalRenderRange rr;
00144 QList<QTextLayout::FormatRange> ret = renderer()->decorationsForLine(thisLine, 0, false, &rr, option.state & QStyle::State_Selected);
00145
00146
00147 for( QList<QTextLayout::FormatRange>::iterator it = ret.begin(); it != ret.end(); ++it )
00148 (*it).format.clearBackground();
00149
00150 return ret;
00151 }
00152
00153