File indexing completed on 2022-09-27 12:31:10

0001 /*
0002     SPDX-FileCopyrightText: 2003-2008 Cies Breijs <cies AT kde DOT nl>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "errordialog.h"
0008 
0009 #include <QBoxLayout>
0010 #include <QDialogButtonBox>
0011 #include <QFontDatabase>
0012 #include <QHeaderView>
0013 #include <QLabel>
0014 #include <QPushButton>
0015 #include <QTableWidget>
0016 
0017 #include <KGuiItem>
0018 #include <KHelpClient>
0019 #include <KLocalizedString>
0020 
0021 
0022 ErrorDialog::ErrorDialog(QWidget* parent)
0023     : QDialog(parent)
0024 {
0025     errorList = nullptr;
0026 
0027     setWindowTitle(i18nc("@title:window", "Errors"));
0028     setModal(false);
0029     QVBoxLayout *mainLayout = new QVBoxLayout;
0030     setLayout(mainLayout);
0031 
0032     QWidget *mainWidget = new QWidget(this);
0033     mainLayout->addWidget(mainWidget);
0034 
0035     QWidget *baseWidget = new QWidget(this);
0036     mainLayout->addWidget(baseWidget);
0037     baseLayout = new QVBoxLayout(baseWidget); 
0038     mainLayout->addWidget(baseWidget);
0039     
0040     label = new QLabel(baseWidget);
0041     mainLayout->addWidget(label);
0042     label->setText(i18n("In this list you find the error(s) that resulted from running your code.\nGood luck!"));
0043     // \nYou can select an error and click the 'Help on Error' button for help.
0044     label->setScaledContents(true);
0045     baseLayout->addWidget(label);
0046     
0047     spacer = new QSpacerItem(10, 10, QSizePolicy::Minimum, QSizePolicy::Fixed);
0048     baseLayout->addItem(spacer);
0049     
0050     errorTable = new QTableWidget(baseWidget);
0051     mainLayout->addWidget(errorTable);
0052     errorTable->setSelectionMode(QAbstractItemView::SingleSelection);
0053     errorTable->setSelectionBehavior(QAbstractItemView::SelectRows);
0054     errorTable->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
0055     errorTable->setShowGrid(false);
0056 
0057     errorTable->setColumnCount(3);
0058     QStringList horizontalHeaderTexts;
0059     horizontalHeaderTexts << i18n("line") << i18n("description") << i18n("code");
0060     errorTable->setHorizontalHeaderLabels(horizontalHeaderTexts);
0061     errorTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
0062 
0063     baseLayout->addWidget(errorTable);
0064 
0065     m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Help);
0066     QPushButton *user1Button = new QPushButton;
0067     m_buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
0068     connect(m_buttonBox, &QDialogButtonBox::accepted, this, &ErrorDialog::accept);
0069     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &ErrorDialog::reject);
0070     connect(m_buttonBox, &QDialogButtonBox::helpRequested, this, &ErrorDialog::helpRequested);
0071     mainLayout->addWidget(m_buttonBox);
0072     KGuiItem::assign(user1Button, KGuiItem(i18n("Hide Errors")));
0073 //  setButtonGuiItem(User1, i18n("Help on &Error"));  // TODO context help in the error dialog
0074     user1Button->setDefault(true);
0075 
0076     clear();
0077 }
0078 
0079 
0080 
0081 void ErrorDialog::clear()
0082 {
0083     disable();
0084     errorList = nullptr;
0085     errorTable->clearContents();
0086 
0087     // put a friendly 'nothing to see here' notice in the empty table
0088     errorTable->setRowCount(1);
0089     QTableWidgetItem* emptyItem = new QTableWidgetItem(i18n("No errors occurred yet."));
0090     QFont emptyFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
0091     emptyFont.setItalic(true);
0092     emptyItem->setFont(emptyFont);
0093     errorTable->setItem(0, 1, emptyItem);
0094 
0095     errorTable->resizeColumnsToContents();
0096 //  errorTable->setColumnWidth(0, errorTable->fontMetrics().width("88888"));
0097 //  errorTable->setColumnWidth(2, errorTable->fontMetrics().width("88888"));
0098 //  errorTable->setColumnWidth(1, errorTable->width() - errorTable->verticalHeader()->width() - errorTable->columnWidth(0) - errorTable->columnWidth(2));
0099 }
0100 
0101 void ErrorDialog::enable()
0102 {
0103     Q_ASSERT (errorList);
0104     errorTable->setEnabled(true);
0105     m_buttonBox->button(QDialogButtonBox::Help)->setEnabled(true);
0106     connect(errorTable, &QTableWidget::itemSelectionChanged, this, &ErrorDialog::selectedErrorChangedProxy);
0107     errorTable->selectRow(0);
0108 }
0109 
0110 void ErrorDialog::disable()
0111 {
0112     disconnect(errorTable, &QTableWidget::itemSelectionChanged, this, &ErrorDialog::selectedErrorChangedProxy);
0113     errorTable->setEnabled(false);
0114     m_buttonBox->button(QDialogButtonBox::Help)->setEnabled(false);
0115     errorTable->clearSelection();
0116 }
0117 
0118 
0119 void ErrorDialog::setErrorList(ErrorList *list)
0120 {
0121     errorList = list;
0122     errorTable->setRowCount(errorList->size());
0123     int row = 0;
0124     for (const ErrorMessage &error : *errorList) {
0125         int col = 0;
0126         QStringList itemTexts;
0127         itemTexts << QString::number(error.token().startRow()) << error.text() << QString::number(error.code());
0128         for (const QString &itemText : std::as_const(itemTexts)) {
0129             errorTable->setItem(row, col, new QTableWidgetItem(itemText));
0130             col++;
0131         }
0132         row++;
0133     }
0134     errorTable->clearSelection();
0135     errorTable->resizeColumnsToContents();
0136     enable();
0137 }
0138 
0139 void ErrorDialog::selectedErrorChangedProxy()
0140 {
0141     Q_ASSERT (errorList);
0142     const Token* t = &errorList->at(errorTable->selectedItems().first()->row()).token();
0143     Q_EMIT currentlySelectedError(t->startRow(), t->startCol(), t->endRow(), t->endCol());
0144     // //qDebug() << "EMITTED: " << t->startRow() << ", " << t->startCol() << ", " << t->endRow() << ", " << t->endCol();
0145 }
0146 
0147 void ErrorDialog::helpRequested()
0148 {
0149     KHelpClient::invokeHelp(QStringLiteral("reference"), QStringLiteral("kturtle"));
0150 }