Files
c-evo-evo/common/Forms/src/FormList.cpp
T

217 lines
7.1 KiB
C++

// This file is part of c-evo-evo.
//
// This software is governed by the CeCILL license under French law and
// abiding by the rules of distribution of free software. You can use,
// modify and/or redistribute the software under the terms of the CeCILL
// license as circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy,
// modify and redistribute granted by the license, users are provided only
// with a limited warranty and the software's author, the holder of the
// economic rights, and the successive licensors have only limited
// liability.
//
// In this respect, the user's attention is drawn to the risks associated
// with loading, using, modifying and/or developing or reproducing the
// software by the user in light of its specific status of free software,
// that may mean that it is complicated to manipulate, and that also
// therefore means that it is reserved for developers and experienced
// professionals having in-depth computer knowledge. Users are therefore
// encouraged to load and test the software's suitability as regards their
// requirements in conditions enabling the security of their systems and/or
// data to be ensured and, more generally, to use and operate it in the
// same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
#include "Cevo/FormList.h"
#include "Cevo/FormItem.h"
#include "ui_FormList.h"
#include <QAction>
#include <QMenu>
#include <QMessageBox>
#include <QShortcut>
namespace Cevo {
FormList::FormList(QWidget *parent) : FormBase(parent), ui(new Ui::FormList)
{
ui->setupUi(this);
m_actionAdd = ui->toolBar1->addAction(tr("Add"), this, &FormList::onAdd);
m_actionAdd->setShortcut(QKeySequence::New);
m_actionModify = ui->toolBar1->addAction(tr("Modify"), this, &FormList::onModify);
m_actionModify->setShortcut(Qt::Key_Return);
m_actionRemove = ui->toolBar1->addAction(tr("Remove"), this, &FormList::onRemove);
m_actionRemove->setShortcut(QKeySequence::Delete);
m_actionClone = ui->toolBar1->addAction(tr("Clone"), this, &FormList::onClone);
auto *selectAll = new QAction(tr("Select all"), this);
selectAll->setShortcut(QKeySequence::SelectAll);
connect(selectAll, &QAction::triggered, this, &FormList::onSelectAll);
addAction(selectAll);
for (QAction *action : { m_actionAdd, m_actionModify, m_actionRemove, m_actionClone })
addAction(action); // also reachable via shortcut when the toolbar isn't focused
connect(ui->listView1, &QTreeWidget::itemDoubleClicked, this, &FormList::onModify);
connect(ui->listView1, &QTreeWidget::itemSelectionChanged, this, &FormList::onSelectionChanged);
connect(ui->listView1, &QTreeWidget::customContextMenuRequested, this, [this](const QPoint &pos) {
QMenu menu(this);
menu.addActions(ui->toolBar1->actions());
menu.exec(ui->listView1->viewport()->mapToGlobal(pos));
});
updateInterface();
}
FormList::~FormList()
{
delete ui;
}
void FormList::setList(BaseItemList *value)
{
if (m_list == value)
return;
m_list = value;
updateInterface();
updateList();
}
void FormList::showEvent(QShowEvent *event)
{
FormBase::showEvent(event);
updateColumns();
updateList();
updateInterface();
}
void FormList::updateColumns()
{
if (!m_list) {
ui->listView1->setColumnCount(0);
return;
}
const ItemFields fields = m_list->getItemFields();
ui->listView1->setColumnCount(fields.count());
for (int i = 0; i < fields.count(); ++i)
ui->listView1->headerItem()->setText(i, fields.at(i).name);
}
void FormList::updateList()
{
ui->listView1->clear();
if (!m_list)
return;
const ItemFields fields = m_list->getItemFields();
for (int i = 0; i < m_list->count(); ++i) {
Item *item = m_list->at(i);
auto *row = new QTreeWidgetItem(ui->listView1);
for (int j = 0; j < fields.count(); ++j) {
const ItemField &field = fields.at(j);
if (field.dataType == DataType::Color)
row->setBackground(j, item->getValueColor(field.index));
else
row->setText(j, item->getValueAsText(field.index));
}
row->setData(0, Qt::UserRole, QVariant::fromValue(item));
}
}
void FormList::updateInterface()
{
const QList<QTreeWidgetItem *> selected = ui->listView1->selectedItems();
const int selCount = selected.count();
const bool atCapacity = maxItemCount != -1 && m_list && m_list->count() >= maxItemCount;
m_actionRemove->setEnabled(m_list && !selected.isEmpty() && m_list->count() - selCount >= minItemCount);
m_actionModify->setEnabled(m_list && !selected.isEmpty());
m_actionAdd->setEnabled(m_list && !atCapacity);
m_actionClone->setEnabled(m_list && !atCapacity);
}
void FormList::onAdd()
{
if (!m_list || (maxItemCount != -1 && m_list->count() >= maxItemCount))
return;
Item *tempItem = m_list->createItem(m_list->getNextAvailableName(m_list->name()));
FormItem dialog(this);
dialog.setItem(tempItem);
if (dialog.exec() == QDialog::Accepted) {
m_list->add(tempItem);
updateList();
updateInterface();
} else {
delete tempItem;
}
}
void FormList::onModify()
{
QTreeWidgetItem *selected = ui->listView1->currentItem();
if (!selected)
return;
auto *item = static_cast<Item *>(selected->data(0, Qt::UserRole).value<Item *>());
Item *tempItem = m_list->createItem();
tempItem->assign(*item);
FormItem dialog(this);
dialog.setItem(tempItem);
if (dialog.exec() == QDialog::Accepted) {
item->assign(*tempItem);
updateList();
updateInterface();
}
delete tempItem;
}
void FormList::onClone()
{
QTreeWidgetItem *selected = ui->listView1->currentItem();
if (!selected || (maxItemCount != -1 && m_list->count() >= maxItemCount))
return;
auto *sourceItem = static_cast<Item *>(selected->data(0, Qt::UserRole).value<Item *>());
Item *tempItem = m_list->createItem();
tempItem->assign(*sourceItem);
tempItem->name = m_list->getNextAvailableName(tempItem->name);
FormItem dialog(this);
dialog.setItem(tempItem);
if (dialog.exec() == QDialog::Accepted) {
m_list->add(tempItem);
updateList();
updateInterface();
} else {
delete tempItem;
}
}
void FormList::onRemove()
{
const QList<QTreeWidgetItem *> selected = ui->listView1->selectedItems();
if (selected.isEmpty())
return;
if (QMessageBox::question(this, tr("Remove items"), tr("Do you want to remove selected items?")) !=
QMessageBox::Yes)
return;
for (QTreeWidgetItem *row : selected) {
if (m_list->count() <= minItemCount)
break;
m_list->remove(row->data(0, Qt::UserRole).value<Item *>());
}
updateList();
updateInterface();
}
void FormList::onSelectAll()
{
ui->listView1->selectAll();
}
void FormList::onSelectionChanged()
{
updateInterface();
}
} // namespace Cevo