Make it possible to use a distinct selection model in the foldertreewidget.
[kdepim.git] / kjots / kjotslinkdialog.cpp
blob687ad3ef4ae30eaaa8036dc228eff50e3e89a1f7
1 //
2 // kjots
3 //
4 // Copyright (C) 2008 Stephen Kelly <steveire@gmail.com>
5 //
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"
23 #include <QLabel>
24 #include <QCompleter>
25 #include <QGridLayout>
26 #include <QRadioButton>
27 #include <QTreeView>
29 #include <KLocale>
30 #include <KComboBox>
31 #include <KLineEdit>
33 #include "KJotsSettings.h"
34 #include "kjotsbookshelfentryvalidator.h"
35 // #include "kdescendantsproxymodel_p.h"
37 KJotsLinkDialog::KJotsLinkDialog(QWidget *parent) :
38 KDialog(parent)
40 setCaption(i18n("Manage Link"));
41 setButtons(Ok | Cancel);
42 setDefaultButton(Ok);
43 setModal(true);
44 showButtonSeparator(true);
46 // KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel( this );
47 // proxyModel->setSourceModel( );
48 // proxyModel->setAncestorSeparator( QLatin1String( " / " ) );
50 QWidget *entries = new QWidget(this);
52 QGridLayout *layout = new QGridLayout(entries);
54 textLabel = new QLabel(i18n("Link Text:"), this);
55 textLineEdit = new KLineEdit(this);
56 textLineEdit->setClearButtonShown(true);
57 linkUrlLabel = new QLabel(i18n("Link URL:"), this);
58 linkUrlLineEdit = new KLineEdit(this);
59 hrefCombo = new KComboBox(this);
60 linkUrlLineEdit->setClearButtonShown(true);
62 tree = new QTreeView();
63 // tree->setModel(proxyModel);
64 tree->expandAll();
65 tree->setColumnHidden(1, true);
66 // hrefCombo->setModel(proxyModel);
67 hrefCombo->setView(tree);
69 hrefCombo->setEditable(true);
70 // QCompleter *completer = new QCompleter(proxyModel, this);
71 // completer->setCaseSensitivity(Qt::CaseInsensitive);
72 // hrefCombo->setCompleter(completer);
73 // KJotsBookshelfEntryValidator* validator = new KJotsBookshelfEntryValidator( proxyModel, this );
74 // hrefCombo->setValidator( validator );
76 QGridLayout* linkLayout = new QGridLayout();
77 linkUrlLineEditRadioButton = new QRadioButton(entries);
78 hrefComboRadioButton = new QRadioButton(entries);
80 connect(linkUrlLineEditRadioButton, SIGNAL(toggled(bool)),
81 linkUrlLineEdit, SLOT(setEnabled(bool)));
82 connect(hrefComboRadioButton, SIGNAL(toggled(bool)),
83 hrefCombo, SLOT(setEnabled(bool)));
84 hrefCombo->setEnabled(false);
85 linkUrlLineEditRadioButton->setChecked(true);
87 linkLayout->addWidget(linkUrlLineEditRadioButton, 0, 0);
88 linkLayout->addWidget(linkUrlLineEdit, 0, 1);
89 linkLayout->addWidget(hrefComboRadioButton, 1, 0);
90 linkLayout->addWidget(hrefCombo, 1, 1);
92 layout->addWidget(textLabel, 0, 0);
93 layout->addWidget(textLineEdit, 0, 1);
94 layout->addWidget(linkUrlLabel, 1, 0);
95 layout->addLayout( linkLayout, 1, 1 );
97 setMainWidget(entries);
99 textLineEdit->setFocus();
101 connect( hrefCombo, SIGNAL( editTextChanged ( const QString & ) ),
102 this, SLOT( trySetEntry(const QString & ) ) );
105 void KJotsLinkDialog::setLinkText(const QString &linkText)
107 textLineEdit->setText(linkText);
108 if (!linkText.trimmed().isEmpty())
109 linkUrlLineEdit->setFocus();
112 void KJotsLinkDialog::setLinkUrl(const QString &linkUrl)
114 // TODO PORT
115 #if 0
116 if (KJotsEntry::isKJotsLink(linkUrl)) {
118 quint64 id = KJotsEntry::idFromLinkUrl(linkUrl);
119 KJotsEntry* item = mBookshelf->entryFromId(id);
120 if ( item ) {
122 QModelIndex index = hrefCombo->model()->index(0,1);
123 if ( hrefCombo->model()->data(index).toULongLong() == id )
125 hrefCombo->view()->setCurrentIndex(index);
126 hrefCombo->setCurrentIndex( index.row() );
127 } else {
128 while ( index.sibling(index.row() + 1, 1).isValid() )
130 index = index.sibling(index.row() + 1, 1);
132 if ( hrefCombo->model()->data(index).toULongLong() == id )
134 hrefCombo->view()->setCurrentIndex(index);
135 hrefCombo->setCurrentIndex( index.row() );
136 break;
141 hrefComboRadioButton->setChecked(true);
142 } else {
143 linkUrlLineEdit->setText(linkUrl);
144 linkUrlLineEditRadioButton->setChecked(true);
146 #endif
149 QString KJotsLinkDialog::linkText() const
151 return textLineEdit->text().trimmed();
154 void KJotsLinkDialog::trySetEntry(const QString & text)
156 QString t(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 return QString();
170 // TODO PORT
171 #if 0
172 if (hrefComboRadioButton->isChecked()){
173 QModelIndex index = hrefCombo->view()->currentIndex();
174 index = index.sibling(index.row(), 1);
175 quint64 id = hrefCombo->model()->data(index).toULongLong();
176 return KJotsEntry::kjotsLinkUrlFromId(id);
177 } else {
178 return linkUrlLineEdit->text();
180 #endif