52 #if QT_VERSION >= 0x050000
58 #include "TextBrowserWidget.hh"
60 QString
const TextBrowserWidget::startRenderObjectTag_ =
"name:";
61 QString
const TextBrowserWidget::startVertexShaderTag_ =
"--vertex-shader--";
62 QString
const TextBrowserWidget::endVertexShaderTag_ =
"--end-vertex-shader--";
63 QString
const TextBrowserWidget::startTessControlShaderTag_ =
"---tesscontrol-shader--";
64 QString
const TextBrowserWidget::endTessControlShaderTag_ =
"--end-tesscontrol-shader--";
65 QString
const TextBrowserWidget::startTessEvalShaderTag_ =
"--tesseval-shader--";
66 QString
const TextBrowserWidget::endTessEvalShaderTag_ =
"--end-tesseval-shader--";
67 QString
const TextBrowserWidget::startGeometryShaderTag_ =
"--geometry-shader--";
68 QString
const TextBrowserWidget::endGeometryShaderTag_ =
"--end-geometry-shader--";
69 QString
const TextBrowserWidget::startFragmentShaderTag_ =
"--fragment-shader--";
70 QString
const TextBrowserWidget::endFragmentShaderTag_ =
"--end-fragment-shader--";
73 TextBrowserWidget::TextBrowserWidget(QWidget *parent) : QPlainTextEdit(parent) {
75 updateTextBrowserSideAreaWidth();
77 connect(
this, SIGNAL(blockCountChanged(
int)),
this, SLOT(updateTextBrowserSideAreaWidth()));
78 connect(
this, SIGNAL(blockCountChanged(
int)),
this, SLOT(updateFolds()));
79 connect(
this, SIGNAL(updateRequest(QRect,
int)),
this, SLOT(updateTextBrowserSideArea(QRect,
int)));
86 int TextBrowserWidget::sideAreaWidth() {
88 int max = qMax(1, blockCount());
94 int space = 3 + fontMetrics().width(QLatin1Char(
'9')) * digits;
101 void TextBrowserWidget::updateTextBrowserSideAreaWidth() {
102 setViewportMargins(sideAreaWidth(), 0, 0, 0);
105 void TextBrowserWidget::updateTextBrowserSideArea(
const QRect &rect,
int dy) {
107 sideArea_->scroll(0, dy);
109 sideArea_->update(0, rect.y(), sideArea_->width(), rect.height());
111 if (rect.contains(viewport()->rect()))
112 updateTextBrowserSideAreaWidth();
117 void TextBrowserWidget::resizeEvent(QResizeEvent *e) {
118 QPlainTextEdit::resizeEvent(e);
120 QRect cr = contentsRect();
121 sideArea_->setGeometry(QRect(cr.left(), cr.top(), sideAreaWidth(), cr.height()));
124 void TextBrowserWidget::sideAreaPaintEvent(QPaintEvent *event) {
126 QPainter painter(sideArea_);
127 painter.fillRect(event->rect(), Qt::lightGray);
128 painter.setPen(Qt::black);
130 QTextBlock block = firstVisibleBlock();
132 int blockNumber = block.blockNumber();
133 int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
134 int bottom = top + (int) blockBoundingRect(block).height();
137 while (block.isValid() && top <=
event->rect().bottom()) {
138 if (block.isVisible() && bottom >=
event->rect().top()) {
139 if (getFold(block.position(), found_fold)) {
140 if (found_fold.type == SHADER) {
141 int fold_first_block = document()->findBlock(found_fold.start).blockNumber();
142 QString text = block.text();
144 if (text.contains(TextBrowserWidget::startVertexShaderTag_) ||
145 text.contains(TextBrowserWidget::endVertexShaderTag_) ||
146 text.contains(TextBrowserWidget::startTessControlShaderTag_) ||
147 text.contains(TextBrowserWidget::endTessControlShaderTag_) ||
148 text.contains(TextBrowserWidget::startTessEvalShaderTag_) ||
149 text.contains(TextBrowserWidget::endTessEvalShaderTag_) ||
150 text.contains(TextBrowserWidget::startGeometryShaderTag_) ||
151 text.contains(TextBrowserWidget::endGeometryShaderTag_) ||
152 text.contains(TextBrowserWidget::startFragmentShaderTag_) ||
153 text.contains(TextBrowserWidget::endFragmentShaderTag_)) {
154 if (found_fold.folded)
155 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight,
"+");
157 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight,
"-");
159 QString number = QString::number(blockNumber - fold_first_block);
160 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight, number);
163 if (found_fold.folded)
164 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight,
"+");
166 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight,
"-");
169 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight,
" ");
172 block = block.next();
174 bottom = top + (int) blockBoundingRect(block).height();
180 std::map<int,size_t>::iterator it = blockPosToFold_.find(_position);
181 if (!folds_.empty() && it != blockPosToFold_.end()) {
182 _fold = folds_[it->second];
188 void TextBrowserWidget::mouseDoubleClickEvent(QMouseEvent* e) {
189 QTextBlock block = firstVisibleBlock();
190 int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
191 int bottom = top + (int) blockBoundingRect(block).height();
192 const int y = e->y();
194 while (block.isValid()) {
195 if (top <= y && y <= bottom) {
196 toggleFold(block.position());
200 block = block.next();
202 bottom = top + (int) blockBoundingRect(block).height();
206 void TextBrowserWidget::foldAll() {
207 for (std::vector<Fold>::iterator it = folds_.begin(); it != folds_.end(); ++it) {
212 void TextBrowserWidget::unfoldAll() {
213 for (std::vector<Fold>::iterator it = folds_.begin(); it != folds_.end(); ++it) {
218 void TextBrowserWidget::fold(Fold& _fold) {
222 QTextBlock startBlock = document()->findBlock(_fold.start);
223 QTextBlock endBlock = document()->findBlock(_fold.end);
225 startBlock = startBlock.next();
226 while (startBlock.isValid() && startBlock != endBlock) {
227 startBlock.setVisible(
false);
228 startBlock = startBlock.next();
230 if (_fold.type == RENDEROBJECT)
231 endBlock.setVisible(
false);
234 document()->markContentsDirty(_fold.start, _fold.end - _fold.start);
237 void TextBrowserWidget::unfold(Fold& _fold) {
241 QTextBlock startBlock = document()->findBlock(_fold.start);
242 QTextBlock endBlock = document()->findBlock(_fold.end);
244 startBlock = startBlock.next();
245 while (startBlock.isValid() && startBlock != endBlock) {
246 startBlock.setVisible(
true);
247 startBlock = startBlock.next();
249 if (_fold.type == RENDEROBJECT)
250 endBlock.setVisible(
true);
252 _fold.folded =
false;
253 document()->markContentsDirty(_fold.start, _fold.end-_fold.start);
256 void TextBrowserWidget::toggleFold(
int _position) {
257 for (std::vector<Fold>::iterator it = folds_.begin(); it != folds_.end(); ++it) {
258 if (it->contains(_position)) {
269 void TextBrowserWidget::updateFolds() {
273 QTextCursor startCursor = document()->find(TextBrowserWidget::startVertexShaderTag_, 0, QTextDocument::FindWholeWords);
274 QTextCursor endCursor = document()->find(TextBrowserWidget::endVertexShaderTag_, 0, QTextDocument::FindWholeWords);
276 while (!startCursor.isNull() && !endCursor.isNull()) {
277 startCursor.movePosition(QTextCursor::StartOfLine);
278 endCursor.movePosition(QTextCursor::EndOfLine);
279 folds_.push_back(Fold(startCursor.position(),endCursor.position(),SHADER));
282 int startPos = startCursor.position();
283 const int endPos = endCursor.position();
284 for (; startPos < endPos; ++startPos) {
285 QTextBlock block = document()->findBlock(startPos);
286 blockPosToFold_[block.position()] = folds_.size() - 1;
289 bool moved = startCursor.movePosition(QTextCursor::Down);
292 moved = endCursor.movePosition(QTextCursor::Down);
296 startCursor = document()->find(TextBrowserWidget::startVertexShaderTag_, startCursor, QTextDocument::FindWholeWords);
297 endCursor = document()->find(TextBrowserWidget::endVertexShaderTag_, endCursor, QTextDocument::FindWholeWords);
301 startCursor = document()->find(TextBrowserWidget::startTessControlShaderTag_, 0, QTextDocument::FindWholeWords);
302 endCursor = document()->find(TextBrowserWidget::endTessControlShaderTag_, 0, QTextDocument::FindWholeWords);
304 while (!startCursor.isNull() && !endCursor.isNull()) {
305 startCursor.movePosition(QTextCursor::StartOfLine);
306 endCursor.movePosition(QTextCursor::EndOfLine);
307 folds_.push_back(Fold(startCursor.position(),endCursor.position(),SHADER));
310 int startPos = startCursor.position();
311 const int endPos = endCursor.position();
312 for (; startPos < endPos; ++startPos) {
313 QTextBlock block = document()->findBlock(startPos);
314 blockPosToFold_[block.position()] = folds_.size() - 1;
317 bool moved = startCursor.movePosition(QTextCursor::Down);
320 moved = endCursor.movePosition(QTextCursor::Down);
324 startCursor = document()->find(TextBrowserWidget::startTessControlShaderTag_, startCursor, QTextDocument::FindWholeWords);
325 endCursor = document()->find(TextBrowserWidget::endTessControlShaderTag_, endCursor, QTextDocument::FindWholeWords);
329 startCursor = document()->find(TextBrowserWidget::startTessEvalShaderTag_, 0, QTextDocument::FindWholeWords);
330 endCursor = document()->find(TextBrowserWidget::endTessEvalShaderTag_, 0, QTextDocument::FindWholeWords);
332 while (!startCursor.isNull() && !endCursor.isNull()) {
333 startCursor.movePosition(QTextCursor::StartOfLine);
334 endCursor.movePosition(QTextCursor::EndOfLine);
335 folds_.push_back(Fold(startCursor.position(),endCursor.position(),SHADER));
338 int startPos = startCursor.position();
339 const int endPos = endCursor.position();
340 for (; startPos < endPos; ++startPos) {
341 QTextBlock block = document()->findBlock(startPos);
342 blockPosToFold_[block.position()] = folds_.size() - 1;
345 bool moved = startCursor.movePosition(QTextCursor::Down);
348 moved = endCursor.movePosition(QTextCursor::Down);
352 startCursor = document()->find(TextBrowserWidget::startTessEvalShaderTag_, startCursor, QTextDocument::FindWholeWords);
353 endCursor = document()->find(TextBrowserWidget::endTessEvalShaderTag_, endCursor, QTextDocument::FindWholeWords);
357 startCursor = document()->find(TextBrowserWidget::startGeometryShaderTag_, 0, QTextDocument::FindWholeWords);
358 endCursor = document()->find(TextBrowserWidget::endGeometryShaderTag_, 0, QTextDocument::FindWholeWords);
360 while (!startCursor.isNull() && !endCursor.isNull()) {
361 startCursor.movePosition(QTextCursor::StartOfLine);
362 endCursor.movePosition(QTextCursor::EndOfLine);
363 folds_.push_back(Fold(startCursor.position(),endCursor.position(),SHADER));
366 int startPos = startCursor.position();
367 const int endPos = endCursor.position();
368 for (; startPos < endPos; ++startPos) {
369 QTextBlock block = document()->findBlock(startPos);
370 blockPosToFold_[block.position()] = folds_.size() - 1;
373 bool moved = startCursor.movePosition(QTextCursor::Down);
376 moved = endCursor.movePosition(QTextCursor::Down);
380 startCursor = document()->find(TextBrowserWidget::startGeometryShaderTag_, startCursor, QTextDocument::FindWholeWords);
381 endCursor = document()->find(TextBrowserWidget::endGeometryShaderTag_, endCursor, QTextDocument::FindWholeWords);
385 startCursor = document()->find(TextBrowserWidget::startFragmentShaderTag_, 0, QTextDocument::FindWholeWords);
386 endCursor = document()->find(TextBrowserWidget::endFragmentShaderTag_, 0, QTextDocument::FindWholeWords);
388 while (!startCursor.isNull() && !endCursor.isNull()) {
389 startCursor.movePosition(QTextCursor::StartOfLine);
390 endCursor.movePosition(QTextCursor::EndOfLine);
391 folds_.push_back(Fold(startCursor.position(),endCursor.position(),SHADER));
394 int startPos = startCursor.position();
395 const int endPos = endCursor.position();
396 for (; startPos < endPos; ++startPos) {
397 QTextBlock block = document()->findBlock(startPos);
398 blockPosToFold_[block.position()] = folds_.size() - 1;
401 bool moved = startCursor.movePosition(QTextCursor::Down);
404 moved = endCursor.movePosition(QTextCursor::Down);
408 startCursor = document()->find(TextBrowserWidget::startFragmentShaderTag_, startCursor, QTextDocument::FindWholeWords);
409 endCursor = document()->find(TextBrowserWidget::endFragmentShaderTag_, endCursor, QTextDocument::FindWholeWords);
413 startCursor = document()->find(TextBrowserWidget::startRenderObjectTag_, 0, QTextDocument::FindWholeWords);
414 endCursor = document()->find(TextBrowserWidget::startVertexShaderTag_, 0, QTextDocument::FindWholeWords);
416 while (!startCursor.isNull() && !endCursor.isNull()) {
417 startCursor.movePosition(QTextCursor::StartOfLine);
419 endCursor.movePosition(QTextCursor::Up);
420 endCursor.movePosition(QTextCursor::EndOfLine);
421 folds_.push_back(Fold(startCursor.position(),endCursor.position(),RENDEROBJECT));
424 int startPos = startCursor.position();
425 const int endPos = endCursor.position();
426 for (; startPos < endPos; ++startPos) {
427 QTextBlock block = document()->findBlock(startPos);
428 blockPosToFold_[block.position()] = folds_.size() - 1;
431 bool moved = startCursor.movePosition(QTextCursor::Down);
435 moved = endCursor.movePosition(QTextCursor::Down);
438 moved = endCursor.movePosition(QTextCursor::Down);
442 startCursor = document()->find(TextBrowserWidget::startRenderObjectTag_, startCursor, QTextDocument::FindWholeWords);
443 endCursor = document()->find(TextBrowserWidget::startVertexShaderTag_, endCursor, QTextDocument::FindWholeWords);
bool getFold(int _position, Fold &_fold)
get the _fold corresponding to the document _position