2019-06-25 18:52:00 +01:00
|
|
|
#include "montabwidget.h"
|
2019-08-07 03:07:04 +01:00
|
|
|
#include "noscrollcombobox.h"
|
2019-10-01 03:24:39 +01:00
|
|
|
#include "editor.h"
|
2023-01-15 04:10:58 +00:00
|
|
|
#include "encountertablemodel.h"
|
|
|
|
#include "encountertabledelegates.h"
|
|
|
|
|
|
|
|
|
2019-06-25 18:52:00 +01:00
|
|
|
|
2019-10-01 03:24:39 +01:00
|
|
|
MonTabWidget::MonTabWidget(Editor *editor, QWidget *parent) : QTabWidget(parent) {
|
|
|
|
this->editor = editor;
|
2019-06-25 18:52:00 +01:00
|
|
|
populate();
|
2023-01-16 20:45:45 +00:00
|
|
|
this->tabBar()->installEventFilter(this);
|
2019-06-25 18:52:00 +01:00
|
|
|
}
|
|
|
|
|
2019-08-07 03:10:22 +01:00
|
|
|
bool MonTabWidget::eventFilter(QObject *, QEvent *event) {
|
2019-07-03 02:44:19 +01:00
|
|
|
// Press right mouse button to activate tab.
|
2019-06-25 18:52:00 +01:00
|
|
|
if (event->type() == QEvent::MouseButtonPress
|
|
|
|
&& static_cast<QMouseEvent *>(event)->button() == Qt::RightButton) {
|
|
|
|
QPoint eventPos = static_cast<QMouseEvent *>(event)->pos();
|
|
|
|
int tabIndex = tabBar()->tabAt(eventPos);
|
|
|
|
if (tabIndex > -1) {
|
|
|
|
askActivateTab(tabIndex, eventPos);
|
|
|
|
}
|
2023-01-16 20:45:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (event->type() == QEvent::Wheel) {
|
|
|
|
return true;
|
2019-06-25 18:52:00 +01:00
|
|
|
}
|
2019-08-07 03:10:22 +01:00
|
|
|
return false;
|
2019-06-25 18:52:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MonTabWidget::populate() {
|
2019-10-01 03:24:39 +01:00
|
|
|
EncounterFields fields = editor->project->wildMonFields;
|
2019-06-25 18:52:00 +01:00
|
|
|
activeTabs = QVector<bool>(fields.size(), false);
|
|
|
|
|
2019-09-30 00:07:34 +01:00
|
|
|
for (EncounterField field : fields) {
|
2023-01-15 04:10:58 +00:00
|
|
|
QTableView *table = new QTableView(this);
|
2019-06-25 18:52:00 +01:00
|
|
|
table->clearFocus();
|
2019-09-30 00:07:34 +01:00
|
|
|
addTab(table, field.name);
|
2019-06-25 18:52:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MonTabWidget::askActivateTab(int tabIndex, QPoint menuPos) {
|
2023-01-17 04:23:41 +00:00
|
|
|
if (activeTabs[tabIndex]) {
|
|
|
|
// copy from another tab
|
|
|
|
QMenu contextMenu(this);
|
|
|
|
|
|
|
|
for (int i = 0; i < this->tabBar()->count(); i++) {
|
|
|
|
if (tabIndex == i) continue;
|
|
|
|
if (!activeTabs[i]) continue;
|
|
|
|
|
|
|
|
QString tabText = this->tabBar()->tabText(i);
|
|
|
|
QAction *actionCopyFrom = new QAction(QString("Copy encounters from %1").arg(tabText), &contextMenu);
|
|
|
|
|
|
|
|
connect(actionCopyFrom, &QAction::triggered, [=](){
|
|
|
|
EncounterTableModel *model = static_cast<EncounterTableModel *>(this->tableAt(i)->model());
|
|
|
|
WildMonInfo copyInfo = model->encounterData();
|
|
|
|
clearTableAt(tabIndex);
|
|
|
|
WildMonInfo newInfo = getDefaultMonInfo(editor->project->wildMonFields.at(tabIndex));
|
|
|
|
combineEncounters(newInfo, copyInfo);
|
|
|
|
populateTab(tabIndex, newInfo);
|
|
|
|
emit editor->wildMonDataChanged();
|
|
|
|
});
|
|
|
|
|
|
|
|
contextMenu.addAction(actionCopyFrom);
|
|
|
|
}
|
|
|
|
contextMenu.exec(mapToGlobal(menuPos));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QMenu contextMenu(this);
|
|
|
|
|
|
|
|
QString tabText = tabBar()->tabText(tabIndex);
|
|
|
|
QAction actionActivateTab(QString("Add %1 data for this map...").arg(tabText), this);
|
|
|
|
connect(&actionActivateTab, &QAction::triggered, [=](){
|
|
|
|
clearTableAt(tabIndex);
|
|
|
|
populateTab(tabIndex, getDefaultMonInfo(editor->project->wildMonFields.at(tabIndex)));
|
|
|
|
editor->saveEncounterTabData();
|
|
|
|
setCurrentIndex(tabIndex);
|
|
|
|
emit editor->wildMonDataChanged();
|
|
|
|
});
|
|
|
|
contextMenu.addAction(&actionActivateTab);
|
|
|
|
contextMenu.exec(mapToGlobal(menuPos));
|
|
|
|
}
|
2019-06-25 18:52:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MonTabWidget::clearTableAt(int tabIndex) {
|
2023-01-15 04:10:58 +00:00
|
|
|
QTableView *table = tableAt(tabIndex);
|
2019-06-25 18:52:00 +01:00
|
|
|
if (table) {
|
2023-01-17 04:23:41 +00:00
|
|
|
table->reset();
|
2019-06-25 18:52:00 +01:00
|
|
|
table->horizontalHeader()->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 04:10:58 +00:00
|
|
|
void MonTabWidget::populateTab(int tabIndex, WildMonInfo monInfo) {
|
|
|
|
QTableView *speciesTable = tableAt(tabIndex);
|
2019-06-25 18:52:00 +01:00
|
|
|
|
2023-01-15 04:10:58 +00:00
|
|
|
EncounterTableModel *model = new EncounterTableModel(monInfo, editor->project->wildMonFields, tabIndex, this);
|
|
|
|
connect(model, &EncounterTableModel::edited, editor, &Editor::saveEncounterTabData);
|
|
|
|
speciesTable->setModel(model);
|
2019-06-25 18:52:00 +01:00
|
|
|
|
2023-01-15 04:10:58 +00:00
|
|
|
speciesTable->setItemDelegateForColumn(EncounterTableModel::ColumnType::Species, new SpeciesComboDelegate(editor->project, this));
|
|
|
|
speciesTable->setItemDelegateForColumn(EncounterTableModel::ColumnType::MinLevel, new SpinBoxDelegate(editor->project, this));
|
|
|
|
speciesTable->setItemDelegateForColumn(EncounterTableModel::ColumnType::MaxLevel, new SpinBoxDelegate(editor->project, this));
|
|
|
|
speciesTable->setItemDelegateForColumn(EncounterTableModel::ColumnType::EncounterRate, new SpinBoxDelegate(editor->project, this));
|
2019-06-25 18:52:00 +01:00
|
|
|
|
2023-01-17 04:23:41 +00:00
|
|
|
speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::Slot, QHeaderView::ResizeToContents);
|
|
|
|
speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::Group, QHeaderView::ResizeToContents);
|
2023-01-15 04:10:58 +00:00
|
|
|
speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::Species, QHeaderView::Stretch);
|
|
|
|
speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::MinLevel, QHeaderView::Stretch);
|
|
|
|
speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::MaxLevel, QHeaderView::Stretch);
|
2023-01-17 04:23:41 +00:00
|
|
|
speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::SlotRatio, QHeaderView::ResizeToContents);
|
2023-01-15 04:10:58 +00:00
|
|
|
speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::EncounterChance, QHeaderView::ResizeToContents);
|
|
|
|
speciesTable->horizontalHeader()->setSectionResizeMode(EncounterTableModel::ColumnType::EncounterRate, QHeaderView::ResizeToContents);
|
2019-06-25 18:52:00 +01:00
|
|
|
|
2023-01-15 04:10:58 +00:00
|
|
|
// give enough vertical space for icons + margins
|
|
|
|
speciesTable->verticalHeader()->setMinimumSectionSize(40);
|
2019-06-26 20:15:50 +01:00
|
|
|
|
2023-01-15 04:10:58 +00:00
|
|
|
if (editor->project->wildMonFields[tabIndex].groups.empty()) {
|
|
|
|
speciesTable->setColumnHidden(1, true);
|
2019-06-25 18:52:00 +01:00
|
|
|
}
|
2019-09-30 00:07:34 +01:00
|
|
|
|
2023-01-15 04:10:58 +00:00
|
|
|
speciesTable->horizontalHeader()->show();
|
|
|
|
this->setTabActive(tabIndex, true);
|
2019-06-25 18:52:00 +01:00
|
|
|
}
|
|
|
|
|
2023-01-15 04:10:58 +00:00
|
|
|
QTableView *MonTabWidget::tableAt(int tabIndex) {
|
|
|
|
return static_cast<QTableView *>(this->widget(tabIndex));
|
2019-06-25 18:52:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MonTabWidget::setTabActive(int index, bool active) {
|
|
|
|
activeTabs[index] = active;
|
|
|
|
setTabEnabled(index, active);
|
2019-08-06 18:53:54 +01:00
|
|
|
if (!active) {
|
|
|
|
setTabToolTip(index, "Right-click an inactive tab to add new fields.");
|
|
|
|
} else {
|
|
|
|
setTabToolTip(index, QString());
|
|
|
|
}
|
2019-06-25 18:52:00 +01:00
|
|
|
}
|