46 #include "TextBrowserWidget.hh" 48 QString
const TextBrowserWidget::startRenderObjectTag_ =
"name:";
49 QString
const TextBrowserWidget::startVertexShaderTag_ =
"--vertex-shader--";
50 QString
const TextBrowserWidget::endVertexShaderTag_ =
"--end-vertex-shader--";
51 QString
const TextBrowserWidget::startTessControlShaderTag_ =
"---tesscontrol-shader--";
52 QString
const TextBrowserWidget::endTessControlShaderTag_ =
"--end-tesscontrol-shader--";
53 QString
const TextBrowserWidget::startTessEvalShaderTag_ =
"--tesseval-shader--";
54 QString
const TextBrowserWidget::endTessEvalShaderTag_ =
"--end-tesseval-shader--";
55 QString
const TextBrowserWidget::startGeometryShaderTag_ =
"--geometry-shader--";
56 QString
const TextBrowserWidget::endGeometryShaderTag_ =
"--end-geometry-shader--";
57 QString
const TextBrowserWidget::startFragmentShaderTag_ =
"--fragment-shader--";
58 QString
const TextBrowserWidget::endFragmentShaderTag_ =
"--end-fragment-shader--";
61 TextBrowserWidget::TextBrowserWidget(QWidget *parent) : QPlainTextEdit(parent) {
63 updateTextBrowserSideAreaWidth();
65 connect(
this, SIGNAL(blockCountChanged(
int)),
this, SLOT(updateTextBrowserSideAreaWidth()));
66 connect(
this, SIGNAL(blockCountChanged(
int)),
this, SLOT(updateFolds()));
67 connect(
this, SIGNAL(updateRequest(QRect,
int)),
this, SLOT(updateTextBrowserSideArea(QRect,
int)));
74 int TextBrowserWidget::sideAreaWidth() {
76 int max = qMax(1, blockCount());
82 int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char(
'9')) * digits;
89 void TextBrowserWidget::updateTextBrowserSideAreaWidth() {
90 setViewportMargins(sideAreaWidth(), 0, 0, 0);
93 void TextBrowserWidget::updateTextBrowserSideArea(
const QRect &rect,
int dy) {
95 sideArea_->scroll(0, dy);
97 sideArea_->update(0, rect.y(), sideArea_->width(), rect.height());
99 if (rect.contains(viewport()->rect()))
100 updateTextBrowserSideAreaWidth();
105 void TextBrowserWidget::resizeEvent(QResizeEvent *e) {
106 QPlainTextEdit::resizeEvent(e);
108 QRect cr = contentsRect();
109 sideArea_->setGeometry(QRect(cr.left(), cr.top(), sideAreaWidth(), cr.height()));
112 void TextBrowserWidget::sideAreaPaintEvent(QPaintEvent *event) {
114 QPainter painter(sideArea_);
115 painter.fillRect(event->rect(), Qt::lightGray);
116 painter.setPen(Qt::black);
118 QTextBlock block = firstVisibleBlock();
120 int blockNumber = block.blockNumber();
121 int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
122 int bottom = top + (int) blockBoundingRect(block).height();
125 while (block.isValid() && top <=
event->rect().bottom()) {
126 if (block.isVisible() && bottom >=
event->rect().top()) {
127 if (getFold(block.position(), found_fold)) {
128 if (found_fold.type == SHADER) {
129 int fold_first_block = document()->findBlock(found_fold.start).blockNumber();
130 QString text = block.text();
132 if (text.contains(TextBrowserWidget::startVertexShaderTag_) ||
133 text.contains(TextBrowserWidget::endVertexShaderTag_) ||
134 text.contains(TextBrowserWidget::startTessControlShaderTag_) ||
135 text.contains(TextBrowserWidget::endTessControlShaderTag_) ||
136 text.contains(TextBrowserWidget::startTessEvalShaderTag_) ||
137 text.contains(TextBrowserWidget::endTessEvalShaderTag_) ||
138 text.contains(TextBrowserWidget::startGeometryShaderTag_) ||
139 text.contains(TextBrowserWidget::endGeometryShaderTag_) ||
140 text.contains(TextBrowserWidget::startFragmentShaderTag_) ||
141 text.contains(TextBrowserWidget::endFragmentShaderTag_)) {
142 if (found_fold.folded)
143 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight,
"+");
145 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight,
"-");
147 QString number = QString::number(blockNumber - fold_first_block);
148 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight, number);
151 if (found_fold.folded)
152 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight,
"+");
154 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight,
"-");
157 painter.drawText(0, top, sideArea_->width(), fontMetrics().height(),Qt::AlignRight,
" ");
160 block = block.next();
162 bottom = top + (int) blockBoundingRect(block).height();
168 std::map<int,size_t>::iterator it = blockPosToFold_.find(_position);
169 if (!folds_.empty() && it != blockPosToFold_.end()) {
170 _fold = folds_[it->second];
176 void TextBrowserWidget::mouseDoubleClickEvent(QMouseEvent* e) {
177 QTextBlock block = firstVisibleBlock();
178 int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
179 int bottom = top + (int) blockBoundingRect(block).height();
180 const int y = e->y();
182 while (block.isValid()) {
183 if (top <= y && y <= bottom) {
184 toggleFold(block.position());
188 block = block.next();
190 bottom = top + (int) blockBoundingRect(block).height();
194 void TextBrowserWidget::foldAll() {
195 for (std::vector<Fold>::iterator it = folds_.begin(); it != folds_.end(); ++it) {
200 void TextBrowserWidget::unfoldAll() {
201 for (std::vector<Fold>::iterator it = folds_.begin(); it != folds_.end(); ++it) {
206 void TextBrowserWidget::fold(
Fold& _fold) {
210 QTextBlock startBlock = document()->findBlock(_fold.start);
211 QTextBlock endBlock = document()->findBlock(_fold.end);
213 startBlock = startBlock.next();
214 while (startBlock.isValid() && startBlock != endBlock) {
215 startBlock.setVisible(
false);
216 startBlock = startBlock.next();
218 if (_fold.type == RENDEROBJECT)
219 endBlock.setVisible(
false);
222 document()->markContentsDirty(_fold.start, _fold.end - _fold.start);
225 void TextBrowserWidget::unfold(
Fold& _fold) {
229 QTextBlock startBlock = document()->findBlock(_fold.start);
230 QTextBlock endBlock = document()->findBlock(_fold.end);
232 startBlock = startBlock.next();
233 while (startBlock.isValid() && startBlock != endBlock) {
234 startBlock.setVisible(
true);
235 startBlock = startBlock.next();
237 if (_fold.type == RENDEROBJECT)
238 endBlock.setVisible(
true);
240 _fold.folded =
false;
241 document()->markContentsDirty(_fold.start, _fold.end-_fold.start);
244 void TextBrowserWidget::toggleFold(
int _position) {
245 for (std::vector<Fold>::iterator it = folds_.begin(); it != folds_.end(); ++it) {
246 if (it->contains(_position)) {
257 void TextBrowserWidget::updateFolds() {
261 QTextCursor startCursor = document()->find(TextBrowserWidget::startVertexShaderTag_, 0, QTextDocument::FindWholeWords);
262 QTextCursor endCursor = document()->find(TextBrowserWidget::endVertexShaderTag_, 0, QTextDocument::FindWholeWords);
264 while (!startCursor.isNull() && !endCursor.isNull()) {
265 startCursor.movePosition(QTextCursor::StartOfLine);
266 endCursor.movePosition(QTextCursor::EndOfLine);
267 folds_.push_back(
Fold(startCursor.position(),endCursor.position(),SHADER));
270 int startPos = startCursor.position();
271 const int endPos = endCursor.position();
272 for (; startPos < endPos; ++startPos) {
273 QTextBlock block = document()->findBlock(startPos);
274 blockPosToFold_[block.position()] = folds_.size() - 1;
277 bool moved = startCursor.movePosition(QTextCursor::Down);
280 moved = endCursor.movePosition(QTextCursor::Down);
284 startCursor = document()->find(TextBrowserWidget::startVertexShaderTag_, startCursor, QTextDocument::FindWholeWords);
285 endCursor = document()->find(TextBrowserWidget::endVertexShaderTag_, endCursor, QTextDocument::FindWholeWords);
289 startCursor = document()->find(TextBrowserWidget::startTessControlShaderTag_, 0, QTextDocument::FindWholeWords);
290 endCursor = document()->find(TextBrowserWidget::endTessControlShaderTag_, 0, QTextDocument::FindWholeWords);
292 while (!startCursor.isNull() && !endCursor.isNull()) {
293 startCursor.movePosition(QTextCursor::StartOfLine);
294 endCursor.movePosition(QTextCursor::EndOfLine);
295 folds_.push_back(
Fold(startCursor.position(),endCursor.position(),SHADER));
298 int startPos = startCursor.position();
299 const int endPos = endCursor.position();
300 for (; startPos < endPos; ++startPos) {
301 QTextBlock block = document()->findBlock(startPos);
302 blockPosToFold_[block.position()] = folds_.size() - 1;
305 bool moved = startCursor.movePosition(QTextCursor::Down);
308 moved = endCursor.movePosition(QTextCursor::Down);
312 startCursor = document()->find(TextBrowserWidget::startTessControlShaderTag_, startCursor, QTextDocument::FindWholeWords);
313 endCursor = document()->find(TextBrowserWidget::endTessControlShaderTag_, endCursor, QTextDocument::FindWholeWords);
317 startCursor = document()->find(TextBrowserWidget::startTessEvalShaderTag_, 0, QTextDocument::FindWholeWords);
318 endCursor = document()->find(TextBrowserWidget::endTessEvalShaderTag_, 0, QTextDocument::FindWholeWords);
320 while (!startCursor.isNull() && !endCursor.isNull()) {
321 startCursor.movePosition(QTextCursor::StartOfLine);
322 endCursor.movePosition(QTextCursor::EndOfLine);
323 folds_.push_back(
Fold(startCursor.position(),endCursor.position(),SHADER));
326 int startPos = startCursor.position();
327 const int endPos = endCursor.position();
328 for (; startPos < endPos; ++startPos) {
329 QTextBlock block = document()->findBlock(startPos);
330 blockPosToFold_[block.position()] = folds_.size() - 1;
333 bool moved = startCursor.movePosition(QTextCursor::Down);
336 moved = endCursor.movePosition(QTextCursor::Down);
340 startCursor = document()->find(TextBrowserWidget::startTessEvalShaderTag_, startCursor, QTextDocument::FindWholeWords);
341 endCursor = document()->find(TextBrowserWidget::endTessEvalShaderTag_, endCursor, QTextDocument::FindWholeWords);
345 startCursor = document()->find(TextBrowserWidget::startGeometryShaderTag_, 0, QTextDocument::FindWholeWords);
346 endCursor = document()->find(TextBrowserWidget::endGeometryShaderTag_, 0, QTextDocument::FindWholeWords);
348 while (!startCursor.isNull() && !endCursor.isNull()) {
349 startCursor.movePosition(QTextCursor::StartOfLine);
350 endCursor.movePosition(QTextCursor::EndOfLine);
351 folds_.push_back(
Fold(startCursor.position(),endCursor.position(),SHADER));
354 int startPos = startCursor.position();
355 const int endPos = endCursor.position();
356 for (; startPos < endPos; ++startPos) {
357 QTextBlock block = document()->findBlock(startPos);
358 blockPosToFold_[block.position()] = folds_.size() - 1;
361 bool moved = startCursor.movePosition(QTextCursor::Down);
364 moved = endCursor.movePosition(QTextCursor::Down);
368 startCursor = document()->find(TextBrowserWidget::startGeometryShaderTag_, startCursor, QTextDocument::FindWholeWords);
369 endCursor = document()->find(TextBrowserWidget::endGeometryShaderTag_, endCursor, QTextDocument::FindWholeWords);
373 startCursor = document()->find(TextBrowserWidget::startFragmentShaderTag_, 0, QTextDocument::FindWholeWords);
374 endCursor = document()->find(TextBrowserWidget::endFragmentShaderTag_, 0, QTextDocument::FindWholeWords);
376 while (!startCursor.isNull() && !endCursor.isNull()) {
377 startCursor.movePosition(QTextCursor::StartOfLine);
378 endCursor.movePosition(QTextCursor::EndOfLine);
379 folds_.push_back(
Fold(startCursor.position(),endCursor.position(),SHADER));
382 int startPos = startCursor.position();
383 const int endPos = endCursor.position();
384 for (; startPos < endPos; ++startPos) {
385 QTextBlock block = document()->findBlock(startPos);
386 blockPosToFold_[block.position()] = folds_.size() - 1;
389 bool moved = startCursor.movePosition(QTextCursor::Down);
392 moved = endCursor.movePosition(QTextCursor::Down);
396 startCursor = document()->find(TextBrowserWidget::startFragmentShaderTag_, startCursor, QTextDocument::FindWholeWords);
397 endCursor = document()->find(TextBrowserWidget::endFragmentShaderTag_, endCursor, QTextDocument::FindWholeWords);
401 startCursor = document()->find(TextBrowserWidget::startRenderObjectTag_, 0, QTextDocument::FindWholeWords);
402 endCursor = document()->find(TextBrowserWidget::startVertexShaderTag_, 0, QTextDocument::FindWholeWords);
404 while (!startCursor.isNull() && !endCursor.isNull()) {
405 startCursor.movePosition(QTextCursor::StartOfLine);
407 endCursor.movePosition(QTextCursor::Up);
408 endCursor.movePosition(QTextCursor::EndOfLine);
409 folds_.push_back(
Fold(startCursor.position(),endCursor.position(),RENDEROBJECT));
412 int startPos = startCursor.position();
413 const int endPos = endCursor.position();
414 for (; startPos < endPos; ++startPos) {
415 QTextBlock block = document()->findBlock(startPos);
416 blockPosToFold_[block.position()] = folds_.size() - 1;
419 bool moved = startCursor.movePosition(QTextCursor::Down);
423 moved = endCursor.movePosition(QTextCursor::Down);
426 moved = endCursor.movePosition(QTextCursor::Down);
430 startCursor = document()->find(TextBrowserWidget::startRenderObjectTag_, startCursor, QTextDocument::FindWholeWords);
431 endCursor = document()->find(TextBrowserWidget::startVertexShaderTag_, endCursor, QTextDocument::FindWholeWords);
bool getFold(int _position, Fold &_fold)
get the _fold corresponding to the document _position