parent
0a2a90811b
commit
e8ffda5a29
@ -0,0 +1,92 @@
|
||||
#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++) {
|
||||
ui->itemListWidget->addItem(itemList[i]);
|
||||
}
|
||||
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();
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
#ifndef LISTEDITORDIALOG_H
|
||||
#define LISTEDITORDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QString>
|
||||
#include <QChar>
|
||||
#include <QList>
|
||||
#include <QStringList>
|
||||
|
||||
namespace Ui {
|
||||
class ListEditorDialog;
|
||||
}
|
||||
|
||||
class ListEditorDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ListEditorDialog(QWidget *parent = nullptr, QString listStr = "", QChar separator = '\0');
|
||||
~ListEditorDialog();
|
||||
QString list();
|
||||
|
||||
private slots:
|
||||
void onAddItemButtonClicked();
|
||||
void onRemoveItemButtonClicked();
|
||||
void onMoveUpItemButtonClicked();
|
||||
void onMoveDownItemButtonClicked();
|
||||
void onOkButtonClicked();
|
||||
void onCancelButtonClicked();
|
||||
|
||||
private:
|
||||
Ui::ListEditorDialog *ui;
|
||||
QString origListStr;
|
||||
bool listModified;
|
||||
};
|
||||
|
||||
#endif // LISTEDITORDIALOG_H
|
@ -0,0 +1,107 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>ListEditorDialog</class>
|
||||
<widget class="QDialog" name="ListEditorDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>430</width>
|
||||
<height>371</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Dialog</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QLineEdit" name="newItemLineEdit"/>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QListWidget" name="itemListWidget"/>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_3">
|
||||
<item>
|
||||
<widget class="QPushButton" name="addItemButton">
|
||||
<property name="text">
|
||||
<string>Add</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="removeItemButton">
|
||||
<property name="text">
|
||||
<string>Remove</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="moveUpItemButton">
|
||||
<property name="text">
|
||||
<string>Move Up</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="moveDownItemButton">
|
||||
<property name="text">
|
||||
<string>Move Down</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="okButton">
|
||||
<property name="text">
|
||||
<string>OK</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="cancelButton">
|
||||
<property name="text">
|
||||
<string>Cancel</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
Loading…
Reference in new issue