File indexing completed on 2024-04-21 05:49:01

0001 /*
0002    SPDX-FileCopyrightText: 2010 Marco Mentasti <marcomentasti@gmail.com>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "textoutputwidget.h"
0008 
0009 #include <KLocalizedString>
0010 #include <KToolBar>
0011 #include <QAction>
0012 
0013 #include <QDateTime>
0014 #include <QFontDatabase>
0015 #include <QHBoxLayout>
0016 #include <QLocale>
0017 #include <QStyle>
0018 #include <QTextEdit>
0019 
0020 TextOutputWidget::TextOutputWidget(QWidget *parent)
0021     : QWidget(parent)
0022     , m_succesTextColor(QColor::fromRgb(3, 191, 3))
0023     , m_succesBackgroundColor(QColor::fromRgb(231, 247, 231))
0024     , m_errorTextColor(QColor::fromRgb(191, 3, 3))
0025     , m_errorBackgroundColor(QColor::fromRgb(247, 231, 231))
0026 {
0027     m_layout = new QHBoxLayout(this);
0028 
0029     m_output = new QTextEdit();
0030     m_output->setReadOnly(true);
0031 
0032     QFont fixedFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
0033 
0034     m_output->setCurrentFont(fixedFont);
0035 
0036     KToolBar *toolbar = new KToolBar(this);
0037     toolbar->setOrientation(Qt::Vertical);
0038     toolbar->setToolButtonStyle(Qt::ToolButtonIconOnly);
0039 
0040     // ensure reasonable icons sizes, like e.g. the quick-open and co. icons
0041     // the normal toolbar sizes are TOO large, e.g. for scaled stuff even more!
0042     const int iconSize = style()->pixelMetric(QStyle::PM_ButtonIconSize, nullptr, this);
0043     toolbar->setIconSize(QSize(iconSize, iconSize));
0044 
0045     /// TODO: disable actions if no results are displayed
0046 
0047     QAction *action = new QAction(QIcon::fromTheme(QStringLiteral("edit-clear")), i18nc("@action:intoolbar", "Clear"), this);
0048     toolbar->addAction(action);
0049     connect(action, &QAction::triggered, m_output, &QTextEdit::clear);
0050     m_layout->addWidget(toolbar);
0051     m_layout->addWidget(m_output, 1);
0052     m_layout->setContentsMargins(0, 0, 0, 0);
0053 
0054     setLayout(m_layout);
0055 }
0056 
0057 TextOutputWidget::~TextOutputWidget()
0058 {
0059 }
0060 
0061 void TextOutputWidget::showErrorMessage(const QString &message)
0062 {
0063     QColor previousBackgroundColor = m_output->textBackgroundColor();
0064     QColor previousColor = m_output->textColor();
0065 
0066     m_output->setTextBackgroundColor(m_errorBackgroundColor);
0067     m_output->setTextColor(m_errorTextColor);
0068 
0069     writeMessage(message);
0070 
0071     m_output->setTextBackgroundColor(previousBackgroundColor);
0072     m_output->setTextColor(previousColor);
0073 }
0074 
0075 void TextOutputWidget::showSuccessMessage(const QString &message)
0076 {
0077     QColor previousBackgroundColor = m_output->textBackgroundColor();
0078     QColor previousColor = m_output->textColor();
0079 
0080     m_output->setTextBackgroundColor(m_succesBackgroundColor);
0081     m_output->setTextColor(m_succesTextColor);
0082 
0083     writeMessage(message);
0084 
0085     m_output->setTextBackgroundColor(previousBackgroundColor);
0086     m_output->setTextColor(previousColor);
0087 }
0088 
0089 void TextOutputWidget::writeMessage(const QString &msg)
0090 {
0091     m_output->append(QStringLiteral("%1: %2\n").arg(QLocale::system().toString(QDateTime::currentDateTime(), QLocale::ShortFormat), msg));
0092 
0093     raise();
0094 }
0095 
0096 #include "moc_textoutputwidget.cpp"