Replaced all std::cout with kDebug.
[tagua/yd.git] / src / hline.cpp
blob94565ea1df07c0e918ddd4e6da5f29fce08db545
1 /*
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.
9 */
11 #include "hline.h"
12 #include <KDebug>
14 class TrivialExecutor : public HLineExecutor {
15 public:
16 virtual ~TrivialExecutor() {
17 kDebug();
20 virtual void writeChunk(const QString& text) {
21 kDebug() << text << "|";
24 virtual void setFormat(const QTextCharFormat&) { }
27 void HLine::Format::applyTo(QTextCharFormat& fmt) const {
28 if (m_bold.first) {
29 fmt.setFontWeight(m_bold.second ? QFont::Bold : QFont::Normal);
31 if (m_italic.first) {
32 fmt.setFontItalic(m_italic.second);
34 if (m_color.first)
35 fmt.setForeground(m_color.second);
38 HLine::HLine(const QString& text, const QTextCharFormat& baseFormat)
39 : m_text(text) {
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();
49 uint r = 0;
50 while (index >= m_regions[r].end())
51 r++;
52 Q_ASSERT(r < m_regions.size());
53 return r;
56 int HLine::begin(uint r) const {
57 if (r == 0) return 0;
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) {
65 m_regions.insert(
66 m_regions.begin() + r + 1,
67 m_regions[r]);
68 m_regions[r].setEnd(index);
69 return r + 1;
71 else return r;
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();
88 format.applyTo(fmt);
89 m_regions[r].setFormat(fmt);
93 void HLine::setBold(int begin, int end, bool value) {
94 Format format;
95 format.m_bold = std::make_pair(true, value);
96 setFormat(begin, end, format);
99 void HLine::setItalic(int begin, int end, bool value) {
100 Format format;
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) {
106 Format format;
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);
120 // adjust text
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);
131 // adjust regions
132 for (uint i = 0; i < res->m_regions.size(); i++) {
133 res->m_regions[i].setEnd(res->m_regions[i].end() - begin);
136 return res;
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();
145 // append text
146 res->m_text += other.mid(begin, end);
148 // add regions
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));
155 // adjust 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);
161 return res;
164 void HLine::dump() const {
165 TrivialExecutor executor;
166 run(executor);
169 void HLine::run(HLineExecutor& executor) const {
170 int begin = 0;
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));
175 begin = end;