void TestBlockEditor::setCourse(int courseId){ m_courseId = courseId; m_testBlockView->model()->clearTestBlocks(); // m_actionEdit->setEnabled(false); // m_actionCopy->setEnabled(false); // m_actionCut->setEnabled(false); m_actionDelete->setEnabled(false); TestBlockList testBlockList = TestBlock::load(courseId); foreach (TestBlock *tb, testBlockList) { m_testBlockView->model()->addTestBlock(tb); //connect(tb, SIGNAL(changed()), this, SLOT(slotTestBlockChanged())); }}
QList<TestBlock*> TestBlock::load(int courseId){ QList<TestBlock*> list; QSqlQuery query; query.exec(QObject::tr("SELECT id FROM test_blocks WHERE course_id=%2").arg(courseId)); while(query.next()) { int id = query.value(0).toInt(); TestBlock *testBlock = new TestBlock(id); list.append(testBlock); } return list; }
TestBlockList testBlockList = TestBlock::load(courseId); TaskList taskList; foreach (TestBlock *tb, testBlockList) { m_testBlockView->model()->addTestBlock(tb); //connect(tb, SIGNAL(changed()), this, SLOT(slotTestBlockChanged())); taskList.clear(); taskList = Task::load(tb->id()); foreach(Task task, taskList) { m_testBlockView->model()->addTaskToTestBlock(task, tb); //connect(task, SIGNAL(changed()), this, SLOT(slotTaskChanged())); } }
void TestBlockEditor::slotDelete(){ DbObject *selection = m_testBlockView->selectedDbObject(); if (!selection) return; if(TestBlock *tb = dynamic_cast<TestBlock *>(selection)) { RemoveTestBlockCommand::TaskData taskData = m_testBlockView->model()->tasksOfTestBlock(tb); RemoveTestBlockCommand *cmd = new RemoveTestBlockCommand(this); cmd->init(tb, taskData); m_commandHistory->push(cmd); } if(Task *task = dynamic_cast<Task *>(selection)) { TestBlock *tb = m_testBlockView->model()->testBlockOfTask(task); if(!tb) return; RemoveTaskCommand *cmd = new RemoveTaskCommand(this); cmd->init(task, tb); m_commandHistory->push(cmd); }}
RemoveTestBlockCommand::RemoveTestBlockCommand(TestBlockEditor *editor): QUndoCommand(QObject::tr("Удалить тестовый блок")), m_editor(editor), m_testBlock(0){}void RemoveTestBlockCommand::init(TestBlock *tb, const TaskData &taskData){ Q_ASSERT(m_testBlock == 0); m_testBlock = tb; m_taskData = taskData;}void RemoveTestBlockCommand::redo(){ m_editor->unmanageTestBlock(m_testBlock);}void RemoveTestBlockCommand::undo(){ m_editor->manageTestBlock(m_testBlock, m_taskData);}
void TestBlockEditor::manageTestBlock(TestBlock *tb, const QList<Task*> &dataTask){ QModelIndex index = m_testBlockView->model()->addTestBlock(tb); m_testBlockView->setExpanded(index, true); m_testBlockView->setCurrentIndex(index); //connect(tb, SIGNAL(changed()), this, SLOT(slotTestBlockChanged())); foreach(Task *task, dataTask) { m_testBlockView->model()->addTaskToTestBlock(task, tb); //connect(task, SIGNAL(changed()), this, SLOT(slotTaskChanged())); }}void TestBlockEditor::unmanageTestBlock(TestBlock *tb){ //disconnect(tb, SIGNAL(changed()), this, SLOT(slotTestBlockChanged())); const QModelIndex idx = m_testBlockView->model()->findObject(tb); if (idx.isValid()) m_testBlockView->model()->removeRow(idx.row());}