Skip to content
Snippets Groups Projects
Commit 4e6e5408 authored by dt's avatar dt
Browse files

Remove code that was long obsoleted and commented out.

parent 7c8735b7
No related branches found
No related tags found
No related merge requests found
......@@ -350,368 +350,3 @@ void OutputWindow::insertLine()
appendPlainText(QString());
}
#if 0
OutputWindow::OutputWindow(QWidget *parent)
: QAbstractScrollArea(parent)
{
max_lines = 1000;
width_used = 0;
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
same_height = true;
block_scroll = false;
setWindowTitle(tr("Application Output Window"));
setWindowIcon(QIcon(":/qt4projectmanager/images/window.png"));
}
void OutputWindow::changed() {
int remove = lines.size() - max_lines;
if (remove > 0) {
selection_start.line -= remove;
selection_end.line -= remove;
selection_start = qMax(selection_start, Selection());
selection_end = qMax(selection_end, Selection());
if (remove > verticalScrollBar()->value()) {
if (same_height)
viewport()->scroll(0, -remove * fontMetrics().lineSpacing());
else
viewport()->update();
} else {
block_scroll = true;
verticalScrollBar()->setValue(verticalScrollBar()->value() - remove);
block_scroll = false;
}
while (remove--)
lines.removeFirst();
}
verticalScrollBar()->setRange(0, lines.size() - 1);
}
bool OutputWindow::getCursorPos(int *lineNumber, int *position, const QPoint &pos) {
if (lines.isEmpty())
return false;
*lineNumber = verticalScrollBar()->value();
int x = 4 - horizontalScrollBar()->value();
int spacing = fontMetrics().lineSpacing();
int leading = fontMetrics().leading();
int height = 0;
QTextLayout textLayout;
textLayout.setFont(font());
if (same_height && pos.y() > 0) {
int skipLines = pos.y() / spacing;
height += skipLines * spacing;
*lineNumber = qMin(*lineNumber + skipLines, lines.size() - 1);
}
same_height = true;
while ( *lineNumber < lines.size()) {
textLayout.setText(lines.at(*lineNumber));
textLayout.beginLayout();
while (1) {
QTextLine line = textLayout.createLine();
if (!line.isValid())
break;
line.setLineWidth(INT_MAX/256);
height += leading;
line.setPosition(QPoint(x, height));
height += static_cast<int>(line.height());
}
textLayout.endLayout();
if (height > pos.y()) {
*position = textLayout.lineAt(0).xToCursor(pos.x());
break;
}
++*lineNumber;
}
return true;
}
void OutputWindow::setNumberOfLines(int max)
{
max_lines = qMax(1, max);
while (lines.size() > max_lines)
lines.removeLast();
changed();
}
int OutputWindow::numberOfLines() const
{
return max_lines;
}
bool OutputWindow::hasSelectedText() const
{
return selection_start != selection_end;
}
void OutputWindow::clearSelection()
{
bool hadSelectedText = hasSelectedText();
selection_start = selection_end = Selection();
if (hadSelectedText)
viewport()->update();
}
QString OutputWindow::selectedText() const
{
Selection sel_start = qMin(selection_start, selection_end);
Selection sel_end = qMax(selection_start, selection_end);
QString text;
if (sel_start.line == sel_end.line) {
text += lines.at(sel_start.line).mid(sel_start.pos, sel_end.pos - sel_start.pos);
} else {
int line = sel_start.line;
text += lines.at(line++).mid(sel_start.pos);
text += QLatin1Char('\n');
while (line < sel_end.line) {
text += lines.at(line++);
text += QLatin1Char('\n');
}
text += lines.at(sel_end.line).left(sel_end.pos);
}
return text;
}
void OutputWindow::appendOutput(const QString &text)
{
lines.append(text);
if (same_height)
viewport()->update(
QRect(0, (lines.size() - verticalScrollBar()->value() - 1) * fontMetrics().lineSpacing(),
viewport()->width(), viewport()->height()));
else
viewport()->update();
changed();
int top = lines.size() - (viewport()->height() / fontMetrics().lineSpacing());
if (verticalScrollBar()->value() == top - 1)
verticalScrollBar()->setValue(top);
}
void OutputWindow::clear()
{
clearSelection();
lines.clear();
viewport()->update();
}
void OutputWindow::copy()
{
if (hasSelectedText())
QApplication::clipboard()->setText(selectedText());
}
void OutputWindow::selectAll()
{
selection_start = Selection();
selection_end.line = lines.size() - 1;
selection_end.pos = lines.last().length() - 1;
viewport()->update();
}
void OutputWindow::scrollContentsBy(int dx, int dy)
{
if (block_scroll)
return;
if (dx && dy) {
viewport()->update();
} else if (dx && !dy) {
viewport()->scroll(dx, 0);
} else {
if (same_height) {
viewport()->scroll(0, fontMetrics().lineSpacing() * dy);
} else {
viewport()->update();
}
}
}
void OutputWindow::keyPressEvent(QKeyEvent *e)
{
bool accept = true;
if (e == QKeySequence::Copy) {
copy();
} else if (e == QKeySequence::SelectAll) {
selectAll();
} else if (e->key() == Qt::Key_Enter
|| e->key() == Qt::Key_Return) {
insertLine();
} else {
accept = false;
}
if (accept)
e->accept();
else
QAbstractScrollArea::keyPressEvent(e);
}
void OutputWindow::paintEvent(QPaintEvent *e)
{
int lineNumber = verticalScrollBar()->value();
int x = 4 - horizontalScrollBar()->value();
QPainter p(viewport());
int spacing = fontMetrics().lineSpacing();
int leading = fontMetrics().leading();
int height = 0;
QTextLayout textLayout;
textLayout.setFont(font());
QTextCharFormat selectionFormat;
selectionFormat.setBackground(palette().highlight());
selectionFormat.setForeground(palette().highlightedText());
if (e->rect().top() <= 0 && e->rect().bottom() >= viewport()->rect().bottom())
width_used = 0; // recalculate
if (same_height) {
int skipLines = e->rect().top() / spacing;
height += skipLines * spacing;
lineNumber += skipLines;
}
same_height = true;
Selection sel_start = qMin(selection_start, selection_end);
Selection sel_end = qMax(selection_start, selection_end);
while ( lineNumber < lines.size() && height <= e->rect().bottom()) {
QString line = lines.at(lineNumber);
if (line.size() == 1 && line.at(0) == QChar::ParagraphSeparator) {
int y = height + spacing/2;
p.drawLine(e->rect().left(), y, e->rect().right(), y);
height += spacing;
} else {
textLayout.setText(line);
textLayout.beginLayout();
while (1) {
QTextLine line = textLayout.createLine();
if (!line.isValid())
break;
line.setLineWidth(INT_MAX/256);
height += leading;
line.setPosition(QPoint(x, height));
height += static_cast<int>(line.height());
same_height = same_height && (line.height() + leading) == spacing;
width_used = qMax(width_used, 8 + static_cast<int>(line.naturalTextWidth()));
}
textLayout.endLayout();
if (lineNumber >= sel_start.line && lineNumber <= sel_end.line) {
QVector<QTextLayout::FormatRange> selection(1);
selection[0].start = (lineNumber == sel_start.line)? sel_start.pos : 0;
selection[0].length = ((lineNumber == sel_end.line) ? sel_end.pos : lines.at(lineNumber).size()) - selection[0].start;
selection[0].format = selectionFormat;
textLayout.draw(&p, QPoint(0, 0), selection);
} else {
textLayout.draw(&p, QPoint(0, 0));
}
}
++lineNumber;
}
horizontalScrollBar()->setRange(0, qMax(0, width_used - viewport()->width()));
if (horizontalScrollBar()->pageStep() != viewport()->width())
horizontalScrollBar()->setPageStep(viewport()->width());
if (height > viewport()->height())
verticalScrollBar()->setPageStep(lineNumber - verticalScrollBar()->value());
else if (verticalScrollBar()->pageStep() != viewport()->height() / fontMetrics().lineSpacing())
verticalScrollBar()->setPageStep(viewport()->height() / fontMetrics().lineSpacing());
}
void OutputWindow::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton) {
clearSelection();
if (getCursorPos(&selection_start.line, &selection_start.pos, e->pos())) {
selection_end = selection_start;
autoscroll = 0;
}
}
}
void OutputWindow::timerEvent(QTimerEvent *e)
{
if (e->timerId() == autoscroll_timer.timerId()) {
int autoscroll = 0;
if (lastMouseMove.y() < 0)
autoscroll = -1;
else if (lastMouseMove.y() > viewport()->height())
autoscroll = 1;
if (autoscroll) {
verticalScrollBar()->setValue(verticalScrollBar()->value() + autoscroll);
OutputWindow::mouseMoveEvent(0);
}
}
QAbstractScrollArea::timerEvent(e);
}
void OutputWindow::mouseReleaseEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton) {
autoscroll_timer.stop();
if (hasSelectedText() && QApplication::clipboard()->supportsSelection())
QApplication::clipboard()->setText(selectedText(), QClipboard::Selection);
}
}
void OutputWindow::mouseMoveEvent(QMouseEvent *e)
{
if (e) {
lastMouseMove = e->pos();
if (viewport()->rect().contains(e->pos()))
autoscroll_timer.stop();
else
autoscroll_timer.start(20, this);
}
Selection old = selection_end;
if (!getCursorPos(&selection_end.line, &selection_end.pos, lastMouseMove))
return;
if (same_height) {
Selection from = qMin(old, selection_end);
Selection to = qMax(old, selection_end);
viewport()->update(QRect(0, -1 + (from.line - verticalScrollBar()->value()) * fontMetrics().lineSpacing(),
viewport()->width(), 2 + (to.line - from.line + 1) * fontMetrics().lineSpacing()));
} else {
viewport()->update();
}
}
void OutputWindow::contextMenuEvent(QContextMenuEvent * e)
{
QMenu menu(this);
QAction *clearAction = menu.addAction("Clear", this, SLOT(clear()));
QAction *copyAction = menu.addAction("Copy", this, SLOT(copy()), QKeySequence::Copy);
QAction *selectAllAction = menu.addAction("Select All", this, SLOT(selectAll()), QKeySequence::SelectAll);
if (lines.empty()) {
clearAction->setDisabled(true);
selectAllAction->setDisabled(true);
}
if (!hasSelectedText())
copyAction->setDisabled(true);
menu.exec(e->globalPos());
}
#endif // 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment