You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.4 KiB

#include "listeditordialog.h"
#include "ui_listeditordialog.h"
ListEditorDialog::ListEditorDialog(QWidget *parent, QString listStr, QChar separator) :
QDialog(parent),
ui(new Ui::ListEditorDialog)
{
ui->setupUi(this);
QStringList itemList = listStr.split(separator);
for (int i = 0;i < itemList.count();i++) {
QString currentStr = itemList[i].trimmed();
if (currentStr.length() != 0) {
ui->itemListWidget->addItem(currentStr);
}
}
origListStr = listStr;
listModified = false;
connect(ui->addItemButton, &QPushButton::clicked, this, &ListEditorDialog::onAddItemButtonClicked);
connect(ui->removeItemButton, &QPushButton::clicked, this, &ListEditorDialog::onRemoveItemButtonClicked);
connect(ui->moveUpItemButton, &QPushButton::clicked, this, &ListEditorDialog::onMoveUpItemButtonClicked);
connect(ui->moveDownItemButton, &QPushButton::clicked, this, &ListEditorDialog::onMoveDownItemButtonClicked);
connect(ui->okButton, &QPushButton::clicked, this, &ListEditorDialog::onOkButtonClicked);
connect(ui->cancelButton, &QPushButton::clicked, this, &ListEditorDialog::onCancelButtonClicked);
}
ListEditorDialog::~ListEditorDialog()
{
delete ui;
}
QString ListEditorDialog::list()
{
if (!listModified) {
return origListStr;
} else {
QStringList final;
for (int i = 0;i < ui->itemListWidget->count();i++) {
final.append(ui->itemListWidget->item(i)->text());
}
return final.join(',');
}
}
void ListEditorDialog::onAddItemButtonClicked()
{
if (ui->newItemLineEdit->text().trimmed() != "") {
ui->itemListWidget->addItem(ui->newItemLineEdit->text().trimmed());
}
ui->newItemLineEdit->setText("");
}
void ListEditorDialog::onRemoveItemButtonClicked()
{
if (ui->itemListWidget->count() != 0) {
delete ui->itemListWidget->item(ui->itemListWidget->currentRow());
}
}
void ListEditorDialog::onMoveUpItemButtonClicked()
{
bool isBottomItemSelected = ui->itemListWidget->currentRow() == ui->itemListWidget->count() - 1;
if (ui->itemListWidget->count() != 0 && ui->itemListWidget->currentRow() != 0) {
ui->itemListWidget->insertItem(ui->itemListWidget->currentRow() - 1, ui->itemListWidget->takeItem(ui->itemListWidget->currentRow()));
// Somehow the actively selected item ends up shifting down a row during the removal and reinsertion of the current item.
// If the actively selected item is the bottommost one, this has no effect, and so we only move up one row.
// Otherwise, we need to move up two rows to compensate.
if (isBottomItemSelected) {
ui->itemListWidget->setCurrentRow(ui->itemListWidget->currentRow() - 1);
} else {
ui->itemListWidget->setCurrentRow(ui->itemListWidget->currentRow() - 2);
}
}
}
void ListEditorDialog::onMoveDownItemButtonClicked()
{
if (ui->itemListWidget->count() != 0 && ui->itemListWidget->currentRow() != ui->itemListWidget->count() - 1)
{
ui->itemListWidget->insertItem(ui->itemListWidget->currentRow() + 1, ui->itemListWidget->takeItem(ui->itemListWidget->currentRow()));
ui->itemListWidget->setCurrentRow(ui->itemListWidget->currentRow() + 1);
}
}
void ListEditorDialog::onOkButtonClicked()
{
listModified = true;
this->close();
}
void ListEditorDialog::onCancelButtonClicked()
{
listModified = false;
this->close();
}