X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=src%2Fdialogs%2FAddBookmarkDialog.cpp;h=041ca3d33df3db53b761d1ab9717c18936783b06;hb=3ea5ede1fd0721bea6813f36388ba6387bdbfcfe;hp=cc0d132d0c2a41d5b3fbea32a79f5f09be3b8e52;hpb=7c6edb3608791950c6146ac242e2b6f493ca8e8c;p=PrivacyBrowserPC.git diff --git a/src/dialogs/AddBookmarkDialog.cpp b/src/dialogs/AddBookmarkDialog.cpp index cc0d132..041ca3d 100644 --- a/src/dialogs/AddBookmarkDialog.cpp +++ b/src/dialogs/AddBookmarkDialog.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2023 Soren Stoutner . + * Copyright 2023-2024 Soren Stoutner . * * This file is part of Privacy Browser PC . * @@ -26,10 +26,12 @@ #include // Qt toolkit headers. +#include #include // Construct the class. -AddBookmarkDialog::AddBookmarkDialog(const QString &bookmarkName, const QString &bookmarkUrl, const QIcon &favoriteIcon) : QDialog(nullptr), icon(favoriteIcon) +AddBookmarkDialog::AddBookmarkDialog(QWidget *parentWidgetPointer, const QString &bookmarkName, const QString &bookmarkUrl, const QIcon &favoriteIcon, const double parentFolderId) : + QDialog(parentWidgetPointer) { // Set the window title. setWindowTitle(i18nc("The add bookmark dialog window title.", "Add Bookmark")); @@ -37,55 +39,145 @@ AddBookmarkDialog::AddBookmarkDialog(const QString &bookmarkName, const QString // Set the window modality. setWindowModality(Qt::WindowModality::ApplicationModal); - // Instantiate the bookmarks dialog UI. + // Instantiate the add bookmark dialog UI. Ui::AddBookmarkDialog addBookmarkDialogUi; // Setup the UI. addBookmarkDialogUi.setupUi(this); // Get handles for the widgets. - QGraphicsView *favoriteIconGraphicsViewPointer = addBookmarkDialogUi.favoriteIconGraphicsView; - bookmarkNamePointer = addBookmarkDialogUi.bookmarkNameLineEdit; - bookmarkUrlPointer = addBookmarkDialogUi.bookmarkUrlLineEdit; + websiteFavoriteIconRadioButtonPointer = addBookmarkDialogUi.websiteFavoriteIconRadioButton; + customFavoriteIconRadioButtonPointer = addBookmarkDialogUi.customFavoriteIconRadioButton; + parentFolderTreeWidgetPointer = addBookmarkDialogUi.parentFolderTreeWidget; + bookmarkNameLineEditPointer = addBookmarkDialogUi.bookmarkNameLineEdit; + bookmarkUrlLineEditPointer = addBookmarkDialogUi.bookmarkUrlLineEdit; + QPushButton *browseButtonPointer = addBookmarkDialogUi.browseButton; QDialogButtonBox *dialogButtonBoxPointer = addBookmarkDialogUi.dialogButtonBox; - // Create a graphics scene. - QGraphicsScene *favoriteIconGraphicsScenePointer = new QGraphicsScene(this); + // Set the icons. + websiteFavoriteIconRadioButtonPointer->setIcon(favoriteIcon); + customFavoriteIconRadioButtonPointer->setIcon(QIcon::fromTheme(QLatin1String("globe"), QIcon::fromTheme(QLatin1String("applications-internet")))); - // Set the graphics scene. - favoriteIconGraphicsViewPointer->setScene(favoriteIconGraphicsScenePointer); + // Instantiate a folder helper. + folderHelperPointer = new FolderHelper(); - // Set the background of the graphics view to be the same as the window - favoriteIconGraphicsViewPointer->setBackgroundRole(QPalette::Window); + // Set the parent folder tree widget column count. + parentFolderTreeWidgetPointer->setColumnCount(2); - // Add the MIME type icon to the scene. - favoriteIconGraphicsScenePointer->addPixmap(favoriteIcon.pixmap(32, 32)); + // Hide the second column. + parentFolderTreeWidgetPointer->hideColumn(folderHelperPointer->FOLDER_ID_COLUMN); + + // Set the column header. + parentFolderTreeWidgetPointer->setHeaderLabel(i18nc("The folder tree widget header", "Select Parent Folder")); + + // Create a bookmarks tree widget item. + QTreeWidgetItem *bookmarksTreeWidgetItemPointer = new QTreeWidgetItem(); + + // Populate the bookmarks tree widget item. + bookmarksTreeWidgetItemPointer->setText(folderHelperPointer->FOLDER_NAME_COLUMN, i18nc("The bookmarks root tree widget name", "Bookmarks")); + bookmarksTreeWidgetItemPointer->setIcon(folderHelperPointer->FOLDER_NAME_COLUMN, QIcon::fromTheme(QLatin1String("bookmarks"), QIcon::fromTheme(QLatin1String("bookmark-new")))); + bookmarksTreeWidgetItemPointer->setText(folderHelperPointer->FOLDER_ID_COLUMN, QLatin1String("0")); + + // Add the bookmarks tree widget item to the root of the tree. + parentFolderTreeWidgetPointer->addTopLevelItem(bookmarksTreeWidgetItemPointer); + + // Select the root bookmarks folder if it is the initial parent folder. + if (parentFolderId == 0) + bookmarksTreeWidgetItemPointer->setSelected(true); + + // Populate the subfolders. + folderHelperPointer->populateSubfolders(bookmarksTreeWidgetItemPointer, parentFolderId); + + // Open all the folders. + parentFolderTreeWidgetPointer->expandAll(); // Populate the line edits. - bookmarkNamePointer->setText(bookmarkName); - bookmarkUrlPointer->setText(bookmarkUrl); + bookmarkNameLineEditPointer->setText(bookmarkName); + bookmarkUrlLineEditPointer->setText(bookmarkUrl); + + // Scroll to the beginning of the bookmark URL line edit. + bookmarkUrlLineEditPointer->setCursorPosition(0); - // Scroll the the beginning of the line edits. - bookmarkNamePointer->setCursorPosition(0); - bookmarkUrlPointer->setCursorPosition(0); + // Focus the bookmark name line edit. + bookmarkNameLineEditPointer->setFocus(); // Add buttons to the dialog button box. - QPushButton *addBookmarkButtonPointer = dialogButtonBoxPointer->addButton(i18nc("The add bookmark button", "Add"), QDialogButtonBox::AcceptRole); + addButtonPointer = dialogButtonBoxPointer->addButton(i18nc("The add bookmark button", "Add"), QDialogButtonBox::AcceptRole); // Set the button icons. - addBookmarkButtonPointer->setIcon(QIcon::fromTheme("list-add")); + addButtonPointer->setIcon(QIcon::fromTheme("list-add")); // Connect the buttons. + connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse())); connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(addBookmark())); connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject())); + + // Update the UI when the line edits change. + connect(bookmarkNameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi())); + connect(bookmarkUrlLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi())); + + // Set the initial UI status. + updateUi(); } void AddBookmarkDialog::addBookmark() { + // Get the selected folders list. + QList selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems(); + + // Get the selected folder. + QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first(); + + // Get the favorite icon. + QIcon favoriteIcon = websiteFavoriteIconRadioButtonPointer->isChecked() ? websiteFavoriteIconRadioButtonPointer->icon() : customFavoriteIconRadioButtonPointer->icon(); + + // Create a bookmark struct. + BookmarkStruct *bookmarkStructPointer = new BookmarkStruct; + + // Populate the bookmark struct. + bookmarkStructPointer->name = bookmarkNameLineEditPointer->text(); + bookmarkStructPointer->url = bookmarkUrlLineEditPointer->text(); + bookmarkStructPointer->parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble(); + bookmarkStructPointer->favoriteIcon = favoriteIcon; + // Add the bookmark. - BookmarksDatabase::addBookmark(bookmarkNamePointer->text(), bookmarkUrlPointer->text(), icon); + BookmarksDatabase::addBookmark(bookmarkStructPointer); + + // Update the list of bookmarks in the menu and toolbar. + emit bookmarkAdded(); // Close the dialog. close(); } +void AddBookmarkDialog::browse() +{ + // Get an image file string from the user. + QString imageFileString = QFileDialog::getOpenFileName(this, tr("Favorite Icon Image"), QDir::homePath(), + tr("Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg (*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files (*)")); + + // Check to see if an image file string was returned. This will be empty if the user selected cancel. + if (!imageFileString.isEmpty()) + { + // Set the custom favorite icon. + customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString)); + + // Check the custom favorite icon radio button. + customFavoriteIconRadioButtonPointer->setChecked(true); + } +} + +void AddBookmarkDialog::updateUi() +{ + // Update the add button status + if (bookmarkNameLineEditPointer->text().isEmpty() || bookmarkUrlLineEditPointer->text().isEmpty()) // At least one of the line edits is empty. + { + // Disable the add button. + addButtonPointer->setEnabled(false); + } + else // Both of the line edits are populated. + { + // Enable the add button. + addButtonPointer->setEnabled(true); + } +}