File indexing completed on 2024-05-12 16:40:55

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004-2012 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KexiNameDialog.h"
0021 #include "KexiNameWidget.h"
0022 #include <core/kexipartinfo.h>
0023 #include <kexi_global.h>
0024 
0025 #include <KDbConnection>
0026 
0027 #include <KIconLoader>
0028 #include <KMessageBox>
0029 
0030 #include <QDebug>
0031 #include <QGridLayout>
0032 #include <QLabel>
0033 #include <QDialogButtonBox>
0034 #include <QPushButton>
0035 #include <QVBoxLayout>
0036 #include <QLineEdit>
0037 
0038 KexiNameDialogValidator::KexiNameDialogValidator()
0039 {
0040 }
0041 
0042 KexiNameDialogValidator::~KexiNameDialogValidator()
0043 {
0044 }
0045 
0046 // --
0047 
0048 class Q_DECL_HIDDEN KexiNameDialog::Private
0049 {
0050 
0051 public:
0052     Private()
0053         : validator(0)
0054         , checkIfObjectExists(false)
0055         , allowOverwriting(false)
0056     {
0057     }
0058     ~Private() {
0059         delete validator;
0060     }
0061 
0062     QLabel *icon;
0063     KexiNameWidget* widget;
0064     const KexiProject *project;
0065     const KexiPart::Part *part;
0066     KexiNameDialogValidator *validator;
0067     QDialogButtonBox *buttonBox;
0068     bool checkIfObjectExists;
0069     bool allowOverwriting;
0070     bool overwriteNeeded;
0071 };
0072 
0073 KexiNameDialog::KexiNameDialog(const QString& message, QWidget * parent)
0074         : QDialog(parent)
0075         , d(new Private)
0076 {
0077     d->widget = new KexiNameWidget(message, this);
0078     init();
0079 }
0080 
0081 KexiNameDialog::KexiNameDialog(const QString& message,
0082                                const QString& nameLabel, const QString& nameText,
0083                                const QString& captionLabel, const QString& captionText,
0084                                QWidget * parent)
0085         : QDialog(parent)
0086         , d(new Private)
0087 {
0088     d->widget = new KexiNameWidget(message, nameLabel, nameText, captionLabel, captionText);
0089     init();
0090 }
0091 
0092 KexiNameDialog::~KexiNameDialog()
0093 {
0094     delete d;
0095 }
0096 
0097 void KexiNameDialog::init()
0098 {
0099     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0100     mainLayout->addWidget(d->widget);
0101 
0102     QGridLayout *lyr = new QGridLayout;
0103     mainLayout->addLayout(lyr);
0104     d->icon = new QLabel;
0105     d->icon->setAlignment(Qt::AlignTop | Qt::AlignLeft);
0106     QSizePolicy sp(QSizePolicy::Fixed, QSizePolicy::Preferred);
0107     sp.setHorizontalStretch(1);
0108     d->icon->setSizePolicy(sp);
0109     d->icon->setFixedWidth(50);
0110     lyr->addWidget(d->icon, 0, 0);
0111 
0112     sp = QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
0113     sp.setHorizontalStretch(1);
0114     d->widget->setSizePolicy(sp);
0115     lyr->addWidget(d->widget, 0, 1);
0116     lyr->addItem(new QSpacerItem(25, 10, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 2);
0117     lyr->addItem(new QSpacerItem(5, 10, QSizePolicy::Minimum, QSizePolicy::Expanding), 1, 1);
0118     connect(d->widget, SIGNAL(messageChanged()), this, SLOT(updateSize()));
0119 
0120     // buttons
0121     d->buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::Help);
0122     QPushButton *okButton = d->buttonBox->button(QDialogButtonBox::Ok);
0123     okButton->setDefault(true);
0124     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0125     okButton->setEnabled(true);
0126     connect(d->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0127     connect(d->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0128     mainLayout->addWidget(d->buttonBox);
0129 
0130     updateSize();
0131     slotTextChanged();
0132     connect(d->widget, SIGNAL(textChanged()), this, SLOT(slotTextChanged()));
0133 }
0134 
0135 void KexiNameDialog::updateSize()
0136 {
0137   resize(QSize(400, 140 + (!d->widget->messageLabel()->text().isEmpty() ?
0138                            d->widget->messageLabel()->height() : 0))
0139            .expandedTo(minimumSizeHint()));
0140 }
0141 
0142 void KexiNameDialog::slotTextChanged()
0143 {
0144     bool enable = true;
0145     if (   (d->widget->isNameRequired() && d->widget->nameText().isEmpty())
0146         || (d->widget->isCaptionRequired() && d->widget->captionText().isEmpty()) )
0147     {
0148         enable = false;
0149     }
0150     d->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enable);
0151 }
0152 
0153 bool KexiNameDialog::canOverwrite()
0154 {
0155     KDbObject tmpObject;
0156     tristate result = d->project->dbConnection()->loadObjectData(
0157                           d->project->typeIdForPluginId(d->part->info()->pluginId()),
0158                           widget()->nameText(), &tmpObject);
0159     if (result == cancelled) {
0160         return true;
0161     }
0162     if (result == false) {
0163         qWarning() << "Cannot load object data for" << widget()->nameText();
0164         return false;
0165     }
0166     if (widget()->originalNameText() == tmpObject.name()) {
0167         return true;
0168     }
0169     if (!d->allowOverwriting) {
0170         KMessageBox::information(this,
0171                                  "<p>" + d->part->i18nMessage("Object <resource>%1</resource> already exists.", 0)
0172                                              .subs(widget()->nameText()).toString()
0173                                  + "</p><p>" + xi18n("Please choose other name.") + "</p>");
0174         return false;
0175     }
0176 
0177     QString msg =
0178         "<p>" + d->part->i18nMessage("Object <resource>%1</resource> already exists.", 0)
0179                     .subs(widget()->nameText()).toString()
0180         + "</p><p>" + xi18n("Do you want to replace it?") + "</p>";
0181     KGuiItem yesItem(KStandardGuiItem::yes());
0182     yesItem.setText(xi18n("&Replace"));
0183     yesItem.setToolTip(xi18n("Replace object"));
0184     const KMessageBox::ButtonCode res = KMessageBox::warningYesNo(
0185                   this, msg, QString(),
0186                   yesItem, KGuiItem(xi18nc("@action:button", "&Choose Other Name...")),
0187                   QString(),
0188                   KMessageBox::Notify | KMessageBox::Dangerous);
0189     if (res == KMessageBox::Yes) {
0190         d->overwriteNeeded = true;
0191     }
0192     return res == KMessageBox::Yes;
0193 }
0194 
0195 void KexiNameDialog::accept()
0196 {
0197     if (d->validator) {
0198         if (!d->validator->validate(this)) {
0199             return;
0200         }
0201     }
0202     if (!d->widget->checkValidity())
0203         return;
0204 
0205     if (d->checkIfObjectExists && d->project) {
0206         if (!canOverwrite()) {
0207             return;
0208         }
0209     }
0210 
0211     QDialog::accept();
0212 }
0213 
0214 void KexiNameDialog::setDialogIcon(const QString &iconName)
0215 {
0216     d->icon->setPixmap(
0217         KIconLoader::global()->loadIcon(iconName, KIconLoader::Desktop, KIconLoader::SizeMedium));
0218 }
0219 
0220 void KexiNameDialog::showEvent(QShowEvent * event)
0221 {
0222     d->widget->captionLineEdit()->selectAll();
0223     d->widget->captionLineEdit()->setFocus();
0224     QDialog::showEvent(event);
0225 }
0226 
0227 KexiNameWidget* KexiNameDialog::widget() const
0228 {
0229     return d->widget;
0230 }
0231 
0232 int KexiNameDialog::execAndCheckIfObjectExists(const KexiProject &project,
0233                                                const KexiPart::Part &part,
0234                                                bool *overwriteNeeded)
0235 {
0236     d->project = &project;
0237     d->part = &part;
0238     d->checkIfObjectExists = true;
0239     if (overwriteNeeded) {
0240         *overwriteNeeded = false;
0241         d->overwriteNeeded = false;
0242     }
0243     int res = exec();
0244     d->project = 0;
0245     d->part = 0;
0246     d->checkIfObjectExists = false;
0247     if (overwriteNeeded) {
0248         *overwriteNeeded = d->overwriteNeeded;
0249     }
0250     return res;
0251 }
0252 
0253 void KexiNameDialog::setAllowOverwriting(bool set)
0254 {
0255     d->allowOverwriting = set;
0256 }
0257 
0258 void KexiNameDialog::setValidator(KexiNameDialogValidator *validator)
0259 {
0260     delete d->validator;
0261     d->validator = validator;
0262 }
0263 
0264 QDialogButtonBox* KexiNameDialog::buttonBox()
0265 {
0266     return d->buttonBox;
0267 }