15 #include <QScriptValue>
75 QApplication::setOverrideCursor(Qt::WaitCursor);
76 setWindowTitle(model->
data(
root, Qt::DisplayRole).toString());
89 setAttribute(Qt::WA_DeleteOnClose);
91 QVBoxLayout*
layout =
new QVBoxLayout(
this);
94 QHBoxLayout* buttons =
new QHBoxLayout();
100 QPushButton*
report =
new QPushButton(tr(
"Report"),
this);
102 buttons->addWidget(
report);
104 layout->addLayout(buttons);
106 auto splitter =
new QSplitter(
this);
107 layout->addWidget(splitter);
109 myTree =
new QTreeView(
this);
112 splitter->addWidget(
myTree);
113 myTree->setIconSize(QSize(32, 32));
116 myTree->setSelectionMode(QAbstractItemView::SingleSelection);
117 myTree->setDragEnabled(
true);
118 myTree->setDropIndicatorShown(
true);
119 myTree->setDragDropMode(QAbstractItemView::DragDrop);
120 myTree->setContextMenuPolicy(Qt::CustomContextMenu);
123 connect(
myModel, &QAbstractItemModel::rowsInserted, [
this](
const QModelIndex &parent,
int ,
int ) {
127 QWidget* w =
new QWidget(splitter);
129 splitter->addWidget(w);
133 dummy->
addLabel(tr(
"No details available."),
nullptr);
166 splitter->setSizes({5000, 5000});
168 QApplication::restoreOverrideCursor();
195 newPanel->setAsset(
item);
205 panel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
213 if (dlg.exec() != QDialog::Accepted)
223 if (dlg.exec() != QDialog::Accepted)
237 QAction* clear =
new QAction(tr(
"(none)"),
this);
246 QAction*
a =
new QAction(
filter->name(),
this);
254 QAction* quick =
new QAction(tr(
"Quick filter..."),
this);
257 QAction* dialog =
new QAction(tr(
"Filters definition..."),
this);
273 if (dlg.exec() != QDialog::Accepted) {
288 if (
myTree->model() ==
nullptr) {
294 myTree->setModel(
nullptr);
309 QAction*
a =
new QAction(
report->name(),
this);
318 QAction* dialog =
new QAction(tr(
"Reports definition..."),
this);
331 QProgressDialog progress(tr(
"Generating report..."), tr(
"Abort report"), 0,
count - 1,
this);
332 progress.setWindowTitle(tr(
"Report: '%1'").arg(
report->name()));
333 progress.setWindowModality(Qt::WindowModal);
335 QStringList headers =
report->header().split(
"\n");
336 QStringList fields =
report->itemDisplay().split(
"\n");
337 QString sort =
report->itemSort();
339 QString
text(
"<table><thead><tr>");
341 foreach(QString
header, headers)
345 text +=
"</tr></thead><tbody>";
348 std::vector<QScriptValue> items;
349 for (
int i = 0; i <
count; i++) {
351 if (!idx.isValid()) {
354 idx =
myModel->mapToSource(idx);
355 auto w =
static_cast<AssetWrapper *
>(idx.internalPointer());
356 items.push_back(engine.newQObject(w));
363 progress.setLabelText(tr(
"Sorting items..."));
365 engine.pushContext();
367 sort =
"(function(left, right) { return " + sort +
"; })";
368 QScriptValue sortFun = engine.evaluate(sort);
369 if (!sortFun.isValid() || engine.hasUncaughtException())
370 throw std::runtime_error(
"A script error happened while compiling the sort criteria:\n" + engine.uncaughtException().toString().toStdString());
372 std::sort(items.begin(), items.end(), [&sortFun, &engine](QScriptValue left, QScriptValue right) {
373 QScriptValueList args;
374 args.push_back(left);
375 args.push_back(right);
376 auto ret = sortFun.call(QScriptValue(), args);
377 if (!ret.isValid() || engine.hasUncaughtException())
379 throw std::runtime_error(
"A script error happened while sorting items:\n" + engine.uncaughtException().toString().toStdString());
382 return ret.isValid() ? ret.toBoolean() :
true;
387 catch (std::runtime_error& ex)
389 QMessageBox::critical(
this,
"Script error", ex.what(), QMessageBox::Ok);
394 progress.setLabelText(tr(
"Generating items text..."));
395 foreach(QScriptValue
item, items)
397 if (progress.wasCanceled())
402 engine.pushContext();
403 engine.globalObject().setProperty(
"item",
item);
405 foreach(QString field, fields)
408 QString
data = engine.evaluate(field).toString();
409 if (engine.hasUncaughtException())
411 QMessageBox::critical(
this,
"Script error",
"A script error happened while display items:\n" + engine.uncaughtException().toString(), QMessageBox::Ok);
420 progress.setValue(progress.value() + 1);
424 QStringList footers =
report->footer().split(
"\n");
427 foreach(QString footer, footers)
429 text +=
"<th>" + footer +
"</th>";
434 qDebug() <<
"report finished";
446 if (
index.isValid() &&
index.internalPointer()) {
449 item->fillMenu(&menu);
453 if (menu.actions().size() == 0)
455 menu.exec(
myTree->mapToGlobal(pos) + QPoint(5, 5));
460 auto name = QInputDialog::getText(
this,
"Create new quest",
"New quest code").toStdString();
465 QMessageBox::critical(
this,
"Quest already exists", tr(
"Quest %1 already exists!").arg(
name.data()));