File indexing completed on 2024-06-16 11:34:29

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2005 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "logview.h"
0012 
0013 #include <KLocalizedString>
0014 #include <katemdi.h>
0015 
0016 // #include <q3popupmenu.h>
0017 #include <QEvent>
0018 #include <QLayout>
0019 #include <QMenu>
0020 
0021 #include <ktechlab_debug.h>
0022 
0023 // BEGIN class LogView
0024 LogView::LogView(KateMDI::ToolView *parent)
0025     : KTextEdit(parent)
0026 {
0027     if (parent->layout()) {
0028         parent->layout()->addWidget(this);
0029         qCDebug(KTL_LOG) << " added item selector to parent's layout " << parent;
0030     } else {
0031         qCWarning(KTL_LOG) << " unexpected null layout on parent " << parent;
0032     }
0033 
0034     setReadOnly(true);
0035     // setPaper( Qt::white ); // TODO re-enable this, get an equivalent
0036     // setTextFormat( Qt::LogText ); // 2018.12.07 - use toHtml() and html() methods
0037     // setWordWrap( WidgetWidth );
0038     setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
0039     setFocusPolicy(Qt::NoFocus);
0040 
0041     // Connect up signal emitted when the user doubleclicks on a paragraph in the log view
0042     // connect( this, SIGNAL(clicked(int,int)), this, SLOT(slotParaClicked(int,int)) );
0043     // ^ reimplemented by: mouseDoubleClickEvent()
0044 }
0045 
0046 LogView::~LogView()
0047 {
0048 }
0049 
0050 void LogView::clear()
0051 {
0052     m_messageInfoMap.clear();
0053     KTextEdit::clear();
0054 }
0055 
0056 void LogView::addOutput(QString text, OutputType outputType, MessageInfo messageInfo)
0057 {
0058     tidyText(text);
0059     switch (outputType) {
0060     case LogView::ot_important:
0061         append(QString("<font color=\"#000000\"><b>%1</b></font>").arg(text));
0062         break;
0063 
0064     case LogView::ot_info:
0065         append(QString("<font color=\"#000000\"><i>%1</i></font>").arg(text));
0066         break;
0067 
0068     case LogView::ot_message:
0069         append(QString("<font color=\"#000000\">%1</font>").arg(text));
0070         break;
0071 
0072     case LogView::ot_warning:
0073         append(QString("<font color=\"#666666\">%1</font>").arg(text));
0074         break;
0075 
0076     case LogView::ot_error:
0077         append(QString("<font color=\"#800000\">%1</font>").arg(text));
0078         break;
0079     }
0080 
0081     // m_messageInfoMap[  paragraphs()-1 ] = messageInfo;
0082     m_messageInfoMap[document()->blockCount() - 1] = messageInfo;
0083 }
0084 
0085 void LogView::mouseDoubleClickEvent(QMouseEvent *e)
0086 {
0087     QTextCursor curs = cursorForPosition(e->pos());
0088     slotParaClicked(curs.blockNumber(), curs.columnNumber());
0089     QTextEdit::mouseDoubleClickEvent(e); // note: is this needed?
0090 }
0091 
0092 void LogView::slotParaClicked(int para, int /*pos*/)
0093 {
0094     // QString t = text(para);
0095     QString t = document()->findBlockByLineNumber(para).text();
0096     untidyText(t);
0097     emit paraClicked(t, m_messageInfoMap[para]);
0098 }
0099 
0100 void LogView::tidyText(QString &t)
0101 {
0102     t.replace("&", "&amp;");
0103     t.replace("<", "&lt;");
0104     t.replace(">", "&gt;");
0105 }
0106 
0107 void LogView::untidyText(QString &t)
0108 {
0109     t.replace("&lt;", "<");
0110     t.replace("&gt;", ">");
0111     t.replace("&amp;", "&");
0112 }
0113 
0114 QMenu *LogView::createPopupMenu(const QPoint &pos)
0115 {
0116     QMenu *menu = KTextEdit::createStandardContextMenu(pos);
0117 
0118     // menu->insertSeparator(); // 2018.12.07
0119     menu->addSeparator();
0120     // int id = menu->insertItem( i18n("Clear All"), this, SLOT(clear()) ); // 2018.12.07
0121     QAction *clearAllAct = menu->addAction(i18n("Clear All"), this, SLOT(clear()));
0122     clearAllAct->setEnabled(document()->blockCount() > 1);
0123 
0124     //// "an empty textedit is always considered to have one paragraph" - qt documentation
0125     //// although this does not always seem to be the case, so I don't know...
0126     ////menu->setItemEnabled( id, paragraphs() > 1 );
0127     // menu->setItemEnabled( id, document()->blockCount() > 1 );
0128 
0129     return menu;
0130 }
0131 // END class LogView
0132 
0133 // BEGIN class MessageInfo
0134 MessageInfo::MessageInfo()
0135 {
0136     m_fileLine = -1;
0137 }
0138 
0139 MessageInfo::MessageInfo(QString fileURL, int fileLine)
0140 {
0141     m_fileURL = fileURL;
0142     m_fileLine = fileLine;
0143 }
0144 // END class MessageInfo
0145 
0146 #include "moc_logview.cpp"