Advertisement
Guest User

fileslistmodel.cpp

a guest
May 4th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include "fileslistmodel.h"
  2. #include <qjson/serializer.h>
  3.  
  4. #include <QtGui/QGuiApplication>
  5. #include <QtGui/QColor>
  6. #include <QtGui/QPalette>
  7. #include <cutecat/core/macros.h>
  8.  
  9. FilesListModel::FilesListModel(ConfigArchsSet set): QAbstractItemModel()
  10. {
  11.     foreach (QString arch, set.keys()) {
  12.         _data << FLNodeGroup(arch, QList<FLNode>());
  13.         foreach (QString target, set[arch].keys()) {
  14.             ConfigFileEntry entry = set[arch][target];
  15.             _data.last().nodes << FLNode(target, entry.symlinking, entry.path);
  16.         }
  17.     }
  18. }
  19.  
  20. ConfigArchsSet FilesListModel::configArchsSet()
  21. {
  22.     ConfigArchsSet set;
  23.     foreach (FLNodeGroup group, _data) {
  24.         set[group.arch] = ConfigFilesSet();
  25.         foreach (FLNode node, group.nodes)
  26.             set[group.arch][node.target] = ConfigFileEntry(node.source, node.symlinking);
  27.     }
  28.     return set;
  29. }
  30.  
  31. QModelIndex FilesListModel::index(int row, int column, const QModelIndex& parent) const
  32. {
  33.     if (!hasIndex(row, column, parent))
  34.         return QModelIndex();
  35.    
  36.     if (!parent.isValid()) {
  37.         return createIndex(row, column, new FLNodePointer(row));
  38.     } else {
  39.         return createIndex(row, column, new FLNodePointer(parent.row(), row));
  40.     }
  41. }
  42.  
  43. QModelIndex FilesListModel::parent(const QModelIndex& child) const
  44. {
  45.     auto ptr = (FLNodePointer*) child.internalPointer();
  46.     if (ptr->fileIndex == -1)
  47.         return QModelIndex();
  48.     else {
  49.         return createIndex(ptr->archIndex, 0, new FLNodePointer(ptr->archIndex, -1));
  50.     }
  51. }
  52.  
  53. int FilesListModel::rowCount(const QModelIndex& parent) const
  54. {
  55.     if (!parent.isValid())
  56.         return _data.count();
  57.    
  58.     auto ptr = (FLNodePointer*) parent.internalPointer();
  59.     if (ptr->fileIndex == -1) {
  60.         return _data[parent.row()].nodes.count();
  61.     } else {
  62.         return 0;
  63.     }
  64. }
  65.  
  66. int FilesListModel::columnCount(const QModelIndex& parent) const
  67. {
  68.     return COLUMNS_COUNT;
  69. }
  70.  
  71. QVariant FilesListModel::data(const QModelIndex& index, int role) const
  72. {
  73.     auto ptr = (FLNodePointer*) index.internalPointer();
  74.     if (ptr->fileIndex == -1) {
  75.        
  76.         if (index.column() == COLUMN_TARGET) {
  77.             switch (role) {
  78.                 case Qt::DisplayRole:
  79.                     return _data[ptr->archIndex].arch;
  80.                 default:
  81.                     return QVariant();
  82.             }
  83.         } else {
  84.             return QVariant();
  85.         }
  86.        
  87.     } else {
  88.        
  89.         if (index.column() == COLUMN_TARGET) {
  90.             switch (role) {
  91.                 case Qt::DisplayRole:
  92.                     return _data[ptr->archIndex].nodes[ptr->fileIndex].target;
  93.                 default:
  94.                     return QVariant();
  95.             }
  96.         } else if (index.column() == COLUMN_SOURCE) {
  97.             switch (role) {
  98.                 case Qt::DisplayRole: {
  99.                     FLNode node = _data[ptr->archIndex].nodes[ptr->fileIndex];
  100.                     return (node.symlinking ? "-> " : "= ") + node.source;
  101.                 }
  102.                 case Qt::ForegroundRole:
  103.                     return QGuiApplication::palette().color(QPalette::Disabled, QPalette::WindowText);
  104.                 default:
  105.                     return QVariant();
  106.             }
  107.         } else {
  108.             return QVariant();
  109.         }
  110.     }
  111. }
  112.  
  113. // bool FilesListModel::removeRows(int row, int count, const QModelIndex& parent)
  114. // {
  115. //     if (!parent.isValid())
  116. //      for (int i=0; i<count; i++)
  117. //          _data.removeAt(row+i);
  118. //  else
  119. //      for (int i=0; i<count; i++)
  120. //          _data[parent.row()].nodes.removeAt(row+i);
  121. //  return true;
  122. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement