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.
16 class TrivialExecutor
: public HLineExecutor
{
18 virtual ~TrivialExecutor() {
19 std::cout
<< std::endl
;
22 virtual void writeChunk(const QString
& text
) {
23 std::cout
<< text
<< "|";
26 virtual void setFormat(const QTextCharFormat
&) { }
29 void HLine::Format::applyTo(QTextCharFormat
& fmt
) const {
31 fmt
.setFontWeight(m_bold
.second
? QFont::Bold
: QFont::Normal
);
34 fmt
.setFontItalic(m_italic
.second
);
37 fmt
.setForeground(m_color
.second
);
40 HLine::HLine(const QString
& text
, const QTextCharFormat
& baseFormat
)
42 m_regions
.push_back(Region(text
.length(), baseFormat
));
45 HLine::HLine(const HLine
& other
)
46 : m_regions(other
.m_regions
)
47 , m_text(other
.m_text
) { }
49 uint
HLine::findRegion(int index
) const {
50 if (index
>= m_text
.length()) return m_regions
.size();
52 while (index
>= m_regions
[r
].end())
54 Q_ASSERT(r
< m_regions
.size());
58 int HLine::begin(uint r
) const {
60 else return m_regions
[r
- 1].end();
63 uint
HLine::splitRegion(int index
) {
64 if (index
>= m_text
.length()) return m_regions
.size();
65 int r
= findRegion(index
);
66 if (begin(r
) != index
) {
68 m_regions
.begin() + r
+ 1,
70 m_regions
[r
].setEnd(index
);
76 const QTextCharFormat
& HLine::getFormat(int index
) const {
77 return m_regions
[findRegion(index
)].format();
80 void HLine::setFormat(int begin
, int end
, const Format
& format
) {
81 Q_ASSERT(begin
< m_text
.length());
82 Q_ASSERT(end
<= m_text
.length());
84 if (begin
>= end
) return;
85 int r1
= splitRegion(begin
);
86 int r2
= splitRegion(end
);
88 for (int r
= r1
; r
< r2
; r
++) {
89 QTextCharFormat fmt
= m_regions
[r
].format();
91 m_regions
[r
].setFormat(fmt
);
95 void HLine::setBold(int begin
, int end
, bool value
) {
97 format
.m_bold
= std::make_pair(true, value
);
98 setFormat(begin
, end
, format
);
101 void HLine::setItalic(int begin
, int end
, bool value
) {
103 format
.m_italic
= std::make_pair(true, value
);
104 setFormat(begin
, end
, format
);
107 void HLine::setColor(int begin
, int end
, const QColor
& color
) {
109 format
.m_color
= std::make_pair(true, color
);
110 setFormat(begin
, end
, format
);
113 QString
HLine::mid(int begin
, int end
) const {
114 return m_text
.mid(begin
, end
- begin
);
117 HLine
* HLine::extract(int begin
, int end
) const {
118 HLine
* res
= new HLine(*this);
119 int r1
= res
->splitRegion(begin
);
120 int r2
= res
->splitRegion(end
);
123 res
->m_text
= mid(begin
, end
);
125 // remove superfluous regions
126 res
->m_regions
.erase(
127 res
->m_regions
.begin() + r2
,
128 res
->m_regions
.end());
129 res
->m_regions
.erase(
130 res
->m_regions
.begin(),
131 res
->m_regions
.begin() + r1
);
134 for (uint i
= 0; i
< res
->m_regions
.size(); i
++) {
135 res
->m_regions
[i
].setEnd(res
->m_regions
[i
].end() - begin
);
141 HLine
* HLine::append(const HLine
& other
, int begin
, int end
) const {
142 HLine
* res
= new HLine(*this);
144 int old_r
= res
->m_regions
.size();
145 int old_length
= res
->m_text
.length();
148 res
->m_text
+= other
.mid(begin
, end
);
151 int r1
= other
.findRegion(begin
);
152 int r2
= other
.findRegion(end
);
153 std::copy(other
.m_regions
.begin() + r1
,
154 other
.m_regions
.begin() + r2
,
155 std::back_insert_iterator
<std::vector
<Region
> >(res
->m_regions
));
158 for (uint i
= old_r
; i
< res
->m_regions
.size(); i
++) {
159 int new_end
= old_length
+ res
->m_regions
[i
].end() - begin
;
160 res
->m_regions
[i
].setEnd(new_end
);
166 void HLine::dump() const {
167 TrivialExecutor executor
;
171 void HLine::run(HLineExecutor
& executor
) const {
173 for (uint i
= 0; i
< m_regions
.size(); i
++) {
174 int end
= m_regions
[i
].end();
175 executor
.setFormat(m_regions
[i
].format());
176 executor
.writeChunk(mid(begin
, end
));