2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
14 class TrivialExecutor
: public HLineExecutor
{
16 virtual ~TrivialExecutor() {
20 virtual void writeChunk(const QString
& text
) {
21 kDebug() << text
<< "|";
24 virtual void setFormat(const QTextCharFormat
&) { }
27 void HLine::Format::applyTo(QTextCharFormat
& fmt
) const {
29 fmt
.setFontWeight(m_bold
.second
? QFont::Bold
: QFont::Normal
);
32 fmt
.setFontItalic(m_italic
.second
);
35 fmt
.setForeground(m_color
.second
);
38 HLine::HLine(const QString
& text
, const QTextCharFormat
& baseFormat
)
40 m_regions
.push_back(Region(text
.length(), baseFormat
));
43 HLine::HLine(const HLine
& other
)
44 : m_regions(other
.m_regions
)
45 , m_text(other
.m_text
) { }
47 uint
HLine::findRegion(int index
) const {
48 if (index
>= m_text
.length()) return m_regions
.size();
50 while (index
>= m_regions
[r
].end())
52 Q_ASSERT(r
< m_regions
.size());
56 int HLine::begin(uint r
) const {
58 else return m_regions
[r
- 1].end();
61 uint
HLine::splitRegion(int index
) {
62 if (index
>= m_text
.length()) return m_regions
.size();
63 int r
= findRegion(index
);
64 if (begin(r
) != index
) {
66 m_regions
.begin() + r
+ 1,
68 m_regions
[r
].setEnd(index
);
74 const QTextCharFormat
& HLine::getFormat(int index
) const {
75 return m_regions
[findRegion(index
)].format();
78 void HLine::setFormat(int begin
, int end
, const Format
& format
) {
79 Q_ASSERT(begin
< m_text
.length());
80 Q_ASSERT(end
<= m_text
.length());
82 if (begin
>= end
) return;
83 int r1
= splitRegion(begin
);
84 int r2
= splitRegion(end
);
86 for (int r
= r1
; r
< r2
; r
++) {
87 QTextCharFormat fmt
= m_regions
[r
].format();
89 m_regions
[r
].setFormat(fmt
);
93 void HLine::setBold(int begin
, int end
, bool value
) {
95 format
.m_bold
= std::make_pair(true, value
);
96 setFormat(begin
, end
, format
);
99 void HLine::setItalic(int begin
, int end
, bool value
) {
101 format
.m_italic
= std::make_pair(true, value
);
102 setFormat(begin
, end
, format
);
105 void HLine::setColor(int begin
, int end
, const QColor
& color
) {
107 format
.m_color
= std::make_pair(true, color
);
108 setFormat(begin
, end
, format
);
111 QString
HLine::mid(int begin
, int end
) const {
112 return m_text
.mid(begin
, end
- begin
);
115 HLine
* HLine::extract(int begin
, int end
) const {
116 HLine
* res
= new HLine(*this);
117 int r1
= res
->splitRegion(begin
);
118 int r2
= res
->splitRegion(end
);
121 res
->m_text
= mid(begin
, end
);
123 // remove superfluous regions
124 res
->m_regions
.erase(
125 res
->m_regions
.begin() + r2
,
126 res
->m_regions
.end());
127 res
->m_regions
.erase(
128 res
->m_regions
.begin(),
129 res
->m_regions
.begin() + r1
);
132 for (uint i
= 0; i
< res
->m_regions
.size(); i
++) {
133 res
->m_regions
[i
].setEnd(res
->m_regions
[i
].end() - begin
);
139 HLine
* HLine::append(const HLine
& other
, int begin
, int end
) const {
140 HLine
* res
= new HLine(*this);
142 int old_r
= res
->m_regions
.size();
143 int old_length
= res
->m_text
.length();
146 res
->m_text
+= other
.mid(begin
, end
);
149 int r1
= other
.findRegion(begin
);
150 int r2
= other
.findRegion(end
);
151 std::copy(other
.m_regions
.begin() + r1
,
152 other
.m_regions
.begin() + r2
,
153 std::back_insert_iterator
<std::vector
<Region
> >(res
->m_regions
));
156 for (uint i
= old_r
; i
< res
->m_regions
.size(); i
++) {
157 int new_end
= old_length
+ res
->m_regions
[i
].end() - begin
;
158 res
->m_regions
[i
].setEnd(new_end
);
164 void HLine::dump() const {
165 TrivialExecutor executor
;
169 void HLine::run(HLineExecutor
& executor
) const {
171 for (uint i
= 0; i
< m_regions
.size(); i
++) {
172 int end
= m_regions
[i
].end();
173 executor
.setFormat(m_regions
[i
].format());
174 executor
.writeChunk(mid(begin
, end
));