Skip to content
Extraits de code Groupes Projets
Valider 6281d654 rédigé par Jordan Aurey's avatar Jordan Aurey
Parcourir les fichiers

model tests

parent 4a54a51c
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -5,12 +5,12 @@ Element::Element() ...@@ -5,12 +5,12 @@ Element::Element()
} }
QString Element::get_name() QString Element::get_name() const
{ {
return name; return name;
} }
QString Element::get_status() QString Element::get_status() const
{ {
return status; return status;
} }
......
...@@ -10,8 +10,8 @@ class Element : public Updatable ...@@ -10,8 +10,8 @@ class Element : public Updatable
{ {
public: public:
Element(); Element();
QString get_name(); QString get_name() const;
QString get_status(); QString get_status() const;
protected: protected:
QString name; QString name;
QString status; QString status;
......
...@@ -7,17 +7,19 @@ ElementsListModel::ElementsListModel(QObject *parent) ...@@ -7,17 +7,19 @@ ElementsListModel::ElementsListModel(QObject *parent)
QVariant ElementsListModel::headerData(int section, Qt::Orientation orientation, int role) const QVariant ElementsListModel::headerData(int section, Qt::Orientation orientation, int role) const
{ {
// FIXME: Implement me! return QVariant();
} }
int ElementsListModel::rowCount(const QModelIndex &parent) const int ElementsListModel::rowCount(const QModelIndex &parent) const
{ {
// For list models only the root node (an invalid parent) should return the list's size. For all // For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model. // other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid()) /*if (parent.isValid())
return 0; return 0;*/
// FIXME: Implement me! // FIXME: Implement me!
return (taskVector.size());
} }
QVariant ElementsListModel::data(const QModelIndex &index, int role) const QVariant ElementsListModel::data(const QModelIndex &index, int role) const
...@@ -25,7 +27,11 @@ QVariant ElementsListModel::data(const QModelIndex &index, int role) const ...@@ -25,7 +27,11 @@ QVariant ElementsListModel::data(const QModelIndex &index, int role) const
if (!index.isValid()) if (!index.isValid())
return QVariant(); return QVariant();
// FIXME: Implement me! if (role == Qt::DisplayRole){
return QVariant::fromValue<Task>(taskVector.at(index.row()));
}
return QVariant(); return QVariant();
} }
...@@ -34,6 +40,7 @@ bool ElementsListModel::insertRows(int row, int count, const QModelIndex &parent ...@@ -34,6 +40,7 @@ bool ElementsListModel::insertRows(int row, int count, const QModelIndex &parent
beginInsertRows(parent, row, row + count - 1); beginInsertRows(parent, row, row + count - 1);
// FIXME: Implement me! // FIXME: Implement me!
endInsertRows(); endInsertRows();
return false;
} }
bool ElementsListModel::removeRows(int row, int count, const QModelIndex &parent) bool ElementsListModel::removeRows(int row, int count, const QModelIndex &parent)
...@@ -41,4 +48,13 @@ bool ElementsListModel::removeRows(int row, int count, const QModelIndex &parent ...@@ -41,4 +48,13 @@ bool ElementsListModel::removeRows(int row, int count, const QModelIndex &parent
beginRemoveRows(parent, row, row + count - 1); beginRemoveRows(parent, row, row + count - 1);
// FIXME: Implement me! // FIXME: Implement me!
endRemoveRows(); endRemoveRows();
return false;
}
void ElementsListModel::insertTask(Task &task, int row){
beginInsertRows(QModelIndex(), row, row);
taskVector.insert(row, task);
endInsertRows();
} }
...@@ -8,7 +8,11 @@ class ElementsListModel : public QAbstractListModel ...@@ -8,7 +8,11 @@ class ElementsListModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
public: public:
void addTaskToVector(Task & task){ taskVector.append(task);};
explicit ElementsListModel(QObject *parent = nullptr); explicit ElementsListModel(QObject *parent = nullptr);
// Header: // Header:
...@@ -25,6 +29,8 @@ public: ...@@ -25,6 +29,8 @@ public:
// Remove data: // Remove data:
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
void insertTask(Task & task, int row);
private: private:
QVector<Task> taskVector; QVector<Task> taskVector;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <QApplication> #include <QApplication>
#include "controller.h" #include "controller.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
...@@ -11,5 +12,6 @@ int main(int argc, char *argv[]) ...@@ -11,5 +12,6 @@ int main(int argc, char *argv[])
// lance le controller qui gère le timer pour l'update régulier // lance le controller qui gère le timer pour l'update régulier
Controller * _controller = new Controller(&w); Controller * _controller = new Controller(&w);
return a.exec(); return a.exec();
} }
#include "mainwindow.h" #include "mainwindow.h"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "elementslistmodel.h"
#include <qdebug.h>
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), QMainWindow(parent),
...@@ -11,6 +13,16 @@ MainWindow::MainWindow(QWidget *parent) : ...@@ -11,6 +13,16 @@ MainWindow::MainWindow(QWidget *parent) :
ui->treeWidget->show(); ui->treeWidget->show();
ui->listWidget->hide(); ui->listWidget->hide();
Task t(1);
ElementsListModel * model = new ElementsListModel();
model->insertTask(t, 0);
model->insertTask(t, 1);
ui->listView->setModel(model);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
......
...@@ -262,6 +262,9 @@ ...@@ -262,6 +262,9 @@
</column> </column>
</widget> </widget>
</item> </item>
<item>
<widget class="QListView" name="listView"/>
</item>
</layout> </layout>
</item> </item>
</layout> </layout>
......
#include "task.h" #include "task.h"
#include <qdebug.h>
Task::Task() : PID(0){
}
Task::Task(int PID) : PID(PID), utime(0), stat("/proc/"+QString::number(PID)+"/stat") Task::Task(int PID) : PID(PID), utime(0), stat("/proc/"+QString::number(PID)+"/stat")
{ {
QFile cmd("/proc"+QString::number(PID)+"/comm"); QFile cmd("/proc/"+QString::number(PID)+"/comm");
if(!cmd.open(QIODevice::ReadOnly)) { if(!cmd.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", cmd.errorString()); QMessageBox::information(0, "error", cmd.errorString());
} }
...@@ -13,7 +17,7 @@ Task::Task(int PID) : PID(PID), utime(0), stat("/proc/"+QString::number(PID)+"/s ...@@ -13,7 +17,7 @@ Task::Task(int PID) : PID(PID), utime(0), stat("/proc/"+QString::number(PID)+"/s
cmd.close(); cmd.close();
QString command("ls -la /proc/"+QString::number(PID)+" | grep ' \.' > .userT"); QString command("ls -la /proc/"+QString::number(PID)+" > .userT 2> /dev/null");
system(command.toStdString().c_str()); system(command.toStdString().c_str());
QFile task_user(".userT"); QFile task_user(".userT");
...@@ -21,15 +25,34 @@ Task::Task(int PID) : PID(PID), utime(0), stat("/proc/"+QString::number(PID)+"/s ...@@ -21,15 +25,34 @@ Task::Task(int PID) : PID(PID), utime(0), stat("/proc/"+QString::number(PID)+"/s
QMessageBox::information(0, "error", task_user.errorString()); QMessageBox::information(0, "error", task_user.errorString());
} }
QTextStream in2(&task_user); QTextStream in2(&task_user);
QStringList fields = in2.readLine().split(" "); in2.readLine();
QStringList fields = in2.readLine().split(" ", QString::SkipEmptyParts);
user = fields.at(fields.length()-8); user = fields.at(2);
task_user.close(); task_user.close();
Task::update(); Task::update();
} }
Task::Task(const Task & task) : Task(task.getPID()){
}
Task & Task::operator=(const Task & task){
this->PID = task.getPID();
this->user = task.getUser();
this->name = task.get_name();
this->utime = task.getUtime();
this->stat.setFileName("/proc/"+QString::number(PID)+"/stat");
this->update();
return *this;
}
void Task::update() void Task::update()
{ {
if(!stat.open(QIODevice::ReadOnly)) { if(!stat.open(QIODevice::ReadOnly)) {
...@@ -56,27 +79,27 @@ void Task::sendSignal(int sig) ...@@ -56,27 +79,27 @@ void Task::sendSignal(int sig)
kill(PID, sig); kill(PID, sig);
} }
int Task::getPID() int Task::getPID() const
{ {
return PID; return PID;
} }
double Task::getCPU() double Task::getCPU() const
{ {
return CPU; return CPU;
} }
int Task::getUtime() int Task::getUtime() const
{ {
return utime; return utime;
} }
double Task::getMem() double Task::getMem() const
{ {
return mem; return mem;
} }
int Task::getPPID() int Task::getPPID() const
{ {
return PPID; return PPID;
} }
QString Task::getUser() QString Task::getUser() const
{ {
return user; return user;
} }
...@@ -10,15 +10,18 @@ ...@@ -10,15 +10,18 @@
class Task : Element class Task : Element
{ {
public: public:
Task();
Task(int PID); Task(int PID);
Task(const Task & task);
void update(); void update();
void sendSignal(int sig); void sendSignal(int sig);
int getPID(); int getPID() const;
double getCPU(); double getCPU() const;
int getUtime(); int getUtime() const;
double getMem(); double getMem() const;
int getPPID(); int getPPID() const;
QString getUser(); QString getUser() const;
Task & operator=(const Task &);
private: private:
int PID; int PID;
double CPU; double CPU;
...@@ -28,5 +31,5 @@ private: ...@@ -28,5 +31,5 @@ private:
QFile stat; QFile stat;
QString user; QString user;
}; };
Q_DECLARE_METATYPE(Task);
#endif // TASK_H #endif // TASK_H
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter