summaryrefslogtreecommitdiff
path: root/src/filetree.cpp
blob: cfc73de09f6063e5ba18099518c04d12e0ce3fca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "filetree.h"
#include "organizercore.h"

using namespace MOShared;

// in mainwindow.cpp
QString UnmanagedModName();


FileTreeItem::FileTreeItem()
  : m_flags(NoFlags), m_loaded(false)
{
}

FileTreeItem::FileTreeItem(
  FileTreeItem* parent,
  std::wstring virtualParentPath, std::wstring realPath, Flags flags,
  std::wstring file, std::wstring mod) :
    m_parent(parent),
    m_virtualParentPath(QString::fromStdWString(virtualParentPath)),
    m_realPath(QString::fromStdWString(realPath)),
    m_flags(flags),
    m_file(QString::fromStdWString(file)),
    m_mod(QString::fromStdWString(mod)),
    m_loaded(false)
{
}

void FileTreeItem::add(std::unique_ptr<FileTreeItem> child)
{
  m_children.push_back(std::move(child));
}

const std::vector<std::unique_ptr<FileTreeItem>>& FileTreeItem::children() const
{
  return m_children;
}

FileTreeItem* FileTreeItem::parent()
{
  return m_parent;
}

const QString& FileTreeItem::filename() const
{
  return m_file;
}

const QString& FileTreeItem::mod() const
{
  return m_mod;
}

bool FileTreeItem::isFromArchive() const
{
  return (m_flags & FromArchive);
}

bool FileTreeItem::isHidden() const
{
  return m_file.endsWith(ModInfo::s_HiddenExt);
}

bool FileTreeItem::isConflicted() const
{
  return (m_flags & Conflicted);
}

QFileIconProvider::IconType FileTreeItem::icon() const
{
  if (m_flags & Directory) {
    return QFileIconProvider::Folder;
  } else {
    return QFileIconProvider::File;
  }
}

void FileTreeItem::setLoaded(bool b)
{
  m_loaded = b;
}



FileTreeModel::FileTreeModel(OrganizerCore& core, QObject* parent)
  : QAbstractItemModel(parent), m_core(core), m_flags(NoFlags)
{
  QFileIconProvider provider;
  m_fileIcon = provider.icon(QFileIconProvider::File);
  m_directoryIcon = provider.icon(QFileIconProvider::Folder);
}

void FileTreeModel::setFlags(Flags f)
{
  m_flags = f;
}

bool FileTreeModel::showConflicts() const
{
  return (m_flags & Conflicts);
}

bool FileTreeModel::showArchives() const
{
  return (m_flags & Archives);
}

void FileTreeModel::refresh()
{
  m_root = {nullptr, L"", L"", FileTreeItem::Directory, L"Data", L""};

  fill(m_root, *m_core.directoryStructure(), L"");
  m_root.setLoaded(true);
}

void FileTreeModel::fill(
  FileTreeItem& parentItem, const MOShared::DirectoryEntry& parentEntry,
  const std::wstring& parentPath)
{
  const std::wstring path = parentPath + L"\\" + parentEntry.getName();
  bool isDirectory = true;


  {
    std::vector<DirectoryEntry*>::const_iterator begin, end;
    parentEntry.getSubDirectories(begin, end);
    fillDirectories(parentItem, path, begin, end);
  }

  {
    fillFiles(parentItem, path, parentEntry.getFiles());
  }
}

void FileTreeModel::fillDirectories(
  FileTreeItem& parentItem, const std::wstring& path,
  DirectoryIterator begin, DirectoryIterator end)
{
  const bool isDirectory = true;

  for (auto itor=begin; itor!=end; ++itor) {
    const auto& dir = **itor;

    auto child = std::make_unique<FileTreeItem>(
      &parentItem, path, L"", FileTreeItem::Directory, dir.getName(), L"");

    if (dir.isEmpty()) {
      child->setLoaded(true);
    } else if (showConflicts() || !showArchives()) {
      fill(*child, dir, path);
    }

    parentItem.add(std::move(child));
  }
}

void FileTreeModel::fillFiles(
  FileTreeItem& parentItem, const std::wstring& path,
  const std::vector<FileEntry::Ptr>& files)
{
  const bool isDirectory = false;

  for (auto&& file : files) {
    if (showConflicts() && (file->getAlternatives().size() == 0)) {
      continue;
    }

    bool isArchive = false;
    int originID = file->getOrigin(isArchive);
    if (!showArchives() && isArchive) {
      continue;
    }

    FileTreeItem::Flags flags = FileTreeItem::NoFlags;

    if (isArchive) {
      flags |= FileTreeItem::FromArchive;
    }

    if (!file->getAlternatives().empty()) {
      flags |= FileTreeItem::Conflicted;
    }

    parentItem.add(std::make_unique<FileTreeItem>(
      &parentItem, path, file->getFullPath(), flags, file->getName(),
      makeModName(*file, originID)));
  }
}

