add buttons for encounter tab actions
This commit is contained in:
parent
6b47d350a0
commit
a2aa5c0129
3 changed files with 82 additions and 51 deletions
|
@ -26,12 +26,17 @@ public:
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void setTabActive(int index, bool active = true);
|
void setTabActive(int index, bool active = true);
|
||||||
|
void deactivateTab(int tabIndex);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool eventFilter(QObject *object, QEvent *event);
|
bool eventFilter(QObject *object, QEvent *event);
|
||||||
void askActivateTab(int tabIndex, QPoint menuPos);
|
|
||||||
|
void actionCopyTab(int index);
|
||||||
|
void actionAddDeleteTab(int index);
|
||||||
|
|
||||||
QVector<bool> activeTabs;
|
QVector<bool> activeTabs;
|
||||||
|
QVector<QPushButton *> addDeleteTabButtons;
|
||||||
|
QVector<QPushButton *> copyTabButtons;
|
||||||
|
|
||||||
Editor *editor;
|
Editor *editor;
|
||||||
};
|
};
|
||||||
|
|
|
@ -657,7 +657,10 @@ void Editor::saveEncounterTabData() {
|
||||||
for (EncounterField monField : project->wildMonFields) {
|
for (EncounterField monField : project->wildMonFields) {
|
||||||
QString fieldName = monField.name;
|
QString fieldName = monField.name;
|
||||||
|
|
||||||
if (!tabWidget->isTabEnabled(fieldIndex++)) continue;
|
if (!tabWidget->isTabEnabled(fieldIndex++)) {
|
||||||
|
encounterHeader.wildMons.erase(fieldName);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
QTableView *monTable = tabWidget->tableAt(fieldIndex - 1);
|
QTableView *monTable = tabWidget->tableAt(fieldIndex - 1);
|
||||||
EncounterTableModel *model = static_cast<EncounterTableModel *>(monTable->model());
|
EncounterTableModel *model = static_cast<EncounterTableModel *>(monTable->model());
|
||||||
|
|
|
@ -13,17 +13,7 @@ MonTabWidget::MonTabWidget(Editor *editor, QWidget *parent) : QTabWidget(parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MonTabWidget::eventFilter(QObject *, QEvent *event) {
|
bool MonTabWidget::eventFilter(QObject *, QEvent *event) {
|
||||||
// Press right mouse button to activate tab.
|
if (event->type() == QEvent::Wheel) {
|
||||||
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);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (event->type() == QEvent::Wheel) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -31,55 +21,74 @@ bool MonTabWidget::eventFilter(QObject *, QEvent *event) {
|
||||||
|
|
||||||
void MonTabWidget::populate() {
|
void MonTabWidget::populate() {
|
||||||
EncounterFields fields = editor->project->wildMonFields;
|
EncounterFields fields = editor->project->wildMonFields;
|
||||||
activeTabs = QVector<bool>(fields.size(), false);
|
activeTabs.resize(fields.size());
|
||||||
|
activeTabs.fill(false);
|
||||||
|
|
||||||
|
addDeleteTabButtons.resize(fields.size());
|
||||||
|
addDeleteTabButtons.fill(nullptr);
|
||||||
|
copyTabButtons.resize(fields.size());
|
||||||
|
copyTabButtons.fill(nullptr);
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
for (EncounterField field : fields) {
|
for (EncounterField field : fields) {
|
||||||
QTableView *table = new QTableView(this);
|
QTableView *table = new QTableView(this);
|
||||||
table->clearFocus();
|
table->clearFocus();
|
||||||
addTab(table, field.name);
|
addTab(table, field.name);
|
||||||
|
|
||||||
|
QPushButton *buttonAddDelete = new QPushButton(QIcon(":/icons/add.ico"), "");
|
||||||
|
connect(buttonAddDelete, &QPushButton::clicked, [=]() { actionAddDeleteTab(index); });
|
||||||
|
addDeleteTabButtons[index] = buttonAddDelete;
|
||||||
|
this->tabBar()->setTabButton(index, QTabBar::RightSide, buttonAddDelete);
|
||||||
|
|
||||||
|
QPushButton *buttonCopy = new QPushButton(QIcon(":/icons/clipboard.ico"), "");
|
||||||
|
connect(buttonCopy, &QPushButton::clicked, [=]() {actionCopyTab(index); });
|
||||||
|
copyTabButtons[index] = buttonCopy;
|
||||||
|
this->tabBar()->setTabButton(index, QTabBar::LeftSide, buttonCopy);
|
||||||
|
|
||||||
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MonTabWidget::askActivateTab(int tabIndex, QPoint menuPos) {
|
void MonTabWidget::actionCopyTab(int index) {
|
||||||
if (activeTabs[tabIndex]) {
|
QMenu contextMenu(this);
|
||||||
// copy from another tab
|
|
||||||
QMenu contextMenu(this);
|
|
||||||
|
|
||||||
for (int i = 0; i < this->tabBar()->count(); i++) {
|
for (int i = 0; i < this->tabBar()->count(); i++) {
|
||||||
if (tabIndex == i) continue;
|
if (index == i) continue;
|
||||||
if (!activeTabs[i]) continue;
|
if (!activeTabs[i]) continue;
|
||||||
|
|
||||||
QString tabText = this->tabBar()->tabText(i);
|
QString tabText = this->tabBar()->tabText(i);
|
||||||
QAction *actionCopyFrom = new QAction(QString("Copy encounters from %1").arg(tabText), &contextMenu);
|
QAction *actionCopyFrom = new QAction(QString("Copy encounters from %1").arg(tabText), &contextMenu);
|
||||||
|
|
||||||
connect(actionCopyFrom, &QAction::triggered, [=](){
|
connect(actionCopyFrom, &QAction::triggered, [=](){
|
||||||
EncounterTableModel *model = static_cast<EncounterTableModel *>(this->tableAt(i)->model());
|
EncounterTableModel *model = static_cast<EncounterTableModel *>(this->tableAt(i)->model());
|
||||||
WildMonInfo copyInfo = model->encounterData();
|
WildMonInfo copyInfo = model->encounterData();
|
||||||
clearTableAt(tabIndex);
|
clearTableAt(index);
|
||||||
WildMonInfo newInfo = getDefaultMonInfo(editor->project->wildMonFields.at(tabIndex));
|
WildMonInfo newInfo = getDefaultMonInfo(editor->project->wildMonFields.at(index));
|
||||||
combineEncounters(newInfo, copyInfo);
|
combineEncounters(newInfo, copyInfo);
|
||||||
populateTab(tabIndex, newInfo);
|
populateTab(index, 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();
|
emit editor->wildMonDataChanged();
|
||||||
});
|
});
|
||||||
contextMenu.addAction(&actionActivateTab);
|
|
||||||
contextMenu.exec(mapToGlobal(menuPos));
|
contextMenu.addAction(actionCopyFrom);
|
||||||
|
}
|
||||||
|
contextMenu.exec(mapToGlobal(this->copyTabButtons[index]->pos() + QPoint(0, this->copyTabButtons[index]->height())));
|
||||||
|
}
|
||||||
|
|
||||||
|
void MonTabWidget::actionAddDeleteTab(int index) {
|
||||||
|
if (activeTabs[index]) {
|
||||||
|
// delete tab
|
||||||
|
clearTableAt(index);
|
||||||
|
deactivateTab(index);
|
||||||
|
editor->saveEncounterTabData();
|
||||||
|
emit editor->wildMonDataChanged();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// add tab
|
||||||
|
clearTableAt(index);
|
||||||
|
populateTab(index, getDefaultMonInfo(editor->project->wildMonFields.at(index)));
|
||||||
|
editor->saveEncounterTabData();
|
||||||
|
setCurrentIndex(index);
|
||||||
|
emit editor->wildMonDataChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,6 +100,18 @@ void MonTabWidget::clearTableAt(int tabIndex) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MonTabWidget::deactivateTab(int tabIndex) {
|
||||||
|
QTableView *speciesTable = tableAt(tabIndex);
|
||||||
|
|
||||||
|
EncounterTableModel *oldModel = static_cast<EncounterTableModel *>(speciesTable->model());
|
||||||
|
WildMonInfo monInfo = oldModel->encounterData();
|
||||||
|
monInfo.active = false;
|
||||||
|
EncounterTableModel *newModel = new EncounterTableModel(monInfo, editor->project->wildMonFields, tabIndex, this);
|
||||||
|
speciesTable->setModel(newModel);
|
||||||
|
|
||||||
|
setTabActive(tabIndex, false);
|
||||||
|
}
|
||||||
|
|
||||||
void MonTabWidget::populateTab(int tabIndex, WildMonInfo monInfo) {
|
void MonTabWidget::populateTab(int tabIndex, WildMonInfo monInfo) {
|
||||||
QTableView *speciesTable = tableAt(tabIndex);
|
QTableView *speciesTable = tableAt(tabIndex);
|
||||||
|
|
||||||
|
@ -131,8 +152,10 @@ void MonTabWidget::setTabActive(int index, bool active) {
|
||||||
activeTabs[index] = active;
|
activeTabs[index] = active;
|
||||||
setTabEnabled(index, active);
|
setTabEnabled(index, active);
|
||||||
if (!active) {
|
if (!active) {
|
||||||
setTabToolTip(index, "Right-click an inactive tab to add new fields.");
|
this->addDeleteTabButtons[index]->setIcon(QIcon(":/icons/add.ico"));
|
||||||
|
this->copyTabButtons[index]->hide();
|
||||||
} else {
|
} else {
|
||||||
setTabToolTip(index, QString());
|
this->addDeleteTabButtons[index]->setIcon(QIcon(":/icons/delete.ico"));
|
||||||
|
this->copyTabButtons[index]->show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue