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

Kate

hlselectionplugin.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2010 Dominik Haumann <dhaumann kde org>
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 "hlselectionplugin.h"
00020 #include "hlselectionplugin.moc"
00021 
00022 #include <ktexteditor/document.h>
00023 #include <ktexteditor/attribute.h>
00024 #include <ktexteditor/searchinterface.h>
00025 #include <ktexteditor/movinginterface.h>
00026 #include <ktexteditor/movingrange.h>
00027 
00028 #include <assert.h>
00029 #include <kaction.h>
00030 #include <kactioncollection.h>
00031 #include <kfiledialog.h>
00032 #include <kpluginfactory.h>
00033 #include <kpluginloader.h>
00034 #include <klocale.h>
00035 #include <kaboutdata.h>
00036 
00037 
00038 K_PLUGIN_FACTORY( HighlightSelectionPluginFactory, registerPlugin<HighlightSelectionPlugin>(); )
00039 K_EXPORT_PLUGIN( HighlightSelectionPluginFactory( KAboutData( "ktexteditor_insertfile", "ktexteditor_plugins", ki18n("Highlight Selection"), "1.0", ki18n("Highlight Selection"), KAboutData::License_LGPL_V2 ) ) )
00040 
00041 //BEGIN HighlightSelectionPlugin
00042 HighlightSelectionPlugin::HighlightSelectionPlugin( QObject *parent, const QVariantList& )
00043   : KTextEditor::Plugin ( parent )
00044 {
00045 }
00046 
00047 HighlightSelectionPlugin::~HighlightSelectionPlugin()
00048 {
00049 }
00050 
00051 void HighlightSelectionPlugin::addView(KTextEditor::View *view)
00052 {
00053   HighlightSelectionPluginView *nview = new HighlightSelectionPluginView (view);
00054   m_views.append (nview);
00055 }
00056 
00057 void HighlightSelectionPlugin::removeView(KTextEditor::View *view)
00058 {
00059   foreach (HighlightSelectionPluginView *pluginView, m_views) {
00060     if (pluginView->view() == view) {
00061       m_views.removeAll(pluginView);
00062       delete pluginView;
00063       break;
00064     }
00065   }
00066 }
00067 //END HighlightSelectionPlugin
00068 
00069 //BEGIN HighlightSelectionPluginView
00070 HighlightSelectionPluginView::HighlightSelectionPluginView( KTextEditor::View *view)
00071   : QObject( view )
00072 //   , KXMLGUIClient( view ) // XMLGUI stuff not needed right now
00073 {
00074   setObjectName("highlight-selection-plugin");
00075 
00076   m_view = view;
00077 
00078   // we don't need any XMLGUI stuff, so comment out
00079 //  setComponentData( HighlightSelectionPluginFactory::componentData() );
00080 //  setXMLFile( "ktexteditor_hlselectionui.rc" );
00081 
00082   connect(view, SIGNAL(selectionChanged(KTextEditor::View*)), this, SLOT(selectionChanged()));
00083   connect(view->document(), SIGNAL(aboutToReload(KTextEditor::Document*)), this, SLOT(clearHighlights()));
00084 }
00085 
00086 HighlightSelectionPluginView::~HighlightSelectionPluginView()
00087 {
00088   clearHighlights();
00089 }
00090 
00091 KTextEditor::View* HighlightSelectionPluginView::view() const
00092 {
00093   return m_view;
00094 }
00095 
00096 void HighlightSelectionPluginView::clearHighlights()
00097 {
00098   qDeleteAll(m_ranges);
00099   m_ranges.clear();
00100   m_currentText.clear();
00101 }
00102 
00103 void HighlightSelectionPluginView::selectionChanged()
00104 {
00105   QString text;
00106   // if text of selection is still the same, abort
00107   if (m_view->selection() && m_view->selectionRange().onSingleLine()) {
00108     text = m_view->selectionText();
00109     if (text == m_currentText) {
00110       return;
00111     }
00112   }
00113 
00114   // text changed: remove all highlights + create new ones
00115   // (do not call clearHighlights(), since this also resets the m_currentText
00116   qDeleteAll(m_ranges);
00117   m_ranges.clear();
00118   
00119   // do not highlight strings with leading and trailing spaces
00120   if (!text.isEmpty() && (text.at(0).isSpace() || text.at(text.length()-1).isSpace())) {
00121     return; 
00122   }
00123 
00124   m_currentText = text;
00125   if (!m_currentText.isEmpty()) {
00126     createHighlights();
00127   }
00128 }
00129 
00130 void HighlightSelectionPluginView::createHighlights()
00131 {
00132   m_currentText = m_view->selectionText();
00133 
00134   KTextEditor::SearchInterface* siface =
00135     qobject_cast<KTextEditor::SearchInterface*>(m_view->document());
00136 
00137   if (!siface) {
00138     return;
00139   }
00140 
00141   KTextEditor::MovingInterface* miface =
00142     qobject_cast<KTextEditor::MovingInterface*>(m_view->document());
00143 
00144   KTextEditor::Attribute::Ptr attr(new KTextEditor::Attribute());
00145   attr->setFontBold(true);
00146   attr->setBackground(Qt::yellow);
00147 
00148   KTextEditor::Cursor start(0, 0);
00149   KTextEditor::Range searchRange;
00150 
00151   QVector<KTextEditor::Range> matches;
00152 
00153   do {
00154     searchRange.setRange(start, m_view->document()->documentEnd());
00155 
00156     matches = siface->searchText(searchRange, m_currentText, KTextEditor::Search::WholeWords);
00157 
00158     if (matches.first().isValid()) {
00159       KTextEditor::MovingRange* mr = miface->newMovingRange(matches.first());
00160       mr->setAttribute(attr);
00161       mr->setView(m_view);
00162       mr->setZDepth(-90000.0); // Set the z-depth to slightly worse than the selection
00163       mr->setAttributeOnlyForViews(true);
00164       m_ranges.append(mr);
00165       start = matches.first().end();
00166     }
00167   } while (matches.first().isValid());
00168 }
00169 //END HighlightSelectionPluginView
00170 
00171 // kate: space-indent on; indent-width 2; replace-tabs on;

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
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.3
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