25 #include <QGridLayout>
26 #include <QRadioButton>
33 #include <Akonadi/Item>
37 #include <kdescendantsproxymodel.h>
38 #include <Akonadi/EntityTreeModel>
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;
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);
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)),
112 textLineEdit->setText(linkText);
113 if (!linkText.trimmed().isEmpty())
114 linkUrlLineEdit->setFocus();
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() );
151 return textLineEdit->text().trimmed();
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 );
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();
183 #include "kjotslinkdialog.moc"
This class is a validator intended to be used with an editable QComboBox.
KJotsLinkDialog(QAbstractItemModel *kjotsModel, QWidget *parent=0)
void setLinkText(const QString &linkText)
Returns the link text shown in the dialog.
QString linkUrl() const
Returns the target link url entered by the user.
void setLinkUrl(const QString &linkUrl)
Sets the target link url shown in the dialog.
QString linkText() const
Returns the link text entered by the user.
void trySetEntry(const QString &text)