|
| 1 | +// Author: Kang Lin <[email protected]> |
| 2 | + |
| 3 | +#include <QLoggingCategory> |
| 4 | +#include "FrmAddBookmark.h" |
| 5 | +#include "ui_FrmAddBookmark.h" |
| 6 | + |
| 7 | +static Q_LOGGING_CATEGORY(log, "WebBrowser.Bookmark.Add") |
| 8 | +CFrmAddBookmark::CFrmAddBookmark(const QString &szTitle, const QUrl &url, |
| 9 | + const QIcon &icon, CParameterWebBrowser* pPara, |
| 10 | + CBookmarkDatabase *db, QWidget *parent) |
| 11 | + : QWidget(parent) |
| 12 | + , ui(new Ui::CFrmAddBookmark) |
| 13 | + , m_szTitle(szTitle) |
| 14 | + , m_Url(url) |
| 15 | + , m_Icon(icon) |
| 16 | + , m_pPara(pPara) |
| 17 | + , m_pDatabase(db) |
| 18 | + , m_pModelTree(nullptr) |
| 19 | +{ |
| 20 | + ui->setupUi(this); |
| 21 | + ui->pbDelete->setVisible(false); |
| 22 | + |
| 23 | + setWindowIcon(QIcon::fromTheme("user-bookmarks")); |
| 24 | + |
| 25 | + if(m_szTitle.isEmpty()) |
| 26 | + m_szTitle = m_Url.toString(); |
| 27 | + |
| 28 | + ui->leTitle->setText(m_szTitle); |
| 29 | + |
| 30 | + if(m_pPara) { |
| 31 | + ui->cbSave->setChecked(m_pPara->GetBookmarkShowEditor()); |
| 32 | + } |
| 33 | + |
| 34 | + m_pModelTree = new QStandardItemModel(ui->tvFolder); |
| 35 | + if(m_pModelTree) { |
| 36 | + ui->tvFolder->setModel(m_pModelTree); |
| 37 | + m_pModelTree->setColumnCount(1); |
| 38 | + } |
| 39 | + |
| 40 | + loadFolder(); |
| 41 | +} |
| 42 | + |
| 43 | +CFrmAddBookmark::~CFrmAddBookmark() |
| 44 | +{ |
| 45 | + delete ui; |
| 46 | +} |
| 47 | + |
| 48 | +void CFrmAddBookmark::on_pbAdd_clicked() |
| 49 | +{ |
| 50 | + if(!m_pPara || !m_pDatabase) return; |
| 51 | + |
| 52 | + m_pPara->SetBookmarkShowEditor(ui->cbSave->isChecked()); |
| 53 | + |
| 54 | + auto index = ui->tvFolder->currentIndex(); |
| 55 | + if(index.isValid()) { |
| 56 | + int id = index.data((int)Role::ID).toInt(); |
| 57 | + m_pPara->SetCurrentBookmarkFolder(id); |
| 58 | + auto items = m_pDatabase->getBookmarkByUrl(m_Url.toString()); |
| 59 | + if(!items.isEmpty()) { |
| 60 | + auto item = items[0]; |
| 61 | + item.title = ui->leTitle->text(); |
| 62 | + item.url = m_Url.toString(); |
| 63 | + item.folderId = id; |
| 64 | + m_pDatabase->updateBookmark(item); |
| 65 | + } else { |
| 66 | + BookmarkItem item; |
| 67 | + item.title = ui->leTitle->text(); |
| 68 | + item.url = m_Url.toString(); |
| 69 | + item.folderId = id; |
| 70 | + m_pDatabase->addBookmark(item); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + close(); |
| 75 | +} |
| 76 | + |
| 77 | +void CFrmAddBookmark::on_pbCancel_clicked() |
| 78 | +{ |
| 79 | + close(); |
| 80 | +} |
| 81 | + |
| 82 | +void CFrmAddBookmark::on_pbDelete_clicked() |
| 83 | +{ |
| 84 | + auto items = m_pDatabase->getBookmarkByUrl(m_Url.toString()); |
| 85 | + m_pDatabase->deleteBookmark(items); |
| 86 | + ui->pbDelete->setVisible(false); |
| 87 | + loadFolder(); |
| 88 | +} |
| 89 | + |
| 90 | +void CFrmAddBookmark::loadFolder() |
| 91 | +{ |
| 92 | + if(!m_pDatabase || !m_pModelTree || !m_pPara) return; |
| 93 | + |
| 94 | + m_pModelTree->clear(); |
| 95 | + m_folderItems.clear(); |
| 96 | + |
| 97 | + // 设置表头 |
| 98 | + m_pModelTree->setHorizontalHeaderLabels(QStringList() << tr("Title")); |
| 99 | + |
| 100 | + QStandardItem* pCurrentItem = nullptr; |
| 101 | + // 加载文件夹结构 |
| 102 | + QList<BookmarkItem> folders = m_pDatabase->getAllFolders(); |
| 103 | + |
| 104 | + // 创建根节点 |
| 105 | + QStandardItem *rootItem = m_pModelTree->invisibleRootItem(); |
| 106 | + |
| 107 | + int nCurrent = m_pPara->GetCurrentBookmarkFolder(); |
| 108 | + auto item = m_pDatabase->getBookmarkByUrl(m_Url.toString()); |
| 109 | + if(!item.isEmpty()) { |
| 110 | + nCurrent = item[0].folderId; |
| 111 | + ui->pbDelete->setVisible(true); |
| 112 | + ui->pbDelete->setText(tr("Delete %1 bookmarks").arg(item.count())); |
| 113 | + } |
| 114 | + |
| 115 | + // 先添加顶级文件夹 |
| 116 | + for (const auto &folder : folders) { |
| 117 | + if (folder.folderId == 0) { // 顶级文件夹 |
| 118 | + QStandardItem *pFolderItem = new QStandardItem(folder.getIcon(), folder.title); |
| 119 | + if(!pFolderItem) continue; |
| 120 | + pFolderItem->setData(folder.id, (int)Role::ID); |
| 121 | + pFolderItem->setData(BookmarkType_Folder, (int)Role::Type); |
| 122 | + rootItem->appendRow(pFolderItem); |
| 123 | + m_folderItems[folder.id] = pFolderItem; |
| 124 | + if(nCurrent == folder.id) |
| 125 | + pCurrentItem = pFolderItem; |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + auto it = m_folderItems.find(folder.folderId); |
| 130 | + if(m_folderItems.end() == it) { |
| 131 | + qWarning(log) << "The parent of folder is not find:" << folder.folderId; |
| 132 | + continue; |
| 133 | + } |
| 134 | + QStandardItem *pFolderItem = new QStandardItem(folder.getIcon(), folder.title); |
| 135 | + if(!pFolderItem) continue; |
| 136 | + pFolderItem->setData(folder.id, (int)Role::ID); |
| 137 | + pFolderItem->setData(BookmarkType_Folder, (int)Role::Type); |
| 138 | + (*it)->appendRow(pFolderItem); |
| 139 | + m_folderItems[folder.id] = pFolderItem; |
| 140 | + if(nCurrent == folder.id) |
| 141 | + pCurrentItem = pFolderItem; |
| 142 | + } |
| 143 | + |
| 144 | + // Set current index |
| 145 | + if(pCurrentItem) { |
| 146 | + auto index = m_pModelTree->indexFromItem(pCurrentItem); |
| 147 | + ui->tvFolder->setCurrentIndex(index); |
| 148 | + } |
| 149 | + |
| 150 | + ui->tvFolder->expandAll(); |
| 151 | +} |
0 commit comments