4 // Copyright (C) 2008 Stephen Kelly <steveire@gmail.com>
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "kjotslinkdialog.h"
25 #include <QGridLayout>
26 #include <QRadioButton>
33 #include <Akonadi/Item>
35 #include "KJotsSettings.h"
36 #include "kjotsbookshelfentryvalidator.h"
37 #include <kdescendantsproxymodel.h>
38 #include <Akonadi/EntityTreeModel>
40 KJotsLinkDialog::KJotsLinkDialog( QAbstractItemModel
*kjotsModel
, QWidget
*parent
)
41 : KDialog(parent
), m_kjotsModel(kjotsModel
)
43 setCaption(i18n("Manage Link"));
44 setButtons(Ok
| Cancel
);
47 showButtonSeparator(true);
49 KDescendantsProxyModel
*proxyModel
= new KDescendantsProxyModel( this );
50 proxyModel
->setSourceModel( kjotsModel
);
51 proxyModel
->setAncestorSeparator( QLatin1String( " / " ) );
53 m_descendantsProxyModel
= proxyModel
;
55 QWidget
*entries
= new QWidget(this);
57 QGridLayout
*layout
= new QGridLayout(entries
);
59 textLabel
= new QLabel(i18n("Link Text:"), this);
60 textLineEdit
= new KLineEdit(this);
61 textLineEdit
->setClearButtonShown(true);
62 linkUrlLabel
= new QLabel(i18n("Link URL:"), this);
63 linkUrlLineEdit
= new KLineEdit(this);
64 hrefCombo
= new KComboBox(this);
65 linkUrlLineEdit
->setClearButtonShown(true);
67 tree
= new QTreeView();
68 tree
->setModel(proxyModel
);
70 tree
->setColumnHidden(1, true);
71 hrefCombo
->setModel(proxyModel
);
72 hrefCombo
->setView(tree
);
74 hrefCombo
->setEditable(true);
75 QCompleter
*completer
= new QCompleter(proxyModel
, this);
76 completer
->setCaseSensitivity(Qt::CaseInsensitive
);
77 hrefCombo
->setCompleter(completer
);
78 KJotsBookshelfEntryValidator
* validator
= new KJotsBookshelfEntryValidator( proxyModel
, this );
79 hrefCombo
->setValidator( validator
);
81 QGridLayout
* linkLayout
= new QGridLayout();
82 linkUrlLineEditRadioButton
= new QRadioButton(entries
);
83 hrefComboRadioButton
= new QRadioButton(entries
);
85 connect(linkUrlLineEditRadioButton
, SIGNAL(toggled(bool)),
86 linkUrlLineEdit
, SLOT(setEnabled(bool)));
87 connect(hrefComboRadioButton
, SIGNAL(toggled(bool)),
88 hrefCombo
, SLOT(setEnabled(bool)));
89 hrefCombo
->setEnabled(false);
90 linkUrlLineEditRadioButton
->setChecked(true);
92 linkLayout
->addWidget(linkUrlLineEditRadioButton
, 0, 0);
93 linkLayout
->addWidget(linkUrlLineEdit
, 0, 1);
94 linkLayout
->addWidget(hrefComboRadioButton
, 1, 0);
95 linkLayout
->addWidget(hrefCombo
, 1, 1);
97 layout
->addWidget(textLabel
, 0, 0);
98 layout
->addWidget(textLineEdit
, 0, 1);
99 layout
->addWidget(linkUrlLabel
, 1, 0);
100 layout
->addLayout( linkLayout
, 1, 1 );
102 setMainWidget(entries
);
104 textLineEdit
->setFocus();
106 connect( hrefCombo
, SIGNAL(editTextChanged(QString
)),
107 this, SLOT(trySetEntry(QString
)) );
110 void KJotsLinkDialog::setLinkText(const QString
&linkText
)
112 textLineEdit
->setText(linkText
);
113 if (!linkText
.trimmed().isEmpty())
114 linkUrlLineEdit
->setFocus();
117 void KJotsLinkDialog::setLinkUrl(const QString
&linkUrl
)
119 Akonadi::Item item
= Akonadi::Item::fromUrl(KUrl(linkUrl
));
120 Akonadi::Collection collection
= Akonadi::Collection::fromUrl(KUrl(linkUrl
));
122 if (!item
.isValid() && !collection
.isValid()) {
123 linkUrlLineEdit
->setText(linkUrl
);
124 linkUrlLineEditRadioButton
->setChecked(true);
130 if (collection
.isValid()) {
131 idx
= Akonadi::EntityTreeModel::modelIndexForCollection( m_descendantsProxyModel
, collection
);
132 } else if (item
.isValid()) {
133 const QModelIndexList list
= Akonadi::EntityTreeModel::modelIndexesForItem( m_descendantsProxyModel
, item
);
143 hrefComboRadioButton
->setChecked(true);
145 hrefCombo
->view()->setCurrentIndex( idx
);
146 hrefCombo
->setCurrentIndex( idx
.row() );
149 QString
KJotsLinkDialog::linkText() const
151 return textLineEdit
->text().trimmed();
154 void KJotsLinkDialog::trySetEntry(const QString
& text
)
157 int pos
= hrefCombo
->lineEdit()->cursorPosition();
158 if ( hrefCombo
->validator()->validate(t
, pos
) == KJotsBookshelfEntryValidator::Acceptable
)
160 int row
= hrefCombo
->findText( t
, Qt::MatchFixedString
);
161 QModelIndex index
= hrefCombo
->model()->index( row
, 0 );
162 hrefCombo
->view()->setCurrentIndex( index
);
163 hrefCombo
->setCurrentIndex( row
);
167 QString
KJotsLinkDialog::linkUrl() const
169 if (hrefComboRadioButton
->isChecked()){
170 const QModelIndex index
= hrefCombo
->view()->currentIndex();
171 const Akonadi::Collection collection
= index
.data(Akonadi::EntityTreeModel::CollectionRole
).value
<Akonadi::Collection
>();
172 if (collection
.isValid()) {
173 return QLatin1String("kjots://org.kjots.book/") + QString::number(collection
.id());
175 const Akonadi::Item item
= index
.data(Akonadi::EntityTreeModel::ItemRole
).value
<Akonadi::Item
>();
176 Q_ASSERT(item
.isValid());
177 return QLatin1String("kjots://org.kjots.page/") + QString::number(item
.id());
179 return linkUrlLineEdit
->text();