File indexing completed on 2024-04-21 05:45:50

0001 /*
0002     KT list view item tasks implementation.
0003     --------------------------------------------------------------------
0004     SPDX-FileCopyrightText: 1999 Gary Meyer <gary@meyer.net>
0005     --------------------------------------------------------------------
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "genericListWidget.h"
0010 
0011 #include <QAction>
0012 #include <QHeaderView>
0013 #include <QKeyEvent>
0014 #include <QVBoxLayout>
0015 
0016 #include "ctcron.h"
0017 #include "cttask.h"
0018 
0019 #include "crontabWidget.h"
0020 #include "taskEditorDialog.h"
0021 #include "taskWidget.h"
0022 
0023 #include "kcm_cron_debug.h"
0024 
0025 /**
0026  * Construct tasks folder from branch.
0027  */
0028 GenericListWidget::GenericListWidget(CrontabWidget *crontabWidget, const QString &label, const QIcon &icon)
0029     : QWidget(crontabWidget)
0030 {
0031     auto mainLayout = new QVBoxLayout(this);
0032     mainLayout->setContentsMargins(0, 0, 0, 0);
0033 
0034     mCrontabWidget = crontabWidget;
0035 
0036     // Label layout
0037     auto labelLayout = new QHBoxLayout();
0038 
0039     auto tasksIcon = new QLabel(this);
0040     tasksIcon->setPixmap(icon.pixmap(style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this)));
0041     labelLayout->addWidget(tasksIcon);
0042 
0043     auto tasksLabel = new QLabel(label, this);
0044     labelLayout->addWidget(tasksLabel, 1, Qt::AlignLeft);
0045 
0046     mainLayout->addLayout(labelLayout);
0047 
0048     // Tree layout
0049     auto treeLayout = new QHBoxLayout();
0050 
0051     mTreeWidget = new QTreeWidget(this);
0052 
0053     mTreeWidget->setRootIsDecorated(true);
0054     mTreeWidget->setAllColumnsShowFocus(true);
0055 
0056     mTreeWidget->header()->setSortIndicatorShown(true);
0057     mTreeWidget->header()->setStretchLastSection(true);
0058     mTreeWidget->header()->setSectionsMovable(true);
0059 
0060     mTreeWidget->setSortingEnabled(true);
0061     mTreeWidget->setAnimated(true);
0062 
0063     mTreeWidget->setRootIsDecorated(false);
0064 
0065     mTreeWidget->setAllColumnsShowFocus(true);
0066 
0067     mTreeWidget->setAlternatingRowColors(true);
0068 
0069     mTreeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
0070     mTreeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
0071 
0072     treeLayout->addWidget(mTreeWidget);
0073 
0074     mActionsLayout = new QVBoxLayout();
0075 
0076     treeLayout->addLayout(mActionsLayout);
0077 
0078     mainLayout->addLayout(treeLayout);
0079 
0080     qCDebug(KCM_CRON_LOG) << "Generic list created";
0081     connect(treeWidget(), &QTreeWidget::itemDoubleClicked, this, &GenericListWidget::modifySelection);
0082 }
0083 
0084 GenericListWidget::~GenericListWidget()
0085 {
0086 }
0087 
0088 QTreeWidget *GenericListWidget::treeWidget() const
0089 {
0090     return mTreeWidget;
0091 }
0092 
0093 CrontabWidget *GenericListWidget::crontabWidget() const
0094 {
0095     return mCrontabWidget;
0096 }
0097 
0098 CTHost *GenericListWidget::ctHost() const
0099 {
0100     return mCrontabWidget->ctHost();
0101 }
0102 
0103 void GenericListWidget::resizeColumnContents()
0104 {
0105     // Resize all columns except the last one (which always take the last available space)
0106     for (int i = 0, total = mTreeWidget->columnCount() - 1; i < total; ++i) {
0107         mTreeWidget->resizeColumnToContents(i);
0108     }
0109 }
0110 
0111 QTreeWidgetItem *GenericListWidget::firstSelected() const
0112 {
0113     const QList<QTreeWidgetItem *> tasksItems = treeWidget()->selectedItems();
0114     if (tasksItems.isEmpty()) {
0115         return nullptr;
0116     }
0117 
0118     return tasksItems.constFirst();
0119 }
0120 
0121 void GenericListWidget::keyPressEvent(QKeyEvent *e)
0122 {
0123     if (e->key() == Qt::Key_Delete) {
0124         deleteSelection();
0125     }
0126 }
0127 
0128 void GenericListWidget::removeAll()
0129 {
0130     // Remove previous items
0131     for (int i = treeWidget()->topLevelItemCount() - 1; i >= 0; --i) {
0132         delete treeWidget()->takeTopLevelItem(i);
0133     }
0134 }
0135 
0136 QAction *GenericListWidget::createSeparator()
0137 {
0138     auto action = new QAction(this);
0139     action->setSeparator(true);
0140 
0141     return action;
0142 }
0143 
0144 void GenericListWidget::addRightAction(QAction *action, const QObject *receiver, const char *member)
0145 {
0146     auto button = new QPushButton(action->text(), this);
0147     button->setIcon(action->icon());
0148     button->setWhatsThis(action->whatsThis());
0149     button->setToolTip(action->toolTip());
0150 
0151     mActionsLayout->addWidget(button);
0152 
0153     button->addAction(action);
0154 
0155     connect(button, SIGNAL(clicked(bool)), receiver, member);
0156     connect(action, SIGNAL(triggered(bool)), receiver, member);
0157 }
0158 
0159 void GenericListWidget::addRightStretch()
0160 {
0161     mActionsLayout->addStretch(1);
0162 }
0163 
0164 void GenericListWidget::setActionEnabled(QAction *action, bool enabled)
0165 {
0166     const auto associatedWidgets = action->associatedWidgets();
0167     for (QWidget *widget : associatedWidgets) {
0168         // Only change status of associated Buttons
0169         auto button = qobject_cast<QPushButton *>(widget);
0170         if (button) {
0171             button->setEnabled(enabled);
0172         }
0173     }
0174 
0175     action->setEnabled(enabled);
0176 }
0177 
0178 #include "moc_genericListWidget.cpp"