#include "BookmarksDialog.h"
#include "ui_BookmarksDialog.h"
#include "databases/BookmarksDatabase.h"
+#include "dialogs/AddBookmarkDialog.h"
+#include "dialogs/EditBookmarkDialog.h"
// KDE Frameworks headers.
#include <KLocalizedString>
// Qt toolkit headers.
#include <QDebug>
#include <QStandardItemModel>
-#include <QTreeView>
// Construct the class.
-BookmarksDialog::BookmarksDialog() : QDialog(nullptr)
+BookmarksDialog::BookmarksDialog(QIcon currentWebsiteFavorieIcon) : QDialog(nullptr), websiteFavoriteIcon(currentWebsiteFavorieIcon)
{
// Set the dialog window title.
setWindowTitle(i18nc("The bookmarks dialog window title", "Bookmarks"));
// Setup the UI.
bookmarksDialogUi.setupUi(this);
- // Get the list of bookmarks.
- std::list<BookmarkStruct> *bookmarksListPointer = BookmarksDatabase::getBookmarks();
-
- // Get a handle for the tree view.
- QTreeView *treeViewPointer = bookmarksDialogUi.treeView;
+ // Get a handle for the draggable tree view.
+ draggableTreeViewPointer = bookmarksDialogUi.draggableTreeView;
// Initialize the tree model.
- QStandardItemModel *treeModelPointer = new QStandardItemModel();
+ treeModelPointer = new QStandardItemModel();
+
+ // Auto resize the headers.
+ draggableTreeViewPointer->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
+
+ // Indicate that all the rows are the same height, which improves performance.
+ draggableTreeViewPointer->setUniformRowHeights(true);
+
+ // Set the selection mode to allow multiple rows to be selected at once.
+ draggableTreeViewPointer->setSelectionMode(QAbstractItemView::ExtendedSelection);
+
+ // Allow dragging of bookmarks to reorder.
+ draggableTreeViewPointer->setDragDropMode(QAbstractItemView::InternalMove);
+
+ // Set the tree model.
+ draggableTreeViewPointer->setModel(treeModelPointer);
+
+ // Get a handle for the tree selection model.
+ treeSelectionModelPointer = draggableTreeViewPointer->selectionModel();
+
+ // Listen for selection changes.
+ connect(treeSelectionModelPointer, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(updateUi()));
+
+ // Repopulate the bookmarks when they are moved.
+ connect(draggableTreeViewPointer, SIGNAL(bookmarksMoved()), this, SLOT(refreshBookmarks()));
+
+ // Get handles for the buttons.
+ QPushButton *addBookmarkButtonPointer = bookmarksDialogUi.addBookmarkButton;
+ editButtonPointer = bookmarksDialogUi.editButton;
+ deleteItemsButtonPointer = bookmarksDialogUi.deleteItemsButton;
+ QDialogButtonBox *dialogButtonBoxPointer = bookmarksDialogUi.dialogButtonBox;
+ QPushButton *closeButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Close);
+
+ // Connect the buttons.
+ connect(addBookmarkButtonPointer, SIGNAL(clicked()), this, SLOT(showAddBookmarkDialog()));
+ connect(editButtonPointer, SIGNAL(clicked()), this, SLOT(showEditDialog()));
+ connect(deleteItemsButtonPointer, SIGNAL(clicked()), this, SLOT(deleteItems()));
+ connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
+
+ // Set the close button to be the default.
+ closeButtonPointer->setDefault(true);
+
+ // Monitor editing of data in the tree model.
+ connect(treeModelPointer, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(updateBookmarkFromTree(QStandardItem*)));
+
+ // Populate the bookmarks.
+ populateBookmarks();
+}
+
+void BookmarksDialog::deleteItems() const
+{
+ // Get the list of selected model indexes.
+ QList<QModelIndex> selectedModelIndexList = treeSelectionModelPointer->selectedRows(DATABASE_ID_COLUMN);
+
+ // Delete each of the bookmarks.
+ for (const QModelIndex &modelIndex : selectedModelIndexList)
+ {
+ // Delete the bookmark.
+ BookmarksDatabase::deleteBookmark(modelIndex.data().toInt());
+ }
+
+ // Repopulate the bookmarks in this dialog
+ populateBookmarks();
+
+ // Emit the bookmark updated signal to redraw the bookmarks in the menu and toolbar.
+ emit bookmarkUpdated();
+}
+
+void BookmarksDialog::populateBookmarks() const
+{
+ // Clear the current contents of the tree model.
+ treeModelPointer->clear();
// Set the column count.
- treeModelPointer->setColumnCount(3);
+ treeModelPointer->setColumnCount(4);
- // Set the tree header data. The first column is the database ID, which is not displayed.
- treeModelPointer->setHeaderData(1, Qt::Horizontal, i18nc("The bookmark Name header.", "Name"));
- treeModelPointer->setHeaderData(2, Qt::Horizontal, i18nc("The bookmark URL header.", "URL"));
+ // Set the tree header data. The last column is the database ID, which is not displayed.
+ treeModelPointer->setHeaderData(BOOKMARK_NAME_COLUMN, Qt::Horizontal, i18nc("The bookmark Name header.", "Name"));
+ treeModelPointer->setHeaderData(BOOKMARK_URL_COLUMN, Qt::Horizontal, i18nc("The bookmark URL header.", "URL"));
+
+ // Hide the backend columns.
+ draggableTreeViewPointer->setColumnHidden(DATABASE_ID_COLUMN, true);
+ draggableTreeViewPointer->setColumnHidden(DISPLAY_ORDER, true);
+
+ // Get the list of bookmarks.
+ std::list<BookmarkStruct> *bookmarksListPointer = BookmarksDatabase::getBookmarks();
// Populate the bookmarks tree view.
- for (BookmarkStruct bookmarkStruct : *bookmarksListPointer)
+ for (const BookmarkStruct &bookmarkStruct : *bookmarksListPointer)
{
// Create a list for the bookmark items.
QList<QStandardItem*> bookmarkItemList;
// Create the bookmark items.
- QStandardItem *idItemPointer = new QStandardItem(QString::number(bookmarkStruct.id));
QStandardItem *nameItemPointer = new QStandardItem(bookmarkStruct.favoriteIcon, bookmarkStruct.bookmarkName);
QStandardItem *urlItemPointer = new QStandardItem(bookmarkStruct.bookmarkUrl);
+ QStandardItem *idItemPointer = new QStandardItem(QString::number(bookmarkStruct.id));
+ QStandardItem *displayOrderPointer = new QStandardItem(QString::number(bookmarkStruct.displayOrder));
- // Populate the cookie standard item list.
- bookmarkItemList.append(idItemPointer);
+ nameItemPointer->setDragEnabled(true);
+ nameItemPointer->setDropEnabled(true);
+
+ // Disable dragging the URL.
+ urlItemPointer->setDragEnabled(false);
+
+ // Disable dropping on the URL. For some reason this doesn't work.
+ urlItemPointer->setDropEnabled(false);
+
+ // Disable selecting the URL.
+ urlItemPointer->setSelectable(false);
+
+ // Populate the bookmark item list.
bookmarkItemList.append(nameItemPointer);
bookmarkItemList.append(urlItemPointer);
+ bookmarkItemList.append(idItemPointer);
+ bookmarkItemList.append(displayOrderPointer);
- // Add the cookie to the tree.
+ // Add the bookmark to the tree.
treeModelPointer->appendRow(bookmarkItemList);
}
- // Auto resize the headers.
- treeViewPointer->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
+ // Update the UI.
+ updateUi();
+}
- // Indicate that all the rows are the same height, which improves performance.
- treeViewPointer->setUniformRowHeights(true);
+void BookmarksDialog::refreshBookmarks() const
+{
+ // Repopulate the bookmarks in this dialog
+ populateBookmarks();
- // Set the tree model.
- treeViewPointer->setModel(treeModelPointer);
+ // Emit the bookmark updated signal to redraw the bookmarks in the menu and toolbar.
+ emit bookmarkUpdated();
+}
- // Hide the database ID column.
- treeViewPointer->setColumnHidden(0, true);
+void BookmarksDialog::showAddBookmarkDialog() const
+{
+ // Instantiate an add bookmark dialog.
+ AddBookmarkDialog *addBookmarkDialogPointer = new AddBookmarkDialog(QLatin1String(""), QLatin1String(""), websiteFavoriteIcon);
- // Get handles for the buttons.
- QDialogButtonBox *dialogButtonBoxPointer = bookmarksDialogUi.dialogButtonBox;
+ // Update the displayed bookmarks when a new one is added.
+ connect(addBookmarkDialogPointer, SIGNAL(bookmarkAdded()), this, SLOT(refreshBookmarks()));
- // Connect the buttons.
- connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
+ // Show the dialog.
+ addBookmarkDialogPointer->show();
+}
- // Monitor editing of data in the tree model.
- connect(treeModelPointer, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(updateBookmarkFromTree(QStandardItem*)));
+void BookmarksDialog::showEditDialog()
+{
+ // Get the current model index.
+ QModelIndex currentIndex = treeSelectionModelPointer->currentIndex();
+
+ // Instantiate an edit bookmark dialog.
+ QDialog *editBookmarkDialogPointer = new EditBookmarkDialog(currentIndex.siblingAtColumn(DATABASE_ID_COLUMN).data().toInt(), websiteFavoriteIcon);
+
+ // Show the dialog.
+ editBookmarkDialogPointer->show();
+
+ // Process bookmark events.
+ connect(editBookmarkDialogPointer, SIGNAL(bookmarkSaved()), this, SLOT(refreshBookmarks()));
}
void BookmarksDialog::updateBookmarkFromTree(QStandardItem *modifiedStandardItem)
QModelIndex modifiedItemModelIndex = modifiedStandardItem->index();
// Get the model index of the database ID.
- QModelIndex databaseIdModelIndex = modifiedItemModelIndex.siblingAtColumn(0);
+ QModelIndex databaseIdModelIndex = modifiedItemModelIndex.siblingAtColumn(DATABASE_ID_COLUMN);
// Get the database ID.
int databaseId = databaseIdModelIndex.data().toInt();
// Check to see if the bookmark name or the URL was edited.
- if (modifiedStandardItem->column() == 1) // The bookmark name was edited.
+ if (modifiedStandardItem->column() == BOOKMARK_NAME_COLUMN) // The bookmark name was edited.
{
// Update the bookmark name.
BookmarksDatabase::updateBookmarkName(databaseId, modifiedStandardItem->text());
// Emit the bookmark updated signal.
emit bookmarkUpdated();
}
+
+void BookmarksDialog::updateUi() const
+{
+ // Set the status of the buttons.
+ if (treeSelectionModelPointer->hasSelection()) // A bookmark or folder is selected.
+ {
+ // Enabled the buttons.
+ editButtonPointer->setEnabled(true);
+ deleteItemsButtonPointer->setEnabled(true);
+
+ // Update the delete items button text.
+ deleteItemsButtonPointer->setText(i18ncp("Delete items button populated text.", "Delete %1 item", "Delete %1 items", treeSelectionModelPointer->selectedRows().count()));
+ }
+ else // Nothing is selected.
+ {
+ // Disable the buttons.
+ editButtonPointer->setEnabled(false);
+ deleteItemsButtonPointer->setEnabled(false);
+
+ // Update the delete items button text.
+ deleteItemsButtonPointer->setText(i18nc("Delete items button default text", "Delete items"));
+ }
+}