size_t ModFlagIconDelegate::getNumIcons(const QModelIndex &index) const
{
  unsigned int modIdx = index.data(Qt::UserRole + 1).toInt();
  if (modIdx < ModInfo::getNumMods()) {
    ModInfo::Ptr info = ModInfo::getByIndex(modIdx);
    std::vector<ModInfo::EFlag> flags = info->getFlags();
    int count = flags.size();
    if (std::find_first_of(flags.begin(), flags.end(), m_ConflictFlags, m_ConflictFlags + 4) == flags.end()) {
      ++count;
    }
    return count;
  } else {
    return 0;
  }
}
bool ModListSortProxy::filterMatchesModOr(ModInfo::Ptr info, bool enabled) const
{
  for (auto iter = m_CategoryFilter.begin(); iter != m_CategoryFilter.end(); ++iter) {
    switch (*iter) {
      case CategoryFactory::CATEGORY_SPECIAL_CHECKED: {
        if (enabled || info->alwaysEnabled()) return true;
      } break;
      case CategoryFactory::CATEGORY_SPECIAL_UNCHECKED: {
        if (!enabled && !info->alwaysEnabled()) return true;
      } break;
      case CategoryFactory::CATEGORY_SPECIAL_UPDATEAVAILABLE: {
        if (info->updateAvailable() || info->downgradeAvailable()) return true;
      } break;
      case CategoryFactory::CATEGORY_SPECIAL_NOCATEGORY: {
        if (info->getCategories().size() == 0) return true;
      } break;
      case CategoryFactory::CATEGORY_SPECIAL_CONFLICT: {
        if (hasConflictFlag(info->getFlags())) return true;
      } break;
      case CategoryFactory::CATEGORY_SPECIAL_NOTENDORSED: {
        ModInfo::EEndorsedState state = info->endorsedState();
        if ((state == ModInfo::ENDORSED_FALSE) || (state == ModInfo::ENDORSED_NEVER)) return true;
      } break;
      case CategoryFactory::CATEGORY_SPECIAL_MANAGED: {
        if (!info->hasFlag(ModInfo::FLAG_FOREIGN)) return true;
      } break;
      case CategoryFactory::CATEGORY_SPECIAL_UNMANAGED: {
        if (info->hasFlag(ModInfo::FLAG_FOREIGN)) return true;
      } break;
      default: {
        if (info->categorySet(*iter)) return true;
      } break;
    }
  }

  foreach (int content, m_ContentFilter) {
    if (info->hasContent(static_cast<ModInfo::EContent>(content))) return true;
  }

  return false;
}
Пример #3
0
void Profile::writeModlistNow()
{
  if (!m_Directory.exists()) return;

  try {
    QString fileName = getModlistFileName();
    SafeWriteFile file(fileName);

    file->write(QString("# This file was automatically generated by Mod Organizer.\r\n").toUtf8());
    if (m_ModStatus.empty()) {
      return;
    }

    for (int i = m_ModStatus.size() - 1; i >= 0; --i) {
      // the priority order was inverted on load so it has to be inverted again
      unsigned int index = m_ModIndexByPriority[i];
      if (index != UINT_MAX) {
        ModInfo::Ptr modInfo = ModInfo::getByIndex(index);
        std::vector<ModInfo::EFlag> flags = modInfo->getFlags();
        if ((modInfo->getFixedPriority() == INT_MIN)) {
          if (std::find(flags.begin(), flags.end(), ModInfo::FLAG_FOREIGN) != flags.end()) {
            file->write("*");
          } else if (m_ModStatus[index].m_Enabled) {
            file->write("+");
          } else {
            file->write("-");
          }
          file->write(modInfo->name().toUtf8());
          file->write("\r\n");
        }
      }
    }

    if (file.commitIfDifferent(m_LastModlistHash)) {
      qDebug("%s saved", QDir::toNativeSeparators(fileName).toUtf8().constData());
    }
  } catch (const std::exception &e) {
    reportError(tr("failed to write mod list: %1").arg(e.what()));
    return;
  }
}
QList<QString> ModFlagIconDelegate::getIcons(const QModelIndex &index) const
{
  QList<QString> result;
  QVariant modid = index.data(Qt::UserRole + 1);
  if (modid.isValid()) {
    ModInfo::Ptr info = ModInfo::getByIndex(modid.toInt());
    std::vector<ModInfo::EFlag> flags = info->getFlags();

    { // insert conflict icon first to provide nicer alignment
      auto iter = std::find_first_of(flags.begin(), flags.end(), m_ConflictFlags, m_ConflictFlags + 4);
      if (iter != flags.end()) {
        result.append(getFlagIcon(*iter));
        flags.erase(iter);
      } else {
        result.append(QString());
      }
    }

    for (auto iter = flags.begin(); iter != flags.end(); ++iter) {
      result.append(getFlagIcon(*iter));
    }
  }
  return result;
}
bool ModListSortProxy::lessThan(const QModelIndex &left,
                                const QModelIndex &right) const
{
  if (sourceModel()->hasChildren(left) || sourceModel()->hasChildren(right)) {
    return QSortFilterProxyModel::lessThan(left, right);
  }

  bool lOk, rOk;
  int leftIndex  = left.data(Qt::UserRole + 1).toInt(&lOk);
  int rightIndex = right.data(Qt::UserRole + 1).toInt(&rOk);
  if (!lOk || !rOk) {
    return false;
  }

  ModInfo::Ptr leftMod = ModInfo::getByIndex(leftIndex);
  ModInfo::Ptr rightMod = ModInfo::getByIndex(rightIndex);

  bool lt = false;

  {
    QModelIndex leftPrioIdx = left.sibling(left.row(), ModList::COL_PRIORITY);
    QVariant leftPrio = leftPrioIdx.data();
    if (!leftPrio.isValid()) leftPrio = left.data(Qt::UserRole);
    QModelIndex rightPrioIdx = right.sibling(right.row(), ModList::COL_PRIORITY);
    QVariant rightPrio = rightPrioIdx.data();
    if (!rightPrio.isValid()) rightPrio = right.data(Qt::UserRole);

    lt = leftPrio.toInt() < rightPrio.toInt();
  }

  switch (left.column()) {
    case ModList::COL_FLAGS: {
      std::vector<ModInfo::EFlag> leftFlags = leftMod->getFlags();
      std::vector<ModInfo::EFlag> rightFlags = rightMod->getFlags();
      if (leftFlags.size() != rightFlags.size()) {
        lt = leftFlags.size() < rightFlags.size();
      } else {
        lt = flagsId(leftFlags) < flagsId(rightFlags);
      }
    } break;
    case ModList::COL_CONTENT: {
      std::vector<ModInfo::EContent> lContent = leftMod->getContents();
      std::vector<ModInfo::EContent> rContent = rightMod->getContents();
      if (lContent.size() != rContent.size()) {
        lt = lContent.size() < rContent.size();
      }

      int lValue = 0;
      int rValue = 0;
      for (ModInfo::EContent content : lContent) {
        lValue += 2 << (unsigned int)content;
      }
      for (ModInfo::EContent content : rContent) {
        rValue += 2 << (unsigned int)content;
      }

      lt = lValue < rValue;
    } break;
    case ModList::COL_NAME: {
      int comp = QString::compare(leftMod->name(), rightMod->name(), Qt::CaseInsensitive);
      if (comp != 0)
        lt = comp < 0;
    } break;
    case ModList::COL_CATEGORY: {
      if (leftMod->getPrimaryCategory() != rightMod->getPrimaryCategory()) {
        if (leftMod->getPrimaryCategory() < 0) lt = false;
        else if (rightMod->getPrimaryCategory() < 0) lt = true;
        else {
          try {
            CategoryFactory &categories = CategoryFactory::instance();
            QString leftCatName = categories.getCategoryName(categories.getCategoryIndex(leftMod->getPrimaryCategory()));
            QString rightCatName = categories.getCategoryName(categories.getCategoryIndex(rightMod->getPrimaryCategory()));
            lt = leftCatName < rightCatName;
          } catch (const std::exception &e) {
            qCritical("failed to compare categories: %s", e.what());
          }
        }
      }
    } break;
    case ModList::COL_MODID: {
      if (leftMod->getNexusID() != rightMod->getNexusID())
        lt = leftMod->getNexusID() < rightMod->getNexusID();
    } break;
    case ModList::COL_VERSION: {
      if (leftMod->getVersion() != rightMod->getVersion())
        lt = leftMod->getVersion() < rightMod->getVersion();
    } break;
    case ModList::COL_INSTALLTIME: {
      QDateTime leftTime = left.data().toDateTime();
      QDateTime rightTime = right.data().toDateTime();
      if (leftTime != rightTime)
        return leftTime < rightTime;
    } break;
    case ModList::COL_PRIORITY: {
      // nop, already compared by priority
    } break;
  }
  return lt;
}