void EventManager::AddListener(EventListener* listener) { if(mListenersLocked) { Logger::Get().Debug("EventManager: Cannot add listener. List locked. Queued listener for adding."); mListenerAddQueue.push_back(listener); return; } if(!HasListener(listener)) { if(listener == nullptr) { Logger::Get().Error("EventManager: Could not add listener. It is NULL."); } else { mListeners.push_back(listener); UpdatePriorities(); } } else { Logger::Get().Error("EventManager: Could not add listener - already registered."); } }
void TorrentFilesModel::ResetFiles (const boost::filesystem::path& basePath, const QList<FileInfo>& infos) { Clear (); BasePath_ = basePath; beginInsertRows ({}, 0, 0); FilesInTorrent_ = infos.size (); Path2Node_ [{}] = RootNode_; const auto& inst = Util::ExtensionsData::Instance (); for (int i = 0; i < infos.size (); ++i) { const auto& fi = infos.at (i); const auto& parentItem = MkParentIfDoesntExist (fi.Path_); const auto& filename = #ifdef Q_OS_WIN32 QString::fromUtf16 (reinterpret_cast<const ushort*> (fi.Path_.leaf ().c_str ())); #else QString::fromUtf8 (fi.Path_.leaf ().c_str ()); #endif const auto item = parentItem->AppendChild (parentItem); item->Name_ = filename; item->ParentPath_ = fi.Path_.branch_path (); item->Priority_ = fi.Priority_; item->FileIndex_ = i; item->SubtreeSize_ = fi.Size_; item->Progress_ = fi.Progress_; item->Icon_ = inst.GetExtIcon (filename.section ('.', -1)); Path2Node_ [fi.Path_] = item; UpdatePriorities (item.get ()); } UpdateSizeGraph (RootNode_); endInsertRows (); }
void TorrentFilesModel::UpdatePriorities (TorrentNodeInfo *node) { const auto& parent = node->GetParent (); if (node == RootNode_.get () || parent == RootNode_) return; const auto prio = node->Priority_; const bool allSame = std::all_of (parent->begin (), parent->end (), [prio] (const TorrentNodeInfo_ptr& child) { return child->Priority_ == prio; }); const auto newPrio = allSame ? prio : -1; if (newPrio == parent->Priority_) return; parent->Priority_ = newPrio; const auto& idx = IndexForNode (parent, ColumnPriority); emit dataChanged (idx.sibling (idx.row (), ColumnPath), idx); UpdatePriorities (parent.get ()); }
bool TorrentFilesModel::setData (const QModelIndex& index, const QVariant& value, int role) { if (!index.isValid ()) return false; const auto node = static_cast<TorrentNodeInfo*> (index.internalPointer ()); switch (index.column ()) { case ColumnPriority: if (const auto rc = rowCount (index)) for (int i = 0; i < rc; ++i) setData (this->index (i, index.column (), index), value, role); else { const auto newPriority = value.toInt (); Core::Instance ()->SetFilePriority (node->FileIndex_, newPriority, Index_); node->Priority_ = newPriority; emit dataChanged (index.sibling (index.row (), ColumnPath), index); UpdatePriorities (node); } return true; case ColumnPath: { switch (role) { case Qt::EditRole: { auto newPath = value.toString (); const auto& curPath = node->GetFullPathStr (); if (curPath.contains ('/') && !newPath.contains ('/')) { auto newCurPath = curPath; const auto lastIdx = newCurPath.lastIndexOf ('/'); newPath = newCurPath.left (lastIdx + 1) + newPath; } if (!node->IsEmpty ()) { const auto curPathSize = curPath.size (); std::function<void (TorrentNodeInfo*)> setter = [this, &setter, &newPath, curPathSize] (TorrentNodeInfo *node) { if (node->IsEmpty ()) { auto specificPath = node->GetFullPathStr (); specificPath.replace (0, curPathSize, newPath); Core::Instance ()->SetFilename (node->FileIndex_, specificPath, Index_); } else for (const auto& subnode : *node) setter (subnode.get ()); }; setter (node); } else Core::Instance ()->SetFilename (node->FileIndex_, newPath, Index_); return true; } case Qt::CheckStateRole: return setData (index.sibling (index.row (), ColumnPriority), value.toInt () == Qt::Checked ? 1 : 0, Qt::EditRole); } } } return false; }