~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/gui/SensorBrowser.h

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
   
 
4
    Copyright (c) 1999, 2000 Chris Schlaeger <cs@kde.org>
 
5
    
 
6
    This program is free software; you can redistribute it and/or
 
7
    modify it under the terms of the GNU General Public
 
8
    License version 2 or at your option version 3 as published by
 
9
    the Free Software Foundation.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 
 
20
*/
 
21
 
 
22
#ifndef KSG_SENSORBROWSER_H
 
23
#define KSG_SENSORBROWSER_H
 
24
 
 
25
#include <QMouseEvent>
 
26
#include <QTreeWidget>
 
27
#include <QMap>
 
28
#include <QHash>
 
29
#include <ksgrd/SensorClient.h>
 
30
#include "ksortfilterproxymodel.h"
 
31
 
 
32
class QMouseEvent;
 
33
 
 
34
namespace KSGRD {
 
35
class SensorManager;
 
36
class SensorAgent;
 
37
}
 
38
 
 
39
class SensorInfo;
 
40
class HostInfo;
 
41
 
 
42
class SensorBrowserModel : public QAbstractItemModel, private KSGRD::SensorClient
 
43
{
 
44
  Q_OBJECT
 
45
  public:
 
46
    SensorBrowserModel();
 
47
    ~SensorBrowserModel();
 
48
    virtual int columnCount( const QModelIndex &) const;
 
49
    virtual QVariant data( const QModelIndex & parent, int role) const;
 
50
    virtual QVariant headerData ( int section, Qt::Orientation orientation, int role) const;
 
51
    virtual QModelIndex index ( int row, int column, const QModelIndex & parent) const;
 
52
    virtual QModelIndex parent ( const QModelIndex & index ) const;
 
53
    virtual int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
 
54
 
 
55
 
 
56
    QStringList listSensors( const QString &hostName ) const;  ///Returns a list of sensors names.  E.g. (cpu/0, mem/free, mem/cache, etc)
 
57
    QStringList listSensors( int parentId ) const; ///Used recursively by listSensor(QString)
 
58
    QStringList listHosts( ) const; ///Returns a list of host names.  E.g. (localhost, 192.168.0.1,...)
 
59
    SensorInfo *getSensorInfo(QModelIndex index) const;
 
60
    HostInfo *getHostInfo(int hostId) const { return mHostInfoMap.value(hostId);}
 
61
    bool hasSensor(int hostId, const QString &sensor) const { return mHostSensorsMap.value(hostId).contains(sensor);} 
 
62
    int makeTreeBranch(int parentId, const QString &name);
 
63
    int makeSensor(HostInfo *hostInfo, int parentId, const QString &sensorName, const QString &name, const QString &sensorType);
 
64
    void removeSensor(HostInfo *hostInfo, int parentId, const QString &sensorName);
 
65
    void addHost(KSGRD::SensorAgent *sensorAgent, const QString &hostName);
 
66
    void clear();
 
67
    void disconnectHost(uint id);
 
68
    void disconnectHost(const HostInfo *hostInfo);
 
69
    void disconnectHost(const QString &hostname);
 
70
    virtual Qt::ItemFlags flags ( const QModelIndex & index ) const;
 
71
    virtual QMimeData * mimeData ( const QModelIndexList & indexes ) const;
 
72
    void retranslate();  /// Retranslate the model
 
73
  Q_SIGNALS:
 
74
    void sensorsAddedToHost(const QModelIndex &index );
 
75
  public Q_SLOTS:
 
76
    void update();
 
77
    void hostAdded(KSGRD::SensorAgent *sensorAgent, const QString &hostName);
 
78
    /**
 
79
     * Remove host from this model. The proper way this is called is with the signal in SensorManager
 
80
     * (from disengage), otherwise
 
81
     * if this is called directly the SensorManager container will be out of sync with ours.  Calling disconnectHost
 
82
     * from this object will also call disengage.
 
83
     */
 
84
    void hostRemoved(const QString &hostName);
 
85
 
 
86
  private:
 
87
    virtual void answerReceived( int id, const QList<QByteArray>& );
 
88
    void removeEmptyParentTreeBranches(int hostId, int id, int parentid);
 
89
    HostInfo* findHostInfoByHostName(const QString &hostName) const;
 
90
    void removeAllSensorUnderBranch(HostInfo* hostInfo, int parentId);
 
91
 
 
92
    int mIdCount; ///The lowest id that has not been used yet
 
93
    QMap<int, HostInfo*> mHostInfoMap; ///So each host has a number
 
94
    QHash<int, QList<int> > mTreeMap;   ///This describes the structure of the tree. It maps a parent branch number (which can be equal to the hostinfo number if it's a host branch) to a list of children.  The children themselves either have branches in the map, or else just relate to a sensor info
 
95
    QHash<int, int > mParentsTreeMap; ///
 
96
    QHash<int, QString> mTreeNodeNames; ///Maps the mTreeMap node id's to (translated) names
 
97
    QHash<int, QHash<QString,bool> > mHostSensorsMap; ///Maps a host id to a hash of sensor names.  Let's us quickly check if a sensor is registered for a given host. bool is just ignored
 
98
    QHash<int, SensorInfo *> mSensorInfoMap; ///Each sensor has a unique number as well.  This relates to the ID in mTreeMap
 
99
};
 
