File indexing completed on 2024-05-19 05:19:21

0001 /*
0002     This file is a part of KJots.
0003 
0004     SPDX-FileCopyrightText: 2008 Stephen Kelly <steveire@gmail.com>
0005                   2020 Igor Poboiko <igor.poboiko@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "kjotslinkdialog.h"
0011 #include "ui_linkdialog.h"
0012 #include "kjotsmodel.h"
0013 
0014 #include <QListView>
0015 #include <QPushButton>
0016 #include <QCompleter>
0017 
0018 #include <KDescendantsProxyModel>
0019 
0020 
0021 KJotsLinkDialog::KJotsLinkDialog(QAbstractItemModel *kjotsModel, QWidget *parent)
0022     : QDialog(parent)
0023     , ui(new Ui::LinkDialog)
0024     , m_descendantsProxyModel(new KDescendantsProxyModel(this))
0025 {
0026     ui->setupUi(this);
0027 
0028     m_descendantsProxyModel->setSourceModel(kjotsModel);
0029     m_descendantsProxyModel->setAncestorSeparator(QStringLiteral(" / "));
0030     m_descendantsProxyModel->setDisplayAncestorData(true);
0031 
0032     ui->hrefCombo->lineEdit()->setPlaceholderText(i18n("Enter link URL, or another note or note book..."));
0033     ui->hrefCombo->setModel(m_descendantsProxyModel.get());
0034     // This is required because otherwise QComboBox will catch Enter, insert a new item and clear
0035     ui->hrefCombo->setInsertPolicy(QComboBox::NoInsert);
0036     ui->hrefCombo->setCurrentIndex(-1);
0037 
0038     auto completer = new QCompleter(m_descendantsProxyModel.get(), this);
0039     completer->setCaseSensitivity(Qt::CaseInsensitive);
0040     ui->hrefCombo->setCompleter(completer);
0041 
0042     connect(ui->hrefCombo, &QComboBox::editTextChanged, this, &KJotsLinkDialog::slotTextChanged);
0043     connect(ui->textEdit, &QLineEdit::textChanged, this, &KJotsLinkDialog::slotTextChanged);
0044     slotTextChanged();
0045 }
0046 
0047 KJotsLinkDialog::~KJotsLinkDialog() = default;
0048 
0049 void KJotsLinkDialog::slotTextChanged()
0050 {
0051     const bool ok = !ui->hrefCombo->currentText().trimmed().isEmpty() && !ui->textEdit->text().trimmed().isEmpty();
0052     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
0053 }
0054 
0055 void KJotsLinkDialog::setLinkText(const QString &linkText)
0056 {
0057     ui->textEdit->setText(linkText);
0058     if (!linkText.isEmpty()) {
0059         ui->textEdit->setFocus();
0060     }
0061 }
0062 
0063 void KJotsLinkDialog::setLinkUrl(const QString &linkUrl)
0064 {
0065     const QModelIndex idx = KJotsModel::modelIndexForUrl(m_descendantsProxyModel.get(), QUrl(linkUrl));
0066     if (idx.isValid()) {
0067         ui->hrefCombo->setCurrentIndex(idx.row());
0068     } else {
0069         ui->hrefCombo->setCurrentIndex(-1);
0070         ui->hrefCombo->setCurrentText(linkUrl);
0071     }
0072 }
0073 
0074 QString KJotsLinkDialog::linkText() const
0075 {
0076     return ui->textEdit->text().trimmed();
0077 }
0078 
0079 QString KJotsLinkDialog::linkUrl() const
0080 {
0081     const int row = ui->hrefCombo->currentIndex();
0082     if (row != -1) {
0083         return ui->hrefCombo->model()->index(row, 0).data(KJotsModel::EntityUrlRole).toString();
0084     } else {
0085         return ui->hrefCombo->currentText().trimmed();
0086     }
0087 }
0088 
0089 
0090 #include "moc_kjotslinkdialog.cpp"