Kate
katelinelayout.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
00020 #include "katelinelayout.h"
00021 #include "katetextlayout.h"
00022
00023 #include <QtGui/QTextLine>
00024
00025 #include <kdebug.h>
00026
00027 #include "katedocument.h"
00028
00029 KateLineLayout::KateLineLayout(KateDocument* doc)
00030 : m_doc(doc)
00031 , m_textLine(0L)
00032 , m_line(-1)
00033 , m_virtualLine(-1)
00034 , m_shiftX(0)
00035 , m_layout(0L)
00036 , m_layoutDirty(true)
00037 {
00038 Q_ASSERT(doc);
00039 }
00040
00041 KateLineLayout::~KateLineLayout()
00042 {
00043 delete m_layout;
00044 }
00045
00046 void KateLineLayout::clear()
00047 {
00048 m_textLine = 0L;
00049 m_line = -1;
00050 m_virtualLine = -1;
00051 m_shiftX = 0;
00052
00053 delete m_layout;
00054 m_layout = 0L;
00055
00056 }
00057
00058 bool KateLineLayout::includesCursor(const KTextEditor::Cursor& realCursor) const
00059 {
00060 return realCursor.line() == line();
00061 }
00062
00063 const KateTextLine::Ptr& KateLineLayout::textLine() const
00064 {
00065 if (!m_textLine)
00066 m_textLine = m_doc->kateTextLine(line());
00067
00068 Q_ASSERT(m_textLine);
00069
00070 return m_textLine;
00071 }
00072
00073 int KateLineLayout::line( ) const
00074 {
00075 return m_line;
00076 }
00077
00078 void KateLineLayout::setLine( int line, int virtualLine )
00079 {
00080 m_line = line;
00081 m_virtualLine = (virtualLine == -1) ? m_doc->getVirtualLine(line) : virtualLine;
00082 m_textLine = 0L;
00083 }
00084
00085 int KateLineLayout::virtualLine( ) const
00086 {
00087 return m_virtualLine;
00088 }
00089
00090 void KateLineLayout::setVirtualLine( int virtualLine )
00091 {
00092 m_virtualLine = virtualLine;
00093 }
00094
00095 bool KateLineLayout::startsInvisibleBlock() const
00096 {
00097 if (!isValid() || virtualLine() == 0)
00098 return false;
00099
00100 return (virtualLine() - 1) != (int)m_doc->getVirtualLine(line() - 1);
00101 }
00102
00103 int KateLineLayout::shiftX() const
00104 {
00105 return m_shiftX;
00106 }
00107
00108 void KateLineLayout::setShiftX(int shiftX)
00109 {
00110 m_shiftX = shiftX;
00111 }
00112
00113 KateDocument* KateLineLayout::doc() const
00114 {
00115 return m_doc;
00116 }
00117
00118 bool KateLineLayout::isValid( ) const
00119 {
00120 return line() != -1 && layout() && textLine();
00121 }
00122
00123 QTextLayout* KateLineLayout::layout() const
00124 {
00125 return m_layout;
00126 }
00127
00128 void KateLineLayout::setLayout(QTextLayout* layout)
00129 {
00130 if (m_layout != layout) {
00131 delete m_layout;
00132 m_layout = layout;
00133 }
00134
00135 m_layoutDirty = !m_layout;
00136 m_dirtyList.clear();
00137 if (m_layout)
00138 for (int i = 0; i < qMax(1, m_layout->lineCount()); ++i)
00139 m_dirtyList.append(true);
00140 }
00141
00142 void KateLineLayout::invalidateLayout( )
00143 {
00144 setLayout(0L);
00145 }
00146
00147 bool KateLineLayout::isDirty( int viewLine ) const
00148 {
00149 Q_ASSERT(isValid() && viewLine >= 0 && viewLine < viewLineCount());
00150 return m_dirtyList[viewLine];
00151 }
00152
00153 bool KateLineLayout::setDirty( int viewLine, bool dirty )
00154 {
00155 Q_ASSERT(isValid() && viewLine >= 0 && viewLine < viewLineCount());
00156 m_dirtyList[viewLine] = dirty;
00157 return dirty;
00158 }
00159
00160 KTextEditor::Cursor KateLineLayout::start( ) const
00161 {
00162 return KTextEditor::Cursor(line(), 0);
00163 }
00164
00165 int KateLineLayout::length( ) const
00166 {
00167 return textLine()->length();
00168 }
00169
00170 int KateLineLayout::viewLineCount( ) const
00171 {
00172 return m_layout->lineCount();
00173 }
00174
00175 KateTextLayout KateLineLayout::viewLine( int viewLine ) const
00176 {
00177 if (viewLine < 0)
00178 viewLine += viewLineCount();
00179 Q_ASSERT(isValid());
00180 Q_ASSERT(viewLine >= 0 && viewLine < viewLineCount());
00181 return KateTextLayout(KateLineLayoutPtr(const_cast<KateLineLayout*>(this)), viewLine);
00182 }
00183
00184 int KateLineLayout::width( ) const
00185 {
00186 int width = 0;
00187
00188 for (int i = 0; i < m_layout->lineCount(); ++i)
00189 width = qMax((int)m_layout->lineAt(i).naturalTextWidth(), width);
00190
00191 return width;
00192 }
00193
00194 int KateLineLayout::widthOfLastLine( ) const
00195 {
00196 const KateTextLayout& lastLine = viewLine(viewLineCount() - 1);
00197 return lastLine.endX();
00198 }
00199
00200 bool KateLineLayout::isOutsideDocument( ) const
00201 {
00202 return line() < 0 || line() >= m_doc->lines();
00203 }
00204
00205 void KateLineLayout::debugOutput() const
00206 {
00207 kDebug( 13033 ) << "KateLineLayout: " << this << " valid " << isValid() << " line " << line() << " length " << length() << " width " << width() << " viewLineCount " << viewLineCount();
00208 }
00209
00210 int KateLineLayout::viewLineForColumn( int column ) const
00211 {
00212 int len = 0;
00213 int i = 0;
00214 for (; i < m_layout->lineCount() - 1; ++i) {
00215 len += m_layout->lineAt(i).textLength();
00216 if (column < len)
00217 return i;
00218 }
00219 return i;
00220 }
00221
00222 bool KateLineLayout::isLayoutDirty( ) const
00223 {
00224 return m_layoutDirty;
00225 }
00226
00227 void KateLineLayout::setLayoutDirty( bool dirty )
00228 {
00229 m_layoutDirty = dirty;
00230 }
00231
00232