• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kjots

  • sources
  • kde-4.14
  • kdepim
  • kjots
kjotslinkdialog.cpp
Go to the documentation of this file.
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.
10 //
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.
15 //
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.
19 //
20 
21 #include "kjotslinkdialog.h"
22 
23 #include <QLabel>
24 #include <QCompleter>
25 #include <QGridLayout>
26 #include <QRadioButton>
27 #include <QTreeView>
28 
29 #include <KLocalizedString>
30 #include <KComboBox>
31 #include <KLineEdit>
32 
33 #include <Akonadi/Item>
34 
35 #include "KJotsSettings.h"
36 #include "kjotsbookshelfentryvalidator.h"
37 #include <kdescendantsproxymodel.h>
38 #include <Akonadi/EntityTreeModel>
39 
40 KJotsLinkDialog::KJotsLinkDialog( QAbstractItemModel *kjotsModel, QWidget *parent)
41  : KDialog(parent), m_kjotsModel(kjotsModel)
42 {
43  setCaption(i18n("Manage Link"));
44  setButtons(Ok | Cancel);
45  setDefaultButton(Ok);
46  setModal(true);
47  showButtonSeparator(true);
48 
49  KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel( this );
50  proxyModel->setSourceModel( kjotsModel );
51  proxyModel->setAncestorSeparator( QLatin1String( " / " ) );
52 
53  m_descendantsProxyModel = proxyModel;
54 
55  QWidget *entries = new QWidget(this);
56 
57  QGridLayout *layout = new QGridLayout(entries);
58 
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);
66 
67  tree = new QTreeView();
68  tree->setModel(proxyModel);
69  tree->expandAll();
70  tree->setColumnHidden(1, true);
71  hrefCombo->setModel(proxyModel);
72  hrefCombo->setView(tree);
73 
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 );
80 
81  QGridLayout* linkLayout = new QGridLayout();
82  linkUrlLineEditRadioButton = new QRadioButton(entries);
83  hrefComboRadioButton = new QRadioButton(entries);
84 
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);
91 
92  linkLayout->addWidget(linkUrlLineEditRadioButton, 0, 0);
93  linkLayout->addWidget(linkUrlLineEdit, 0, 1);
94  linkLayout->addWidget(hrefComboRadioButton, 1, 0);
95  linkLayout->addWidget(hrefCombo, 1, 1);
96 
97  layout->addWidget(textLabel, 0, 0);
98  layout->addWidget(textLineEdit, 0, 1);
99  layout->addWidget(linkUrlLabel, 1, 0);
100  layout->addLayout( linkLayout, 1, 1 );
101 
102  setMainWidget(entries);
103 
104  textLineEdit->setFocus();
105 
106  connect( hrefCombo, SIGNAL(editTextChanged(QString)),
107  this, SLOT(trySetEntry(QString)) );
108 }
109 
110 void KJotsLinkDialog::setLinkText(const QString &linkText)
111 {
112  textLineEdit->setText(linkText);
113  if (!linkText.trimmed().isEmpty())
114  linkUrlLineEdit->setFocus();
115 }
116 
117 void KJotsLinkDialog::setLinkUrl(const QString &linkUrl)
118 {
119  Akonadi::Item item = Akonadi::Item::fromUrl(KUrl(linkUrl));
120  Akonadi::Collection collection = Akonadi::Collection::fromUrl(KUrl(linkUrl));
121 
122  if (!item.isValid() && !collection.isValid()) {
123  linkUrlLineEdit->setText(linkUrl);
124  linkUrlLineEditRadioButton->setChecked(true);
125  return;
126  }
127 
128  QModelIndex idx;
129 
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 );
134  if (list.isEmpty())
135  return;
136 
137  idx = list.first();
138  }
139 
140  if (!idx.isValid())
141  return;
142 
143  hrefComboRadioButton->setChecked(true);
144 
145  hrefCombo->view()->setCurrentIndex( idx );
146  hrefCombo->setCurrentIndex( idx.row() );
147 }
148 
149 QString KJotsLinkDialog::linkText() const
150 {
151  return textLineEdit->text().trimmed();
152 }
153 
154 void KJotsLinkDialog::trySetEntry(const QString & text)
155 {
156  QString t(text);
157  int pos = hrefCombo->lineEdit()->cursorPosition();
158  if ( hrefCombo->validator()->validate(t, pos) == KJotsBookshelfEntryValidator::Acceptable )
159  {
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 );
164  }
165 }
166 
167 QString KJotsLinkDialog::linkUrl() const
168 {
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());
174  }
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());
178  } else {
179  return linkUrlLineEdit->text();
180  }
181 }
182 
KJotsBookshelfEntryValidator
This class is a validator intended to be used with an editable QComboBox.
Definition: kjotsbookshelfentryvalidator.h:36
QModelIndex
QCompleter::setCaseSensitivity
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
QWidget
QGridLayout::addWidget
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
KJotsLinkDialog::KJotsLinkDialog
KJotsLinkDialog(QAbstractItemModel *kjotsModel, QWidget *parent=0)
Definition: kjotslinkdialog.cpp:40
QVariant::value
T value() const
QGridLayout
KDialog
kjotslinkdialog.h
QModelIndex::isValid
bool isValid() const
KJotsLinkDialog::setLinkText
void setLinkText(const QString &linkText)
Returns the link text shown in the dialog.
Definition: kjotslinkdialog.cpp:110
QString::number
QString number(int n, int base)
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
QModelIndex::row
int row() const
QString
QTreeView::setColumnHidden
void setColumnHidden(int column, bool hide)
QTreeView::expandAll
void expandAll()
KJotsLinkDialog::linkUrl
QString linkUrl() const
Returns the target link url entered by the user.
Definition: kjotslinkdialog.cpp:167
QAbstractButton::setChecked
void setChecked(bool)
QGridLayout::addLayout
void addLayout(QLayout *layout, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
kjotsbookshelfentryvalidator.h
QModelIndex::data
QVariant data(int role) const
QLatin1String
QTreeView
QRadioButton
QTreeView::setModel
virtual void setModel(QAbstractItemModel *model)
KJotsLinkDialog::setLinkUrl
void setLinkUrl(const QString &linkUrl)
Sets the target link url shown in the dialog.
Definition: kjotslinkdialog.cpp:117
QAbstractItemModel
KJotsLinkDialog::linkText
QString linkText() const
Returns the link text entered by the user.
Definition: kjotslinkdialog.cpp:149
QCompleter
QLabel
KJotsLinkDialog::trySetEntry
void trySetEntry(const QString &text)
Definition: kjotslinkdialog.cpp:154
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:12 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjots

Skip menu "kjots"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal