alarminfow.cpp 4.21 KB
Newer Older
1
2
3
4
5
6
7
8
#include "alarminfow.h"
#include <cumbiapool.h>
#include <cucontrolsfactorypool.h>
#include <quwatcher.h>
#include <qustringlist.h>
#include <qustring.h>
#include <QStandardItemModel>
#include <QStandardItem>
9
10
11
12
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonObject>
13
14
15
16
17
18
19

AlarmInfoW::AlarmInfoW(QWidget *parent, CumbiaPool *cu_pool, const CuControlsFactoryPool &fpoo) : QTreeView(parent)
{
    QuWatcher *iw = new QuWatcher(this, cu_pool, fpoo);
    iw->setObjectName("alarm_info_watcher");
    connect(iw, SIGNAL(newData(CuData)), this, SLOT(update(CuData)));
    QStandardItemModel *m = new QStandardItemModel(this);
20
    setHeaderHidden(true);
21
    setModel(m);
22
    setMouseTracking(true); // for hand cursor over urls
23
    setAlternatingRowColors(true);
24
25
    connect(this, SIGNAL(clicked(QModelIndex)), this, SLOT(onItemClicked(QModelIndex)));
    connect(this, SIGNAL(entered(QModelIndex)), this, SLOT(onItemEntered(QModelIndex)));
26
27
28
29
30
31
32
33
34
35
36
}

void AlarmInfoW::update(const CuData &da) {
    printf("AlarmInfo.update: %s\n", datos(da));
    model()->removeRows(0, model()->rowCount());
    setDisabled(da["err"].toBool());
    setToolTip(da["msg"].toString().c_str());
    QStandardItemModel *m = qobject_cast<QStandardItemModel *>(model());
    if(!da["err"].toBool()) {
        QuStringList l(da["value"]);
        foreach(QString s, l) {
37
38
            QString t = m_line_break(s.section('=', 1), 120);
            // break into lines if too long
39
            QStandardItem *si1 = new QStandardItem(s.section('=', 0, 0));
40
            QStandardItem *si2 = new QStandardItem(t);
41
            si2->setToolTip(si2->text());
42
43
44
45
46
47
            if(si1->text() == "url" && si2->text().length() > 0) {
                si2->setForeground(QColor(Qt::blue));
                QFont f = si2->font();
                f.setUnderline(true);
                si2->setFont(f);
            }
48
            m->appendRow(QList<QStandardItem *>() << si1 << si2);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
        }
    }/*
#if CUMBIA_VERSION < 0x010203
        m->appendRow(QList<QStandardItem *>() << new QStandardItem("cumbia version required") <<  new QStandardItem(">= 1.2.3"));
#endif*/
}

void AlarmInfoW::activate(const QString&dev, const QString& arg) {
    QTreeView::setVisible(!dev.isEmpty());
    if(dev.isEmpty()) {
        printf("AlarmInfoW.activate: unsetSource %s\n", qstoc(findChild<QuWatcher *>()->source()));
        findChild<QuWatcher *>()->unsetSource();
    }
    else {
        printf("AlarmInfoW.activate: %s\n", qstoc(QString("%1->GetAlarmInfo(%2,)").arg(dev).arg(arg)));
        findChild<QuWatcher *>()->setSource(QString("%1->GetAlarmInfo(%2,)").arg(dev).arg(arg));
    }
}
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

void AlarmInfoW::onItemClicked(const QModelIndex &mi) {
    QString t = mi.data().toString();
    QModelIndex sibling = model()->index(mi.row(), 0);
    if(sibling.data() == "url" && !t.isEmpty())
        emit urlClicked(t);
}

void AlarmInfoW::onItemEntered(const QModelIndex &mi) {
    QString t = mi.data().toString();
    QModelIndex sibling = model()->index(mi.row(), mi.column() - 1);
    setCursor(QCursor(sibling.data() == "url" ?  Qt::PointingHandCursor : Qt::ArrowCursor));

}

QString AlarmInfoW::m_line_break(const QString &s, int linelen) const
{
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
    QString s1;
    QJsonDocument jd = QJsonDocument::fromJson(s.toLatin1());
    if(jd.isNull()) {
        s1 = s;
        if(s.contains("||") || s.contains("&&")) {
            s1.replace("||", "||\n").replace("&&", "&&\n");
        }
    }
    else {
        if(jd.isObject()) {
            QJsonObject jo = jd.object();
            foreach(const QString& k, jo.keys()) {
                QJsonValue val = jo[k];
                if(val.isDouble()) s1 += QString("%1: %2\n").arg(k).arg(val.toDouble());
                else if(val.isBool())  s1 += QString("%1: %2\n").arg(k).arg(val.toBool() ? "TRUE" : "FALSE");
                else if(val.isString()) s1 += QString("%1: \"%2\"\n").arg(k).arg(val.toString());
            }
        }
    }
    QString out;
    foreach(QString line, s1.split("\n")) {
        int i = 0;
        while(i + linelen  < line.length()) {
            line.insert(i + linelen, "↵\n");
            i+= linelen + 2;
        }
        out += line + "\n";
111
    }
112
113
    for(int i = out.length() - 1; i >=0 && out[i] == '\n'; i--)
        out.remove(i, 1);
114
    return out;
115
}