summaryrefslogtreecommitdiffstats
path: root/kode/code.cpp
blob: 3c4c38165e3977cf23b721fd0be0e823ee87e702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
    This file is part of tdepim.

    Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "code.h"

#include <kdebug.h>

#include <tqfile.h>
#include <textstream.h>

using namespace KODE;

Code::Code()
  : mIndent( 0 )
{
}

Code::Code( int indent )
  : mIndent( indent )
{
}

void Code::clear()
{
  mIndent = 0;
  mText = TQString();
}

bool Code::isEmpty() const
{
  return mText.isEmpty();
}

void Code::setIndent( int indent )
{
  mIndent = indent;
}

void Code::indent()
{
  mIndent += 2;
}

void Code::unindent()
{
  mIndent -= 2;
  if ( mIndent < 0 ) mIndent = 0;
}

void Code::addLine( const TQString &line )
{
  mText += spaces( mIndent );
  mText += line;
  mText += '\n';
}

void Code::newLine()
{
  mText += '\n';
}

TQString Code::spaces( int count )
{
  TQString str;
  for( int i = 0; i < count; ++i ) {
    str += ' ';
  }
  return str;
}

void Code::addBlock( const TQString &block )
{
  TQStringList lines = TQStringList::split( "\n", block, true );
  if ( !lines.isEmpty() && lines.last().isEmpty() ) {
    lines.pop_back();
  }
  TQStringList::ConstIterator it;
  for( it = lines.begin(); it != lines.end(); ++it ) {
    if ( !(*it).isEmpty() ) mText += spaces( mIndent );
    mText += *it;
    mText += '\n';
  }
}

void Code::addBlock( const TQString &block, int indent )
{
  int tmp = mIndent;
  mIndent = indent;
  addBlock( block );
  mIndent = tmp;
}

void Code::addBlock( const Code &c )
{
  addBlock( c.text() );
}

void Code::addWrappedText( const TQString &txt )
{
  int maxWidth = 80 - mIndent;
  unsigned int pos = 0;
  while ( pos < txt.length() ) {
    TQString line = txt.mid( pos, maxWidth );
    addLine( line );
    pos += maxWidth;
  }
}

void Code::addFormattedText( const TQString &text )
{
  int maxWidth = 80 - mIndent;
  int lineLength = 0;

  TQString line;
  const TQStringList words = TQStringList::split( ' ', text, false );

  TQStringList::ConstIterator it;
  for ( it = words.begin(); it != words.end(); ++it ) {
    if ( (int)(*it).length() + lineLength >= maxWidth ) {
      addLine( line );
      line.truncate( 0 );
      lineLength = 0;
    }

    line += *it + " ";
    lineLength += (*it).length() + 1;
  }

  addLine( line );
}

Code &Code::operator+=( const TQString &str )
{
  addLine( str );

  return *this;
}

Code &Code::operator+=( const char *str )
{
  addLine( TQString::fromLocal8Bit( str ) );
  return *this;
}

Code &Code::operator+=( const Code &code )
{
  mText += code.mText;
  return *this;
}