KBookmarks

kbookmarkdialog.cpp
1 // -*- c-basic-offset:4; indent-tabs-mode:nil -*-
2 /*
3  This file is part of the KDE libraries
4  SPDX-FileCopyrightText: 2007 Daniel Teske <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-only
7 */
8 
9 #include "kbookmarkdialog.h"
10 #include "kbookmarkdialog_p.h"
11 #include "kbookmarkmanager.h"
12 #include "kbookmarkmenu.h"
13 #include "kbookmarkmenu_p.h"
14 
15 #include <QDialogButtonBox>
16 #include <QFormLayout>
17 #include <QInputDialog>
18 #include <QLabel>
19 #include <QLineEdit>
20 #include <QPushButton>
21 #include <QTreeWidget>
22 #include <QHeaderView>
23 
24 #include <QIcon>
25 #include <KGuiItem>
26 
27 KBookmarkDialogPrivate::KBookmarkDialogPrivate(KBookmarkDialog *q)
28  : q(q)
29  , folderTree(nullptr)
30  , layout(false)
31 {
32 }
33 
34 KBookmarkDialogPrivate::~KBookmarkDialogPrivate()
35 {
36 }
37 
38 void KBookmarkDialogPrivate::initLayout()
39 {
40  QBoxLayout *vbox = new QVBoxLayout;
41 
42  QFormLayout *form = new QFormLayout();
43  vbox->addLayout(form);
44 
45  form->addRow(titleLabel, title);
46  form->addRow(urlLabel, url);
47  form->addRow(commentLabel, comment);
48 
49  vbox->addWidget(folderTree);
50  vbox->addWidget(buttonBox);
51 
52  q->setLayout(vbox);
53 }
54 
55 void KBookmarkDialogPrivate::initLayoutPrivate()
56 {
57  title = new QLineEdit(q);
58  title->setMinimumWidth(300);
59  titleLabel = new QLabel(KBookmarkDialog::tr("Name:", "@label:textbox"), q);
60  titleLabel->setBuddy(title);
61 
62  url = new QLineEdit(q);
63  url->setMinimumWidth(300);
64  urlLabel = new QLabel(KBookmarkDialog::tr("Location:", "@label:textbox"), q);
65  urlLabel->setBuddy(url);
66 
67  comment = new QLineEdit(q);
68  comment->setMinimumWidth(300);
69  commentLabel = new QLabel(KBookmarkDialog::tr("Comment:", "@label:textbox"), q);
70  commentLabel->setBuddy(comment);
71 
72  folderTree = new QTreeWidget(q);
73  folderTree->setColumnCount(1);
74  folderTree->header()->hide();
75  folderTree->setSortingEnabled(false);
76  folderTree->setSelectionMode(QTreeWidget::SingleSelection);
77  folderTree->setSelectionBehavior(QTreeWidget::SelectRows);
78  folderTree->setMinimumSize(60, 100);
79  QTreeWidgetItem *root = new KBookmarkTreeItem(folderTree);
80  fillGroup(root, mgr->root());
81 
82  buttonBox = new QDialogButtonBox(q);
83  buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
84  q->connect(buttonBox, &QDialogButtonBox::accepted, q, &KBookmarkDialog::accept);
85  q->connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
86 
87  initLayout();
88  layout = true;
89 }
90 
91 void KBookmarkDialogPrivate::fillGroup(QTreeWidgetItem *parentItem, const KBookmarkGroup &group, const KBookmarkGroup &selectGroup)
92 {
93  for (KBookmark bk = group.first(); !bk.isNull(); bk = group.next(bk)) {
94  if (bk.isGroup()) {
95  const KBookmarkGroup bkGroup = bk.toGroup();
96  QTreeWidgetItem* item = new KBookmarkTreeItem(parentItem, folderTree, bkGroup);
97  if (selectGroup == bkGroup) {
98  folderTree->setCurrentItem(item);
99  }
100  fillGroup(item, bkGroup, selectGroup);
101  }
102  }
103 }
104 
105 void KBookmarkDialogPrivate::setParentBookmark(const KBookmark &bm)
106 {
107  QString address = bm.address();
108  KBookmarkTreeItem *item = static_cast<KBookmarkTreeItem *>(folderTree->topLevelItem(0));
109  while (true) {
110  if (item->address() == bm.address()) {
111  folderTree->setCurrentItem(item);
112  return;
113  }
114  for (int i = 0; i < item->childCount(); ++i) {
115  KBookmarkTreeItem *child = static_cast<KBookmarkTreeItem *>(item->child(i));
116  if (KBookmark::commonParent(child->address(), address) == child->address()) {
117  item = child;
118  break;
119  }
120  }
121  }
122 }
123 
124 KBookmarkGroup KBookmarkDialogPrivate::parentBookmark()
125 {
126  KBookmarkTreeItem *item = dynamic_cast<KBookmarkTreeItem *>(folderTree->currentItem());
127  if (!item) {
128  return mgr->root();
129  }
130  const QString &address = item->address();
131  return mgr->findByAddress(address).toGroup();
132 }
133 
134 void KBookmarkDialog::accept()
135 {
136  if (d->mode == KBookmarkDialogPrivate::NewFolder) {
137  KBookmarkGroup parent = d->parentBookmark();
138  if (d->title->text().isEmpty()) {
139  d->title->setText(QStringLiteral("New Folder"));
140  }
141  d->bm = parent.createNewFolder(d->title->text());
142  d->bm.setDescription(d->comment->text());
143  d->mgr->emitChanged(parent);
144  } else if (d->mode == KBookmarkDialogPrivate::NewBookmark) {
145  KBookmarkGroup parent = d->parentBookmark();
146  if (d->title->text().isEmpty()) {
147  d->title->setText(QStringLiteral("New Bookmark"));
148  }
149  d->bm = parent.addBookmark(d->title->text(), QUrl(d->url->text()), d->icon);
150  d->bm.setDescription(d->comment->text());
151  d->mgr->emitChanged(parent);
152  } else if (d->mode == KBookmarkDialogPrivate::NewMultipleBookmarks) {
153  KBookmarkGroup parent = d->parentBookmark();
154  if (d->title->text().isEmpty()) {
155  d->title->setText(QStringLiteral("New Folder"));
156  }
157  d->bm = parent.createNewFolder(d->title->text());
158  d->bm.setDescription(d->comment->text());
159  for (const KBookmarkOwner::FutureBookmark &fb : qAsConst(d->list)) {
160  d->bm.toGroup().addBookmark(fb.title(), fb.url(), fb.icon());
161  }
162  d->mgr->emitChanged(parent);
163  } else if (d->mode == KBookmarkDialogPrivate::EditBookmark) {
164  d->bm.setFullText(d->title->text());
165  d->bm.setUrl(QUrl(d->url->text()));
166  d->bm.setDescription(d->comment->text());
167  d->mgr->emitChanged(d->bm.parentGroup());
168  } else if (d->mode == d->SelectFolder) {
169  d->bm = d->parentBookmark();
170  }
171  QDialog::accept();
172 }
173 
174 KBookmark KBookmarkDialog::editBookmark(const KBookmark &bm)
175 {
176  if (!d->layout) {
177  d->initLayoutPrivate();
178  }
179 
180  KGuiItem::assign(d->buttonBox->button(QDialogButtonBox::Ok), KGuiItem(tr("Update", "@action:button")));
181  setWindowTitle(tr("Bookmark Properties", "@title:window"));
182  d->url->setVisible(!bm.isGroup());
183  d->urlLabel->setVisible(!bm.isGroup());
184  d->bm = bm;
185  d->title->setText(bm.fullText());
186  d->url->setText(bm.url().toString());
187  d->comment->setVisible(true);
188  d->commentLabel->setVisible(true);
189  d->comment->setText(bm.description());
190  d->folderTree->setVisible(false);
191 
192  d->mode = KBookmarkDialogPrivate::EditBookmark;
193 
194  if (exec() == QDialog::Accepted) {
195  return d->bm;
196  } else {
197  return KBookmark();
198  }
199 
200 }
201 
202 KBookmark KBookmarkDialog::addBookmark(const QString &title, const QUrl &url, const QString &icon, KBookmark parent)
203 {
204  if (!d->layout) {
205  d->initLayoutPrivate();
206  }
207  if (parent.isNull()) {
208  parent = d->mgr->root();
209  }
210 
211  QPushButton *newButton = new QPushButton;
212  KGuiItem::assign(newButton, KGuiItem(tr("&New Folder...", "@action:button"), QStringLiteral("folder-new")));
213  d->buttonBox->addButton(newButton, QDialogButtonBox::ActionRole);
214  connect(newButton, &QAbstractButton::clicked, this, &KBookmarkDialog::newFolderButton);
215 
216  KGuiItem::assign(d->buttonBox->button(QDialogButtonBox::Ok), KGuiItem(tr("Add", "@action:button"), QStringLiteral("bookmark-new")));
217  setWindowTitle(tr("Add Bookmark", "@title:window"));
218  d->url->setVisible(true);
219  d->urlLabel->setVisible(true);
220  d->title->setText(title);
221  d->url->setText(url.toString());
222  d->comment->setText(QString());
223  d->comment->setVisible(true);
224  d->commentLabel->setVisible(true);
225  d->setParentBookmark(parent);
226  d->folderTree->setVisible(true);
227  d->icon = icon;
228 
229  d->mode = KBookmarkDialogPrivate::NewBookmark;
230 
231  if (exec() == QDialog::Accepted) {
232  return d->bm;
233  } else {
234  return KBookmark();
235  }
236 }
237 
239 {
240  if (!d->layout) {
241  d->initLayoutPrivate();
242  }
243  if (parent.isNull()) {
244  parent = d->mgr->root();
245  }
246 
247  d->list = list;
248 
249  QPushButton *newButton = new QPushButton;
250  KGuiItem::assign(newButton, KGuiItem(tr("&New Folder...", "@action:button"), QStringLiteral("folder-new")));
251  d->buttonBox->addButton(newButton, QDialogButtonBox::ActionRole);
252  connect(newButton, &QAbstractButton::clicked, this, &KBookmarkDialog::newFolderButton);
253 
254  KGuiItem::assign(d->buttonBox->button(QDialogButtonBox::Ok), KGuiItem(tr("Add", "@action:button"), QStringLiteral("bookmark-new")));
255  setWindowTitle(tr("Add Bookmarks", "@title:window"));
256  d->url->setVisible(false);
257  d->urlLabel->setVisible(false);
258  d->title->setText(name);
259  d->comment->setVisible(true);
260  d->commentLabel->setVisible(true);
261  d->comment->setText(QString());
262  d->setParentBookmark(parent);
263  d->folderTree->setVisible(true);
264 
265  d->mode = KBookmarkDialogPrivate::NewMultipleBookmarks;
266 
267  if (exec() == QDialog::Accepted) {
268  return d->bm.toGroup();
269  } else {
270  return KBookmarkGroup();
271  }
272 }
273 
275 {
276  if (!d->layout) {
277  d->initLayoutPrivate();
278  }
279  if (parent.isNull()) {
280  parent = d->mgr->root();
281  }
282 
283  QPushButton *newButton = new QPushButton;
284  KGuiItem::assign(newButton, KGuiItem(tr("&New Folder...", "@action:button"), QStringLiteral("folder-new")));
285  d->buttonBox->addButton(newButton, QDialogButtonBox::ActionRole);
286  connect(newButton, &QAbstractButton::clicked, this, &KBookmarkDialog::newFolderButton);
287 
288  setWindowTitle(tr("Select Folder", "@title:window"));
289  d->url->setVisible(false);
290  d->urlLabel->setVisible(false);
291  d->title->setVisible(false);
292  d->titleLabel->setVisible(false);
293  d->comment->setVisible(false);
294  d->commentLabel->setVisible(false);
295  d->setParentBookmark(parent);
296  d->folderTree->setVisible(true);
297 
298  d->mode = d->SelectFolder;
299 
300  if (exec() == QDialog::Accepted) {
301  return d->bm.toGroup();
302  } else {
303  return KBookmarkGroup();
304  }
305 }
306 
308 {
309  if (!d->layout) {
310  d->initLayoutPrivate();
311  }
312  if (parent.isNull()) {
313  parent = d->mgr->root();
314  }
315 
316  setWindowTitle(tr("New Folder", "@title:window"));
317  d->url->setVisible(false);
318  d->urlLabel->setVisible(false);
319  d->comment->setVisible(true);
320  d->commentLabel->setVisible(true);
321  d->comment->setText(QString());
322  d->title->setText(name);
323  d->setParentBookmark(parent);
324  d->folderTree->setVisible(true);
325 
326  d->mode = KBookmarkDialogPrivate::NewFolder;
327 
328  if (exec() == QDialog::Accepted) {
329  return d->bm.toGroup();
330  } else {
331  return KBookmarkGroup();
332  }
333 }
334 
336  : QDialog(parent)
337  , d(new KBookmarkDialogPrivate(this))
338 {
339  d->mgr = mgr;
340 }
341 
342 KBookmarkDialog::~KBookmarkDialog()
343 {
344  delete d;
345 }
346 
347 void KBookmarkDialog::newFolderButton()
348 {
349 
350  QString caption = d->parentBookmark().fullText().isEmpty() ?
351  tr("Create New Bookmark Folder", "@title:window") :
352  tr("Create New Bookmark Folder in %1", "@title:window").arg(d->parentBookmark().text());
353  bool ok;
354  QString text = QInputDialog::getText(this, caption, tr("New folder:", "@label:textbox"),
355  QLineEdit::Normal, QString(), &ok);
356  if (!ok) {
357  return;
358  }
359 
360  KBookmarkGroup group = d->parentBookmark().createNewFolder(text);
361  if (!group.isNull()) {
362  KBookmarkGroup parentGroup = group.parentGroup();
363  d->mgr->emitChanged(parentGroup);
364  d->folderTree->clear();
365  QTreeWidgetItem *root = new KBookmarkTreeItem(d->folderTree);
366  d->fillGroup(root, d->mgr->root(), group);
367  }
368 }
369 
370 /********************************************************************/
371 
372 KBookmarkTreeItem::KBookmarkTreeItem(QTreeWidget *tree)
373  : QTreeWidgetItem(tree), m_address(QLatin1String(""))
374 {
375  setText(0, KBookmarkDialog::tr("Bookmarks", "name of the container of all browser bookmarks"));
376  setIcon(0, QIcon::fromTheme(QStringLiteral("bookmarks")));
377  tree->expandItem(this);
378  tree->setCurrentItem(this);
379  setSelected(true);
380 }
381 
382 KBookmarkTreeItem::KBookmarkTreeItem(QTreeWidgetItem *parent, QTreeWidget *tree, const KBookmarkGroup &bk)
383  : QTreeWidgetItem(parent)
384 {
385  setIcon(0, QIcon::fromTheme(bk.icon()));
386  setText(0, bk.fullText());
387  tree->expandItem(this);
388  m_address = bk.address();
389 }
390 
391 KBookmarkTreeItem::~KBookmarkTreeItem()
392 {
393 }
394 
395 QString KBookmarkTreeItem::address()
396 {
397  return m_address;
398 }
KBookmark addBookmark(const QString &title, const QUrl &url, const QString &icon, KBookmark parent=KBookmark())
Shows a "Add Bookmark" dialog Note: this updates the bookmark and calls KBookmarkManager::emitChanged...
void expandItem(const QTreeWidgetItem *item)
virtual void reject()
KBookmarkGroup createNewFolder(const QString &name, KBookmark parent=KBookmark())
Shows a dialog to create a new folder.
QIcon icon() const const
This class implements the reading/writing of bookmarks in XML.
QString toString(QUrl::FormattingOptions options) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
PostalAddress address(const QVariant &location)
KBookmark addBookmark(const KBookmark &bm)
Create a new bookmark, as the last child of this group Don&#39;t forget to use KBookmarkManager::self()->...
Definition: kbookmark.cpp:201
void clicked(bool checked)
KBookmark editBookmark(const KBookmark &bm)
Shows a properties dialog Note: this updates the bookmark and calls KBookmarkManager::emitChanged.
A group of bookmarks.
Definition: kbookmark.h:316
void addRow(QWidget *label, QWidget *field)
KBookmarkGroup selectFolder(KBookmark start=KBookmark())
Shows a dialog to select a folder.
virtual void accept()
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
void setCurrentItem(QTreeWidgetItem *item)
KBookmarkDialog(KBookmarkManager *manager, QWidget *parent=nullptr)
Creates a KBookmarkDialog instance.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Represents the data for a bookmark that will be added.
KBookmarkGroup addBookmarks(const QList< KBookmarkOwner::FutureBookmark > &list, const QString &name=QString(), KBookmarkGroup parent=KBookmarkGroup())
Creates a folder from a list of bookmarks Note: this updates the bookmark and calls KBookmarkManager:...
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
KBookmark next(const KBookmark &current) const
Return the next sibling of a child bookmark of this group.
Definition: kbookmark.cpp:116
KBookmark first() const
Return the first child bookmark of this group.
Definition: kbookmark.cpp:106
This class provides a Dialog for editing properties, adding Bookmarks and creating new folders...
KBookmarkGroup createNewFolder(const QString &text)
Create a new bookmark folder, as the last child of this group.
Definition: kbookmark.cpp:149
void addLayout(QLayout *layout, int stretch)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Aug 8 2020 22:38:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.