File indexing completed on 2024-11-10 04:56:47
0001 /* 0002 SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 #include "buttonsmodel.h" 0007 0008 #include <KLocalizedString> 0009 0010 #include <QFontDatabase> 0011 0012 namespace KDecoration2 0013 { 0014 0015 namespace Preview 0016 { 0017 0018 ButtonsModel::ButtonsModel(const QList<DecorationButtonType> &buttons, QObject *parent) 0019 : QAbstractListModel(parent) 0020 , m_buttons(buttons) 0021 { 0022 } 0023 0024 ButtonsModel::ButtonsModel(QObject *parent) 0025 : ButtonsModel(QList<DecorationButtonType>({ 0026 DecorationButtonType::Menu, 0027 DecorationButtonType::ApplicationMenu, 0028 DecorationButtonType::OnAllDesktops, 0029 DecorationButtonType::Minimize, 0030 DecorationButtonType::Maximize, 0031 DecorationButtonType::Close, 0032 DecorationButtonType::ContextHelp, 0033 DecorationButtonType::Shade, 0034 DecorationButtonType::KeepBelow, 0035 DecorationButtonType::KeepAbove, 0036 DecorationButtonType::Spacer, 0037 }), 0038 parent) 0039 { 0040 } 0041 0042 ButtonsModel::~ButtonsModel() = default; 0043 0044 int ButtonsModel::rowCount(const QModelIndex &parent) const 0045 { 0046 if (parent.isValid()) { 0047 return 0; 0048 } 0049 return m_buttons.count(); 0050 } 0051 0052 static QString buttonToName(DecorationButtonType type) 0053 { 0054 switch (type) { 0055 case DecorationButtonType::Menu: 0056 return i18n("More actions for this window"); 0057 case DecorationButtonType::ApplicationMenu: 0058 return i18n("Application menu"); 0059 case DecorationButtonType::OnAllDesktops: 0060 return i18n("On all desktops"); 0061 case DecorationButtonType::Minimize: 0062 return i18n("Minimize"); 0063 case DecorationButtonType::Maximize: 0064 return i18n("Maximize"); 0065 case DecorationButtonType::Close: 0066 return i18n("Close"); 0067 case DecorationButtonType::ContextHelp: 0068 return i18n("Context help"); 0069 case DecorationButtonType::Shade: 0070 return i18n("Shade"); 0071 case DecorationButtonType::KeepBelow: 0072 return i18n("Keep below other windows"); 0073 case DecorationButtonType::KeepAbove: 0074 return i18n("Keep above other windows"); 0075 case DecorationButtonType::Spacer: 0076 return i18n("Spacer"); 0077 default: 0078 return QString(); 0079 } 0080 } 0081 0082 QVariant ButtonsModel::data(const QModelIndex &index, int role) const 0083 { 0084 if (!index.isValid() || index.row() < 0 || index.row() >= m_buttons.count() || index.column() != 0) { 0085 return QVariant(); 0086 } 0087 switch (role) { 0088 case Qt::DisplayRole: 0089 return buttonToName(m_buttons.at(index.row())); 0090 case Qt::UserRole: 0091 return QVariant::fromValue(int(m_buttons.at(index.row()))); 0092 } 0093 return QVariant(); 0094 } 0095 0096 QHash<int, QByteArray> ButtonsModel::roleNames() const 0097 { 0098 QHash<int, QByteArray> roles; 0099 roles.insert(Qt::DisplayRole, QByteArrayLiteral("display")); 0100 roles.insert(Qt::UserRole, QByteArrayLiteral("button")); 0101 return roles; 0102 } 0103 0104 void ButtonsModel::remove(int row) 0105 { 0106 if (row < 0 || row >= m_buttons.count()) { 0107 return; 0108 } 0109 beginRemoveRows(QModelIndex(), row, row); 0110 m_buttons.removeAt(row); 0111 endRemoveRows(); 0112 } 0113 0114 void ButtonsModel::down(int index) 0115 { 0116 if (m_buttons.count() < 2 || index == m_buttons.count() - 1) { 0117 return; 0118 } 0119 beginMoveRows(QModelIndex(), index, index, QModelIndex(), index + 2); 0120 m_buttons.insert(index + 1, m_buttons.takeAt(index)); 0121 endMoveRows(); 0122 } 0123 0124 void ButtonsModel::up(int index) 0125 { 0126 if (m_buttons.count() < 2 || index == 0) { 0127 return; 0128 } 0129 beginMoveRows(QModelIndex(), index, index, QModelIndex(), index - 1); 0130 m_buttons.insert(index - 1, m_buttons.takeAt(index)); 0131 endMoveRows(); 0132 } 0133 0134 void ButtonsModel::add(DecorationButtonType type) 0135 { 0136 beginInsertRows(QModelIndex(), m_buttons.count(), m_buttons.count()); 0137 m_buttons.append(type); 0138 endInsertRows(); 0139 } 0140 0141 void ButtonsModel::add(int index, int type) 0142 { 0143 beginInsertRows(QModelIndex(), index, index); 0144 m_buttons.insert(index, KDecoration2::DecorationButtonType(type)); 0145 endInsertRows(); 0146 } 0147 0148 void ButtonsModel::move(int sourceIndex, int targetIndex) 0149 { 0150 if (sourceIndex == std::max(0, targetIndex)) { 0151 return; 0152 } 0153 0154 /* When moving an item down, the destination index needs to be incremented 0155 by one, as explained in the documentation: 0156 https://doc.qt.io/qt-5/qabstractitemmodel.html#beginMoveRows */ 0157 if (targetIndex > sourceIndex) { 0158 // Row will be moved down 0159 beginMoveRows(QModelIndex(), sourceIndex, sourceIndex, QModelIndex(), targetIndex + 1); 0160 } else { 0161 beginMoveRows(QModelIndex(), sourceIndex, sourceIndex, QModelIndex(), std::max(0, targetIndex)); 0162 } 0163 0164 m_buttons.move(sourceIndex, std::max(0, targetIndex)); 0165 endMoveRows(); 0166 } 0167 0168 void ButtonsModel::clear() 0169 { 0170 beginResetModel(); 0171 m_buttons.clear(); 0172 endResetModel(); 0173 } 0174 0175 void ButtonsModel::replace(const QList<DecorationButtonType> &buttons) 0176 { 0177 if (buttons.isEmpty()) { 0178 return; 0179 } 0180 0181 beginResetModel(); 0182 m_buttons = buttons; 0183 endResetModel(); 0184 } 0185 0186 } 0187 } 0188 0189 #include "moc_buttonsmodel.cpp"