File indexing completed on 2024-04-28 09:25:35

0001 /*
0002     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "appletsmodel.h"
0007 
0008 // local
0009 #include "../../layout/abstractlayout.h"
0010 
0011 // Qt
0012 #include <QFont>
0013 #include <QIcon>
0014 
0015 // KDE
0016 #include <KLocalizedString>
0017 
0018 namespace Latte {
0019 namespace Settings {
0020 namespace Model {
0021 
0022 Applets::Applets(QObject *parent)
0023     : QAbstractTableModel(parent)
0024 {
0025     m_appletsWithNoPersonalData = {
0026         "org.kde.latte.separator",
0027         "org.kde.latte.spacer",
0028         "org.kde.latte.plasmoid",
0029         "org.kde.windowtitle",
0030         "org.kde.windowbuttons",
0031         "org.kde.windowappmenu",
0032         "org.kde.plasma.marginsseparator"
0033     };
0034 }
0035 
0036 Applets::~Applets()
0037 {
0038 }
0039 
0040 bool Applets::hasChangedData() const
0041 {
0042     return c_applets != o_applets;
0043 }
0044 
0045 int Applets::rowCount() const
0046 {
0047     return c_applets.rowCount();
0048 }
0049 
0050 int Applets::rowCount(const QModelIndex &parent) const
0051 {
0052     Q_UNUSED(parent);
0053 
0054     return c_applets.rowCount();
0055 }
0056 
0057 int Applets::columnCount(const QModelIndex &parent) const
0058 {
0059     Q_UNUSED(parent);
0060 
0061     return 1;
0062 }
0063 
0064 int Applets::row(const QString &id)
0065 {
0066     for (int i=0; i<c_applets.rowCount(); ++i){
0067         if (c_applets[i].id == id) {
0068             return i;
0069         }
0070     }
0071 
0072     return -1;
0073 }
0074 
0075 bool Applets::inDefaultValues() const
0076 {
0077     return c_applets == o_applets;
0078 }
0079 
0080 void Applets::initDefaults()
0081 {
0082     for(int i=0; i<c_applets.rowCount(); ++i) {
0083         c_applets[i].isSelected = m_appletsWithNoPersonalData.contains(c_applets[i].id);
0084     }
0085 }
0086 
0087 void Applets::clear()
0088 {
0089     if (c_applets.rowCount() > 0) {
0090         beginRemoveRows(QModelIndex(), 0, c_applets.rowCount() - 1);
0091         c_applets.clear();
0092         endRemoveRows();
0093 
0094         emit appletsDataChanged();
0095     }
0096 }
0097 
0098 void Applets::reset()
0099 {
0100     c_applets = o_applets;
0101 
0102     QVector<int> roles;
0103     roles << Qt::CheckStateRole;
0104 
0105     emit dataChanged(index(0, NAMECOLUMN), index(c_applets.rowCount()-1, NAMECOLUMN), roles);
0106     emit appletsDataChanged();
0107 }
0108 
0109 void Applets::setData(const Latte::Data::AppletsTable &applets)
0110 {
0111     clear();
0112 
0113     if (applets.rowCount() > 0) {
0114         beginInsertRows(QModelIndex(), 0, applets.rowCount()-1);
0115         c_applets = applets;
0116         initDefaults();
0117         o_applets = c_applets;
0118         endInsertRows();
0119 
0120         emit appletsDataChanged();
0121     }
0122 }
0123 
0124 void Applets::selectAll()
0125 {
0126     QVector<int> roles;
0127     roles << Qt::CheckStateRole;
0128 
0129     bool changed{false};
0130 
0131     for(int i=0; i<c_applets.rowCount(); ++i) {
0132         if (!c_applets[i].isSelected) {
0133             c_applets[i].isSelected = true;
0134             emit dataChanged(index(i, NAMECOLUMN), index(i, NAMECOLUMN), roles);
0135             changed = true;
0136         }
0137     }
0138 
0139     if (changed) {
0140         emit appletsDataChanged();
0141     }
0142 }
0143 
0144 void Applets::deselectAll()
0145 {
0146     QVector<int> roles;
0147     roles << Qt::CheckStateRole;
0148 
0149     bool changed{false};
0150 
0151     for(int i=0; i<c_applets.rowCount(); ++i) {
0152         if (c_applets[i].isSelected) {
0153             c_applets[i].isSelected = false;
0154             emit dataChanged(index(i, NAMECOLUMN), index(i, NAMECOLUMN), roles);
0155             changed = true;
0156         }
0157     }
0158 
0159     if (changed) {
0160         emit appletsDataChanged();
0161     }
0162 }
0163 
0164 void Applets::setSelected(const Latte::Data::AppletsTable &applets)
0165 {
0166     bool changed{false};
0167 
0168     for(int i=0; i<applets.rowCount(); ++i) {
0169         int pos = c_applets.indexOf(applets[i].id);
0170 
0171         if (pos>=0 && applets[i].isSelected != c_applets[pos].isSelected) {
0172             QVector<int> roles;
0173             roles << Qt::CheckStateRole;
0174 
0175             c_applets[pos].isSelected = applets[i].isSelected;
0176             emit dataChanged(index(pos, NAMECOLUMN), index(pos, NAMECOLUMN), roles);
0177             changed = true;
0178         }
0179     }
0180 
0181     if (changed) {
0182         emit appletsDataChanged();
0183     }
0184 }
0185 
0186 Latte::Data::AppletsTable Applets::selectedApplets()
0187 {
0188     Data::AppletsTable selected;
0189 
0190     for(int i=0; i<c_applets.rowCount(); ++i) {
0191         if (c_applets[i].isSelected) {
0192             selected << c_applets[i];
0193         }
0194     }
0195     return selected;
0196 }
0197 
0198 Qt::ItemFlags Applets::flags(const QModelIndex &index) const
0199 {
0200     const int column = index.column();
0201     const int row = index.row();
0202 
0203     auto flags = QAbstractTableModel::flags(index);
0204 
0205     flags |= Qt::ItemIsUserCheckable;
0206 
0207     return flags;
0208 }
0209 
0210 QVariant Applets::headerData(int section, Qt::Orientation orientation, int role) const
0211 {
0212     if (orientation != Qt::Horizontal) {
0213         return QAbstractTableModel::headerData(section, orientation, role);
0214     }
0215 
0216     if (role == Qt::FontRole) {
0217         QFont font = qvariant_cast<QFont>(QAbstractTableModel::headerData(section, orientation, role));
0218         font.setBold(true);
0219         return font;
0220     }
0221 
0222     switch(section) {
0223     case NAMECOLUMN:
0224         if (role == Qt::DisplayRole) {
0225             return QString(i18nc("column for current applets", "Current Applets"));
0226         }
0227         break;
0228     default:
0229         break;
0230     };
0231 
0232     return QAbstractTableModel::headerData(section, orientation, role);
0233 }
0234 
0235 bool Applets::setData(const QModelIndex &index, const QVariant &value, int role)
0236 {
0237     const int row = index.row();
0238     const int column = index.column();
0239 
0240     if (!c_applets.rowExists(row) || column<0 || column > NAMECOLUMN) {
0241         return false;
0242     }
0243 
0244     //! specific roles to each independent cell
0245     switch (column) {
0246     case NAMECOLUMN:
0247         if (role == Qt::CheckStateRole) {
0248             c_applets[row].isSelected = (value.toInt() > 0 ? true : false);
0249             emit appletsDataChanged();
0250             return true;
0251         }
0252         break;
0253     };
0254 
0255     return false;
0256 }
0257 
0258 QVariant Applets::data(const QModelIndex &index, int role) const
0259 {
0260     const int row = index.row();
0261     int column = index.column();
0262 
0263     if (row >= rowCount()) {
0264         return QVariant{};
0265     }
0266 
0267     if (role == NAMEROLE || role == Qt::DisplayRole) {
0268         return c_applets[row].name;
0269     } else if (role == Qt::CheckStateRole) {
0270         return (c_applets[row].isSelected ? Qt::Checked : Qt::Unchecked);
0271     } else if (role== Qt::DecorationRole) {
0272         return QIcon::fromTheme(c_applets[row].icon);
0273     } else if (role == IDROLE) {
0274             return c_applets[row].id;
0275     } else if (role == SELECTEDROLE) {
0276         return c_applets[row].isSelected;
0277     } else if (role == ICONROLE) {
0278         return c_applets[row].icon;
0279     } else if (role == DESCRIPTIONROLE) {
0280         return c_applets[row].description;
0281     } else if (role == SORTINGROLE) {
0282         return c_applets[row].isInstalled() ? QString::number(1000) + c_applets[row].name : QString::number(0000) + c_applets[row].name;
0283     }
0284 
0285     return QVariant{};
0286 }
0287 
0288 }
0289 }
0290 }