1 /* This file is part of the KDE libraries
2 Copyright (C) 2005 Hamish Rodda <rodda@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "arbitraryhighlighttest.h"
21 #include <ktexteditor/document.h>
22 #include <ktexteditor/smartinterface.h>
23 #include <ktexteditor/rangefeedback.h>
24 #include <ktexteditor/attribute.h>
26 #include <QtCore/QTimer>
28 using namespace KTextEditor
;
30 ArbitraryHighlightTest::ArbitraryHighlightTest(Document
* parent
)
34 QTimer::singleShot(0, this, SLOT(slotCreateTopRange()));
37 ArbitraryHighlightTest::~ArbitraryHighlightTest()
41 Document
* ArbitraryHighlightTest::doc( ) const
43 return qobject_cast
<Document
*>(const_cast<QObject
*>(parent()));
46 KTextEditor::SmartInterface
* ArbitraryHighlightTest::smart( ) const
48 return dynamic_cast<SmartInterface
*>(doc());
51 void ArbitraryHighlightTest::slotRangeChanged(SmartRange
* range
, SmartRange
* mostSpecificChild
)
53 static Attribute::Ptr ranges
[10] = {Attribute::Ptr(),Attribute::Ptr(),Attribute::Ptr(),Attribute::Ptr(),Attribute::Ptr(),Attribute::Ptr(),Attribute::Ptr(),Attribute::Ptr(),Attribute::Ptr(),Attribute::Ptr()};
54 static const QChar openBrace
= QChar('{');
55 static const QChar closeBrace
= QChar('}');
58 for (int i
= 0; i
< 10; ++i
) {
59 ranges
[i
] = new Attribute();
60 ranges
[i
]->setBackground(QColor(0xFF - (i
* 0x20), 0xFF, 0xFF));
62 //ranges[2]->setFontBold();
63 //ranges[2]->setForeground(Qt::red);
65 Attribute::Ptr
dyn(new Attribute());
66 dyn
->setBackground(Qt::blue
);
67 dyn
->setForeground(Qt::white
);
68 //dyn->setTextOutline(QPen(Qt::yellow));
69 dyn
->setEffects(Attribute::EffectFadeIn
| Attribute::EffectFadeOut
);
70 ranges
[1]->setDynamicAttribute(Attribute::ActivateMouseIn
, dyn
);
72 Attribute::Ptr
dyn2(new Attribute());
73 dyn2
->setBackground(Qt::green
);
74 dyn2
->setForeground(Qt::white
);
75 ranges
[1]->setDynamicAttribute(Attribute::ActivateCaretIn
, dyn2
);
77 ranges
[3]->setFontUnderline(true);
78 ranges
[3]->setSelectedForeground(Qt::magenta
);
79 ranges
[4]->setFontStrikeOut(true);
80 ranges
[5]->setOutline(Qt::blue
);
81 ranges
[5]->setForeground(Qt::white
);
84 SmartRange
* currentRange
= mostSpecificChild
;
85 currentRange
->deleteChildRanges();
87 Cursor current
= currentRange
->start();
90 Range textNeeded
= *currentRange
;
91 if (range
!= currentRange
) {
92 if (textNeeded
.start() >= textNeeded
.end() - Cursor(0,2)) {
93 outputRange(range
, mostSpecificChild
);
97 textNeeded
.start() += Cursor(0,1);
98 textNeeded
.end() -= Cursor(0,1);
100 current
+= Cursor(0,1);
103 text
= currentRange
->document()->textLines(textNeeded
);
105 foreach (const QString
&string
, text
) {
106 for (int i
= 0; i
< string
.length(); ++i
) {
107 if (string
.at(i
) == openBrace
) {
108 currentRange
= smart()->newSmartRange(current
, currentRange
->end(), currentRange
);
109 connect(currentRange
->primaryNotifier(), SIGNAL(mouseEnteredRange(KTextEditor::SmartRange
*, KTextEditor::View
*)), SLOT(slotMouseEnteredRange(KTextEditor::SmartRange
*, KTextEditor::View
*)));
110 connect(currentRange
->primaryNotifier(), SIGNAL(mouseExitedRange(KTextEditor::SmartRange
*, KTextEditor::View
*)), SLOT(slotMouseExitedRange(KTextEditor::SmartRange
*, KTextEditor::View
*)));
111 connect(currentRange
->primaryNotifier(), SIGNAL(caretEnteredRange(KTextEditor::SmartRange
*, KTextEditor::View
*)), SLOT(slotCaretEnteredRange(KTextEditor::SmartRange
*, KTextEditor::View
*)));
112 connect(currentRange
->primaryNotifier(), SIGNAL(caretExitedRange(KTextEditor::SmartRange
*, KTextEditor::View
*)), SLOT(slotCaretExitedRange(KTextEditor::SmartRange
*, KTextEditor::View
*)));
114 if (currentRange
->depth() < 10)
115 currentRange
->setAttribute(ranges
[currentRange
->depth()]);
117 } else if (string
.at(i
) == closeBrace
&& currentRange
->parentRange()) {
118 currentRange
->end() = current
+ Cursor(0,1);
119 currentRange
= currentRange
->parentRange();
121 current
.setColumn(current
.column() + 1);
123 current
.setPosition(current
.line() + 1, 0);
126 //outputRange(range, mostSpecificChild);
129 void ArbitraryHighlightTest::outputRange( KTextEditor::SmartRange
* range
, KTextEditor::SmartRange
* mostSpecific
)
131 kDebug() << (mostSpecific
== range
? "==> " : " ") << QString(range
->depth(), ' ') << *range
;
132 foreach (SmartRange
* child
, range
->childRanges())
133 outputRange(child
, mostSpecific
);
136 void ArbitraryHighlightTest::slotRangeDeleted( KTextEditor::SmartRange
* )
139 QTimer::singleShot(0, this, SLOT(slotCreateTopRange()));
142 void ArbitraryHighlightTest::slotCreateTopRange( )
144 m_topRange
= smart()->newSmartRange(static_cast<Document
*>(parent())->documentRange());
145 smart()->addHighlightToDocument(m_topRange
, true);
146 m_topRange
->setInsertBehavior(SmartRange::ExpandRight
);
147 connect(m_topRange
->primaryNotifier(), SIGNAL(rangeContentsChanged(KTextEditor::SmartRange
*, KTextEditor::SmartRange
*)), SLOT(slotRangeChanged(KTextEditor::SmartRange
*, KTextEditor::SmartRange
*)));
148 connect(m_topRange
->primaryNotifier(), SIGNAL(rangeDeleted(KTextEditor::SmartRange
*)), SLOT(slotRangeDeleted(KTextEditor::SmartRange
*)));
150 slotRangeChanged(m_topRange
, m_topRange
);
153 void ArbitraryHighlightTest::slotMouseEnteredRange(KTextEditor::SmartRange
* range
, KTextEditor::View
* view
)
155 kDebug() << k_funcinfo
<< *range
;
158 void ArbitraryHighlightTest::slotMouseExitedRange(KTextEditor::SmartRange
* range
, KTextEditor::View
* view
)
160 kDebug() << k_funcinfo
<< *range
;
163 void ArbitraryHighlightTest::slotCaretEnteredRange(KTextEditor::SmartRange
* range
, KTextEditor::View
* view
)
165 kDebug() << k_funcinfo
<< *range
;
168 void ArbitraryHighlightTest::slotCaretExitedRange(KTextEditor::SmartRange
* range
, KTextEditor::View
* view
)
170 kDebug() << k_funcinfo
<< *range
;
173 #include "arbitraryhighlighttest.moc"