File indexing completed on 2024-04-21 04:32:10

0001 /*
0002  * Copyright (C) 2010-2015 by Stephen Allewell
0003  * steve.allewell@gmail.com
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2 of the License, or
0008  * (at your option) any later version.
0009  */
0010 
0011 #include "NewFlossDlg.h"
0012 
0013 #include <KConfigGroup>
0014 #include <KHelpClient>
0015 #include <KLocalizedString>
0016 #include <KMessageBox>
0017 #include <KSharedConfig>
0018 #include <kwidgetsaddons_version.h>
0019 
0020 #include "FlossScheme.h"
0021 #include "SchemeManager.h"
0022 
0023 NewFlossDlg::NewFlossDlg(QWidget *parent, FlossScheme *flossScheme)
0024     : QDialog(parent)
0025     , m_flossScheme(flossScheme)
0026     , m_floss(nullptr)
0027 {
0028     setWindowTitle(i18n("New Floss"));
0029     ui.setupUi(this);
0030 
0031     ui.SchemeName->setText(m_flossScheme->schemeName());
0032     ui.DialogButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0033 }
0034 
0035 Floss *NewFlossDlg::floss()
0036 {
0037     return m_floss;
0038 }
0039 
0040 void NewFlossDlg::hideEvent(QHideEvent *event)
0041 {
0042     KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).writeEntry(QStringLiteral("NewFlossDlg"), size());
0043 
0044     QDialog::hideEvent(event);
0045 }
0046 
0047 void NewFlossDlg::showEvent(QShowEvent *event)
0048 {
0049     QDialog::showEvent(event);
0050 
0051     if (KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).hasKey(QStringLiteral("NewFlossDlg"))) {
0052         resize(KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("DialogSizes")).readEntry(QStringLiteral("NewFlossDlg"), QSize()));
0053     }
0054 }
0055 
0056 void NewFlossDlg::on_FlossName_textEdited(const QString &text)
0057 {
0058     if (!text.isEmpty() && !ui.FlossDescription->text().isEmpty()) {
0059         ui.DialogButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
0060     } else {
0061         ui.DialogButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0062     }
0063 }
0064 
0065 void NewFlossDlg::on_FlossDescription_textEdited(const QString &text)
0066 {
0067     if (!text.isEmpty() && !ui.FlossName->text().isEmpty()) {
0068         ui.DialogButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
0069     } else {
0070         ui.DialogButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0071     }
0072 }
0073 
0074 void NewFlossDlg::on_DialogButtonBox_accepted()
0075 {
0076     if (!m_flossScheme->find(ui.FlossName->text()) ||
0077 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
0078         KMessageBox::questionTwoActions(this,
0079 #else
0080         KMessageBox::questionYesNo(this,
0081 #endif
0082                                         i18n("The floss name %1 is already used.\nOverwrite with the description and color selected.", ui.FlossName->text()),
0083                                         i18n("Overwrite"),
0084                                         KStandardGuiItem::overwrite(),
0085                                         KGuiItem(i18nc("@action:button", "Do Not Overwrite"), QStringLiteral("dialog-cancel")))
0086 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
0087             == KMessageBox::PrimaryAction) {
0088 #else
0089             == KMessageBox::Yes) {
0090 #endif
0091         m_floss = new Floss(ui.FlossName->text(), ui.FlossDescription->text(), ui.ColorButton->color());
0092         m_flossScheme->addFloss(m_floss);
0093         SchemeManager::writeScheme(m_flossScheme->schemeName());
0094     }
0095 
0096     accept();
0097 }
0098 
0099 void NewFlossDlg::on_DialogButtonBox_rejected()
0100 {
0101     reject();
0102 }
0103 
0104 void NewFlossDlg::on_DialogButtonBox_helpRequested()
0105 {
0106     KHelpClient::invokeHelp(QStringLiteral("NewFlossDialog"), QStringLiteral("kxstitch"));
0107 }
0108 
0109 #include "moc_NewFlossDlg.cpp"