100
 
 
101
class SensorBrowserTreeWidget : public QTreeView
 
102
{
 
103
  Q_OBJECT
 
104
 
 
105
  public:
 
106
    SensorBrowserTreeWidget( QWidget* parent, KSGRD::SensorManager* sm );
 
107
    ~SensorBrowserTreeWidget();
 
108
 
 
109
    QStringList listHosts() const 
 
110
      { return mSensorBrowserModel.listHosts(); }
 
111
    QStringList listSensors( const QString &hostName ) const 
 
112
      { return mSensorBrowserModel.listSensors(hostName); }
 
113
    KSortFilterProxyModel & model()
 
114
      { return mSortFilterProxyModel; }
 
115
 
 
116
  public Q_SLOTS:
 
117
    void disconnect();
 
118
    void hostReconfigured( const QString &hostName );
 
119
  protected Q_SLOTS:
 
120
    void expandItem(const QModelIndex& model_index);
 
121
    void updateView();
 
122
  private:
 
123
    void retranslateUi();
 
124
    void changeEvent( QEvent * event );
 
125
 
 
126
    KSGRD::SensorManager* mSensorManager;
 
127
 
 
128
    QString mDragText;
 
129
    SensorBrowserModel mSensorBrowserModel;
 
130
    KSortFilterProxyModel mSortFilterProxyModel;
 
131
};
 
132
 
 
133
/**
 
134
 * The SensorBrowserWidget is the graphical front-end of the SensorManager. It
 
135
 * displays the currently available hosts and their sensors.
 
136
 */
 
137
class SensorBrowserWidget : public QWidget
 
138
{
 
139
    Q_OBJECT
 
140
    public:
 
141
      SensorBrowserWidget( QWidget* parent, KSGRD::SensorManager* sm );
 
142
      ~SensorBrowserWidget();
 
143
      QStringList listHosts() const 
 
144
      { return m_treeWidget->listHosts(); }
 
145
      QStringList listSensors( const QString &hostName ) const 
 
146
      { return m_treeWidget->listSensors(hostName); }
 
147
 
 
148
    private:
 
149
      SensorBrowserTreeWidget *m_treeWidget;
 
150
 
 
151
};
 
152
 
 
153
class SensorInfo
 
154
{
 
155
  public:
 
156
    SensorInfo( HostInfo *hostInfo, const QString &name,
 
157
                const QString &description, const QString &type );
 
158
 
 
159
    /**
 
160
      Returns the name of the sensor.  e.g. "cpu/free".  Not translated.
 
161
     */
 
162
    QString name() const;
 
163
 
 
164
    /**
 
165
      Returns the description of the sensor.  e.g. "free"
 
166
     */
 
167
    QString description() const;
 
168
 
 
169
    /**
 
170
      Returns the type of the sensor. e.g. "Integer"
 
171
     */
 
172
    QString type() const;
 
173
 
 
174
    /**
 
175
      Returns the host that this sensor is on. 
 
176
     */
 
177
    HostInfo *hostInfo() const;
 
178
 
 
179
  private:
 
180
    QString mName;
 
181
    QString mDesc;
 
182
    QString mType;
 
183
    HostInfo *mHostInfo;
 
184
};
 
185
 
 
186
class HostInfo
 
187
{
 
188
  public:
 
189
    HostInfo( int id, KSGRD::SensorAgent *agent, const QString &name) : mId(id), mSensorAgent(agent), mHostName(name) {}
 
190
 
 
191
    /**
 
192
      Returns the unique id of the host.
 
193
     */
 
194
    int id() const {return mId;}
 
195
 
 
196
    /**
 
197
      Returns a pointer to the sensor agent of the host.
 
198
     */
 
199
    KSGRD::SensorAgent* sensorAgent() const {return mSensorAgent;}
 
200
 
 
201
    /**
 
202
      Returns the name of the host.
 
203
     */
 
204
    QString hostName() const { return mHostName;}
 
205
 
 
206
  private:
 
207
    int mId;
 
208
    KSGRD::SensorAgent* mSensorAgent;
 
209
    const QString mHostName;
 
210
};
 
211
 
 
212
#endif