📄 dialogattach.cpp
字号:
/*Copyright (C) 2006 Evan Teran eteran@alum.rit.eduThis program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/#include "DialogAttach.h"#include "types.h"#include <string>#include <fstream>#include <iostream>#include <QMap>#include <QHeaderView>#include <QDir>#include <QStringList>#include <QString>#include <QFileInfo>struct ProcessInfo { BaseTypes::pid_t pid; BaseTypes::uid_t uid; QString name;};//------------------------------------------------------------------------------// Name: DialogAttach(QWidget *parent) : QDialog(parent)// Desc: constructor//------------------------------------------------------------------------------DialogAttach::DialogAttach(QWidget *parent) : QDialog(parent) { ui.setupUi(this); ui.tableWidget->verticalHeader()->hide(); ui.tableWidget->horizontalHeader()->setStretchLastSection(true);}//------------------------------------------------------------------------------// Name: isNumeric(const QString &s)// Desc: returns true if the string only contains decimal digits//------------------------------------------------------------------------------bool DialogAttach::isNumeric(const QString &s) { bool ret = false; foreach(QChar ch, s) { ret = ch.isDigit(); if(!ret) { break; } } return ret;}//------------------------------------------------------------------------------// Name: on_txtSearch_textChanged(const QString &text)// Desc: //------------------------------------------------------------------------------void DialogAttach::on_txtSearch_textChanged(const QString &text) { updateList(text);}//------------------------------------------------------------------------------// Name: updateList(const QString &filter)// Desc: //------------------------------------------------------------------------------void DialogAttach::updateList(const QString &filter) { QMap<BaseTypes::pid_t, ProcessInfo> procs; QDir procDir("/proc/"); QFileInfoList entries = procDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); foreach(QFileInfo info, entries) { if(isNumeric(info.fileName())) { const QDir proc(info.absoluteFilePath()); const QString status = proc.absolutePath() + "/status"; const char *const strStatus = qPrintable(status); std::ifstream file(strStatus); ProcessInfo procInfo; procInfo.pid = info.fileName().toUInt(); while(file) { std::string line; std::getline(file, line); const QString qLine(line.c_str()); const QStringList values = qLine.split(':', QString::SkipEmptyParts); if(values.size() >= 2) { if(values[0] == "Name") { procInfo.name = values[1].simplified(); } if(values[0] == "Uid") { const QStringList uids = values[1].simplified().split(' ', QString::SkipEmptyParts); procInfo.uid = uids[0].toUInt(); } } } procs.insert(procInfo.pid, procInfo); } } const BaseTypes::uid_t myUID = getuid(); const bool filterUID = ui.chkFilterUID->isChecked(); const QString lowerFilter = filter.toLower(); ui.tableWidget->setSortingEnabled(false); ui.tableWidget->setRowCount(0); foreach(ProcessInfo procInfo, procs) { const QString procName = procInfo.name; if(filter.isEmpty() || procName.toLower().contains(lowerFilter)) { if(!filterUID || procInfo.uid == myUID) { const int row = ui.tableWidget->rowCount(); ui.tableWidget->insertRow(row); ui.tableWidget->setItem(row, 0, new QTableWidgetItem(QString().sprintf("%d", procInfo.pid))); ui.tableWidget->setItem(row, 1, new QTableWidgetItem(QString().sprintf("%d", procInfo.uid))); ui.tableWidget->setItem(row, 2, new QTableWidgetItem(procInfo.name)); } } } ui.tableWidget->setSortingEnabled(true);}//------------------------------------------------------------------------------// Name: showEvent(QShowEvent *event)// Desc: //------------------------------------------------------------------------------void DialogAttach::showEvent(QShowEvent *event) { Q_UNUSED(event); updateList(ui.txtSearch->text());}//------------------------------------------------------------------------------// Name: on_chkFilterUID_clicked(bool checked)// Desc: //------------------------------------------------------------------------------void DialogAttach::on_chkFilterUID_clicked(bool checked) { Q_UNUSED(checked); updateList(ui.txtSearch->text());}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -