| 1 | /* ---------------------------------- logDialog.cpp --------------------------------------------------------------------------- |
| 2 | Class to display the "view log" dialog |
| 3 | |
| 4 | =============================================================================================================================== |
| 5 | =============================================================================================================================== |
| 6 | This file is part of "luckyBackup" project |
| 7 | Copyright, Loukas Avgeriou |
| 8 | luckyBackup is distributed under the terms of the GNU General Public License |
| 9 | luckyBackup is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 3 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | luckyBackup is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with luckyBackup. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | |
| 23 | project version : Please see "main.cpp" for project version |
| 24 | |
| 25 | developer : luckyb |
| 26 | last modified : 22 May 2016 |
| 27 | =============================================================================================================================== |
| 28 | =============================================================================================================================== |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include "logDialog.h" |
| 33 | |
| 34 | #include <QTimer> |
| 35 | |
| 36 | #include "global.h" |
| 37 | #include "readLogFile.h" |
| 38 | |
| 39 | // class logDialog Constructor================================================================================================= |
| 40 | |
| 41 | logDialog::logDialog (QUrl logUrl, QWidget *parent) : QDialog (parent) |
| 42 | { |
| 43 | uiL.setupUi(this); |
| 44 | logText = ""; |
| 45 | errorCount = 0; |
| 46 | totalErrors = 0; |
| 47 | |
| 48 | readlogfile = new ReadLogFile(this); |
| 49 | connect( readlogfile, SIGNAL(error(const QString)), this, SLOT(refreshWindowError(const QString &)) ); |
| 50 | connect( readlogfile, SIGNAL(newText(const QString)), this, SLOT(refreshWindow(const QString &)) ); |
| 51 | connect( readlogfile, SIGNAL(terminated()), this, SLOT(logReadEnd())); |
| 52 | connect( readlogfile, SIGNAL(finished()), this, SLOT(logReadEnd())); |
| 53 | |
| 54 | (uiL.logView->document())->setMaximumBlockCount(50000); // set maximum blocks to avoid freeze |
| 55 | |
| 56 | connect ( uiL.button_close, SIGNAL( clicked() ), this, SLOT( closeLog() ) ); //connect close pushButton SLOT ---------------- |
| 57 | connect ( uiL.button_previous, SIGNAL( clicked() ), this, SLOT( prevError() ) ); //connect previous pushButton SLOT |
| 58 | connect ( uiL.button_next, SIGNAL( clicked() ), this, SLOT( nextError() ) ); //connect next pushButton SLOT |
| 59 | |
| 60 | readlogfile -> setLogUrl(logUrl.toString()); |
| 61 | QTimer::singleShot(500, this, SLOT(readLogFileNOW())); // allow some time for the dialog to appear |
| 62 | } |
| 63 | |
| 64 | // readLogFileNOW ===================================================================================================== |
| 65 | //Start a new thread to read the logfile |
| 66 | void logDialog::readLogFileNOW() |
| 67 | { |
| 68 | uiL.logView -> setText(tr("loading logfile...")); |
| 69 | uiL.button_close -> setText(tr("wait")); |
| 70 | readlogfile -> start(); |
| 71 | } |
| 72 | |
| 73 | // refreshWindow ===================================================================================================== |
| 74 | //Refresh the log window with text |
| 75 | void logDialog::refreshWindow(const QString &newText) |
| 76 | { |
| 77 | if (uiL.logView -> toPlainText() == "loading logfile...") |
| 78 | uiL.logView -> clear(); |
| 79 | uiL.logView -> append(newText); |
| 80 | totalErrors = readlogfile -> TotalErrors(); |
| 81 | // QCoreApplication::processEvents(); |
| 82 | } |
| 83 | |
| 84 | // logReadEnd ===================================================================================================== |
| 85 | //Do some stuff when finished reading log |
| 86 | void logDialog::logReadEnd() |
| 87 | { |
| 88 | if (totalErrors > 0) |
| 89 | uiL.button_next -> setEnabled(true); |
| 90 | uiL.button_close -> setText(tr("close")); |
| 91 | uiL.button_close -> setEnabled(true); |
| 92 | errorCount = 0; //set current error to be the first of the logfile |
| 93 | firstScroll=true; |
| 94 | } |
| 95 | |
| 96 | // refreshWindowError ===================================================================================================== |
| 97 | //Refresh the log window with Error text |
| 98 | void logDialog::refreshWindowError(const QString &errorText) |
| 99 | { |
| 100 | uiL.logView -> setText(errorText); |
| 101 | uiL.button_next -> setEnabled(false); |
| 102 | uiL.button_previous -> setEnabled(false); |
| 103 | uiL.button_close -> setText(tr("close")); |
| 104 | uiL.button_close -> setEnabled(true); |
| 105 | } |
| 106 | |
| 107 | // close button pressed===================================================================================================== |
| 108 | void logDialog::closeLog() |
| 109 | { |
| 110 | close(); //emmit a QcloseEvent |
| 111 | } |
| 112 | |
| 113 | // previous button pressed===================================================================================================== |
| 114 | void logDialog::prevError() |
| 115 | { |
| 116 | errorCount--; //decrease the current error by one |
| 117 | |
| 118 | if (errorCount == 0 ) // if the current error is the first within the logfile disable the previous button |
| 119 | uiL.button_previous -> setEnabled(false); |
| 120 | |
| 121 | if (errorCount < totalErrors-1) //if the current error is less than the last one within the logfile enable the next button |
| 122 | uiL.button_next -> setEnabled(true); |
| 123 | |
| 124 | uiL.logView -> scrollToAnchor("error" + countStr.setNum(errorCount+1)); |
| 125 | } |
| 126 | |
| 127 | // next button pressed===================================================================================================== |
| 128 | void logDialog::nextError() |
| 129 | { |
| 130 | if (!firstScroll) |
| 131 | errorCount++; // increase the current error by one |
| 132 | firstScroll = false; |
| 133 | |
| 134 | if (errorCount == totalErrors-1) // If the current error is the last within the logfile disable the next button |
| 135 | uiL.button_next -> setEnabled(false); |
| 136 | |
| 137 | if (errorCount > 0) // if the current error is greater than the first one within the logfile enable the previous button |
| 138 | uiL.button_previous -> setEnabled(true); |
| 139 | |
| 140 | uiL.logView -> scrollToAnchor("error" + countStr.setNum(errorCount+1)); //scroll to the current error |
| 141 | } |
| 142 | |
| 143 | // end of logDialog.cpp --------------------------------------------------------------------------- |
| 144 |