File indexing completed on 2025-01-26 05:06:23

0001 /*
0002     SPDX-FileCopyrightText: 2014 Eike Hein <hein@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "viewpropertiesmenu.h"
0008 
0009 #include <QActionGroup>
0010 #include <QApplication>
0011 #include <QMenu>
0012 
0013 #include <KDirModel>
0014 #include <KLocalizedString>
0015 
0016 ViewPropertiesMenu::ViewPropertiesMenu(QObject *parent)
0017     : QObject(parent)
0018 {
0019     m_menu = new QMenu();
0020 
0021     QMenu *menu = m_menu->addMenu(QIcon::fromTheme(QStringLiteral("view-sort")), i18n("Sort By"));
0022     m_sortMode = new QActionGroup(this);
0023     connect(m_sortMode, &QActionGroup::triggered, this, &ViewPropertiesMenu::sortModeChanged);
0024     QAction *action = menu->addAction(i18nc("@item:inmenu Sort icons manually", "Unsorted"));
0025     action->setCheckable(true);
0026     action->setData(-1);
0027     m_sortMode->addAction(action);
0028     action = menu->addAction(i18nc("@item:inmenu Sort icons by name", "Name"));
0029     action->setCheckable(true);
0030     action->setData(int(KDirModel::Name));
0031     m_sortMode->addAction(action);
0032     action = menu->addAction(i18nc("@item:inmenu Sort icons by size", "Size"));
0033     action->setCheckable(true);
0034     action->setData(int(KDirModel::Size));
0035     m_sortMode->addAction(action);
0036     action = menu->addAction(i18nc("@item:inmenu Sort icons by file type", "Type"));
0037     action->setCheckable(true);
0038     action->setData(int(KDirModel::Type));
0039     m_sortMode->addAction(action);
0040     action = menu->addAction(i18nc("@item:inmenu Sort icons by date", "Date"));
0041     action->setCheckable(true);
0042     action->setData(int(KDirModel::ModifiedTime));
0043     m_sortMode->addAction(action);
0044     menu->addSeparator();
0045     m_sortDesc = menu->addAction(i18nc("@item:inmenu Sort icons in descending order", "Descending"), this, &ViewPropertiesMenu::sortDescChanged);
0046     m_sortDesc->setCheckable(true);
0047     m_sortDirsFirst = menu->addAction(i18nc("@item:inmenu Sort icons with folders first", "Folders First"), this, &ViewPropertiesMenu::sortDirsFirstChanged);
0048     m_sortDirsFirst->setCheckable(true);
0049 
0050     m_iconSizeMenu = m_menu->addMenu(QIcon::fromTheme(QStringLiteral("transform-scale")), i18n("Icon Size"));
0051     m_iconSize = new QActionGroup(this);
0052     connect(m_iconSize, &QActionGroup::triggered, this, &ViewPropertiesMenu::iconSizeChanged);
0053     const QStringList iconSizes{i18nc("@item:inmenu size of the icons", "Tiny"),
0054                                 i18nc("@item:inmenu size of the icons", "Very Small"),
0055                                 i18nc("@item:inmenu size of the icons", "Small"),
0056                                 i18nc("@item:inmenu size of the icons", "Small-Medium"),
0057                                 i18nc("@item:inmenu size of the icons", "Medium"),
0058                                 i18nc("@item:inmenu size of the icons", "Large"),
0059                                 i18nc("@item:inmenu size of the icons", "Huge")};
0060     for (int i = 0; i < iconSizes.count(); ++i) {
0061         action = m_iconSizeMenu->addAction(iconSizes.at(i));
0062         action->setCheckable(true);
0063         action->setData(i);
0064         m_iconSize->addAction(action);
0065     }
0066 
0067     m_arrangementMenu = m_menu->addMenu(QIcon::fromTheme(QStringLiteral("object-rows")), i18nc("@item:inmenu arrangement of icons", "Arrange"));
0068     m_arrangement = new QActionGroup(this);
0069     connect(m_arrangement, &QActionGroup::triggered, this, &ViewPropertiesMenu::arrangementChanged);
0070     const auto isLtR = qApp->layoutDirection() == Qt::LeftToRight;
0071     action = m_arrangementMenu->addAction(isLtR ? i18nc("@item:inmenu arrangement of icons", "Left to Right")
0072                                                 : i18nc("@item:inmenu arrangement of icons", "Right to Left"));
0073     action->setCheckable(true);
0074     action->setData(0);
0075     m_arrangement->addAction(action);
0076     action = m_arrangementMenu->addAction(i18nc("@item:inmenu arrangement of icons", "Top to Bottom"));
0077     action->setData(1);
0078     action->setCheckable(true);
0079     m_arrangement->addAction(action);
0080 
0081     m_alignmentMenu = m_menu->addMenu(QIcon::fromTheme(QStringLiteral("align-horizontal-left")), i18n("Align"));
0082     m_alignment = new QActionGroup(this);
0083     connect(m_alignment, &QActionGroup::triggered, this, &ViewPropertiesMenu::alignmentChanged);
0084     action = m_alignmentMenu->addAction(i18nc("@item:inmenu alignment of icons", "Left"));
0085     action->setCheckable(true);
0086     action->setData(0);
0087     m_alignment->addAction(action);
0088     action = m_alignmentMenu->addAction(i18nc("@item:inmenu alignment of icons", "Right"));
0089     action->setCheckable(true);
0090     action->setData(1);
0091     m_alignment->addAction(action);
0092 
0093     m_previews = m_menu->addAction(QIcon::fromTheme(QStringLiteral("view-preview")), i18n("Show Previews"), this, &ViewPropertiesMenu::previewsChanged);
0094     m_previews->setCheckable(true);
0095 
0096     m_locked = m_menu->addAction(QIcon::fromTheme(QStringLiteral("lock")),
0097                                  i18nc("@item:inmenu lock icon positions in place", "Locked"),
0098                                  this,
0099                                  &ViewPropertiesMenu::lockedChanged);
0100     m_locked->setCheckable(true);
0101 }
0102 
0103 ViewPropertiesMenu::~ViewPropertiesMenu()
0104 {
0105     delete m_menu;
0106 }
0107 
0108 QObject *ViewPropertiesMenu::menu() const
0109 {
0110     return m_menu;
0111 }
0112 
0113 bool ViewPropertiesMenu::showLayoutActions() const
0114 {
0115     return m_alignmentMenu->menuAction()->isVisible() && m_arrangementMenu->menuAction()->isVisible();
0116 }
0117 
0118 void ViewPropertiesMenu::setShowLayoutActions(bool show)
0119 {
0120     if (showLayoutActions() != show) {
0121         m_arrangementMenu->menuAction()->setVisible(show);
0122         m_alignmentMenu->menuAction()->setVisible(show);
0123 
0124         Q_EMIT showLayoutActionsChanged();
0125     }
0126 }
0127 
0128 bool ViewPropertiesMenu::showLockAction() const
0129 {
0130     return m_locked->isVisible();
0131 }
0132 
0133 void ViewPropertiesMenu::setShowLockAction(bool show)
0134 {
0135     if (m_locked->isVisible() != show) {
0136         m_locked->setVisible(show);
0137 
0138         Q_EMIT showLockActionChanged();
0139     }
0140 }
0141 
0142 bool ViewPropertiesMenu::showIconSizeActions() const
0143 {
0144     return m_iconSizeMenu->menuAction()->isVisible();
0145 }
0146 
0147 void ViewPropertiesMenu::setShowIconSizeActions(bool show)
0148 {
0149     if (showIconSizeActions() != show) {
0150         m_iconSizeMenu->menuAction()->setVisible(show);
0151         Q_EMIT showIconSizeActionsChanged();
0152     }
0153 }
0154 
0155 int ViewPropertiesMenu::arrangement() const
0156 {
0157     return m_arrangement->checkedAction()->data().toInt();
0158 }
0159 
0160 void ViewPropertiesMenu::setArrangement(int arrangement)
0161 {
0162     if (!m_arrangement->checkedAction() || m_arrangement->checkedAction()->data().toInt() != arrangement) {
0163         const QList<QAction *> actions = m_arrangement->actions();
0164         for (QAction *action : actions) {
0165             if (action->data().toInt() == arrangement) {
0166                 action->setChecked(true);
0167                 break;
0168             }
0169         }
0170     }
0171 }
0172 
0173 int ViewPropertiesMenu::alignment() const
0174 {
0175     return m_alignment->checkedAction()->data().toInt();
0176 }
0177 
0178 void ViewPropertiesMenu::setAlignment(int alignment)
0179 {
0180     if (!m_alignment->checkedAction() || m_alignment->checkedAction()->data().toInt() != alignment) {
0181         const QList<QAction *> actions = m_alignment->actions();
0182         for (QAction *action : actions) {
0183             if (action->data().toInt() == alignment) {
0184                 action->setChecked(true);
0185                 break;
0186             }
0187         }
0188     }
0189 }
0190 
0191 bool ViewPropertiesMenu::previews() const
0192 {
0193     return m_previews->isChecked();
0194 }
0195 
0196 void ViewPropertiesMenu::setPreviews(bool previews)
0197 {
0198     if (m_previews->isChecked() != previews) {
0199         m_previews->setChecked(previews);
0200     }
0201 }
0202 
0203 bool ViewPropertiesMenu::locked() const
0204 {
0205     return m_locked->isChecked();
0206 }
0207 
0208 void ViewPropertiesMenu::setLocked(bool locked)
0209 {
0210     if (m_locked->isChecked() != locked) {
0211         m_locked->setChecked(locked);
0212     }
0213 }
0214 
0215 bool ViewPropertiesMenu::lockedEnabled() const
0216 {
0217     return m_locked->isEnabled();
0218 }
0219 
0220 void ViewPropertiesMenu::setLockedEnabled(bool enabled)
0221 {
0222     if (m_locked->isEnabled() != enabled) {
0223         m_locked->setEnabled(enabled);
0224         Q_EMIT lockedEnabledChanged();
0225     }
0226 }
0227 
0228 int ViewPropertiesMenu::sortMode() const
0229 {
0230     return m_sortMode->checkedAction()->data().toInt();
0231 }
0232 
0233 void ViewPropertiesMenu::setSortMode(int sortMode)
0234 {
0235     if (!m_sortMode->checkedAction() || m_sortMode->checkedAction()->data().toInt() != sortMode) {
0236         const QList<QAction *> actions = m_sortMode->actions();
0237         for (QAction *action : actions) {
0238             if (action->data().toInt() == sortMode) {
0239                 action->setChecked(true);
0240                 break;
0241             }
0242         }
0243     }
0244 }
0245 
0246 bool ViewPropertiesMenu::sortDesc() const
0247 {
0248     return m_sortDesc->isChecked();
0249 }
0250 
0251 void ViewPropertiesMenu::setSortDesc(bool sortDesc)
0252 {
0253     if (m_sortDesc->isChecked() != sortDesc) {
0254         m_sortDesc->setChecked(sortDesc);
0255     }
0256 }
0257 
0258 bool ViewPropertiesMenu::sortDirsFirst() const
0259 {
0260     return m_sortDirsFirst->isChecked();
0261 }
0262 
0263 void ViewPropertiesMenu::setSortDirsFirst(bool sortDirsFirst)
0264 {
0265     if (m_sortDirsFirst->isChecked() != sortDirsFirst) {
0266         m_sortDirsFirst->setChecked(sortDirsFirst);
0267     }
0268 }
0269 
0270 int ViewPropertiesMenu::iconSize() const
0271 {
0272     return m_iconSize->checkedAction()->data().toInt();
0273 }
0274 
0275 void ViewPropertiesMenu::setIconSize(int iconSize)
0276 {
0277     if (!m_iconSize->checkedAction() || m_iconSize->checkedAction()->data().toInt() != iconSize) {
0278         QAction *action = m_iconSize->actions().value(iconSize);
0279         if (action) {
0280             action->setChecked(true);
0281         }
0282     }
0283 }