aboutsummaryrefslogtreecommitdiff
path: root/qtmips_gui/messagesmodel.cpp
blob: df9bef8c701578d393d5be4689c3f22aebbffbf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// SPDX-License-Identifier: GPL-2.0+
/*******************************************************************************
 * QtMips - MIPS 32-bit Architecture Subset Simulator
 *
 * Implemented to support following courses:
 *
 *   B35APO - Computer Architectures
 *   https://cw.fel.cvut.cz/wiki/courses/b35apo
 *
 *   B4M35PAP - Advanced Computer Architectures
 *   https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start
 *
 * Copyright (c) 2017-2019 Karel Koci<cynerd@email.cz>
 * Copyright (c) 2019      Pavel Pisa <pisa@cmp.felk.cvut.cz>
 *
 * Faculty of Electrical Engineering (http://www.fel.cvut.cz)
 * Czech Technical University        (http://www.cvut.cz/)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 ******************************************************************************/

#include <QBrush>

#include "messagesmodel.h"

class MessagesEntry {
public:
    inline MessagesEntry(messagetype::Type type, QString file, int line,
                  int column, QString text, QString hint) {

        this->type = type;
        this->file = file;
        this->line = line;
        this->column = column;
        this->text = text;
        this->hint = hint;
    }
    messagetype::Type type;
    QString file;
    int line;
    int column;
    QString text;
    QString hint;
};


MessagesModel::MessagesModel(QObject *parent) : Super(parent) {

}

MessagesModel::~MessagesModel() {
    clear_messages();
}

int MessagesModel::rowCount(const QModelIndex & /*parent*/) const {
   return messages.count();
}

int MessagesModel::columnCount(const QModelIndex & /*parent*/) const {
    return 1;
}

QVariant MessagesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Horizontal) {
        if(role == Qt::DisplayRole) {
            switch (section) {
            case 0:
                return tr("Type");
            case 1:
                return tr("Source");
            case 2:
                return tr("Line");
            case 3:
                return tr("Column");
            case 4:
                return tr("Text");
            default:
                return tr("");
            }
        }
    }
    return Super::headerData(section, orientation, role);
}

QVariant MessagesModel::data(const QModelIndex &index, int role) const {
    if (index.row() >= rowCount())
        return QVariant();

    if (role == Qt::DisplayRole || role == Qt::EditRole)
    {
        MessagesEntry *ent = messages.at(index.row());
        QString ret = "";
        if (!ent->file.isEmpty())
            ret += ent->file + ":";
        if (ent->line)
            ret += QString::number(ent->line) + ":";
        if (ent->column)
            ret += QString::number(ent->column) + ":";
        ret += ent->text;
        return ret;
    }
    if (role == Qt::BackgroundRole) {
        MessagesEntry *ent = messages.at(index.row());
        switch (ent->type) {
        case messagetype::MSG_ERROR:
            return QBrush(QColor(255, 230, 230));
        case messagetype::MSG_WARNING:
            return QBrush(QColor(255, 255, 220));
        default:
            return QVariant();
        }
    }
    return QVariant();
}

void MessagesModel::insert_line(messagetype::Type type, QString file, int line,
                               int column, QString text, QString hint) {

    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    messages.append(new MessagesEntry(type, file, line, column, text, hint));
    endInsertRows();
}

void MessagesModel::clear_messages() {
    beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
    while (!messages.isEmpty()) {
        delete messages.takeFirst();
    }
    endRemoveRows();
}

void MessagesModel::activated(QModelIndex index) {
    if (index.row() >= rowCount())
        return;

    MessagesEntry *ent = messages.at(index.row());
    emit message_selected(ent->type, ent->file, ent->line, ent->column, ent->text, ent->hint);
}