// Find all PackCreationQueue XML files inside a directory // and create model branches bool screenPath(const QString &absPath) { // Path already screened in model? if (_screenedAbsPath.contains(absPath)) return true; _screenedAbsPath.append(absPath); // Find all packcreationqueue-xmlfiles (recursively) QFileInfoList files = Utils::getFiles(QDir(absPath), QString(Constants::PACKCREATIONQUEUE_DEFAULT_FILENAME)); // Add all packcreationqueue to the model foreach(const QFileInfo &info, files) { // Read the Queue file PackCreationQueue queue; if (!queue.fromXmlFile(info.absoluteFilePath())) { LOG_ERROR_FOR(q, tkTr(Trans::Constants::FILE_1_ISNOT_READABLE).arg(info.absoluteFilePath())); continue; } // Create the queue branch if (!packCreationQueueToItem(queue)) { LOG_ERROR_FOR(q, QString("Unable to create the queue branch: %1").arg(info.absoluteFilePath())); continue; } _queues << queue; }
// Will create a set of queues and packs inside the object _tempPath void test_createFakeSetOfQueue() { using namespace DataPack; // Create multiple subpath with multiple Queue/Pack files QList<PackCreationQueue *> queues; for(int i=0; i < loop; ++i) { PackCreationQueue *queue = new PackCreationQueue; QString path = QString("%1/Q_%2").arg(_tempPath).arg(Utils::createUid()); createFakeQueue(path, *queue); queues.append(queue); } // Create Model populate it directly with queues // Test: number of available packs in models PackCreationModel globalModel(this); for(int i=0; i < queues.count(); ++i) { PackCreationQueue *queue = queues.at(i); // Create a new model and test the number of packs // Test: When we add a new queue, all Packs must be checked PackCreationModel model(this); QVERIFY(model.addPackCreationQueue(*queue) == true); QVERIFY(model.getCheckedPacks().count() == loop); // Test the global model QVERIFY(globalModel.addPackCreationQueue(*queue) == true); // Check globalModel (incrising the total row count of packs) QVERIFY(globalModel.getCheckedPacks().count() == (loop * (i+1))); } // Test: clearModel & addQueue for(int i=0; i < queues.count(); ++i) { // WARNING: using clear() expose to segfaulting globalModel.clearPackModel(); QVERIFY(globalModel.getCheckedPacks().isEmpty() == true); PackCreationQueue *queue = queues.at(i); QVERIFY(globalModel.addPackCreationQueue(*queue) == true); QVERIFY(globalModel.getCheckedPacks().count() == loop); } // All queues are saved in the _tempPath // Test: addScreeningPath (queue by queue) globalModel.clearPackModel(); for(int i=0; i < queues.count(); ++i) { PackCreationQueue *queue = queues.at(i); PackCreationModel model(this); QVERIFY(model.addScreeningPath(QFileInfo(queue->sourceAbsolutePathFile()).absolutePath()) == true); QVERIFY(model.getCheckedPacks().count() == loop); // Check globalModel (incrising the total row count of packs) QVERIFY(globalModel.addScreeningPath(QFileInfo(queue->sourceAbsolutePathFile()).absolutePath()) == true); QVERIFY(globalModel.getCheckedPacks().count() == (loop * (i+1))); } // Test: Qt::CheckStateRole & getCheckedPacks // for(int i = 0; i < globalModel.rowCount(); ++i) { // // In each branch, get the latest checkable index == Pack // QModelIndex root = globalModel.index(i,0); // } }
// Insert a queue to the model void insertPackCreationQueueInCache(const PackCreationQueue &queue, QStandardItem *item) { _insertedPackCreationQueueUids.insert(queue.uid(), item); }
// Insert a queue content to the model bool packCreationQueueToItem(const PackCreationQueue &queue) { // Already inserted? if (isPackCreationQueueAlreadyInserted(queue)) return true; QFont bold; bold.setBold(true); // Create the root item for this queue QStandardItem *rootItem = 0; if (_format == PackCreationModel::ShowByServer) { // nothing to do } else if (_format == PackCreationModel::ShowByQueue) { rootItem = new QStandardItem(tkTr(Trans::Constants::_1_COLON_2) .arg(tkTr(Trans::Constants::QUEUE)) .arg(queue.sourceAbsolutePathFile())); rootItem->setToolTip(queue.sourceAbsolutePathFile()); rootItem->setFont(bold); rootItem->setCheckable(true); rootItem->setCheckState(Qt::Checked); q->invisibleRootItem()->appendRow(rootItem); insertPackCreationQueueInCache(queue, rootItem); } else { LOG_ERROR_FOR(q, "Format not supported"); return false; } // Get the content of the queue and create the tree for each pack QHash<QString, QStandardItem *> serversUidToItem; foreach(const RequestedPackCreation &request, queue.queue()) { if (_packDescriptionFilesIncluded.contains(request.descriptionFilePath)) continue; _packDescriptionFilesIncluded.append(request.descriptionFilePath); // Get the serverUid root item QStandardItem *server = 0; if (_format == PackCreationModel::ShowByQueue) { server = serversUidToItem.value(request.serverUid, 0); } else if (_format == PackCreationModel::ShowByServer) { server = serverItem(request.serverUid); } // If the server is not available yet: create it if (!server) { server = new QStandardItem(tkTr(Trans::Constants::_1_COLON_2) .arg(tkTr(Trans::Constants::SERVER)) .arg(request.serverUid)); if (_format == PackCreationModel::ShowByQueue) { // In this format // append the server item in the cache and the queueItem serversUidToItem.insert(request.serverUid, server); server->setCheckable(true); server->setCheckState(Qt::Checked); server->setFont(bold); rootItem->appendRow(server); } else if (_format == PackCreationModel::ShowByServer) { // In this format // append the server item in the central cache and the rootItem addServerItem(request.serverUid, server); rootItem = server; rootItem->setFont(bold); rootItem->setCheckable(true); rootItem->setCheckState(Qt::Checked); q->invisibleRootItem()->appendRow(rootItem); } } // Include datapack to the server item server->appendRow(packToItem(request.descriptionFilePath, queue)); } return true; }
// Checks if a queue is already included in the model bool isPackCreationQueueAlreadyInserted(const PackCreationQueue &queue) { return _insertedPackCreationQueueUids.keys().contains(queue.uid()); }