Kate
katerenderrange.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) 2003-2005 Hamish Rodda <rodda@kde.org> 00004 * Copyright (C) 2007 Mirko Stocker <me@misto.ch> 00005 * Copyright (C) 2008 David Nolden <david.nolden.kdevelop@art-master.de> 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Library General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Library General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Library General Public License 00018 * along with this library; see the file COPYING.LIB. If not, write to 00019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 * Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #include "katerenderrange.h" 00024 00025 #include <limits.h> 00026 00027 #include <kcolorutils.h> 00028 00029 void mergeAttributes(KTextEditor::Attribute::Ptr base, KTextEditor::Attribute::Ptr add) 00030 { 00031 if(!add) 00032 return; 00033 00034 bool hadBg = base->hasProperty(KTextEditor::Attribute::BackgroundBrush); 00035 bool hasBg = add->hasProperty(KTextEditor::Attribute::BackgroundBrush); 00036 00037 bool hadFg = base->hasProperty(KTextEditor::Attribute::ForegroundBrush); 00038 bool hasFg = add->hasProperty(KTextEditor::Attribute::ForegroundBrush); 00039 00040 if(((!hadBg || !hasBg) && (!hadFg || !hasFg))) { 00041 //Nothing to blend 00042 *base += *add; 00043 return; 00044 } 00045 00046 //We eventually have to blend 00047 00048 QBrush baseBgBrush, baseFgBrush; 00049 00050 if(hadBg) 00051 baseBgBrush = base->background(); 00052 00053 if(hadFg) 00054 baseFgBrush = base->foreground(); 00055 00056 *base += *add; 00057 00058 if(hadBg && hasBg) 00059 { 00060 QBrush bg = add->background(); 00061 if(!bg.isOpaque()) { 00062 QColor mixWithColor = bg.color(); 00063 mixWithColor.setAlpha(255); 00064 bg.setColor(KColorUtils::mix(baseBgBrush.color(), mixWithColor, bg.color().alphaF())); 00065 base->setBackground(bg); 00066 } 00067 } 00068 if(hadFg && hasFg) 00069 { 00070 QBrush fg = add->foreground(); 00071 if(!fg.isOpaque()) { 00072 QColor mixWithColor = fg.color(); 00073 mixWithColor.setAlpha(255); 00074 fg.setColor(KColorUtils::mix(baseFgBrush.color(), mixWithColor, fg.color().alphaF())); 00075 base->setForeground(fg); 00076 } 00077 } 00078 } 00079 00080 bool KateRenderRange::isReady() const { 00081 return false; 00082 } 00083 00084 NormalRenderRange::NormalRenderRange() 00085 : m_currentRange(0) 00086 { 00087 } 00088 00089 NormalRenderRange::~NormalRenderRange() 00090 { 00091 QListIterator<pairRA> it = m_ranges; 00092 while (it.hasNext()) 00093 delete it.next().first; 00094 } 00095 00096 void NormalRenderRange::addRange(KTextEditor::Range* range, KTextEditor::Attribute::Ptr attribute) 00097 { 00098 m_ranges.append(pairRA(range, attribute)); 00099 } 00100 00101 KTextEditor::Cursor NormalRenderRange::nextBoundary() const 00102 { 00103 return m_nextBoundary; 00104 } 00105 00106 bool NormalRenderRange::advanceTo(const KTextEditor::Cursor& pos) 00107 { 00108 int index = m_currentRange; 00109 while (index < m_ranges.count()) { 00110 const pairRA& p = m_ranges.at(index); 00111 KTextEditor::Range* r = p.first; 00112 if (r->end() <= pos) { 00113 ++index; 00114 } else { 00115 bool ret = index != m_currentRange; 00116 m_currentRange = index; 00117 00118 if (r->start() > pos) { 00119 m_nextBoundary = r->start(); 00120 } else { 00121 m_nextBoundary = r->end(); 00122 } 00123 if (r->contains(pos)) { 00124 m_currentAttribute = p.second; 00125 } else { 00126 m_currentAttribute.clear(); 00127 } 00128 00129 return ret; 00130 } 00131 } 00132 00133 m_nextBoundary = KTextEditor::Cursor(INT_MAX, INT_MAX); 00134 m_currentAttribute.clear(); 00135 return false; 00136 } 00137 00138 KTextEditor::Attribute::Ptr NormalRenderRange::currentAttribute() const 00139 { 00140 return m_currentAttribute; 00141 } 00142 00143 KTextEditor::Cursor RenderRangeList::nextBoundary() const 00144 { 00145 KTextEditor::Cursor ret = m_currentPos; 00146 bool first = true; 00147 foreach (KateRenderRange* r, *this) { 00148 if (first) { 00149 ret = r->nextBoundary(); 00150 first = false; 00151 00152 } else { 00153 KTextEditor::Cursor nb = r->nextBoundary(); 00154 if (ret > nb) 00155 ret = nb; 00156 } 00157 } 00158 return ret; 00159 } 00160 00161 RenderRangeList::~RenderRangeList() 00162 { 00163 } 00164 00165 void RenderRangeList::advanceTo(const KTextEditor::Cursor& pos) 00166 { 00167 foreach (KateRenderRange* r, *this) 00168 r->advanceTo(pos); 00169 00170 //Delete lists that are ready, else the list may get too large due to temporaries 00171 for(int a = size()-1; a >= 0; --a) { 00172 KateRenderRange* r = at(a); 00173 if(r->isReady()) { 00174 delete r; 00175 removeAt(a); 00176 } 00177 } 00178 } 00179 00180 bool RenderRangeList::hasAttribute() const 00181 { 00182 foreach (KateRenderRange* r, *this) 00183 if (r->currentAttribute()) 00184 return true; 00185 00186 return false; 00187 } 00188 00189 KTextEditor::Attribute::Ptr RenderRangeList::generateAttribute() const 00190 { 00191 KTextEditor::Attribute::Ptr a; 00192 bool ownsAttribute = false; 00193 00194 foreach (KateRenderRange* r, *this) { 00195 if (KTextEditor::Attribute::Ptr a2 = r->currentAttribute()) { 00196 if(!a) { 00197 a = a2; 00198 }else { 00199 if(!ownsAttribute) { 00200 //Make an own copy of the attribute.. 00201 ownsAttribute = true; 00202 a = new KTextEditor::Attribute(*a); 00203 } 00204 mergeAttributes(a, a2); 00205 } 00206 } 00207 } 00208 00209 return a; 00210 } 00211
KDE 4.6 API Reference