15 #include <QScriptValue>
74 QApplication::setOverrideCursor(Qt::WaitCursor);
75 setWindowTitle(model->
data(
root, Qt::DisplayRole).toString());
88 setAttribute(Qt::WA_DeleteOnClose);
90 QVBoxLayout*
layout =
new QVBoxLayout(
this);
93 QHBoxLayout* buttons =
new QHBoxLayout();
99 QPushButton*
report =
new QPushButton(tr(
"Report"),
this);
101 buttons->addWidget(
report);
103 layout->addLayout(buttons);
105 auto splitter =
new QSplitter(
this);
106 layout->addWidget(splitter);
108 myTree =
new QTreeView(
this);
111 splitter->addWidget(
myTree);
112 myTree->setIconSize(QSize(32, 32));
115 myTree->setSelectionMode(QAbstractItemView::SingleSelection);
116 myTree->setDragEnabled(
true);
117 myTree->setDropIndicatorShown(
true);
118 myTree->setDragDropMode(QAbstractItemView::DragDrop);
119 myTree->setContextMenuPolicy(Qt::CustomContextMenu);
122 connect(
myModel, &QAbstractItemModel::rowsInserted, [
this](
const QModelIndex &parent,
int ,
int ) {
126 QWidget* w =
new QWidget(splitter);
128 splitter->addWidget(w);
132 dummy->
addLabel(tr(
"No details available."),
nullptr);
165 splitter->setSizes({5000, 5000});
167 QApplication::restoreOverrideCursor();
194 newPanel->setAsset(
item);
204 panel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
212 if (dlg.exec() != QDialog::Accepted)
222 if (dlg.exec() != QDialog::Accepted)
236 QAction* clear =
new QAction(tr(
"(none)"),
this);
245 QAction*
a =
new QAction(
filter->name(),
this);
253 QAction* quick =
new QAction(tr(
"Quick filter..."),
this);
256 QAction* dialog =
new QAction(tr(
"Filters definition..."),
this);
272 QString
filter = QInputDialog::getText(
this, tr(
"Quick filter"), tr(
"Filter:"), QLineEdit::Normal,
myModel->
filter(), &ok);
287 if (
myTree->model() ==
nullptr) {
293 myTree->setModel(
nullptr);
308 QAction*
a =
new QAction(
report->name(),
this);
317 QAction* dialog =
new QAction(tr(
"Reports definition..."),
this);
330 QProgressDialog progress(tr(
"Generating report..."), tr(
"Abort report"), 0,
count - 1,
this);
331 progress.setWindowTitle(tr(
"Report: '%1'").arg(
report->name()));
332 progress.setWindowModality(Qt::WindowModal);
334 QStringList headers =
report->header().split(
"\n");
335 QStringList fields =
report->itemDisplay().split(
"\n");
336 QString sort =
report->itemSort();
338 QString
text(
"<table><thead><tr>");
340 foreach(QString
header, headers)
344 text +=
"</tr></thead><tbody>";
347 std::vector<QScriptValue> items;
348 for (
int i = 0; i <
count; i++) {
350 if (!idx.isValid()) {
353 idx =
myModel->mapToSource(idx);
354 auto w =
static_cast<AssetWrapper *
>(idx.internalPointer());
355 items.push_back(engine.newQObject(w));
362 progress.setLabelText(tr(
"Sorting items..."));
364 engine.pushContext();
366 sort =
"(function(left, right) { return " + sort +
"; })";
367 QScriptValue sortFun = engine.evaluate(sort);
368 if (!sortFun.isValid() || engine.hasUncaughtException())
369 throw std::runtime_error(
"A script error happened while compiling the sort criteria:\n" + engine.uncaughtException().toString().toStdString());
371 std::sort(items.begin(), items.end(), [&sortFun, &engine](QScriptValue left, QScriptValue right) {
372 QScriptValueList args;
373 args.push_back(left);
374 args.push_back(right);
375 auto ret = sortFun.call(QScriptValue(), args);
376 if (!ret.isValid() || engine.hasUncaughtException())
378 throw std::runtime_error(
"A script error happened while sorting items:\n" + engine.uncaughtException().toString().toStdString());
381 return ret.isValid() ? ret.toBoolean() :
true;
386 catch (std::runtime_error& ex)
388 QMessageBox::critical(
this,
"Script error", ex.what(), QMessageBox::Ok);
393 progress.setLabelText(tr(
"Generating items text..."));
394 foreach(QScriptValue
item, items)
396 if (progress.wasCanceled())
401 engine.pushContext();
402 engine.globalObject().setProperty(
"item",
item);
404 foreach(QString field, fields)
407 QString
data = engine.evaluate(field).toString();
408 if (engine.hasUncaughtException())
410 QMessageBox::critical(
this,
"Script error",
"A script error happened while display items:\n" + engine.uncaughtException().toString(), QMessageBox::Ok);
419 progress.setValue(progress.value() + 1);
423 QStringList footers =
report->footer().split(
"\n");
426 foreach(QString footer, footers)
428 text +=
"<th>" + footer +
"</th>";
433 qDebug() <<
"report finished";
445 if (
index.isValid() &&
index.internalPointer()) {
448 item->fillMenu(&menu);
452 if (menu.actions().size() == 0)
454 menu.exec(
myTree->mapToGlobal(pos) + QPoint(5, 5));
459 auto name = QInputDialog::getText(
this,
"Create new quest",
"New quest code").toStdString();
464 QMessageBox::critical(
this,
"Quest already exists", tr(
"Quest %1 already exists!").arg(
name.data()));