/* * console.cpp * * Created on: Jan 31, 2012 * Author: joe */ #include "console.h" Console::Console(QMainWindow* parent) : QTextEdit(parent) { setWindowFlags(Qt::FramelessWindowHint); // fanciness QPalette pal; pal.setColor(QPalette::Base, Qt::black); setPalette(pal); setTextColor(Qt::white); // CTRL + D toggles debug window QAction* _toggle = new QAction(tr("&toggleDebug"), parentWidget()); _toggle->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_D)); parentWidget()->addAction(_toggle); connect(_toggle, SIGNAL(triggered()), this, SLOT(toggle())); // watcher to detect file change QFileSystemWatcher* _watcher = new QFileSystemWatcher(this); // read from log file _logFile = new QFile(logFilePath); _logFileIn = new QTextStream(_logFile); if (!_logFile->open(QFile::ReadOnly | QFile::Text)) { } else { // watch log file _watcher->addPath(logFilePath); connect(_watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(refresh(const QString&))); } } Console::~Console() { delete _logFile; delete _logFileIn; } void Console::refresh(const QString& fileName) { if (fileName == logFilePath) { while (!_logFileIn->atEnd()) { QString line = _logFileIn->readLine(); if (!line.isEmpty()) this->append(line); } this->moveCursor(QTextCursor::End); } } void Console::toggle() { (this->isVisible()) ? this->hide() : this->show(); }