File indexing completed on 2024-05-12 05:09:49

0001 /***************************************************************************
0002     Copyright (C) 2005-2021 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "urlfieldwidget.h"
0026 #include "../field.h"
0027 #include "../document.h"
0028 
0029 #include <KLineEdit>
0030 #include <KUrlRequester>
0031 #include <KUrlLabel>
0032 #include <kwidgetsaddons_version.h>
0033 
0034 #include <QUrl>
0035 #include <QDesktopServices>
0036 
0037 using Tellico::GUI::URLFieldWidget;
0038 
0039 // subclass of KUrlCompletion is needed so the KUrlLabel
0040 // can open relative links. I don't want to have to have to update
0041 // the base directory of the completion every time a new document is opened
0042 QString URLFieldWidget::URLCompletion::makeCompletion(const QString& text_) {
0043   // KUrlCompletion::makeCompletion() uses an internal variable instead
0044   // of calling KUrlCompletion::dir() so need to set the base dir before completing
0045   setDir(Data::Document::self()->URL().adjusted(QUrl::PreferLocalFile | QUrl::RemoveFilename));
0046   return KUrlCompletion::makeCompletion(text_);
0047 }
0048 
0049 URLFieldWidget::URLFieldWidget(Tellico::Data::FieldPtr field_, QWidget* parent_)
0050     : FieldWidget(field_, parent_) {
0051 
0052   // the label is a KUrlLabel
0053   KUrlLabel* urlLabel = dynamic_cast<KUrlLabel*>(label());
0054   Q_ASSERT(urlLabel);
0055 
0056   m_requester = new KUrlRequester(this);
0057   m_requester->lineEdit()->setCompletionObject(new URLCompletion());
0058   m_requester->lineEdit()->setAutoDeleteCompletionObject(true);
0059   connect(m_requester, &KUrlRequester::textChanged, this, &URLFieldWidget::checkModified);
0060   connect(m_requester, &KUrlRequester::textChanged, urlLabel, &KUrlLabel::setUrl);
0061 #if KWIDGETSADDONS_VERSION < QT_VERSION_CHECK(5, 65, 0)
0062   void (KUrlLabel::* clickedSignal)(const QString&) = &KUrlLabel::leftClickedUrl;
0063 #else
0064   void (KUrlLabel::* clickedSignal)(void) = &KUrlLabel::leftClickedUrl;
0065 #endif
0066   connect(urlLabel, clickedSignal, this, &URLFieldWidget::slotOpenURL);
0067   registerWidget();
0068 
0069   // special case, remember if it's a relative url
0070   m_logic.setRelative(field_->property(QStringLiteral("relative")) == QLatin1String("true"));
0071 }
0072 
0073 URLFieldWidget::~URLFieldWidget() {
0074 }
0075 
0076 QString URLFieldWidget::text() const {
0077   // for comparison purposes and to be consistent with the file listing importer
0078   // I want the full url here, including the protocol
0079   m_logic.setBaseUrl(Data::Document::self()->URL());
0080   return m_logic.urlText(m_requester->url());
0081 }
0082 
0083 void URLFieldWidget::setTextImpl(const QString& text_) {
0084   if(text_.isEmpty()) {
0085     m_requester->clear();
0086     return;
0087   }
0088 
0089   QUrl url = m_logic.isRelative() ?
0090              Data::Document::self()->URL().resolved(QUrl(text_)) :
0091              QUrl::fromUserInput(text_);
0092   m_requester->setUrl(url);
0093   static_cast<KUrlLabel*>(label())->setUrl(url.url());
0094 }
0095 
0096 void URLFieldWidget::clearImpl() {
0097   m_requester->clear();
0098   editMultiple(false);
0099 }
0100 
0101 void URLFieldWidget::updateFieldHook(Tellico::Data::FieldPtr, Tellico::Data::FieldPtr newField_) {
0102   m_logic.setRelative(newField_->property(QStringLiteral("relative")) == QLatin1String("true"));
0103 }
0104 
0105 void URLFieldWidget::slotOpenURL() {
0106   const QString url = static_cast<KUrlLabel*>(label())->url();
0107   if(url.isEmpty()) {
0108     return;
0109   }
0110   QDesktopServices::openUrl(m_logic.isRelative() ?
0111                             Data::Document::self()->URL().resolved(QUrl(url)) :
0112                             QUrl::fromUserInput(url));
0113 }
0114 
0115 QWidget* URLFieldWidget::widget() {
0116   return m_requester;
0117 }