1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/*
* viewer.cpp - plugin
* Copyright (C) 2009-2010 Evgeny Khryukin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the 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 of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "viewer.h"
#include <QFile>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QPushButton>
#include <QTextStream>
Viewer::Viewer(QString filename, IconFactoryAccessingHost *IcoHost, QWidget *parent) :
QDialog(parent), icoHost_(IcoHost), fileName_(filename)
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(filename);
setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint
| Qt::CustomizeWindowHint);
QVBoxLayout *layout = new QVBoxLayout(this);
textWid = new QTextEdit();
QPalette pal = textWid->palette();
pal.setColor(QPalette::Inactive, QPalette::Highlight, pal.color(QPalette::Active, QPalette::Highlight));
pal.setColor(QPalette::Inactive, QPalette::HighlightedText, pal.color(QPalette::Active, QPalette::HighlightedText));
textWid->setPalette(pal);
layout->addWidget(textWid);
findBar = new ClientSwitcher::TypeAheadFindBar(icoHost_, textWid, tr("Find"), this);
QPushButton *Close = new QPushButton(icoHost_->getIcon("psi/quit"), tr("Close"));
QPushButton *Save = new QPushButton(icoHost_->getIcon("psi/save"), tr("Save Changes"));
QPushButton *Delete = new QPushButton(icoHost_->getIcon("psi/remove"), tr("Delete Log"));
QPushButton *Update = new QPushButton(icoHost_->getIcon("psi/reload"), tr("Update Log"));
QHBoxLayout *butLayout = new QHBoxLayout();
butLayout->addWidget(Delete);
butLayout->addStretch();
butLayout->addWidget(Update);
butLayout->addWidget(Save);
butLayout->addWidget(Close);
layout->addWidget(findBar);
layout->addLayout(butLayout);
connect(Close, &QPushButton::released, this, &Viewer::close);
connect(Delete, &QPushButton::released, this, &Viewer::deleteLog);
connect(Save, &QPushButton::released, this, &Viewer::saveLog);
connect(Update, &QPushButton::released, this, &Viewer::updateLog);
connect(findBar, &ClientSwitcher::TypeAheadFindBar::firstPage, this, &Viewer::firstPage);
connect(findBar, &ClientSwitcher::TypeAheadFindBar::lastPage, this, &Viewer::lastPage);
connect(findBar, &ClientSwitcher::TypeAheadFindBar::prevPage, this, &Viewer::prevPage);
connect(findBar, &ClientSwitcher::TypeAheadFindBar::nextPage, this, &Viewer::nextPage);
}
void Viewer::closeEvent(QCloseEvent *e)
{
emit onClose(width(), height());
QDialog::closeEvent(e);
e->accept();
}
void Viewer::deleteLog()
{
int ret = QMessageBox::question(this, tr("Delete log file"), tr("Are you sure?"), QMessageBox::Yes,
QMessageBox::Cancel);
if (ret == QMessageBox::Cancel) {
return;
}
close();
QFile file(fileName_);
if (file.open(QIODevice::ReadWrite)) {
file.remove();
}
}
void Viewer::saveLog()
{
QDateTime Modified = QFileInfo(fileName_).lastModified();
if (lastModified_ < Modified) {
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Save log"));
msgBox.setText(tr("New messages has been added to log. If you save your changes, you will lose them"));
msgBox.setInformativeText(tr("Do you want to save your changes?"));
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Cancel);
int ret = msgBox.exec();
if (ret == QMessageBox::Cancel) {
return;
}
} else {
int ret
= QMessageBox::question(this, tr("Save log"), tr("Are you sure?"), QMessageBox::Yes, QMessageBox::Cancel);
if (ret == QMessageBox::Cancel) {
return;
}
}
QFile file(fileName_);
if (file.open(QIODevice::ReadWrite)) {
file.remove();
}
if (file.open(QIODevice::ReadWrite)) {
QTextStream out(&file);
out.setCodec("UTF-8");
QString Text = textWid->toPlainText();
pages_.insert(currentPage_, Text);
for (int i = 0; i < pages_.size(); i++) {
out.setGenerateByteOrderMark(false);
out << pages_.value(i);
}
}
}
void Viewer::updateLog()
{
pages_.clear();
init();
}
bool Viewer::init()
{
bool b = false;
QFile file(fileName_);
if (file.open(QIODevice::ReadOnly)) {
QString page;
int numPage = 0;
QTextStream in(&file);
in.setCodec("UTF-8");
while (!in.atEnd()) {
page = "";
for (int i = 0; i < 500; i++) {
if (in.atEnd())
break;
page += in.readLine() + "\n";
}
pages_.insert(numPage++, page);
}
currentPage_ = pages_.size() - 1;
lastModified_ = QDateTime::currentDateTime();
setPage();
b = true;
}
return b;
}
void Viewer::setPage()
{
QString text = pages_.value(currentPage_);
textWid->setText(text);
QTextCursor cur = textWid->textCursor();
cur.setPosition(text.length());
textWid->setTextCursor(cur);
}
void Viewer::nextPage()
{
if (currentPage_ < pages_.size() - 1)
currentPage_++;
setPage();
}
void Viewer::prevPage()
{
if (currentPage_ > 0)
currentPage_--;
setPage();
}
void Viewer::lastPage()
{
currentPage_ = pages_.size() - 1;
setPage();
}
void Viewer::firstPage()
{
currentPage_ = 0;
setPage();
}
|