2018-08-17 08:25:58 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2018 Srivats P.
|
|
|
|
|
|
|
|
This file is part of "Ostinato"
|
|
|
|
|
|
|
|
This 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 3 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 <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "logswindow.h"
|
|
|
|
|
|
|
|
#include "logsmodel.h"
|
2018-08-23 12:53:07 -05:00
|
|
|
#include "modeltest.h"
|
2018-08-17 08:25:58 -05:00
|
|
|
|
2018-08-23 12:53:07 -05:00
|
|
|
#include <QDockWidget>
|
2018-08-17 08:25:58 -05:00
|
|
|
#include <QHeaderView>
|
2019-02-10 06:45:35 -06:00
|
|
|
#include <QMainWindow>
|
|
|
|
#include <QMovie>
|
2020-05-10 08:13:08 -05:00
|
|
|
#include <QPropertyAnimation>
|
2019-02-10 06:45:35 -06:00
|
|
|
|
|
|
|
extern QMainWindow *mainWindow;
|
2018-08-17 08:25:58 -05:00
|
|
|
|
|
|
|
LogsWindow::LogsWindow(LogsModel *model, QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
setupUi(this);
|
2018-08-23 12:53:07 -05:00
|
|
|
logs->setModel(model);
|
|
|
|
autoScroll->setChecked(true);
|
2018-08-17 08:25:58 -05:00
|
|
|
|
|
|
|
logs->verticalHeader()->setHighlightSections(false);
|
|
|
|
logs->verticalHeader()->setDefaultSectionSize(
|
|
|
|
logs->verticalHeader()->minimumSectionSize());
|
|
|
|
logs->setShowGrid(false);
|
|
|
|
logs->setAlternatingRowColors(true);
|
|
|
|
logs->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch);
|
|
|
|
|
2018-08-23 12:53:07 -05:00
|
|
|
parentDock_ = qobject_cast<QDockWidget*>(parent);
|
|
|
|
windowTitle_ = parentDock_->windowTitle();
|
|
|
|
|
2019-02-10 06:45:35 -06:00
|
|
|
warnAnime_ = new QMovie(":/icons/anime_warn.gif", QByteArray(), this);
|
|
|
|
errorAnime_ = new QMovie(":/icons/anime_error.gif", QByteArray(), this);
|
|
|
|
|
2020-05-10 08:13:08 -05:00
|
|
|
alert_ = new QLabel("ALERT!", this,
|
|
|
|
Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
|
|
|
|
alert_->setScaledContents(true);
|
2020-06-13 11:06:51 -05:00
|
|
|
alert_->hide();
|
2020-05-10 08:13:08 -05:00
|
|
|
alertAnime_ = new QPropertyAnimation(alert_, "geometry", this);
|
|
|
|
alertAnime_->setDuration(2000);
|
|
|
|
alertAnime_->setEasingCurve(QEasingCurve::InOutExpo);
|
|
|
|
|
2018-08-23 12:53:07 -05:00
|
|
|
connect(level, SIGNAL(currentIndexChanged(int)),
|
|
|
|
model, SLOT(setLogLevel(int)));
|
|
|
|
connect(clear, SIGNAL(clicked()), model, SLOT(clear()));
|
|
|
|
|
|
|
|
connect(parentDock_, SIGNAL(visibilityChanged(bool)),
|
|
|
|
SLOT(when_visibilityChanged(bool)));
|
|
|
|
connect(model, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
|
|
|
|
SLOT(when_rowsInserted(const QModelIndex&, int, int)));
|
|
|
|
|
2019-02-25 10:15:39 -06:00
|
|
|
connect(logs->horizontalHeader(), SIGNAL(sectionResized(int, int, int)),
|
|
|
|
logs, SLOT(resizeRowsToContents()));
|
|
|
|
|
2018-08-23 12:53:07 -05:00
|
|
|
#if defined(QT_NO_DEBUG) || QT_VERSION < 0x050700
|
|
|
|
logsModelTest_ = nullptr;
|
|
|
|
#else
|
|
|
|
logsModelTest_ = new ModelTest(model);
|
|
|
|
#endif
|
2018-08-17 08:25:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
LogsWindow::~LogsWindow()
|
|
|
|
{
|
2019-02-10 06:45:35 -06:00
|
|
|
delete warnAnime_;
|
|
|
|
delete errorAnime_;
|
2018-08-23 12:53:07 -05:00
|
|
|
delete logsModelTest_;
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:10:31 -05:00
|
|
|
void LogsWindow::clearCurrentSelection()
|
|
|
|
{
|
|
|
|
logs->selectionModel()->clearCurrentIndex();
|
|
|
|
logs->clearSelection();
|
|
|
|
}
|
|
|
|
|
2018-08-23 12:53:07 -05:00
|
|
|
void LogsWindow::when_visibilityChanged(bool visible)
|
|
|
|
{
|
|
|
|
if (visible) {
|
2019-02-25 10:15:39 -06:00
|
|
|
logs->resizeRowsToContents();
|
2020-05-10 08:13:08 -05:00
|
|
|
setState(kInfo);
|
2018-08-23 12:53:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
isVisible_ = visible;
|
|
|
|
qDebug("isVisible = %u", isVisible_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogsWindow::when_rowsInserted(const QModelIndex &parent,
|
|
|
|
int first, int last)
|
|
|
|
{
|
2020-05-10 08:13:08 -05:00
|
|
|
if (isVisible_)
|
2019-02-25 10:15:39 -06:00
|
|
|
logs->resizeRowsToContents();
|
2018-08-23 12:53:07 -05:00
|
|
|
|
2020-05-10 08:13:08 -05:00
|
|
|
State incrementalState = kInfo;
|
2018-08-23 12:53:07 -05:00
|
|
|
for (int i = first; i <= last; i++) {
|
|
|
|
// FIXME: use a user-role instead, so we don't need to know column and
|
|
|
|
// have to compare strings?
|
|
|
|
QString level = logs->model()->data(logs->model()->index(i, 1, parent))
|
|
|
|
.toString();
|
|
|
|
if (level == "Error") {
|
2020-05-10 08:13:08 -05:00
|
|
|
incrementalState = kError;
|
2018-08-23 12:53:07 -05:00
|
|
|
break; // Highest level - no need to look further
|
|
|
|
}
|
|
|
|
else if (level == "Warning") {
|
2020-05-10 08:13:08 -05:00
|
|
|
incrementalState = kWarning;
|
2018-08-23 12:53:07 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-10 08:13:08 -05:00
|
|
|
alert(incrementalState);
|
|
|
|
if (incrementalState > state())
|
|
|
|
setState(incrementalState);
|
2018-08-17 08:25:58 -05:00
|
|
|
}
|
|
|
|
|
2018-08-23 12:53:07 -05:00
|
|
|
void LogsWindow::on_autoScroll_toggled(bool checked)
|
|
|
|
{
|
|
|
|
if (checked) {
|
|
|
|
connect(logs->model(),
|
|
|
|
SIGNAL(rowsInserted(const QModelIndex&, int, int)),
|
|
|
|
logs, SLOT(scrollToBottom()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
disconnect(logs->model(),
|
|
|
|
SIGNAL(rowsInserted(const QModelIndex&, int, int)),
|
|
|
|
logs, SLOT(scrollToBottom()));
|
|
|
|
}
|
|
|
|
}
|
2019-02-10 06:45:35 -06:00
|
|
|
|
2020-05-10 08:13:08 -05:00
|
|
|
LogsWindow::State LogsWindow::state()
|
|
|
|
{
|
|
|
|
return state_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogsWindow::setState(State state)
|
|
|
|
{
|
|
|
|
if (isVisible_)
|
|
|
|
return;
|
|
|
|
state_ = state;
|
|
|
|
notify();
|
|
|
|
}
|
|
|
|
|
2019-02-10 06:45:35 -06:00
|
|
|
QLabel* LogsWindow::tabIcon()
|
|
|
|
{
|
|
|
|
QList<QTabBar*> tabBars = mainWindow->findChildren<QTabBar*>();
|
|
|
|
foreach(QTabBar* tabBar, tabBars) {
|
|
|
|
for (int i = 0; i < tabBar->count(); i++) {
|
|
|
|
if (tabBar->tabText(i).startsWith(windowTitle_)) {
|
|
|
|
QLabel *icon = qobject_cast<QLabel*>(
|
|
|
|
tabBar->tabButton(i, QTabBar::LeftSide));
|
|
|
|
if (!icon) { // Lazy create
|
|
|
|
icon = new QLabel();
|
|
|
|
tabBar->setTabButton(i, QTabBar::LeftSide, icon);
|
|
|
|
}
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-31 10:27:16 -05:00
|
|
|
//! Popup and animate a big icon
|
2020-05-10 08:13:08 -05:00
|
|
|
void LogsWindow::alert(State state)
|
|
|
|
{
|
|
|
|
if (state == kInfo)
|
|
|
|
return;
|
|
|
|
|
2020-05-31 10:27:16 -05:00
|
|
|
// start - center of main window
|
2020-05-10 08:13:08 -05:00
|
|
|
QRect start;
|
2020-05-31 10:27:16 -05:00
|
|
|
QWidget *view = mainWindow;
|
2022-02-09 10:00:51 -06:00
|
|
|
if (!view)
|
|
|
|
return;
|
2020-05-10 08:13:08 -05:00
|
|
|
alert_->setParent(view);
|
2020-05-31 10:27:16 -05:00
|
|
|
alert_->raise();
|
2020-05-10 08:13:08 -05:00
|
|
|
start.setSize(QSize(256, 256).scaled(view->size()/2, Qt::KeepAspectRatio));
|
|
|
|
start.moveCenter(QPoint(view->size().width()/2,
|
|
|
|
view->size().height()/2));
|
2020-05-31 10:27:16 -05:00
|
|
|
// end - center of logs window if visible, tab icon otherwise
|
2020-05-10 08:13:08 -05:00
|
|
|
QPoint c;
|
|
|
|
QLabel *icon = tabIcon();
|
2020-05-31 10:27:16 -05:00
|
|
|
view = isVisible_ ? dynamic_cast<QWidget*>(this) : mainWindow;
|
2020-05-10 08:13:08 -05:00
|
|
|
if (icon && !isVisible_) {
|
|
|
|
c = icon->geometry().center(); // in icon's parent (tabBar) coords
|
|
|
|
c = icon->mapFromParent(c); // in icon's own coords
|
2020-05-31 10:27:16 -05:00
|
|
|
c = icon->mapTo(view, c); // in mainWindow's coords
|
|
|
|
} else {
|
2020-05-10 08:13:08 -05:00
|
|
|
c = view->geometry().center();
|
2020-05-31 10:27:16 -05:00
|
|
|
c = view->mapTo(mainWindow, c); // in mainWindow's coords
|
|
|
|
}
|
2020-05-10 08:13:08 -05:00
|
|
|
|
|
|
|
QRect end;
|
|
|
|
end.moveCenter(c);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case kError:
|
|
|
|
alert_->setPixmap(QPixmap(":/icons/error.svg"));
|
|
|
|
break;
|
|
|
|
case kWarning:
|
|
|
|
alert_->setPixmap(QPixmap(":/icons/warn.svg"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
alertAnime_->setStartValue(start);
|
|
|
|
alertAnime_->setEndValue(end);
|
|
|
|
alert_->show(); // ensure it's visible before starting animation
|
|
|
|
alertAnime_->start();
|
|
|
|
}
|
|
|
|
|
2020-05-31 10:27:16 -05:00
|
|
|
//! Show tab icon
|
2019-02-10 06:45:35 -06:00
|
|
|
void LogsWindow::notify()
|
|
|
|
{
|
|
|
|
QString annotation;
|
|
|
|
QMovie *anime = nullptr;
|
|
|
|
|
|
|
|
// Stop all animations before we start a new one
|
|
|
|
warnAnime_->stop();
|
|
|
|
errorAnime_->stop();
|
|
|
|
|
2020-05-10 08:13:08 -05:00
|
|
|
switch (state()) {
|
2019-02-10 06:45:35 -06:00
|
|
|
case kError:
|
|
|
|
anime = errorAnime_;
|
|
|
|
annotation = " - Error(s)";
|
|
|
|
break;
|
|
|
|
case kWarning:
|
|
|
|
anime = warnAnime_;
|
|
|
|
annotation = " - Warning(s)";
|
|
|
|
break;
|
|
|
|
case kInfo:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
QLabel *icon = tabIcon(); // NOTE: we may not have a icon if not tabified
|
|
|
|
if (icon) {
|
|
|
|
if (anime) {
|
|
|
|
icon->setMovie(anime);
|
|
|
|
anime->start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
icon->clear();
|
|
|
|
icon->adjustSize();
|
|
|
|
}
|
|
|
|
parentDock_->setWindowTitle(windowTitle_ + annotation);
|
|
|
|
}
|
|
|
|
|