dtor first
[personal-kdebase.git] / workspace / ksysguard / gui / SensorBrowser.h
blob35b6b78c844a496c80a723178889ca4e31e9249c
1 /*
2 KSysGuard, the KDE System Guard
4 Copyright (c) 1999, 2000 Chris Schlaeger <cs@kde.org>
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.
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.
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.
22 #ifndef KSG_SENSORBROWSER_H
23 #define KSG_SENSORBROWSER_H
25 #include <QMouseEvent>
26 #include <QTreeWidget>
27 #include <QMap>
28 #include <QHash>
29 #include <ksgrd/SensorClient.h>
31 class QMouseEvent;
33 namespace KSGRD {
34 class SensorManager;
35 class SensorAgent;
38 class SensorInfo;
39 class HostInfo;
42 class SensorBrowserModel : public QAbstractItemModel, private KSGRD::SensorClient
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;
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();
78 private:
79 virtual void answerReceived( int id, const QList<QByteArray>& );
80 void removeEmptyParentTreeBranches(int hostId, int id, int parentid);
82 int mIdCount; ///The lowest id that has not been used yet
83 QMap<int, HostInfo*> mHostInfoMap; ///So each host has a number
84 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
85 QHash<int, int > mParentsTreeMap; ///
86 QHash<int, QString> mTreeNodeNames; ///Maps the mTreeMap node id's to (translated) names
87 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
88 QHash<int, SensorInfo *> mSensorInfoMap; ///Each sensor has a unique number as well. This relates to the ID in mTreeMap
92 /**
93 * The SensorBrowserWidget is the graphical front-end of the SensorManager. It
94 * displays the currently available hosts and their sensors.
96 class SensorBrowserWidget : public QTreeView
98 Q_OBJECT
100 public:
101 SensorBrowserWidget( QWidget* parent, KSGRD::SensorManager* sm );
102 ~SensorBrowserWidget();
104 QStringList listHosts() const
105 { return mSensorBrowserModel.listHosts(); }
106 QStringList listSensors( const QString &hostName ) const
107 { return mSensorBrowserModel.listSensors(hostName); }
109 public Q_SLOTS:
110 void disconnect();
111 void hostReconfigured( const QString &hostName );
113 private:
114 void retranslateUi();
115 void changeEvent( QEvent * event );
117 KSGRD::SensorManager* mSensorManager;
119 QString mDragText;
120 SensorBrowserModel mSensorBrowserModel;
123 class SensorInfo
125 public:
126 SensorInfo( HostInfo *hostInfo, const QString &name,
127 const QString &description, const QString &type );
130 Returns the name of the sensor. e.g. "cpu/free". Not translated.
132 QString name() const;
135 Returns the description of the sensor. e.g. "free"
137 QString description() const;
140 Returns the type of the sensor. e.g. "Integer"
142 QString type() const;
145 Returns the host that this sensor is on.
147 HostInfo *hostInfo() const;
149 private:
150 QString mName;
151 QString mDesc;
152 QString mType;
153 HostInfo *mHostInfo;
156 class HostInfo
158 public:
159 HostInfo( int id, KSGRD::SensorAgent *agent, const QString &name) : mId(id), mSensorAgent(agent), mHostName(name) {}
162 Returns the unique id of the host.
164 int id() const {return mId;}
167 Returns a pointer to the sensor agent of the host.
169 KSGRD::SensorAgent* sensorAgent() const {return mSensorAgent;}
172 Returns the name of the host.
174 QString hostName() const { return mHostName;}
176 private:
177 int mId;
178 KSGRD::SensorAgent* mSensorAgent;
179 const QString mHostName;
182 #endif