QTfrontend/model/ThemeModel.cpp
branchios-develop
changeset 13418 ba39a1d396c0
parent 13272 48b23e6f26d9
child 14833 8ed0c3761640
equal deleted inserted replaced
13416:6e8b807bda4b 13418:ba39a1d396c0
    31     m_data = QList<QMap<int, QVariant> >();
    31     m_data = QList<QMap<int, QVariant> >();
    32 
    32 
    33     m_themesLoaded = false;
    33     m_themesLoaded = false;
    34 
    34 
    35     m_filteredNoDLC = NULL;
    35     m_filteredNoDLC = NULL;
       
    36     m_filteredNoHidden = NULL;
       
    37     m_filteredNoDLCOrHidden = NULL;
    36 }
    38 }
    37 
    39 
    38 QSortFilterProxyModel * ThemeModel::withoutDLC()
    40 // Filters out DLC themes, e.g. themes which do not come by default
       
    41 ThemeFilterProxyModel * ThemeModel::withoutDLC()
    39 {
    42 {
    40     if (m_filteredNoDLC == NULL)
    43     if (m_filteredNoDLC == NULL)
    41     {
    44     {
    42         m_filteredNoDLC = new QSortFilterProxyModel(this);
    45         m_filteredNoDLC = new ThemeFilterProxyModel(this);
    43         m_filteredNoDLC->setSourceModel(this);
    46         m_filteredNoDLC->setSourceModel(this);
    44         // filtering based on IsDlcRole would be nicer
    47         m_filteredNoDLC->setFilterDLC(true);
    45         // but seems this model can only do string-based filtering :|
       
    46         m_filteredNoDLC->setFilterRegExp(QRegExp("^[^*]"));
       
    47     }
    48     }
    48     return m_filteredNoDLC;
    49     return m_filteredNoDLC;
       
    50 }
       
    51 
       
    52 // Filters out hidden themes, these are themes which are not supposed to be
       
    53 // seen by the user.
       
    54 ThemeFilterProxyModel * ThemeModel::withoutHidden()
       
    55 {
       
    56     if (m_filteredNoHidden == NULL)
       
    57     {
       
    58         m_filteredNoHidden = new ThemeFilterProxyModel(this);
       
    59         m_filteredNoHidden->setSourceModel(this);
       
    60         m_filteredNoHidden->setFilterHidden(true);
       
    61     }
       
    62     return m_filteredNoHidden;
       
    63 }
       
    64 
       
    65 // Combination of the two above for convenience
       
    66 ThemeFilterProxyModel * ThemeModel::withoutDLCOrHidden()
       
    67 {
       
    68     if (m_filteredNoDLCOrHidden == NULL)
       
    69     {
       
    70         m_filteredNoDLCOrHidden = new ThemeFilterProxyModel(this);
       
    71         m_filteredNoDLCOrHidden->setSourceModel(this);
       
    72         m_filteredNoDLCOrHidden->setFilterDLC(true);
       
    73         m_filteredNoDLCOrHidden->setFilterHidden(true);
       
    74     }
       
    75     return m_filteredNoDLCOrHidden;
    49 }
    76 }
    50 
    77 
    51 int ThemeModel::rowCount(const QModelIndex &parent) const
    78 int ThemeModel::rowCount(const QModelIndex &parent) const
    52 {
    79 {
    53     if(parent.isValid())
    80     if(parent.isValid())
    93     m_data.reserve(themes.size());
   120     m_data.reserve(themes.size());
    94 #endif
   121 #endif
    95 
   122 
    96     foreach (QString theme, themes)
   123     foreach (QString theme, themes)
    97     {
   124     {
       
   125         QMap<int, QVariant> dataset;
       
   126 
    98         // themes without icon are supposed to be hidden
   127         // themes without icon are supposed to be hidden
    99         QString iconpath = QString("physfs://Themes/%1/icon.png").arg(theme);
   128         QString iconpath = QString("physfs://Themes/%1/icon.png").arg(theme);
   100 
   129 
   101         if (!QFile::exists(iconpath))
   130         if (!QFile::exists(iconpath))
   102             continue;
   131         {
   103 
   132             dataset.insert(IsHiddenRole, true);
   104         QMap<int, QVariant> dataset;
   133         }
       
   134         else
       
   135         {
       
   136             // themes with the key “hidden” in theme.cfg are hidden, too
       
   137             QFile themeCfgFile(QString("physfs://Themes/%1/theme.cfg").arg(theme));
       
   138             if (themeCfgFile.open(QFile::ReadOnly))
       
   139             {
       
   140                 QTextStream stream(&themeCfgFile);
       
   141                 QString line = stream.readLine();
       
   142                 QString key;
       
   143                 while (!line.isNull())
       
   144                 {
       
   145                     key = QString(line);
       
   146                     int equalsPos = line.indexOf('=');
       
   147                     key.truncate(equalsPos - 1);
       
   148                     key = key.simplified();
       
   149                     if (!line.startsWith(';') && key == "hidden")
       
   150                     {
       
   151                         dataset.insert(IsHiddenRole, true);
       
   152                         break;
       
   153                     }
       
   154                     line = stream.readLine();
       
   155                 }
       
   156             }
       
   157         }
   105 
   158 
   106         // detect if theme is dlc
   159         // detect if theme is dlc
   107         QString themeDir = PHYSFS_getRealDir(QString("Themes/%1/icon.png").arg(theme).toLocal8Bit().data());
   160         QString themeDir = PHYSFS_getRealDir(QString("Themes/%1").arg(theme).toLocal8Bit().data());
   108         bool isDLC = !themeDir.startsWith(datadir->absolutePath());
   161         bool isDLC = !themeDir.startsWith(datadir->absolutePath());
   109         dataset.insert(IsDlcRole, isDLC);
   162         dataset.insert(IsDlcRole, isDLC);
   110 
   163 
   111         // set icon path
   164         // set icon path
   112         dataset.insert(IconPathRole, iconpath);
   165         dataset.insert(IconPathRole, iconpath);
   116 
   169 
   117         // set displayed name
   170         // set displayed name
   118         dataset.insert(Qt::DisplayRole, (isDLC ? "*" : "") + theme);
   171         dataset.insert(Qt::DisplayRole, (isDLC ? "*" : "") + theme);
   119 
   172 
   120         // load and set preview icon
   173         // load and set preview icon
   121         QIcon preview(QString("physfs://Themes/%1/icon@2x.png").arg(theme));
   174         iconpath = QString("physfs://Themes/%1/icon@2x.png").arg(theme);
   122         dataset.insert(Qt::DecorationRole, preview);
   175         if (QFile::exists(iconpath))
       
   176         {
       
   177             QIcon preview(QString("physfs://Themes/%1/icon@2x.png").arg(theme));
       
   178             dataset.insert(Qt::DecorationRole, preview);
       
   179         }
   123 
   180 
   124         m_data.append(dataset);
   181         m_data.append(dataset);
   125     }
   182     }
   126 }
   183 }