File indexing completed on 2024-06-23 05:49:08

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2010, 2012-2013 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 "viewprofilesmanagedialog.hpp"
0010 
0011 // this
0012 #include "viewprofileeditdialog.hpp"
0013 #include "viewprofiletablemodel.hpp"
0014 // Okteta Gui Kasten
0015 #include <Kasten/Okteta/ByteArrayViewProfileManager>
0016 #include <Kasten/Okteta/ByteArrayViewProfileLock>
0017 // KF
0018 #include <KLocalizedString>
0019 #include <KGuiItem>
0020 // Qt
0021 #include <QPushButton>
0022 #include <QHBoxLayout>
0023 #include <QTreeView>
0024 #include <QDialogButtonBox>
0025 #include <QItemSelectionModel>
0026 
0027 namespace Kasten {
0028 
0029 ViewProfilesManageDialog::ViewProfilesManageDialog(ByteArrayViewProfileManager* viewProfileManager,
0030                                                    QWidget* parent)
0031     : QDialog(parent)
0032     , mViewProfileManager(viewProfileManager)
0033 {
0034     setAttribute(Qt::WA_DeleteOnClose, true);
0035 
0036     setWindowTitle(i18nc("@title:window", "View Profiles"));
0037 
0038     auto* pageLayout = new QHBoxLayout;
0039 
0040     // profile list
0041     mViewProfileTableView = new QTreeView;
0042     mViewProfileTableModel = new ViewProfileTableModel(mViewProfileManager, this);
0043     mViewProfileTableView->setObjectName(QStringLiteral("ViewProfileTableView"));
0044     mViewProfileTableView->setHeaderHidden(true);
0045     mViewProfileTableView->setRootIsDecorated(false);
0046     mViewProfileTableView->setItemsExpandable(false);
0047     mViewProfileTableView->setUniformRowHeights(true);
0048     mViewProfileTableView->setAllColumnsShowFocus(true);
0049     mViewProfileTableView->setModel(mViewProfileTableModel);
0050     mViewProfileTableView->resizeColumnToContents(ViewProfileTableModel::CurrentColumnId);
0051     connect(mViewProfileTableView->selectionModel(),
0052             &QItemSelectionModel::selectionChanged,
0053             this, &ViewProfilesManageDialog::onViewProfileSelectionChanged);
0054     connect(mViewProfileTableModel, &ViewProfileTableModel::modelReset,
0055             this, &ViewProfilesManageDialog::onModelReset);
0056 
0057     // buttons
0058     auto* buttonLayout = new QVBoxLayout;
0059     auto* createButton = // copy from selected
0060                          new QPushButton;
0061     KGuiItem::assign(createButton,
0062                      KGuiItem(i18nc("@action:button",
0063                                     "&Create new..."),
0064                               QStringLiteral("document-new"),
0065                               i18nc("@info:tooltip",
0066                                     "Opens an editor for a new view profile."),
0067                               xi18nc("@info:whatsthis",
0068                                      "If you press the <interface>Create new...</interface> button, "
0069                                      "an editor is opened where you can create and edit a new view profile. "
0070                                      "The values will be based on the ones of the view profile you selected "
0071                                      "in the list.")));
0072     connect(createButton, &QPushButton::clicked, this, &ViewProfilesManageDialog::onCreateNewButtonClicked);
0073     buttonLayout->addWidget(createButton);
0074     mEditButton = new QPushButton;
0075     KGuiItem::assign(mEditButton,
0076                      KGuiItem(i18nc("@action:button",
0077                                     "&Edit..."),
0078                               QStringLiteral("document-edit"),
0079                               i18nc("@info:tooltip",
0080                                     "Opens an editor for the view profile."),
0081                               xi18nc("@info:whatsthis",
0082                                      "If you press the <interface>Edit...</interface> button, "
0083                                      "an editor will be opened for the view profile you selected "
0084                                      "in the list.")));
0085     connect(mEditButton, &QPushButton::clicked, this, &ViewProfilesManageDialog::onEditButtonClicked);
0086     buttonLayout->addWidget(mEditButton);
0087     mSetDefaultButton = new QPushButton;
0088     KGuiItem::assign(mSetDefaultButton,
0089                      KGuiItem(i18nc("@action:button",
0090                                     "&Set as Default"),
0091                               QStringLiteral("starred-symbolic"),
0092                               i18nc("@info:tooltip",
0093                                     "Sets the selected view profile as default for all views."),
0094                               xi18nc("@info:whatsthis",
0095                                      "If you press the <interface>Set as Default</interface> button, "
0096                                      "the view profile you selected in the list is set as default for all views.")));
0097     connect(mSetDefaultButton, &QPushButton::clicked, this, &ViewProfilesManageDialog::onSetDefaultButtonClicked);
0098     buttonLayout->addWidget(mSetDefaultButton);
0099     mDeleteButton = new QPushButton;
0100     KGuiItem::assign(mDeleteButton,
0101                      KGuiItem(i18nc("@action:button",
0102                                     "&Delete"),
0103                               QStringLiteral("edit-delete"),
0104                               i18nc("@info:tooltip",
0105                                     "Deletes the selected view profile."),
0106                               xi18nc("@info:whatsthis",
0107                                      "If you press the <interface>Delete</interface> button, "
0108                                      "the view profile you selected in the list is deleted.")));
0109     connect(mDeleteButton, &QPushButton::clicked, this, &ViewProfilesManageDialog::onDeleteButtonClicked);
0110     buttonLayout->addWidget(mDeleteButton);
0111     buttonLayout->addStretch();
0112 
0113     pageLayout->addWidget(mViewProfileTableView);
0114     pageLayout->addLayout(buttonLayout);
0115 
0116     // dialog buttons
0117     auto* dialogButtonBox = new QDialogButtonBox;
0118     mCloseButton = dialogButtonBox->addButton(QDialogButtonBox::Close);
0119     connect(mCloseButton, &QAbstractButton::clicked, this, &QDialog::accept);
0120 
0121     // main layout
0122     auto* layout = new QVBoxLayout;
0123     layout->addLayout(pageLayout);
0124     layout->addWidget(dialogButtonBox);
0125     setLayout(layout);
0126 
0127     mCloseButton->setDefault(true);
0128 
0129     connect(mViewProfileManager, &ByteArrayViewProfileManager::viewProfilesLocked,
0130             this, &ViewProfilesManageDialog::onViewProfilesLocked);
0131     connect(mViewProfileManager, &ByteArrayViewProfileManager::viewProfilesLocked,
0132             this, &ViewProfilesManageDialog::onViewProfilesUnlocked);
0133     connect(mViewProfileManager, &ByteArrayViewProfileManager::defaultViewProfileChanged,
0134             this, &ViewProfilesManageDialog::onDefaultViewProfileChanged);
0135 
0136     // select first by default
0137     onModelReset();
0138 }
0139 
0140 ViewProfilesManageDialog::~ViewProfilesManageDialog() = default;
0141 
0142 void
0143 ViewProfilesManageDialog::onViewProfileSelectionChanged()
0144 {
0145     const QItemSelectionModel* selectionModel = mViewProfileTableView->selectionModel();
0146     const QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
0147     const bool hasSelection = (!selectedIndexes.isEmpty());
0148 
0149     mCurrentViewProfileId = hasSelection ? mViewProfileTableModel->viewProfileId(selectedIndexes.at(0)) : ByteArrayViewProfile::Id();
0150 
0151     const bool isEditable = hasSelection &&
0152                             !mViewProfileManager->isViewProfileLocked(mCurrentViewProfileId);
0153 
0154     mEditButton->setEnabled(isEditable);
0155     mDeleteButton->setEnabled(isEditable);
0156 
0157     mSetDefaultButton->setEnabled(isEditable &&
0158                                   mCurrentViewProfileId != mViewProfileManager->defaultViewProfileId());
0159 }
0160 
0161 void
0162 ViewProfilesManageDialog::onCreateNewButtonClicked()
0163 {
0164     auto* dialog = new ViewProfileEditDialog(this);
0165     {
0166         const bool isBasedOnExisting = (!mCurrentViewProfileId.isEmpty());
0167         ByteArrayViewProfile newByteArrayViewProfile = isBasedOnExisting ?
0168                                                        mViewProfileManager->viewProfile(mCurrentViewProfileId) : ByteArrayViewProfile();
0169 
0170         if (isBasedOnExisting) {
0171             // reset id
0172             newByteArrayViewProfile.setId(QString());
0173             // twist title
0174             const QString modifiedTitle = i18n("Modification of %1", newByteArrayViewProfile.viewProfileTitle());
0175             newByteArrayViewProfile.setViewProfileTitle(modifiedTitle);
0176         }
0177 
0178         dialog->setViewProfile(newByteArrayViewProfile);
0179         const QString dialogTitle = i18nc("@window:title",
0180                                           "New View Profile");
0181         dialog->setWindowTitle(dialogTitle);
0182 
0183         connect(dialog, &ViewProfileEditDialog::viewProfileAccepted,
0184                 this, &ViewProfilesManageDialog::saveViewProfile);
0185     }
0186 
0187     dialog->open();
0188 
0189     mCloseButton->setDefault(true);
0190 }
0191 
0192 void
0193 ViewProfilesManageDialog::onEditButtonClicked()
0194 {
0195     if (mCurrentViewProfileId.isEmpty()) {
0196         return;
0197     }
0198 
0199     ByteArrayViewProfileLock viewProfileLock =
0200         mViewProfileManager->createLock(mCurrentViewProfileId);
0201 
0202     if (!viewProfileLock.isLocked()) {
0203         return;
0204     }
0205 
0206     const ByteArrayViewProfile viewProfile =
0207         mViewProfileManager->viewProfile(mCurrentViewProfileId);
0208     auto* dialog = new ViewProfileEditDialog(this);
0209     dialog->setViewProfile(viewProfile);
0210     const QString dialogTitle = i18nc("@window:title",
0211                                       "\"%1\" View Profile", viewProfile.viewProfileTitle());
0212     dialog->setWindowTitle(dialogTitle);
0213     connect(dialog, &ViewProfileEditDialog::viewProfileAccepted,
0214             this, &ViewProfilesManageDialog::saveViewProfile);
0215 
0216     dialog->open();
0217 
0218     mCloseButton->setDefault(true);
0219 }
0220 
0221 void ViewProfilesManageDialog::saveViewProfile(const ByteArrayViewProfile& viewProfile)
0222 {
0223     QVector<ByteArrayViewProfile> viewProfiles {
0224         viewProfile
0225     };
0226     mViewProfileManager->saveViewProfiles(viewProfiles);
0227 }
0228 
0229 void
0230 ViewProfilesManageDialog::onSetDefaultButtonClicked()
0231 {
0232     if (mCurrentViewProfileId.isEmpty()) {
0233         return;
0234     }
0235 
0236     mViewProfileManager->setDefaultViewProfile(mCurrentViewProfileId);
0237 
0238     mCloseButton->setDefault(true);
0239 }
0240 
0241 void
0242 ViewProfilesManageDialog::onDeleteButtonClicked()
0243 {
0244     if (mCurrentViewProfileId.isEmpty()) {
0245         return;
0246     }
0247 
0248     // TODO: ask user if she really wants to delete
0249     const QVector<ByteArrayViewProfile::Id> viewProfileIds {
0250         mCurrentViewProfileId
0251     };
0252     mViewProfileManager->removeViewProfiles(viewProfileIds);
0253 
0254     mCloseButton->setDefault(true);
0255 }
0256 
0257 void
0258 ViewProfilesManageDialog::onModelReset()
0259 {
0260     int row = mViewProfileTableModel->row(mCurrentViewProfileId);
0261 
0262     // no longer exists? set current to first if there is one
0263     if ((row < 0) && (0 < mViewProfileManager->viewProfilesCount())) {
0264         row = 0;
0265     }
0266 
0267     const bool isViewProfileSelected = (0 <= row);
0268     if (isViewProfileSelected) {
0269         const QItemSelection selection = QItemSelection(mViewProfileTableModel->index(row, ViewProfileTableModel::CurrentColumnId),
0270                                                         mViewProfileTableModel->index(row, ViewProfileTableModel::NameColumnId));
0271 
0272         mViewProfileTableView->selectionModel()->select(selection, QItemSelectionModel::Select);
0273     } else {
0274         mCurrentViewProfileId.clear();
0275     }
0276     // TODO: show a ghost profile with the built-in parameters if there is none
0277 
0278     mEditButton->setEnabled(isViewProfileSelected);
0279     mDeleteButton->setEnabled(isViewProfileSelected);
0280     mSetDefaultButton->setEnabled(isViewProfileSelected &&
0281                                   mCurrentViewProfileId != mViewProfileManager->defaultViewProfileId());
0282 }
0283 
0284 void
0285 ViewProfilesManageDialog::onViewProfilesLocked(const QVector<ByteArrayViewProfile::Id>& viewProfileIds)
0286 {
0287     // find if any locked profile is the currently selected, then disable all buttons
0288     if (viewProfileIds.contains(mCurrentViewProfileId)) {
0289         mEditButton->setEnabled(false);
0290         mDeleteButton->setEnabled(false);
0291         mSetDefaultButton->setEnabled(false);
0292     }
0293 }
0294 
0295 void
0296 ViewProfilesManageDialog::onViewProfilesUnlocked(const QVector<ByteArrayViewProfile::Id>& viewProfileIds)
0297 {
0298     // find if any locked profile is the currently selected, then enable all buttons
0299     if (viewProfileIds.contains(mCurrentViewProfileId)) {
0300         mEditButton->setEnabled(true);
0301         mDeleteButton->setEnabled(true);
0302         if (mCurrentViewProfileId != mViewProfileManager->defaultViewProfileId()) {
0303             mSetDefaultButton->setEnabled(true);
0304         }
0305     }
0306 }
0307 
0308 void
0309 ViewProfilesManageDialog::onDefaultViewProfileChanged(const ByteArrayViewProfile::Id& viewProfileId)
0310 {
0311     mSetDefaultButton->setEnabled((!mCurrentViewProfileId.isEmpty()) &&
0312                                   (mCurrentViewProfileId != viewProfileId));
0313 }
0314 
0315 }
0316 
0317 #include "moc_viewprofilesmanagedialog.cpp"