| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/gui/formaddeditlabel.h" |
| 4 | |
| 5 | #include "gui/guiutilities.h" |
| 6 | #include "miscellaneous/application.h" |
| 7 | #include "miscellaneous/iconfactory.h" |
| 8 | #include "services/abstract/label.h" |
| 9 | |
| 10 | #include <QPushButton> |
| 11 | |
| 12 | FormAddEditLabel::FormAddEditLabel(QWidget* parent) : QDialog(parent), m_editableLabel(nullptr) { |
| 13 | m_ui.setupUi(this); |
| 14 | m_ui.m_txtName->lineEdit()->setPlaceholderText(tr("Name for your label")); |
| 15 | |
| 16 | connect(m_ui.m_txtName->lineEdit(), &QLineEdit::textChanged, this, [this](const QString& text) { |
| 17 | m_ui.m_buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled(!text.isEmpty()); |
| 18 | |
| 19 | if (text.isEmpty()) { |
| 20 | m_ui.m_txtName->setStatus(LineEditWithStatus::StatusType::Error, tr("Label's name cannot be empty.")); |
| 21 | } |
| 22 | else { |
| 23 | m_ui.m_txtName->setStatus(LineEditWithStatus::StatusType::Ok, tr("Perfect!")); |
| 24 | } |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | Label* FormAddEditLabel::execForAdd() { |
| 29 | GuiUtilities::applyDialogProperties(*this, qApp->icons()->fromTheme(QSL("tag-new")), tr("Create new label")); |
| 30 | |
| 31 | m_ui.m_btnColor->setRandomColor(); |
| 32 | m_ui.m_txtName->lineEdit()->setText(tr("Hot stuff")); |
| 33 | m_ui.m_txtName->setFocus(); |
| 34 | |
| 35 | auto exit_code = exec(); |
| 36 | |
| 37 | if (exit_code == QDialog::DialogCode::Accepted) { |
| 38 | return new Label(m_ui.m_txtName->lineEdit()->text(), m_ui.m_btnColor->color()); |
| 39 | } |
| 40 | else { |
| 41 | return nullptr; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | bool FormAddEditLabel::execForEdit(Label* lbl) { |
| 46 | GuiUtilities::applyDialogProperties(*this, |
| 47 | qApp->icons()->fromTheme(QSL("tag-properties")), |
| 48 | tr("Edit label '%1'").arg(lbl->title())); |
| 49 | |
| 50 | m_editableLabel = lbl; |
| 51 | m_ui.m_btnColor->setColor(lbl->color()); |
| 52 | m_ui.m_txtName->lineEdit()->setText(lbl->title()); |
| 53 | m_ui.m_txtName->setFocus(); |
| 54 | |
| 55 | auto exit_code = exec(); |
| 56 | |
| 57 | if (exit_code == QDialog::DialogCode::Accepted) { |
| 58 | m_editableLabel->setColor(m_ui.m_btnColor->color()); |
| 59 | m_editableLabel->setTitle(m_ui.m_txtName->lineEdit()->text()); |
| 60 | return true; |
| 61 | } |
| 62 | else { |
| 63 | return false; |
| 64 | } |
| 65 | } |