diff --git a/app/element.cpp b/app/element.cpp index a84ea4ad23ddb7a17f4aaec0654f344e8c095c2a..a770ffa73c87dcb0161a7dbcde3c3d0689664606 100644 --- a/app/element.cpp +++ b/app/element.cpp @@ -5,12 +5,12 @@ Element::Element() } -QString Element::get_name() +QString Element::get_name() const { return name; } -QString Element::get_status() +QString Element::get_status() const { return status; } diff --git a/app/element.h b/app/element.h index be0978db0c618db983c031af0caaa546c6cbe371..5abd48e53eb2fe4e053145063e546ff0e9ac78c5 100644 --- a/app/element.h +++ b/app/element.h @@ -10,8 +10,8 @@ class Element : public Updatable { public: Element(); - QString get_name(); - QString get_status(); + QString get_name() const; + QString get_status() const; protected: QString name; QString status; diff --git a/app/elementslistmodel.cpp b/app/elementslistmodel.cpp index 1a9c8bdef744c536b543556cf11115753c3795e8..b206fe759d50830971c7fbabfd2fff32351a3d4f 100644 --- a/app/elementslistmodel.cpp +++ b/app/elementslistmodel.cpp @@ -7,17 +7,19 @@ ElementsListModel::ElementsListModel(QObject *parent) QVariant ElementsListModel::headerData(int section, Qt::Orientation orientation, int role) const { - // FIXME: Implement me! + return QVariant(); } 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 // other (valid) parents, rowCount() should return 0 so that it does not become a tree model. - if (parent.isValid()) - return 0; + /*if (parent.isValid()) + return 0;*/ // FIXME: Implement me! + return (taskVector.size()); + } 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()) return QVariant(); - // FIXME: Implement me! + if (role == Qt::DisplayRole){ + return QVariant::fromValue<Task>(taskVector.at(index.row())); + } + + return QVariant(); } @@ -34,6 +40,7 @@ bool ElementsListModel::insertRows(int row, int count, const QModelIndex &parent beginInsertRows(parent, row, row + count - 1); // FIXME: Implement me! endInsertRows(); + return false; } 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); // FIXME: Implement me! endRemoveRows(); + return false; } + +void ElementsListModel::insertTask(Task &task, int row){ + beginInsertRows(QModelIndex(), row, row); + taskVector.insert(row, task); + endInsertRows(); + +} + diff --git a/app/elementslistmodel.h b/app/elementslistmodel.h index ea4e4d1ee974ba5d3c943a454ced53b758bb3360..46b5b0c1bc4b5338a8c4081efb4574d0cb5ca3d8 100644 --- a/app/elementslistmodel.h +++ b/app/elementslistmodel.h @@ -8,7 +8,11 @@ class ElementsListModel : public QAbstractListModel { Q_OBJECT + public: + void addTaskToVector(Task & task){ taskVector.append(task);}; + + explicit ElementsListModel(QObject *parent = nullptr); // Header: @@ -25,6 +29,8 @@ public: // Remove data: bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; + void insertTask(Task & task, int row); + private: QVector<Task> taskVector; diff --git a/app/main.cpp b/app/main.cpp index 59761060ec941999d9fb4a83aad9001e9e297ac3..b5e55083e5ecf5934610776f1122254ae4369dc8 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -2,6 +2,7 @@ #include <QApplication> #include "controller.h" + int main(int argc, char *argv[]) { QApplication a(argc, argv); @@ -11,5 +12,6 @@ int main(int argc, char *argv[]) // lance le controller qui gère le timer pour l'update régulier Controller * _controller = new Controller(&w); + return a.exec(); } diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index efbf14c0489c5bb73d3b10ef6933c091686a6cc6..dc13e149458536d97a5b34148c588ad09ad7a974 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -1,5 +1,7 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include "elementslistmodel.h" +#include <qdebug.h> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), @@ -11,6 +13,16 @@ MainWindow::MainWindow(QWidget *parent) : ui->treeWidget->show(); ui->listWidget->hide(); + + + Task t(1); + ElementsListModel * model = new ElementsListModel(); + model->insertTask(t, 0); + model->insertTask(t, 1); + ui->listView->setModel(model); + + + } MainWindow::~MainWindow() diff --git a/app/mainwindow.ui b/app/mainwindow.ui index a7b1f8dec62e3838618805f0a0b2578a5c20a587..f3d8986819e0e3cc95eeddab211dd9ed81afa6ce 100644 --- a/app/mainwindow.ui +++ b/app/mainwindow.ui @@ -262,6 +262,9 @@ </column> </widget> </item> + <item> + <widget class="QListView" name="listView"/> + </item> </layout> </item> </layout> diff --git a/app/task.cpp b/app/task.cpp index a10be23030b02e0a958b751a1d41e4970ed4aded..23509b009534ca132f94519b37b53003edba1fbc 100644 --- a/app/task.cpp +++ b/app/task.cpp @@ -1,9 +1,13 @@ #include "task.h" +#include <qdebug.h> +Task::Task() : PID(0){ + +} 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)) { QMessageBox::information(0, "error", cmd.errorString()); } @@ -13,7 +17,7 @@ Task::Task(int PID) : PID(PID), utime(0), stat("/proc/"+QString::number(PID)+"/s 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()); QFile task_user(".userT"); @@ -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()); } 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::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() { if(!stat.open(QIODevice::ReadOnly)) { @@ -56,27 +79,27 @@ void Task::sendSignal(int sig) kill(PID, sig); } -int Task::getPID() +int Task::getPID() const { return PID; } -double Task::getCPU() +double Task::getCPU() const { return CPU; } -int Task::getUtime() +int Task::getUtime() const { return utime; } -double Task::getMem() +double Task::getMem() const { return mem; } -int Task::getPPID() +int Task::getPPID() const { return PPID; } -QString Task::getUser() +QString Task::getUser() const { return user; } diff --git a/app/task.h b/app/task.h index 6687f2e0dd48b2f5e989964d01e76eb8d29e12bc..9c49d45fb6308e5ef36d7e4405e5471eaf0d8289 100644 --- a/app/task.h +++ b/app/task.h @@ -10,15 +10,18 @@ class Task : Element { public: + Task(); Task(int PID); + Task(const Task & task); void update(); void sendSignal(int sig); - int getPID(); - double getCPU(); - int getUtime(); - double getMem(); - int getPPID(); - QString getUser(); + int getPID() const; + double getCPU() const; + int getUtime() const; + double getMem() const; + int getPPID() const; + QString getUser() const; + Task & operator=(const Task &); private: int PID; double CPU; @@ -28,5 +31,5 @@ private: QFile stat; QString user; }; - + Q_DECLARE_METATYPE(Task); #endif // TASK_H