Advertisement
Guest User

Untitled

a guest
Feb 17th, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.99 KB | None | 0 0
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the examples of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:BSD$
  10. ** You may use this file under the terms of the BSD license as follows:
  11. **
  12. ** "Redistribution and use in source and binary forms, with or without
  13. ** modification, are permitted provided that the following conditions are
  14. ** met:
  15. **   * Redistributions of source code must retain the above copyright
  16. **     notice, this list of conditions and the following disclaimer.
  17. **   * Redistributions in binary form must reproduce the above copyright
  18. **     notice, this list of conditions and the following disclaimer in
  19. **     the documentation and/or other materials provided with the
  20. **     distribution.
  21. **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
  22. **     the names of its contributors may be used to endorse or promote
  23. **     products derived from this software without specific prior written
  24. **     permission.
  25. **
  26. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40.  
  41. #include <QtGui>
  42.  
  43. #include "logEditor.h"
  44.  
  45.  
  46. logEditor::logEditor(QWidget *parent) : QPlainTextEdit(parent)
  47. {
  48.     lineNumberArea = new LineNumberArea(this);
  49.  
  50.     connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
  51.     connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
  52.     connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
  53.  
  54.     updateLineNumberAreaWidth(0);
  55.     highlightCurrentLine();
  56.     isUpdating=false;
  57. }
  58. void logEditor::setIsUpdating(bool updating)
  59. {
  60.     isUpdating=updating;
  61.     if(updating)
  62.     { qDebug()<<"disco";
  63.         disconnect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
  64.         disconnect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
  65.         disconnect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
  66.     } else { qDebug()<<"coco";
  67.         connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
  68.         connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
  69.         connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
  70.     }
  71. }
  72.  
  73.  
  74.  
  75. bool logEditor::event(QEvent *event)
  76. {
  77.     if (event->type() == QEvent::ToolTip)
  78.         {
  79.                /* QHelpEvent* helpEvent = static_cast <QHelpEvent*>(event);
  80.                 QPoint adjustedPos=helpEvent->pos();
  81.                 adjustedPos.setX(adjustedPos.x()-lineNumberAreaWidth());
  82.                 QTextCursor cursor = cursorForPosition(adjustedPos);
  83.                 cursor.select(QTextCursor::WordUnderCursor);
  84.                 QToolTip::showText(helpEvent->globalPos(), cursor.selectedText());*/
  85.  
  86.                 processTooltip(static_cast <QHelpEvent*>(event)->pos(),static_cast <QHelpEvent*>(event)->globalPos());
  87.                 return true;
  88.         }
  89.  
  90.         return QPlainTextEdit::event(event);
  91. }
  92.  
  93.  
  94. int logEditor::lineNumberAreaWidth()
  95. {
  96.     int digits = 1;
  97.     int max = qMax(1, blockCount());
  98.     while (max >= 10) {
  99.         max /= 10;
  100.         ++digits;
  101.     }
  102.  
  103.     int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
  104.  
  105.     return space;
  106. }
  107.  
  108. void logEditor::processTooltip(QPoint pos, QPoint globalpos)
  109. {
  110.     QPoint adjustedPos=pos;
  111.    // adjustedPos.setX(adjustedPos.x()-lineNumberAreaWidth());
  112.     QTextCursor cursor = cursorForPosition(adjustedPos);
  113.     cursor.select(QTextCursor::WordUnderCursor);
  114.     QToolTip::showText(globalpos, cursor.selectedText());
  115.  
  116. }
  117.  
  118. void logEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
  119. {
  120.     qDebug()<<"updateLineNumberAreaWidth";
  121.     setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
  122. }
  123.  
  124.  
  125.  
  126. void logEditor::updateLineNumberArea(const QRect &rect, int dy)
  127. {
  128.         qDebug()<<"updateLineNumberArea";
  129.     if (dy)
  130.         lineNumberArea->scroll(0, dy);
  131.     else
  132.         lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
  133.  
  134.     if (rect.contains(viewport()->rect()))
  135.         updateLineNumberAreaWidth(0);
  136. }
  137.  
  138.  
  139.  
  140. void logEditor::resizeEvent(QResizeEvent *e)
  141. {
  142.     QPlainTextEdit::resizeEvent(e);
  143.  
  144.     QRect cr = contentsRect();
  145.     lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
  146. }
  147.  
  148.  
  149.  
  150. void logEditor::highlightCurrentLine()
  151. {
  152.  
  153.     qDebug()<<"highlightCurrentLine";
  154.     QList<QTextEdit::ExtraSelection> extraSelections;
  155.  
  156.     if (!isReadOnly()) {
  157.         QTextEdit::ExtraSelection selection;
  158.  
  159.         QColor lineColor = QColor(Qt::yellow).lighter(160);
  160.  
  161.         selection.format.setBackground(lineColor);
  162.         selection.format.setProperty(QTextFormat::FullWidthSelection, true);
  163.         selection.cursor = textCursor();
  164.         selection.cursor.clearSelection();
  165.         extraSelections.append(selection);
  166.     }
  167.  
  168.     setExtraSelections(extraSelections);
  169. }
  170.  
  171.  
  172.  
  173. void logEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
  174. {
  175.     if(isUpdating) {qDebug() << "skipped da fuck"; return;  }
  176.     qDebug() << "Painting da fuck";
  177.     QPainter painter(lineNumberArea);
  178.     painter.fillRect(event->rect(), Qt::lightGray);
  179.  
  180.  
  181.     QTextBlock block = firstVisibleBlock();
  182.     int blockNumber = block.blockNumber();
  183.     int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
  184.     int bottom = top + (int) blockBoundingRect(block).height();
  185.  
  186.     while (block.isValid() && top <= event->rect().bottom()) {
  187.         if (block.isVisible() && bottom >= event->rect().top()) {
  188.             QString number = QString::number(blockNumber + 1);
  189.             painter.setPen(Qt::black);
  190.             painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
  191.                              Qt::AlignCenter, number);
  192.         }
  193.  
  194.         block = block.next();
  195.         top = bottom;
  196.         bottom = top + (int) blockBoundingRect(block).height();
  197.         ++blockNumber;
  198.     }
  199. }
  200.  
  201. void logEditor::dropEvent(QDropEvent *de)
  202. {
  203.     // Unpack dropped data and handle it the way you want
  204.     const QMimeData* mimeData = de->mimeData();
  205.  
  206.        // check for our needed mime type, here a file or a list of files
  207.        if (mimeData->hasUrls())
  208.        {
  209.            QList<QUrl> urlList = mimeData->urls();
  210.            // extract the local paths of the files
  211.            for (int i = 0; i < urlList.size(); ++i)
  212.            {
  213.                QFile file(urlList.at(i).toLocalFile());
  214.                if (!file.open (QIODevice::ReadOnly))
  215.                     return;
  216.  
  217.                QTextStream stream ( &file );
  218.                //QString line;
  219.                setIsUpdating();
  220.                while( !stream.atEnd() ) {
  221.                     //line = stream.readLine();
  222.                     this->appendPlainText(stream.readLine());
  223.                }
  224.                file.close(); // when your done.
  225.                setIsUpdating(false);
  226.            }
  227.  
  228.        }
  229.      /*  QFont fnt = this->font() ;
  230.        fnt.setPointSize(24) ;
  231.        QTextDocument* doc = this->document() ;
  232.        QTextCharFormat test_fmt ;
  233.        QPen pen_out(Qt::DashLine) ;
  234.        QBrush bred (Qt::red);
  235.        test_fmt.setFontUnderline(true);
  236.        test_fmt.setUnderlineStyle(QTextCharFormat::WaveUnderline);
  237.        test_fmt.setTextOutline(pen_out) ;
  238.        test_fmt.setForeground(bred);
  239.        test_fmt.setFont(fnt);
  240.  
  241.        QTextCursor curseur(doc) ;
  242.        curseur.movePosition(QTextCursor::Start) ;
  243.        curseur.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor) ;
  244.        curseur.setCharFormat(test_fmt) ;
  245.        //fnt.setPointSize(12);
  246.       // curseur.movePosition(QTextCursor::Start) ;
  247.     qDebug("Contents: %s", de->mimeData()->text().toLatin1().data());*/
  248. }
  249.  
  250. void logEditor::dragMoveEvent(QDragMoveEvent *de)
  251. {
  252.     // The event needs to be accepted here
  253.     de->accept();
  254. }
  255.  
  256. void logEditor::dragEnterEvent(QDragEnterEvent *event)
  257. {
  258.     // Set the drop action to be the proposed action.
  259.     event->acceptProposedAction();
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement