#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_action_Quit_triggered() { close(); } #include void MainWindow::on_action_Run_triggered() { // QDir dir ("/usr/share/icons/oxygen/base"); QDir dir ("../tree"); QTreeWidgetItem * item = new QTreeWidgetItem; QString path = dir.absolutePath(); item->setText (0, path); item->setToolTip (0, path); item->setData (0, Qt::UserRole, path); item->setForeground (0, QColor ("red")); item->setIcon (0, QIcon::fromTheme ("folder")); ui->treeWidget->invisibleRootItem()->addChild (item); showDir(item, dir); } void MainWindow::showDir (QTreeWidgetItem *target, QDir dir) { QFileInfoList list = dir.entryInfoList (QDir::NoDotAndDotDot | QDir::AllEntries , QDir::Name); for (QFileInfo info : list) { QTreeWidgetItem * item = new QTreeWidgetItem; item->setText (0, info.fileName()); QString path = info.absoluteFilePath(); item->setToolTip (0, path); item->setData (0, Qt::UserRole, path); target->addChild(item); if (info.isDir()) { item->setForeground (0, QColor ("red")); item->setIcon (0, QIcon::fromTheme ("folder")); showDir (item, QDir (path)); } else { item->setForeground (0, QColor ("blue")); QString ext = info.suffix(); QString fname = path; if (ext == "cpp") item->setForeground (0, QColor ("lime")); else if (ext == "png") { item->setForeground (0, QColor ("orange")); QIcon icon = QIcon (fname); item->setIcon (0, icon); } } } } void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column) { QString path = item->data (0, Qt::UserRole).toString(); // ui->textEdit->append ("Click " + path); QFileInfo fi (path); QString ext = fi.suffix(); if (ext == "h" || ext == "cpp") readText (path); else if (ext == "dat") readData (path); else if (ext == "png" || ext == "bmp") readImage (path); else if (ext == "xml" || ext == "ui") readXml (path); } #include void MainWindow::readXml (QString path) { ui->tabWidget->setCurrentIndex(3); ui->xmlWidget->clear(); QTreeWidgetItem * current = ui->xmlWidget->invisibleRootItem(); QFile f (path); if (f.open (QFile::ReadOnly | QFile::Text)) { QXmlStreamReader xml (&f); while (!xml.atEnd()) { xml.readNext(); if (xml.isStartElement ()) { QString s = xml.name ().toString(); QTreeWidgetItem * item = new QTreeWidgetItem; item->setText (0, s); current->addChild (item); current = item; } else if (xml.isEndElement ()) { current = current->parent(); } f.close (); }}+} 7| QPixmap p (path); ui->label->setPixmap (p); } #include void MainWindow::readData (QString path) { ui->tabWidget->setCurrentIndex(1); ui->tableWidget->setColumnCount(0); ui->tableWidget->setRowCount(0); QFile f (path); if (f.open (QFile::ReadOnly | QFile::Text)) { int line = 0; while (! f.atEnd()) { QString s = f.readLine (); s = s.simplified(); if (s != "") { QStringList list = s.split(';'); int col = 0; for (QString t : list) { t = t.simplified (); if (line+1 > ui->tableWidget->rowCount()) ui->tableWidget->setRowCount(line+1); if (col+1 > ui->tableWidget->columnCount()) ui->tableWidget->setColumnCount(col+1); QTableWidgetItem * item = new QTableWidgetItem; item->setText (t); ui->tableWidget->setItem (line, col, item); if (t == "0") item->setForeground (QColor ("red")); else item->setForeground (QColor ("blue")); if (col == line) item->setBackground (QColor ("lightgray")); col ++; } line ++; } } f.close (); } } void MainWindow::on_actionCalc_triggered() { int r = ui->tableWidget->rowCount(); int s = ui->tableWidget->columnCount(); for (int i = 0; i < r; i++) for (int j = 0; j < s; j++) { QTableWidgetItem * item = ui->tableWidget->item (i, j); if (item != NULL) { QString t = item->text(); t = t.simplified(); bool ok; double x = t.toDouble (&ok); if (! ok) item->setBackground(QColor ("orange")); else item->setBackground(QColor ("lime")); } } } void MainWindow::readText (QString path) { ui->tabWidget->setCurrentIndex(0); ui->textEdit->clear (); QFile f (path); if (f.open (QFile::ReadOnly | QFile::Text)) { QByteArray b = f.readAll(); ui->textEdit->setText (b); f.close (); } }