std::wstring FileTreeModel::makeModName(const FileEntry& file, int originID) const
{
  const auto origin = m_core.directoryStructure()->getOriginByID(originID);
  return origin.getName();

  //const auto index = ModInfo::getIndex(QString::fromStdWString(origin.getName()));
  //if (index == UINT_MAX) {
  //  return UnmanagedModName();
  //}
  //
  //std::wstring name = ModInfo::getByIndex(index)->name();
  //
  //std::pair<std::wstring, int> archive = file.getArchive();
  //if (archive.first.length() != 0) {
  //  name += L" (" + archive.first + L")";
  //}
  //
  //return name;
}

FileTreeItem* FileTreeModel::itemFromIndex(const QModelIndex& index) const
{
  auto* data = index.internalPointer();
  if (!data) {
    return nullptr;
  }

  return static_cast<FileTreeItem*>(data);
}

QModelIndex FileTreeModel::index(int row, int col, const QModelIndex& parentIndex) const
{
  FileTreeItem* parent = nullptr;

  if (!parentIndex.isValid()) {
    parent = &m_root;
  } else {
    parent = itemFromIndex(parentIndex);
  }

  if (!parent) {
    return {};
  }

  if (static_cast<std::size_t>(row) >= parent->children().size()) {
    return {};
  }

  if (col >= columnCount({})) {
    return {};
  }

  auto* item = parent->children()[static_cast<std::size_t>(row)].get();
  return createIndex(row, col, item);
}

QModelIndex FileTreeModel::parent(const QModelIndex& index) const
{
  if (!index.isValid()) {
    return {};
  }

  auto* item = itemFromIndex(index);
  if (!item) {
    return {};
  }

  auto* parent = item->parent();
  if (!parent) {
    return {};
  }

  int row = 0;
  for (auto&& child : parent->children()) {
    if (child.get() == item) {
      return createIndex(row, 0, parent);
    }

    ++row;
  }

  return {};
}

int FileTreeModel::rowCount(const QModelIndex& parent) const
{
  if (!parent.isValid()) {
    return static_cast<int>(m_root.children().size());
  } else {
    if (auto* item=itemFromIndex(parent)) {
      return static_cast<int>(item->children().size());
    }
  }

  return 0;
}

int FileTreeModel::columnCount(const QModelIndex&) const
{
  return 2;
}

QVariant FileTreeModel::data(const QModelIndex& index, int role) const
{
  switch (role)
  {
    case Qt::DisplayRole:
    {
      if (auto* item=itemFromIndex(index)) {
        if (index.column() == 0) {
          return item->filename();
        } else if (index.column() == 1) {
          return item->mod();
        }
      }

      break;
    }

    case Qt::FontRole:
    {
      if (auto* item=itemFromIndex(index)) {
        QFont f;

        if (item->isFromArchive()) {
          f.setItalic(true);
        } else if (item->isHidden()) {
          f.setStrikeOut(true);
        }

        return f;
      }

      break;
    }

    case Qt::ToolTipRole:
    {
      /*
        const auto alternatives = file->getAlternatives();

        if (!alternatives.empty()) {
          std::wostringstream altString;
          altString << tr("Also in: <br>").toStdWString();
          for (std::vector<std::pair<int, std::pair<std::wstring, int>>>::iterator altIter = alternatives.begin();
            altIter != alternatives.end(); ++altIter) {
            if (altIter != alternatives.begin()) {
              altString << " , ";
            }
            altString << "<span style=\"white-space: nowrap;\"><i>" << m_core.directoryStructure()->getOriginByID(altIter->first).getName() << "</font></span>";
          }
          fileChild->setToolTip(1, QString("%1").arg(QString::fromStdWString(altString.str())));
          fileChild->setForeground(1, QBrush(Qt::red));
        } else {
          fileChild->setToolTip(1, tr("No conflict"));
        }
      */

      break;
    }

    case Qt::ForegroundRole:
    {
      if (index.column() == 1) {
        if (auto* item=itemFromIndex(index)) {
          if (item->isConflicted()) {
            return QBrush(Qt::red);
          }
        }
      }

      break;
    }

    case Qt::DecorationRole:
    {
      if (index.column() == 0) {
        if (auto* item=itemFromIndex(index)) {
          const auto iconType = item->icon();

          if (iconType == QFileIconProvider::File) {
            return m_fileIcon;
          } else if (iconType == QFileIconProvider::Folder) {
            return m_directoryIcon;
          }
        }
      }

      break;
    }
  }

  return {};
}

QVariant FileTreeModel::headerData(int i, Qt::Orientation ori, int role) const
{
  if (role == Qt::DisplayRole) {
    if (i == 0) {
      return tr("File");
    } else if (i == 1) {
      return tr("Mod");
    }
  }

  return {};
}