• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Kate

katecompletiondelegate.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2007 David Nolden <david.nolden.kdevelop@art-master.de>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
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 //Currently disable because it doesn't work
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         //We always want to use the match-color if available, also for highlighted items.
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     // TODO: config enable specifying no highlight as default
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     //kDebug( 13035 ) << "About to highlight with mode " << highlightMethod << " text [" << thisLine->string() << "]";
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   //Remove background-colors
00147   for( QList<QTextLayout::FormatRange>::iterator it = ret.begin(); it != ret.end(); ++it )
00148     (*it).format.clearBackground();
00149   
00150   return ret;
00151 }
00152 
00153 

Kate

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal