File indexing completed on 2024-05-26 05:56:25

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "bookmarksview.hpp"
0010 
0011 // tool
0012 #include "bookmarklistmodel.hpp"
0013 #include "bookmarkstool.hpp"
0014 // Okteta core
0015 #include <Okteta/Bookmark>
0016 // KF
0017 #include <KLocalizedString>
0018 // Qt
0019 #include <QToolBar>
0020 #include <QAction>
0021 #include <QVBoxLayout>
0022 #include <QTreeView>
0023 #include <QHeaderView>
0024 #include <QIcon>
0025 
0026 namespace Kasten {
0027 
0028 BookmarksView::BookmarksView(BookmarksTool* tool, QWidget* parent)
0029     : QWidget(parent)
0030     , mTool(tool)
0031 {
0032     mBookmarkListModel = new BookmarkListModel(mTool, this);
0033     connect(mBookmarkListModel, &BookmarkListModel::modelReset,
0034             this, &BookmarksView::onBookmarkSelectionChanged);
0035 
0036     auto* baseLayout = new QVBoxLayout(this);
0037     baseLayout->setContentsMargins(0, 0, 0, 0);
0038     baseLayout->setSpacing(0);
0039 
0040     // bookmarks list
0041     mBookmarkListView = new QTreeView(this);
0042     mBookmarkListView->setObjectName(QStringLiteral("BookmarkListView"));
0043     mBookmarkListView->setRootIsDecorated(false);
0044     mBookmarkListView->setItemsExpandable(false);
0045     mBookmarkListView->setUniformRowHeights(true);
0046     mBookmarkListView->setAllColumnsShowFocus(true);
0047     mBookmarkListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
0048     mBookmarkListView->setModel(mBookmarkListModel);
0049     mBookmarkListView->header()->setSectionResizeMode(QHeaderView::Interactive);
0050     connect(mBookmarkListView, &QTreeView::doubleClicked,
0051             this, &BookmarksView::onBookmarkDoubleClicked);
0052     connect(mBookmarkListView->selectionModel(),
0053             &QItemSelectionModel::selectionChanged,
0054             this, &BookmarksView::onBookmarkSelectionChanged);
0055 
0056     baseLayout->addWidget(mBookmarkListView, 10);
0057 
0058     // actions TODO: make this view work like the filebrowser, with actions on top?
0059     auto* actionsToolBar = new QToolBar(this);
0060 
0061     mCreateBookmarkAction =
0062         actionsToolBar->addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
0063                                   QString() /*i18n("C&opy")*/,
0064                                   this, &BookmarksView::onCreateBookmarkButtonClicked);
0065     mCreateBookmarkAction->setToolTip(i18nc("@info:tooltip",
0066                                             "Creates a new bookmark for the current cursor position."));
0067     mCreateBookmarkAction->setWhatsThis(i18nc("@info:whatsthis",
0068                                               "If you press this button, a new bookmark will be created "
0069                                               "for the current cursor position."));
0070     mCreateBookmarkAction->setEnabled(mTool->canCreateBookmark());
0071     connect(mTool, &BookmarksTool::canCreateBookmarkChanged,
0072             mCreateBookmarkAction, &QAction::setEnabled);
0073 
0074     mDeleteBookmarksAction =
0075         actionsToolBar->addAction(QIcon::fromTheme(QStringLiteral("edit-delete")),
0076                                   QString(),
0077                                   this, &BookmarksView::onDeleteBookmarkButtonClicked);
0078     mDeleteBookmarksAction->setToolTip(i18nc("@info:tooltip",
0079                                              "Deletes all the selected bookmarks."));
0080     mDeleteBookmarksAction->setWhatsThis(i18nc("@info:whatsthis",
0081                                                "If you press this button, all bookmarks which are "
0082                                                "selected will be deleted."));
0083 
0084     auto* stretcher = new QWidget(this);
0085     stretcher->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
0086     actionsToolBar->addWidget(stretcher);
0087 
0088     mGotoBookmarkAction =
0089         actionsToolBar->addAction(QIcon::fromTheme(QStringLiteral("go-jump")),
0090                                   QString() /*i18n("&Go to")*/,
0091                                   this, &BookmarksView::onGotoBookmarkButtonClicked);
0092     mGotoBookmarkAction->setToolTip(i18nc("@info:tooltip",
0093                                           "Moves the cursor to the selected bookmark."));
0094     mGotoBookmarkAction->setWhatsThis(i18nc("@info:whatsthis",
0095                                             "If you press this button, the cursor is moved to the position "
0096                                             "of the bookmark which has been last selected."));
0097 
0098     mRenameBookmarkAction =
0099         actionsToolBar->addAction(QIcon::fromTheme(QStringLiteral("edit-rename")),
0100                                   QString(),
0101                                   this, &BookmarksView::onRenameBookmarkButtonClicked);
0102     mRenameBookmarkAction->setToolTip(i18nc("@info:tooltip",
0103                                             "Enables renaming of the selected bookmark."));
0104     mRenameBookmarkAction->setWhatsThis(i18nc("@info:whatsthis",
0105                                               "If you press this button, the name of the bookmark "
0106                                               "which was last selected can be edited."));
0107 
0108     baseLayout->addWidget(actionsToolBar);
0109 
0110     onBookmarkSelectionChanged();
0111 }
0112 
0113 BookmarksView::~BookmarksView() = default;
0114 
0115 void BookmarksView::onBookmarkDoubleClicked(const QModelIndex& index)
0116 {
0117     const int column = index.column();
0118     const bool isOffsetColum = (column == BookmarkListModel::OffsetColumnId);
0119     if (isOffsetColum) {
0120         mTool->gotoBookmark(mBookmarkListModel->bookmark(index));
0121     }
0122 }
0123 
0124 void BookmarksView::onBookmarkSelectionChanged()
0125 {
0126     const QItemSelectionModel* selectionModel = mBookmarkListView->selectionModel();
0127 
0128     // TODO: selectionModel->selectedIndexes() is a expensive operation,
0129     // but with Qt 4.4.3 hasSelection() has the flaw to return true with a current index
0130     const bool hasSelection = !selectionModel->selectedIndexes().isEmpty();
0131     mDeleteBookmarksAction->setEnabled(hasSelection);
0132 
0133     const bool bookmarkSelected = selectionModel->isSelected(selectionModel->currentIndex());
0134     mRenameBookmarkAction->setEnabled(bookmarkSelected);
0135     mGotoBookmarkAction->setEnabled(bookmarkSelected);
0136 }
0137 
0138 void BookmarksView::onCreateBookmarkButtonClicked()
0139 {
0140     const Okteta::Bookmark bookmark = mTool->createBookmark();
0141     if (bookmark.isValid()) {
0142         const QModelIndex index = mBookmarkListModel->index(bookmark, BookmarkListModel::TitleColumnId);
0143         if (index.isValid()) {
0144             mBookmarkListView->edit(index);
0145         }
0146     }
0147 }
0148 
0149 void BookmarksView::onDeleteBookmarkButtonClicked()
0150 {
0151     const QModelIndexList selectedRows = mBookmarkListView->selectionModel()->selectedRows();
0152 
0153     QVector<Okteta::Bookmark> bookmarksToBeDeleted;
0154     bookmarksToBeDeleted.reserve(selectedRows.size());
0155     for (const QModelIndex& index : selectedRows) {
0156         const Okteta::Bookmark& bookmark = mBookmarkListModel->bookmark(index);
0157         bookmarksToBeDeleted.append(bookmark);
0158     }
0159 
0160     mTool->deleteBookmarks(bookmarksToBeDeleted);
0161 }
0162 
0163 void BookmarksView::onGotoBookmarkButtonClicked()
0164 {
0165     const QModelIndex index = mBookmarkListView->selectionModel()->currentIndex();
0166     if (index.isValid()) {
0167         mTool->gotoBookmark(mBookmarkListModel->bookmark(index));
0168     }
0169 }
0170 
0171 void BookmarksView::onRenameBookmarkButtonClicked()
0172 {
0173     QModelIndex index = mBookmarkListView->selectionModel()->currentIndex();
0174     const QModelIndex nameIndex = index.sibling(index.row(), BookmarkListModel::TitleColumnId);
0175     if (nameIndex.isValid()) {
0176         mBookmarkListView->edit(nameIndex);
0177     }
0178 }
0179 
0180 }
0181 
0182 #include "moc_bookmarksview.cpp"