File indexing completed on 2025-01-05 04:49:29

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "quicktexttreewidget.h"
0008 #include "quicktextmanager.h"
0009 
0010 #include <KLocalizedString>
0011 #include <KMessageBox>
0012 
0013 #include <MailCommon/SnippetsModel>
0014 #include <QContextMenuEvent>
0015 #include <QHeaderView>
0016 #include <QMenu>
0017 
0018 QuicktextTreeWidget::QuicktextTreeWidget(QuicktextManager *manager, QWidget *parent)
0019     : QTreeView(parent)
0020     , mSnippetsManager(manager)
0021 {
0022     header()->hide();
0023     setAcceptDrops(true);
0024     setDragEnabled(true);
0025     setRootIsDecorated(true);
0026     setAlternatingRowColors(true);
0027     connect(mSnippetsManager, &QuicktextManager::insertPlainText, this, &QuicktextTreeWidget::insertSnippetText);
0028 
0029     setModel(mSnippetsManager->model());
0030     setSelectionModel(mSnippetsManager->selectionModel());
0031 
0032     connect(mSnippetsManager->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QuicktextTreeWidget::selectionWasChanged);
0033 
0034     connect(mSnippetsManager->model(), &QAbstractItemModel::rowsInserted, this, &QTreeView::expandAll);
0035     connect(mSnippetsManager->model(), &QAbstractItemModel::rowsRemoved, this, &QTreeView::expandAll);
0036 
0037     mAddSnippetAction = new QAction(i18n("Add Snippet..."), this);
0038     mAddSnippetAction->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0039     mEditSnippetAction = new QAction(i18n("Edit Snippet..."), this);
0040     mEditSnippetAction->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
0041     mDeleteSnippetAction = new QAction(i18n("Remove Snippet"), this);
0042     mDeleteSnippetAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
0043 
0044     mAddSnippetGroupAction = new QAction(i18n("Add Group..."), this);
0045     mAddSnippetGroupAction->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0046     mEditSnippetGroupAction = new QAction(i18n("Rename Group..."), this);
0047     mEditSnippetGroupAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
0048     mDeleteSnippetGroupAction = new QAction(i18n("Remove Group"), this);
0049     mDeleteSnippetGroupAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
0050 
0051     connect(mAddSnippetAction, &QAction::triggered, this, &QuicktextTreeWidget::addSnippet);
0052     connect(mEditSnippetAction, &QAction::triggered, this, &QuicktextTreeWidget::editSnippet);
0053     connect(mAddSnippetGroupAction, &QAction::triggered, this, &QuicktextTreeWidget::addSnippetGroup);
0054     connect(mEditSnippetGroupAction, &QAction::triggered, this, &QuicktextTreeWidget::editSnippetGroup);
0055 
0056     connect(mDeleteSnippetGroupAction, &QAction::triggered, this, &QuicktextTreeWidget::deleteSnippetGroup);
0057     connect(mDeleteSnippetAction, &QAction::triggered, this, &QuicktextTreeWidget::deleteSnippet);
0058 
0059     expandAll();
0060     selectionWasChanged();
0061 }
0062 
0063 QuicktextTreeWidget::~QuicktextTreeWidget() = default;
0064 
0065 void QuicktextTreeWidget::contextMenuEvent(QContextMenuEvent *event)
0066 {
0067     QMenu popup;
0068 
0069     const bool itemSelected = mSnippetsManager->selectionModel()->hasSelection();
0070 
0071     bool canAddSnippet = true;
0072     if (itemSelected) {
0073         popup.setTitle(mSnippetsManager->selectedName());
0074         if (mSnippetsManager->snippetGroupSelected()) {
0075             popup.addAction(mEditSnippetGroupAction);
0076             popup.addAction(mDeleteSnippetGroupAction);
0077         } else {
0078             canAddSnippet = false; // subsnippets are not permitted
0079             popup.addAction(mAddSnippetAction);
0080             popup.addAction(mEditSnippetAction);
0081             popup.addAction(mDeleteSnippetAction);
0082         }
0083         popup.addSeparator();
0084     } else {
0085         popup.setTitle(i18n("Text Snippets"));
0086     }
0087     if (canAddSnippet) {
0088         popup.addAction(mAddSnippetAction);
0089     }
0090     popup.addAction(mAddSnippetGroupAction);
0091 
0092     popup.exec(event->globalPos());
0093 }
0094 
0095 void QuicktextTreeWidget::dropEvent(QDropEvent *event)
0096 {
0097     if (event->source() == this) {
0098         event->setDropAction(Qt::MoveAction);
0099     }
0100     QTreeView::dropEvent(event);
0101 }
0102 
0103 QuicktextManager *QuicktextTreeWidget::snippetsManager() const
0104 {
0105     return mSnippetsManager;
0106 }
0107 
0108 void QuicktextTreeWidget::selectionWasChanged()
0109 {
0110     const bool itemSelected = !mSnippetsManager->selectionModel()->selectedIndexes().isEmpty();
0111 
0112     if (itemSelected) {
0113         const QModelIndex index = mSnippetsManager->selectionModel()->selectedIndexes().first();
0114         const bool isGroup = index.data(MailCommon::SnippetsModel::IsGroupRole).toBool();
0115         if (isGroup) {
0116             mEditSnippetAction->setEnabled(false);
0117             mDeleteSnippetAction->setEnabled(false);
0118             mEditSnippetGroupAction->setEnabled(true);
0119             mDeleteSnippetGroupAction->setEnabled(true);
0120         } else {
0121             mEditSnippetAction->setEnabled(true);
0122             mDeleteSnippetAction->setEnabled(true);
0123             mEditSnippetGroupAction->setEnabled(false);
0124             mDeleteSnippetGroupAction->setEnabled(false);
0125         }
0126     } else {
0127         mEditSnippetAction->setEnabled(false);
0128         mDeleteSnippetAction->setEnabled(false);
0129         mEditSnippetGroupAction->setEnabled(false);
0130         mDeleteSnippetGroupAction->setEnabled(false);
0131     }
0132 }
0133 
0134 void QuicktextTreeWidget::deleteSnippet()
0135 {
0136     const QModelIndex index = mSnippetsManager->selectionModel()->selectedIndexes().first();
0137 
0138     const QString snippetName = index.data(MailCommon::SnippetsModel::NameRole).toString();
0139 
0140     if (KMessageBox::warningContinueCancel(this,
0141                                            xi18nc("@info",
0142                                                   "Do you really want to remove snippet \"%1\"?<nl/>"
0143                                                   "<warning>There is no way to undo the removal.</warning>",
0144                                                   snippetName),
0145                                            QString(),
0146                                            KStandardGuiItem::remove())
0147         == KMessageBox::Cancel) {
0148         return;
0149     }
0150 
0151     mSnippetsManager->model()->removeRow(index.row(), mSnippetsManager->currentGroupIndex());
0152     mSnippetsManager->save();
0153 }
0154 
0155 void QuicktextTreeWidget::deleteSnippetGroup()
0156 {
0157     const QModelIndex groupIndex = mSnippetsManager->currentGroupIndex();
0158     if (!groupIndex.isValid()) {
0159         return;
0160     }
0161 
0162     const QString groupName = groupIndex.data(MailCommon::SnippetsModel::NameRole).toString();
0163 
0164     if (mSnippetsManager->model()->rowCount(groupIndex) > 0) {
0165         if (KMessageBox::warningContinueCancel(this,
0166                                                xi18nc("@info",
0167                                                       "Do you really want to remove group \"%1\" along with all its snippets?<nl/>"
0168                                                       "<warning>There is no way to undo the removal.</warning>",
0169                                                       groupName),
0170                                                QString(),
0171                                                KStandardGuiItem::remove())
0172             == KMessageBox::Cancel) {
0173             return;
0174         }
0175     } else {
0176         if (KMessageBox::warningContinueCancel(nullptr,
0177                                                i18nc("@info", "Do you really want to remove group \"%1\"?", groupName),
0178                                                QString(),
0179                                                KStandardGuiItem::remove())
0180             == KMessageBox::Cancel) {
0181             return;
0182         }
0183     }
0184 
0185     mSnippetsManager->model()->removeRow(groupIndex.row(), QModelIndex());
0186     mSnippetsManager->save();
0187 }
0188 
0189 #include "moc_quicktexttreewidget.cpp"