File indexing completed on 2024-05-05 05:51:24

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
0003    SPDX-FileCopyrightText: 2001 Joseph Wenninger <jowenn@kde.org>
0004    SPDX-FileCopyrightText: 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
0005    SPDX-FileCopyrightText: 2007 Mirko Stocker <me@misto.ch>
0006 
0007    SPDX-License-Identifier: LGPL-2.0-only
0008 */
0009 
0010 #include "katefilebrowserconfig.h"
0011 
0012 #include "katefilebrowser.h"
0013 
0014 #include <QAction>
0015 #include <QApplication>
0016 #include <QGroupBox>
0017 #include <QListWidget>
0018 #include <QRegularExpression>
0019 #include <QStyle>
0020 #include <QVBoxLayout>
0021 
0022 #include <KActionCollection>
0023 #include <KActionSelector>
0024 #include <KConfigGroup>
0025 #include <KDirOperator>
0026 #include <KLocalizedString>
0027 #include <KSharedConfig>
0028 
0029 // BEGIN ACtionLBItem
0030 /*
0031   QListboxItem that can store and return a string,
0032   used for the toolbar action selector.
0033 */
0034 class ActionLBItem : public QListWidgetItem
0035 {
0036 public:
0037     explicit ActionLBItem(QListWidget *lb = nullptr, const QIcon &pm = QIcon(), const QString &text = QString(), const QString &str = QString())
0038         : QListWidgetItem(pm, text, lb, 0)
0039         , _str(str)
0040     {
0041     }
0042     QString idstring()
0043     {
0044         return _str;
0045     }
0046 
0047 private:
0048     QString _str;
0049 };
0050 // END ActionLBItem
0051 
0052 // BEGIN KateFileBrowserConfigPage
0053 KateFileBrowserConfigPage::KateFileBrowserConfigPage(QWidget *parent, KateFileBrowser *kfb)
0054     : KTextEditor::ConfigPage(parent)
0055     , fileBrowser(kfb)
0056 {
0057     QVBoxLayout *lo = new QVBoxLayout(this);
0058     int spacing = QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing);
0059     lo->setSpacing(spacing);
0060     lo->setContentsMargins(0, 0, 0, 0);
0061 
0062     // Toolbar - a lot for a little...
0063     QGroupBox *gbToolbar = new QGroupBox(i18n("Toolbar"), this);
0064     gbToolbar->setFlat(true);
0065     acSel = new KActionSelector(gbToolbar);
0066     acSel->setAvailableLabel(i18n("A&vailable actions:"));
0067     acSel->setSelectedLabel(i18n("S&elected actions:"));
0068 
0069     QVBoxLayout *vbox = new QVBoxLayout;
0070     vbox->addWidget(acSel);
0071     gbToolbar->setLayout(vbox);
0072 
0073     lo->addWidget(gbToolbar);
0074     connect(acSel, &KActionSelector::added, this, &KateFileBrowserConfigPage::slotMyChanged);
0075     connect(acSel, &KActionSelector::removed, this, &KateFileBrowserConfigPage::slotMyChanged);
0076     connect(acSel, &KActionSelector::movedUp, this, &KateFileBrowserConfigPage::slotMyChanged);
0077     connect(acSel, &KActionSelector::movedDown, this, &KateFileBrowserConfigPage::slotMyChanged);
0078 
0079     init();
0080 }
0081 
0082 QString KateFileBrowserConfigPage::name() const
0083 {
0084     return i18n("Filesystem Browser");
0085 }
0086 
0087 QString KateFileBrowserConfigPage::fullName() const
0088 {
0089     return i18n("Filesystem Browser Settings");
0090 }
0091 
0092 QIcon KateFileBrowserConfigPage::icon() const
0093 {
0094     return QIcon::fromTheme(QStringLiteral("document-open-folder"));
0095 }
0096 
0097 void KateFileBrowserConfigPage::apply()
0098 {
0099     if (!m_changed) {
0100         return;
0101     }
0102 
0103     m_changed = false;
0104 
0105     KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("filebrowser"));
0106     QStringList l;
0107     ActionLBItem *aItem;
0108     const QList<QListWidgetItem *> list = acSel->selectedListWidget()->findItems(QStringLiteral("*"), Qt::MatchWildcard);
0109     for (QListWidgetItem *item : list) {
0110         aItem = static_cast<ActionLBItem *>(item);
0111         l << aItem->idstring();
0112     }
0113     config.writeEntry("toolbar actions", l);
0114 
0115     fileBrowser->setupToolbar();
0116 }
0117 
0118 void KateFileBrowserConfigPage::reset()
0119 {
0120     // hmm, what is this supposed to do, actually??
0121     init();
0122     m_changed = false;
0123 }
0124 
0125 void KateFileBrowserConfigPage::init()
0126 {
0127     KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("filebrowser"));
0128     // toolbar
0129     QStringList l = config.readEntry("toolbar actions", QStringList());
0130     if (l.isEmpty()) { // default toolbar
0131         l << QStringLiteral("back") << QStringLiteral("forward") << QStringLiteral("bookmarks") << QStringLiteral("sync_dir") << QStringLiteral("configure");
0132     }
0133 
0134     // actions from diroperator + two of our own
0135     const QStringList allActions{QStringLiteral("up"),
0136                                  QStringLiteral("back"),
0137                                  QStringLiteral("forward"),
0138                                  QStringLiteral("home"),
0139                                  QStringLiteral("reload"),
0140                                  QStringLiteral("mkdir"),
0141                                  QStringLiteral("delete"),
0142                                  QStringLiteral("short view"),
0143                                  QStringLiteral("detailed view"),
0144                                  QStringLiteral("tree view"),
0145                                  QStringLiteral("detailed tree view"),
0146                                  QStringLiteral("show hidden"),
0147                                  // QStringLiteral("view menu"),
0148                                  // QStringLiteral("properties"),
0149                                  QStringLiteral("bookmarks"),
0150                                  QStringLiteral("sync_dir"),
0151                                  QStringLiteral("configure")};
0152 
0153     QRegularExpression re(QStringLiteral("&(?=[^&])"));
0154     QAction *ac = nullptr;
0155     QListWidget *lb;
0156     for (const auto &actionName : allActions) {
0157         lb = l.contains(actionName) ? acSel->selectedListWidget() : acSel->availableListWidget();
0158 
0159         if (actionName == QLatin1String("bookmarks") || actionName == QLatin1String("sync_dir") || actionName == QLatin1String("configure")) {
0160             ac = fileBrowser->actionCollection()->action(actionName);
0161         } else {
0162             ac = fileBrowser->dirOperator()->action(KateFileBrowser::actionFromName(actionName));
0163         }
0164 
0165         if (ac) {
0166             QString text = ac->text().remove(re);
0167 // CJK languages need a filtering message for action texts in lists,
0168 // to remove special accelerators that they use.
0169 // The exact same filtering message exists in kdelibs; hence,
0170 // avoid extraction here and let it be sourced from kdelibs.
0171 #define i18ncX i18nc
0172             text = i18ncX("@item:intable Action name in toolbar editor", "%1", text);
0173             new ActionLBItem(lb, ac->icon(), text, actionName);
0174         }
0175     }
0176 }
0177 
0178 void KateFileBrowserConfigPage::slotMyChanged()
0179 {
0180     m_changed = true;
0181     Q_EMIT changed();
0182 }
0183 // END KateFileBrowserConfigPage
0184 
0185 #include "moc_katefilebrowserconfig.cpp"