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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/*
Actiona
Copyright (C) 2005 Jonathan Mercier-Ganady
Actiona 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.
Actiona 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/>.
Contact : jmgr@jmgr.info
*/
#include "consolewidget.h"
#include "ui_consolewidget.h"
#include <QStandardItemModel>
namespace ActionTools
{
ConsoleWidget::ConsoleWidget(QWidget *parent)
: QWidget(parent),
ui(new Ui::ConsoleWidget)
{
ui->setupUi(this);
ui->console->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->console->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->clearPushButton->setEnabled(false);
}
ConsoleWidget::~ConsoleWidget()
{
delete ui;
}
void ConsoleWidget::setup(QStandardItemModel *model)
{
mModel = (model ? model : new QStandardItemModel(0, 1, this));
QItemSelectionModel *oldModel = ui->console->selectionModel();
ui->console->setModel(mModel);
delete oldModel;
// Note: this connection is still string based because it uses a private signal
connect(mModel, SIGNAL(rowsInserted(QModelIndex,int,int)), ui->console, SLOT(scrollToBottom()));
}
void ConsoleWidget::addScriptParameterLine(const QString &message, int parameter, int line, int column, Type type)
{
auto item = new QStandardItem();
item->setData(parameter, ParameterRole);
item->setData(line, LineRole);
item->setData(column, ColumnRole);
addLine(message, item, Parameters, type);
}
void ConsoleWidget::addResourceLine(const QString &message, const QString &resourceKey, ConsoleWidget::Type type)
{
auto item = new QStandardItem();
item->setData(resourceKey, ResourceRole);
addLine(message, item, Resources, type);
}
void ConsoleWidget::addActionLine(const QString &message, qint64 actionRuntimeId, const QString &field, const QString &subField, int line, int column, Type type)
{
auto item = new QStandardItem();
item->setData(actionRuntimeId, ActionRole);
item->setData(field, FieldRole);
item->setData(subField, SubFieldRole);
item->setData(line, LineRole);
item->setData(column, ColumnRole);
addLine(message, item, Action, type);
}
void ConsoleWidget::addUserLine(const QString &message, qint64 actionRuntimeId, const QString &field, const QString &subField, int line, int column, const QStringList &backtrace, Type type)
{
auto item = new QStandardItem();
item->setData(actionRuntimeId, ActionRole);
item->setData(field, FieldRole);
item->setData(subField, SubFieldRole);
item->setData(line, LineRole);
item->setData(column, ColumnRole);
item->setData(backtrace, BacktraceRole);
addLine(message, item, User, type);
}
void ConsoleWidget::addExceptionLine(const QString &message, qint64 actionRuntimeId, int exception, Type type)
{
auto item = new QStandardItem();
item->setData(actionRuntimeId, ActionRole);
item->setData(exception, ExceptionRole);
addLine(message, item, Exception, type);
}
void ConsoleWidget::addDesignErrorLine(const QString &message, Type type)
{
auto item = new QStandardItem();
addLine(message, item, DesignError, type);
}
void ConsoleWidget::addStartSeparator()
{
mStartTime = QDateTime::currentDateTime();
QStandardItem *item = new QStandardItem(tr("Execution started at %1").arg(mStartTime.toString(QStringLiteral("dd/MM/yyyy hh:mm:ss:zzz"))));
item->setTextAlignment(Qt::AlignCenter);
addSeparator(item);
}
void ConsoleWidget::addEndSeparator()
{
const QDateTime ¤tDateTime = QDateTime::currentDateTime();
int days = mStartTime.daysTo(currentDateTime);
QString durationString;
if(days > 0)
durationString += tr("%n day(s) ", "", days);
mStartTime = mStartTime.addDays(-days);
int seconds = mStartTime.secsTo(currentDateTime);
int hours = seconds / 3600;
seconds = seconds % 3600;
int minutes = seconds / 60;
seconds = seconds % 60;
if(hours > 0)
durationString += tr("%n hour(s) ", "", hours);
if(minutes > 0)
durationString += tr("%n minute(s) ", "", minutes);
if(seconds > 0)
durationString += tr("%n second(s) ", "", seconds);
int startMSec = mStartTime.toString(QStringLiteral("z")).toInt();
int endMSec = currentDateTime.toString(QStringLiteral("z")).toInt();
int msec = (endMSec > startMSec) ? (endMSec - startMSec) : (1000 - (startMSec - endMSec));
durationString += tr("%n millisecond(s)", "", msec);
QStandardItem *item = new QStandardItem(tr("Execution ended at %1\n(%2)").arg(currentDateTime.toString(QStringLiteral("dd/MM/yyyy hh:mm:ss:zzz"))).arg(durationString));
item->setTextAlignment(Qt::AlignCenter);
addSeparator(item);
}
void ConsoleWidget::clear()
{
mModel->removeRows(0, mModel->rowCount());
ui->clearPushButton->setEnabled(false);
}
void ConsoleWidget::clearExceptSeparators()
{
int rowCount = mModel->rowCount();
for(int index = rowCount - 1; index >= 0; --index)
{
QStandardItem *item = mModel->item(index);
Type type = item->data(TypeRole).value<Type>();
if(type != Separator)
mModel->removeRow(index);
}
if(mModel->rowCount() == 0)
ui->clearPushButton->setEnabled(false);
}
void ConsoleWidget::updateClearButton()
{
ui->clearPushButton->setEnabled(mModel->rowCount() > 0);
}
void ConsoleWidget::on_clearPushButton_clicked()
{
clear();
}
void ConsoleWidget::on_console_doubleClicked(const QModelIndex &index)
{
emit itemDoubleClicked(index.row());
}
void ActionTools::ConsoleWidget::on_console_clicked(const QModelIndex &index)
{
emit itemClicked(index.row());
}
void ConsoleWidget::addLine(const QString &message, QStandardItem *item, Source source, Type type)
{
QIcon icon;
switch(type)
{
case Information:
icon = QIcon(QStringLiteral(":/images/information.png"));
break;
case Warning:
icon = QIcon(QStringLiteral(":/images/warning.png"));
break;
case Error:
icon = QIcon(QStringLiteral(":/images/error.png"));
break;
case Separator:
Q_ASSERT(false && "Should use addSeparator instead");
break;
}
item->setText(message);
if(source == DesignError)
item->setToolTip(message);
else
item->setToolTip(message + tr("\nDouble-click to show"));
item->setIcon(icon);
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
item->setData(QVariant::fromValue<Source>(source), SourceRole);
item->setData(QVariant::fromValue<Type>(type), TypeRole);
mModel->appendRow(item);
qApp->processEvents(); // This is needed so that the console output gets displayed before a blocking call (such as sleep)
// It would be better not to have any blocking code calls, but then this would cause some bugs when the user cancels the execution during a non-blocking sleep
// Pausing the execution and then resuming it after some time seems to be the best way to do this, but would require important changes in the code
ui->clearPushButton->setEnabled(true);
}
void ConsoleWidget::addSeparator(QStandardItem *item)
{
item->setFlags(nullptr);
item->setBackground(QBrush(Qt::lightGray));
item->setForeground(QBrush(Qt::white));
QFont appFont = QApplication::font();
appFont.setPointSize(7);
item->setFont(appFont);
item->setData(QVariant::fromValue<Type>(Separator), TypeRole);
mModel->appendRow(item);
}
}
|