alarminfow.cpp 3.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "alarminfow.h"
#include <cumbiapool.h>
#include <cucontrolsfactorypool.h>
#include <quwatcher.h>
#include <qustringlist.h>
#include <qustring.h>
#include <QStandardItemModel>
#include <QStandardItem>

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);
16
    setHeaderHidden(true);
17
    setModel(m);
18
19
20
    setMouseTracking(true); // for hand cursor over urls
    connect(this, SIGNAL(clicked(QModelIndex)), this, SLOT(onItemClicked(QModelIndex)));
    connect(this, SIGNAL(entered(QModelIndex)), this, SLOT(onItemEntered(QModelIndex)));
21
22
23
24
25
26
27
28
29
30
31
}

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) {
32
33
            QString t = m_line_break(s.section('=', 1), 120);
            // break into lines if too long
34
            QStandardItem *si1 = new QStandardItem(s.section('=', 0, 0));
35
            QStandardItem *si2 = new QStandardItem(t);
36
            si2->setToolTip(si2->text());
37
38
39
40
41
42
            if(si1->text() == "url" && si2->text().length() > 0) {
                si2->setForeground(QColor(Qt::blue));
                QFont f = si2->font();
                f.setUnderline(true);
                si2->setFont(f);
            }
43
            m->appendRow(QList<QStandardItem *>() << si1 << si2);
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        }
    }/*
#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));
    }
}
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

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
{
    QString s1(s);
    int l = s.length();
    while(l > linelen) {
        s1.insert(l - linelen, "↵\n");
        l -= (linelen + 2);
    }
    return s1;
}