| 1 | /* |
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "issuestable.h" |
| 18 | #include "../utils.h" |
| 19 | #include <QHeaderView> |
| 20 | #include "../settings.h" |
| 21 | #include "../mainwindow.h" |
| 22 | #include "../editor.h" |
| 23 | #include "../editorlist.h" |
| 24 | |
| 25 | |
| 26 | IssuesTable::IssuesTable(QWidget *parent): |
| 27 | QTableView(parent) |
| 28 | { |
| 29 | mModel = new IssuesModel(this); |
| 30 | this->setModel(mModel); |
| 31 | this->setColumnWidth(0,200); |
| 32 | this->setColumnWidth(1,45); |
| 33 | this->setColumnWidth(2,45); |
| 34 | } |
| 35 | |
| 36 | const QVector<PCompileIssue> &IssuesTable::issues() const |
| 37 | { |
| 38 | return mModel->issues(); |
| 39 | } |
| 40 | |
| 41 | IssuesModel *IssuesTable::issuesModel() |
| 42 | { |
| 43 | return mModel; |
| 44 | } |
| 45 | |
| 46 | void IssuesTable::setErrorColor(QColor color) |
| 47 | { |
| 48 | mModel->setErrorColor(color); |
| 49 | } |
| 50 | |
| 51 | void IssuesTable::setWarningColor(QColor color) |
| 52 | { |
| 53 | mModel->setWarningColor(color); |
| 54 | } |
| 55 | |
| 56 | QString IssuesTable::toHtml() |
| 57 | { |
| 58 | QString result; |
| 59 | result.append( |
| 60 | QString("<table><thead><th>%1</th><th>%2</th><th>%3</th><th>%4</th></thead>" ) |
| 61 | .arg(tr("Filename" )) |
| 62 | .arg(tr("Line" )) |
| 63 | .arg(tr("Col" )) |
| 64 | .arg(tr("Description" ))); |
| 65 | foreach (const PCompileIssue& issue, mModel->issues()) { |
| 66 | result.append(QString("<tr><td>%1</td><td>%2</td><td>%3</td><td>%4</td></tr>\n" ) |
| 67 | .arg(issue->filename) |
| 68 | .arg(issue->line) |
| 69 | .arg(issue->column) |
| 70 | .arg(issue->description)); |
| 71 | } |
| 72 | result.append(QString("</table>" )); |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | QString IssuesTable::toTxt() |
| 77 | { |
| 78 | QString result; |
| 79 | foreach (const PCompileIssue& issue, mModel->issues()) { |
| 80 | result.append(QString("%1\t%2\t%3\t%4\n" ) |
| 81 | .arg(issue->filename) |
| 82 | .arg(issue->line) |
| 83 | .arg(issue->column) |
| 84 | .arg(issue->description)); |
| 85 | } |
| 86 | return result; |
| 87 | } |
| 88 | |
| 89 | IssuesModel::IssuesModel(QObject *parent): |
| 90 | QAbstractTableModel(parent) |
| 91 | { |
| 92 | |
| 93 | } |
| 94 | |
| 95 | void IssuesModel::addIssue(PCompileIssue issue) |
| 96 | { |
| 97 | beginInsertRows(QModelIndex(),mIssues.size(),mIssues.size()); |
| 98 | mIssues.push_back(issue); |
| 99 | endInsertRows(); |
| 100 | } |
| 101 | |
| 102 | void IssuesModel::clearIssues() |
| 103 | { |
| 104 | QSet<QString> issueFiles; |
| 105 | foreach(const PCompileIssue& issue, mIssues) { |
| 106 | if (!(issue->filename.isEmpty())){ |
| 107 | issueFiles.insert(issue->filename); |
| 108 | } |
| 109 | } |
| 110 | foreach (const QString& filename, issueFiles) { |
| 111 | Editor *e=pMainWindow->editorList()->getOpenedEditorByFilename(filename); |
| 112 | if (e) |
| 113 | e->clearSyntaxIssues(); |
| 114 | } |
| 115 | if (mIssues.size()>0) { |
| 116 | beginResetModel(); |
| 117 | mIssues.clear(); |
| 118 | endResetModel(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void IssuesModel::setErrorColor(QColor color) |
| 123 | { |
| 124 | mErrorColor = color; |
| 125 | } |
| 126 | |
| 127 | void IssuesModel::setWarningColor(QColor color) |
| 128 | { |
| 129 | mWarningColor = color; |
| 130 | } |
| 131 | |
| 132 | PCompileIssue IssuesModel::issue(int row) |
| 133 | { |
| 134 | if (row<0 || row>=static_cast<int>(mIssues.size())) { |
| 135 | return PCompileIssue(); |
| 136 | } |
| 137 | |
| 138 | return mIssues[row]; |
| 139 | } |
| 140 | |
| 141 | const QVector<PCompileIssue> &IssuesModel::issues() const |
| 142 | { |
| 143 | return mIssues; |
| 144 | } |
| 145 | |
| 146 | int IssuesModel::count() |
| 147 | { |
| 148 | return mIssues.size(); |
| 149 | } |
| 150 | |
| 151 | void IssuesTable::addIssue(PCompileIssue issue) |
| 152 | { |
| 153 | mModel->addIssue(issue); |
| 154 | } |
| 155 | |
| 156 | PCompileIssue IssuesTable::issue(const QModelIndex &index) |
| 157 | { |
| 158 | if (!index.isValid()) |
| 159 | return PCompileIssue(); |
| 160 | return issue(index.row()); |
| 161 | } |
| 162 | |
| 163 | PCompileIssue IssuesTable::issue(const int row) |
| 164 | { |
| 165 | return mModel->issue(row); |
| 166 | } |
| 167 | |
| 168 | int IssuesTable::count() |
| 169 | { |
| 170 | return mModel->count(); |
| 171 | } |
| 172 | |
| 173 | void IssuesTable::clearIssues() |
| 174 | { |
| 175 | mModel->clearIssues(); |
| 176 | } |
| 177 | |
| 178 | int IssuesModel::rowCount(const QModelIndex &) const |
| 179 | { |
| 180 | return mIssues.size(); |
| 181 | } |
| 182 | |
| 183 | int IssuesModel::columnCount(const QModelIndex &) const |
| 184 | { |
| 185 | return 4; |
| 186 | } |
| 187 | |
| 188 | QVariant IssuesModel::data(const QModelIndex &index, int role) const |
| 189 | { |
| 190 | if (!index.isValid()) |
| 191 | return QVariant(); |
| 192 | if (index.row()<0 || index.row() >= static_cast<int>(mIssues.size())) |
| 193 | return QVariant(); |
| 194 | PCompileIssue issue = mIssues[index.row()]; |
| 195 | if (!issue) |
| 196 | return QVariant(); |
| 197 | switch (role) { |
| 198 | case Qt::DisplayRole: |
| 199 | case Qt::ToolTipRole: |
| 200 | switch (index.column()) { |
| 201 | case 0: { |
| 202 | if (role == Qt::DisplayRole) |
| 203 | return extractFileName(issue->filename); |
| 204 | else |
| 205 | return issue->filename; |
| 206 | } |
| 207 | case 1: |
| 208 | if (issue->line>0) |
| 209 | return issue->line; |
| 210 | else |
| 211 | return "" ; |
| 212 | case 2: |
| 213 | if (issue->column>0) |
| 214 | return issue->column; |
| 215 | else |
| 216 | return "" ; |
| 217 | case 3: |
| 218 | return issue->description; |
| 219 | default: |
| 220 | return QVariant(); |
| 221 | } |
| 222 | case Qt::ForegroundRole: |
| 223 | switch(issue->type) { |
| 224 | case CompileIssueType::Error: |
| 225 | return mErrorColor; |
| 226 | case CompileIssueType::Warning: |
| 227 | return mWarningColor; |
| 228 | default: |
| 229 | return QVariant(); |
| 230 | } |
| 231 | case Qt::FontRole: { |
| 232 | QFont newFont=((IssuesTable *)parent())->font(); |
| 233 | switch(issue->type) { |
| 234 | case CompileIssueType::Error: |
| 235 | case CompileIssueType::Warning: |
| 236 | { |
| 237 | newFont.setBold(true); |
| 238 | break; |
| 239 | } |
| 240 | default: |
| 241 | newFont.setBold(issue->line == 0); |
| 242 | } |
| 243 | return newFont; |
| 244 | } |
| 245 | default: |
| 246 | return QVariant(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | QVariant IssuesModel::(int section, Qt::Orientation orientation, int role) const |
| 251 | { |
| 252 | if (orientation == Qt::Horizontal ) { |
| 253 | switch(role) { |
| 254 | case Qt::DisplayRole: |
| 255 | switch(section) { |
| 256 | case 0: |
| 257 | return tr("Filename" ); |
| 258 | case 1: |
| 259 | return tr("Line" ); |
| 260 | case 2: |
| 261 | return tr("Col" ); |
| 262 | case 3: |
| 263 | return tr("Description" ); |
| 264 | } |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | return QVariant(); |
| 269 | } |
| 270 | |