File indexing completed on 2024-04-28 16:24:35

0001 /* This file is part of the KDE project
0002    Copyright (C) 20079 Dag Andersen <danders@get2net.dk>
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 // clazy:excludeall=qstring-arg
0021 #include "kptinsertfiledlg.h"
0022 #include "kptnode.h"
0023 #include "kptproject.h"
0024 
0025 #include <KLocalizedString>
0026 #include <KIO/StatJob>
0027 
0028 namespace KPlato
0029 {
0030 
0031 InsertFileDialog::InsertFileDialog(Project &project, Node *currentNode, QWidget *parent)
0032     : KoDialog(parent)
0033 {
0034     setCaption(i18n("Insert File"));
0035     setButtons(KoDialog::Ok | KoDialog::Cancel);
0036     setDefaultButton(Ok);
0037     showButtonSeparator(true);
0038     
0039     m_panel = new InsertFilePanel(project, currentNode, this);
0040 
0041     setMainWidget(m_panel);
0042     
0043     enableButtonOk(false);
0044 
0045     connect(m_panel, &InsertFilePanel::enableButtonOk, this, &KoDialog::enableButtonOk);
0046 }
0047 
0048 QUrl InsertFileDialog::url() const
0049 {
0050     return m_panel->url();
0051 }
0052 
0053 Node *InsertFileDialog::parentNode() const
0054 {
0055     return m_panel->parentNode();
0056 }
0057 
0058 Node *InsertFileDialog::afterNode() const
0059 {
0060     return m_panel->afterNode();
0061 }
0062 
0063 //------------------------
0064 InsertFilePanel::InsertFilePanel(Project &project, Node *currentNode, QWidget *parent)
0065     : QWidget(parent),
0066     m_project(project),
0067     m_node(currentNode)
0068 {
0069     ui.setupUi(this);
0070     
0071     if (currentNode == 0 || currentNode->type() == Node::Type_Project) {
0072         ui.ui_isAfter->setEnabled(false);
0073         ui.ui_isParent->setEnabled(false);
0074         ui.ui_useProject->setChecked(true);
0075 
0076         ui.ui_name->setText(project.name());
0077     } else {
0078         ui.ui_isAfter->setChecked(true);
0079 
0080         ui.ui_name->setText(currentNode->name());
0081     }
0082     connect(ui.ui_url, &KUrlRequester::textChanged, this, &InsertFilePanel::changed);
0083 
0084     connect(ui.ui_url, &KUrlRequester::openFileDialog, this, &InsertFilePanel::slotOpenFileDialog);
0085 }
0086 
0087 void InsertFilePanel::slotOpenFileDialog(KUrlRequester *)
0088 {
0089     ui.ui_url->setFilter("*.plan");
0090 }
0091 
0092 void InsertFilePanel::changed(const QString &text)
0093 {
0094     KIO::StatJob* statJob = KIO::stat(QUrl::fromUserInput(text));
0095     statJob->setSide(KIO::StatJob::SourceSide);
0096 
0097     const bool isUrlReadable = statJob->exec();
0098 
0099     emit enableButtonOk(isUrlReadable);
0100 }
0101 
0102 QUrl InsertFilePanel::url() const
0103 {
0104     return ui.ui_url->url();
0105 }
0106 
0107 Node *InsertFilePanel::parentNode() const
0108 {
0109     if (ui.ui_useProject->isChecked()) {
0110         return &(m_project);
0111     }
0112     if (ui.ui_isParent->isChecked()) {
0113         return m_node;
0114     }
0115     if (ui.ui_isAfter->isChecked()) {
0116         return m_node->parentNode();
0117     }
0118     return &(m_project);
0119 }
0120 
0121 Node *InsertFilePanel::afterNode() const
0122 {
0123     if (ui.ui_isAfter->isChecked()) {
0124         return m_node;
0125     }
0126     return 0;
0127 }
0128 
0129 
0130 }  //KPlato namespace